special_api.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <?php
  2. /**
  3. * special_api.class.php 专题接口类
  4. *
  5. * @copyright (C) 2005-2010 PHPCMS
  6. * @license http://www.phpcms.cn/license/
  7. * @lastmodify 2010-8-3
  8. */
  9. defined('IN_PHPCMS') or exit('No permission resources.');
  10. class special_api {
  11. private $db, $type_db, $c_db, $data_db;
  12. public $pages;
  13. public function __construct() {
  14. $this->db = pc_base::load_model('special_model'); //专题数据模型
  15. $this->type_db = pc_base::load_model('type_model'); //专题分类数据模型
  16. $this->c_db = pc_base::load_model('special_content_model'); //专题内容数据模型
  17. $this->data_db = pc_base::load_model('special_c_data_model');
  18. }
  19. /**
  20. * 更新分类
  21. * @param intval $pid 专题ID
  22. * @param string $type 分类字符串 每行一个分类。格式为:分类名|分类目录,例:最新新闻|news last
  23. * @param string $a 添加时直接加入到数据库,修改是需要判断。
  24. * @return boolen
  25. */
  26. public function _update_type($specialid, $type, $a = 'add') {
  27. $specialid = intval($specialid);
  28. if (!$specialid) return false;
  29. $special_info = $this->db->get_one(array('id'=>$specialid));
  30. $app_path = substr(APP_PATH, 0, -1);
  31. foreach ($type as $k => $v) {
  32. if (!$v['name'] || !$v['typedir']) continue;
  33. //添加时,无需判断直接加到数据表中,修改时应判断是否为新添加、修改还是删除
  34. $siteid = get_siteid();
  35. if ($a == 'add' && !$v['del']) {
  36. $typeid = $this->type_db->insert(array('siteid'=>$siteid, 'module'=>'special', 'name'=>$v['name'], 'listorder'=>$v['listorder'], 'typedir'=>$v['typedir'], 'parentid'=>$specialid, 'listorder'=>$k), true);
  37. if ($siteid>1) {
  38. $site = pc_base::load_app_class('sites', 'admin');
  39. $site_info = $site->get_by_id($siteid);
  40. if ($special_info['ishtml']) {
  41. $url = $site_info['domain'].'special/'.$special_info['filename'].'/'.$v['typedir'].'/'.'type-'.$typeid.'.html';
  42. } else {
  43. $url = $site_info['domain'].'index.php?m=special&c=index&a=type&specialid='.$specialid.'&typeid='.$typeid;
  44. }
  45. } else {
  46. if($special_info['ishtml']) $url = addslashes($app_path.pc_base::load_config('system', 'html_root').'/special/'.$special_info['filename'].'/'.$v['typedir'].'/'.'type-'.$typeid.'.html');
  47. else $url = APP_PATH.'index.php?m=special&c=index&a=type&specialid='.$specialid.'&typeid='.$typeid;
  48. }
  49. $this->type_db->update(array('url'=>$url), array('typeid'=>$typeid));
  50. } elseif ($a == 'edit') {
  51. if ((!isset($v['typeid']) || empty($v['typeid'])) && (!isset($v['del']) || empty($v['del']))) {
  52. $typeid = $this->type_db->insert(array('siteid'=>$siteid, 'module'=>'special', 'name'=>$v['name'], 'listorder'=>$v['listorder'], 'typedir'=>$v['typedir'], 'parentid'=>$specialid, 'listorder'=>$k), true);
  53. if ($siteid>1) {
  54. $site = pc_base::load_app_class('sites', 'admin');
  55. $site_info = $site->get_by_id($siteid);
  56. if ($special_info['ishtml']) {
  57. $url = $site_info['domain'].'special/'.$special_info['filename'].'/'.$v['typedir'].'/'.'type-'.$typeid.'.html';
  58. } else {
  59. $url = $site_info['domain'].'index.php?m=special&c=index&a=type&specialid='.$specialid.'&typeid='.$typeid;
  60. }
  61. } else {
  62. if($special_info['ishtml']) $url = addslashes($app_path.pc_base::load_config('system', 'html_root').'/special/'.$special_info['filename'].'/'.$v['typedir'].'/'.'type-'.$typeid.'.html');
  63. else $url = APP_PATH.'index.php?m=special&c=index&a=type&specialid='.$specialid.'&typeid='.$typeid;
  64. }
  65. $v['url'] = $url;
  66. $this->type_db->update($v, array('typeid'=>$typeid));
  67. }
  68. if ((!isset($v['del']) || empty($v['del'])) && $v['typeid']) {
  69. $this->type_db->update(array('name'=>$v['name'], 'typedir'=>$v['typedir'], 'listorder'=>$v['listorder']), array('typeid'=>$r['typeid']));
  70. if ($siteid>1) {
  71. $site = pc_base::load_app_class('sites', 'admin');
  72. $site_info = $site->get_by_id($siteid);
  73. if ($special_info['ishtml']) {
  74. $url = $site_info['domain'].'special/'.$special_info['filename'].'/'.$v['typedir'].'/'.'type-'.$v['typeid'].'.html';
  75. } else {
  76. $url = $site_info['domain'].'index.php?m=special&c=index&a=type&specialid='.$specialid.'&typeid='.$v['typeid'];
  77. }
  78. } else {
  79. if($special_info['ishtml']) $url = addslashes($app_path.pc_base::load_config('system', 'html_root').'/special/'.$special_info['filename'].'/'.$v['typedir'].'/'.'type-'.$v['typeid'].'.html');
  80. else $url = APP_PATH.'index.php?m=special&c=index&a=type&specialid='.$specialid.'&typeid='.$v['typeid'];
  81. }
  82. $v['url'] = $url;
  83. $typeid = $v['typeid'];
  84. unset($v['typeid']);
  85. $this->type_db->update($v, array('typeid'=>$typeid));
  86. }
  87. if ($v['typeid'] && $v['del']) {
  88. $this->delete_type($v['typeid'], $siteid, $special_info['ishtml']);
  89. }
  90. }
  91. }
  92. return true;
  93. }
  94. /**
  95. * 调取内容信息
  96. * @param intval $modelid 模型ID
  97. * @param string $where sql语句
  98. * @param intval $page 分页
  99. * @return array 返回调取的数据
  100. */
  101. public function _get_import_data($modelid = 0, $where = '', $page) {
  102. $c = pc_base::load_model('content_model');
  103. if(!$modelid) return '';
  104. $c->set_model($modelid);
  105. $data = $c->listinfo($where, '`id` DESC', $page);
  106. $this->pages = $c->pages;
  107. return $data;
  108. }
  109. /**
  110. * 信息推荐至专题接口
  111. * @param array $param 属性 请求时,为模型、栏目数组。 例:array('modelid'=>1, 'catid'=>12); 提交添加为二维信息数据 。例:array(1=>array('title'=>'多发发送方法', ....))
  112. * @param array $arr 参数 表单数据,只在请求添加时传递。
  113. * @return 返回专题的下拉列表
  114. */
  115. public function _get_special($param = array(), $arr = array()) {
  116. if ($arr['dosubmit']) {
  117. foreach ($param as $id => $v) {
  118. if (!$arr['specialid'] || !$arr['typeid']) continue;
  119. if (!$this->c_db->get_one(array('title'=>$v['title'], 'specialid'=>$arr['specialid']))) {
  120. $info['specialid'] = $arr['specialid'];
  121. $info['typeid'] = $arr['typeid'];
  122. $info['title'] = $v['title'];
  123. $info['thumb'] = $v['thumb'];
  124. $info['url'] = $v['url'];
  125. $info['curl'] = $v['id'].'|'.$v['catid'];
  126. $info['description'] = $v['description'];
  127. $info['userid'] = $v['userid'];
  128. $info['username'] = $v['username'];
  129. $info['inputtime'] = $v['inputtime'];
  130. $info['updatetime'] = $v['updatetime'];
  131. $info['islink'] = 1;
  132. $this->c_db->insert($info, true);
  133. }
  134. }
  135. return true;
  136. } else {
  137. $datas = getcache('special', 'commons');
  138. $special = array(L('please_select'));
  139. if (is_array($datas)) {
  140. foreach ($datas as $sid => $d) {
  141. if ($d['siteid']==get_siteid()) {
  142. $special[$sid] = $d['title'];
  143. }
  144. }
  145. }
  146. return array(
  147. 'specialid' => array('name'=>L('special_list','','special'), 'htmltype'=>'select', 'data'=>$special, 'ajax'=>array('name'=>L('for_type','','special'), 'action'=>'_get_type', 'm'=>'special', 'id'=>'typeid')),
  148. 'validator' => '$(\'#specialid\').formValidator({autotip:true,onshow:"'.L('please_choose_special','','special').'",oncorrect:"'.L('true', '', 'special').'"}).inputValidator({min:1,onerror:"'.L('please_choose_special','','special').'"});$(\'#typeid\').formValidator({autotip:true,onshow:"'.L('please_choose_type', '', 'special').'",oncorrect:"'.L('true', '', 'special').'"}).inputValidator({min:1,onerror:"'.L('please_choose_type', '', 'special').'"});',
  149. );
  150. }
  151. }
  152. /**
  153. * 获取分类
  154. * @param intval $specialid 专题ID
  155. */
  156. public function _get_type($specialid = 0) {
  157. $type_db = pc_base::load_model('type_model');
  158. $data = $arr = array();
  159. $data = $type_db->select(array('module'=>'special', 'parentid'=>$specialid));
  160. pc_base::load_sys_class('form', '', 0);
  161. foreach ($data as $r) {
  162. $arr[$r['typeid']] = $r['name'];
  163. }
  164. return form::select($arr, '', 'name="typeid", id="typeid"', L('please_select'));
  165. }
  166. /**
  167. * 调取专题的附属分类
  168. * @param intval $specialid 专题ID
  169. * @return array 专题的附属分类
  170. */
  171. public function _get_types($specialid = 0) {
  172. if (!$specialid) return false;
  173. $rs = $this->type_db->select(array('parentid'=>$specialid, 'siteid'=>get_siteid()), 'typeid, name');
  174. $types = array();
  175. foreach ($rs as $r) {
  176. $types[$r['typeid']] = $r['name'];
  177. }
  178. return $types;
  179. }
  180. /**
  181. * 删除专题 执行删除操作的方法,同时删除专题下的分类、信息、及生成静态文件和图片
  182. * @param intval $id 专题ID
  183. * @return boolen
  184. */
  185. public function _del_special($id = 0) {
  186. $id = intval($id);
  187. if (!$id) return false;
  188. //检查专题下是否有信息
  189. $rs = $this->c_db->select(array('specialid'=>$id), 'id');
  190. $info = $this->db->get_one(array('id'=>$id, 'siteid'=>get_siteid()), 'siteid, ispage, filename, ishtml');
  191. //有信息时,循环删除
  192. if (is_array($rs) && !empty($rs)) {
  193. foreach ($rs as $r) {
  194. $this->_delete_content($r['id'], $info['siteid'], $info['ishtml']);
  195. }
  196. }
  197. //删除专题的附属分类
  198. $type_info = $this->type_db->select(array('module'=>'special', 'parentid'=>$id, 'siteid'=>get_siteid()), '`typeid`');
  199. if (is_array($type_info) && !empty($type_info)) {
  200. foreach ($type_info as $t) {
  201. $this->delete_type($t['typeid'], $info['siteid'], $info['ishtml']);
  202. }
  203. }
  204. pc_base::load_sys_func('dir');
  205. $this->db->delete(array('id'=>$id, 'siteid'=>get_siteid()));
  206. $info['filename'] = str_replace('..','',$info['filename']);
  207. if ($info['siteid']>1) {
  208. if ($info['ishtml']) {
  209. $queue = pc_base::load_model('queue_model');
  210. $site = pc_base::load_app_class('sites', 'admin');
  211. $site_info = $site->get_by_id($info['siteid']);
  212. $file = pc_base::load_config('system', 'html_root').'/'.$site_info['dirname'].'/special/'.$info['filename'].'/index.html';
  213. if ($info['ispage']) {
  214. for ($i==1; $i>0; $i++) {
  215. if ($i>1) {
  216. $file = str_replace('.html', '-'.$i.'.html', $file);
  217. }
  218. if (!file_exists(PHPCMS_PATH.$file)) {
  219. break;
  220. } else {
  221. $queue->add_queue('del', $file, $info['siteid']);
  222. unlink(PHPCMS_PATH.$file);
  223. }
  224. }
  225. } else {
  226. $queue->add_queue('del', $file, $info['siteid']);
  227. unlink(PHPCMS_PATH.$file);
  228. }
  229. $queue->add_queue('del', pc_base::load_config('system', 'html_root').'/'.$site_info['dirname'].'/special/'.$info['filename'].'/', $info['siteid']);
  230. dir_delete(pc_base::load_config('system', 'html_root').'/'.$site_info['dirname'].'/special/'.$info['filename']);
  231. }
  232. } else {
  233. if ($info['ishtml']) {
  234. dir_delete(PHPCMS_PATH.pc_base::load_config('system', 'html_root').DIRECTORY_SEPARATOR.'special'.DIRECTORY_SEPARATOR.$info['filename']); //删除专题目录
  235. }
  236. }
  237. if(pc_base::load_config('system','attachment_stat')) {
  238. $keyid = 'special-'.$id;
  239. $this->attachment_db = pc_base::load_model('attachment_model');
  240. $this->attachment_db->api_delete($keyid);
  241. }
  242. return true;
  243. }
  244. /**
  245. * 导入的数据添加到数据表
  246. * @param intval $modelid 模型ID
  247. * @param intval $specialid 信息的所属专题ID
  248. * @param intval $id 信息的ID
  249. * @param intval $typeid 信息的分类ID
  250. * @param intval $listorder 信息的排序
  251. */
  252. public function _import($modelid, $specialid, $id, $typeid, $listorder = 0) {
  253. if (!$specialid || !$id || !$typeid) return false;
  254. $c = pc_base::load_model('content_model');
  255. $c->set_model($modelid);
  256. $info = $c->get_one(array('id'=>$id, 'status'=>99), '`id`, `catid`, `title`, `thumb`, `url`, `description`, `username`, `inputtime`, `updatetime`');
  257. if ($info) {
  258. $info['curl'] = $info['id'].'|'.$info['catid'];
  259. unset($info['id'], $info['catid']);
  260. if(!$this->c_db->get_one(array('title'=>addslashes($info['title']), 'specialid'=>$specialid, 'typeid'=>$typeid))) {
  261. $info['specialid'] = $specialid;
  262. $info['typeid'] = $typeid;
  263. $info['islink'] = 1;
  264. $info['listorder'] = $listorder;
  265. $info = new_addslashes($info);
  266. return $this->c_db->insert($info, true);
  267. }
  268. }
  269. return false;
  270. }
  271. /**
  272. * 删除专题分类
  273. * @param intval $typeid 专题附属分类ID
  274. * @param intval $siteid 站点ID
  275. * @param intval $ishtml 专题是否生成静态
  276. */
  277. private function delete_type($typeid = 0, $siteid = 0, $ishtml = 0) {
  278. $typeid = intval($typeid);
  279. if (!$typeid) return false;
  280. pc_base::load_sys_func('dir');
  281. $info = $this->type_db->get_one(array('typeid'=>$typeid));
  282. if ($ishtml) {
  283. $siteid = $siteid ? intval($siteid) : get_siteid();
  284. if ($siteid>1) {
  285. $site = pc_base::load_app_class('sites', 'admin');
  286. $site_info = $site->get_by_id($siteid);
  287. $queue = pc_base::load_model('queue_model');
  288. for ($i = 1; $i>0; $i++) {
  289. if ($i==1) $file = str_replace($site_info['domain'], pc_base::load_config('system', 'html_root').'/'.$site_info['dirname'].'/', $info['url']);
  290. else $file = str_replace(array($site_info['domain'], '.html'), array(pc_base::load_config('system', 'html_root').'/'.$site_info['dirname'].'/', '-'.$i.'.html'), $info['url']);
  291. if (!file_exists(PHPCMS_PATH.$file)) {
  292. break;
  293. } else {
  294. $queue->add_queue('del', $file, $siteid); //并加入到消息队列中,便以其他站点删除文件
  295. unlink(PHPCMS_PATH.$file); //删除生成的静态文件
  296. }
  297. }
  298. } else {
  299. for ($i = 1; $i>0; $i++) {
  300. if ($i==1) $file = str_replace(APP_PATH, '', $info['url']);
  301. else $file = str_replace(array(APP_PATH, '.html'), array('', '-'.$i.'.html'), $info['url']);
  302. if (!file_exists(PHPCMS_PATH.$file)) {
  303. break;
  304. } else {
  305. unlink(PHPCMS_PATH.$file); //删除生成的静态文件
  306. }
  307. }
  308. }
  309. }
  310. $this->type_db->delete(array('typeid'=>$typeid)); //删除数据表记录
  311. return true;
  312. }
  313. /**
  314. * 删除专题信息,同时删除专题的信息,及相关的静态文件、图片
  315. * @param intval $cid 专题信息ID
  316. * @param intval $siteid 所属站点
  317. * @param intval $ishtml 专题是否生成静态
  318. */
  319. public function _delete_content($cid = 0, $siteid = 0, $ishtml = 0) {
  320. $info = $this->c_db->get_one(array('id'=>$cid), 'inputtime, isdata');
  321. if ($info['isdata']) {
  322. if ($ishtml) {
  323. pc_base::load_app_func('global', 'special');
  324. $siteid = $siteid ? intval($siteid) : get_siteid();
  325. if ($siteid>1) {
  326. $site = pc_base::load_app_class('sites', 'admin');
  327. $site_info = $site->get_by_id($siteid);
  328. $queue = pc_base::load_model('queue_model');
  329. for ($i = 1; $i>0; $i++) {
  330. $file = content_url($cid, $i, $info['inputtime'], 'html', $site_info);
  331. if (!file_exists(PHPCMS_PATH.$file[1])) {
  332. break;
  333. } else {
  334. $queue->add_queue('del', $file[1], $siteid); //并加入到消息队列中,便以其他站点删除文件
  335. unlink(PHPCMS_PATH.$file[1]); //删除生成的静态文件
  336. }
  337. }
  338. } else {
  339. for ($i = 1; $i>0; $i++) {
  340. $file = content_url($cid, $i, $info['inputtime']);
  341. if (!file_exists(PHPCMS_PATH.$file[1])) {
  342. break;
  343. } else {
  344. unlink(PHPCMS_PATH.$file[1]); //删除生成的静态文件
  345. }
  346. }
  347. }
  348. }
  349. //删除全站搜索数据
  350. $this->search_api($cid, '', '', 'delete');
  351. // 删除数据统计表数据
  352. $count = pc_base::load_model('hits_model');
  353. $hitsid = 'special-c-'.$info['specialid'].'-'.$cid;
  354. $count->delete(array('hitsid'=>$hitsid));
  355. //删除信息内容表中的数据
  356. $this->data_db->delete(array('id'=>$cid));
  357. }
  358. $this->c_db->delete(array('id'=>$cid)); //删除信息表中的数据
  359. return true;
  360. }
  361. /**
  362. * Function importfalbum
  363. * 将专辑载入到专题
  364. * @param array $info 专辑详细信息
  365. */
  366. public function importfalbum($info = array()) {
  367. static $siteid,$sitelists;
  368. if (!$siteid) $siteid = get_siteid();
  369. if (!$sitelists) $sitelists = getcache('sitelist', 'commons');
  370. pc_base::load_sys_func('iconv');
  371. if (is_array($info)) {
  372. $username = param::get_cookie('admin_username');
  373. $userid = param::get_cookie('userid');
  374. $arr = array(
  375. 'siteid' => $siteid,
  376. 'aid' => $info['id'],
  377. 'title' => $info['title'],
  378. 'thumb' => format_url($info['coverurl']),
  379. 'banner' => format_url($info['coverurl']),
  380. 'description' => $info['desc'],
  381. 'ishtml' => 0,
  382. 'ispage' => 0,
  383. 'style' => 'default',
  384. 'index_template' => 'index_video',
  385. 'list_template' => 'list_video',
  386. 'show_template' => 'show_video',
  387. 'username' => $username,
  388. 'userid' =>$userid,
  389. 'createtime' => SYS_TIME,
  390. 'isvideo' => 1,
  391. );
  392. //将数据插入到专题表中
  393. $arr = new_html_special_chars($arr);
  394. $specialid = $this->db->insert($arr, true);
  395. $url = $sitelists[$siteid]['domain'].'index.php?m=special&c=index&id='.$specialid;
  396. $this->db->update(array('url'=>$url), array('id'=>$specialid));
  397. //组合子分类数组
  398. $letters = gbk_to_pinyin($info['title']);
  399. $type_info = array(
  400. 'siteid' => $siteid,
  401. 'module' => 'special',
  402. 'modelid' => 0,
  403. 'name' => new_html_special_chars($info['title']),
  404. 'parentid' => $specialid,
  405. 'typedir' => strtolower(implode('', $letters)),
  406. 'listorder' => 1,
  407. );
  408. $typeid = $this->type_db->insert($type_info, true);
  409. $url = $sitelists[$siteid]['domain'].'index.php?m=special&c=index&a=type&specialid='.$specialid.'&typeid='.$typeid;
  410. $this->type_db->update(array('url'=>$url), array('typeid'=>$typeid));
  411. return $specialid;
  412. } else {
  413. return false;
  414. }
  415. }
  416. /**
  417. * 添加到全站搜索
  418. * @param intval $id 文章ID
  419. * @param array $data 数组
  420. * @param string $title 标题
  421. * @param string $action 动作
  422. */
  423. private function search_api($id = 0, $data = array(), $title, $action = 'update') {
  424. $this->search_db = pc_base::load_model('search_model');
  425. $siteid = get_siteid();
  426. $type_arr = getcache('type_module_'.$siteid,'search');
  427. $typeid = $type_arr['special'];
  428. if($action == 'update') {
  429. $fulltextcontent = $data['content'];
  430. return $this->search_db->update_search($typeid ,$id, $fulltextcontent,$title);
  431. } elseif($action == 'delete') {
  432. $this->search_db->delete_search($typeid ,$id);
  433. }
  434. }
  435. }
  436. ?>