category.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. <?php
  2. defined('IN_PHPCMS') or exit('No permission resources.');
  3. pc_base::load_app_class('admin','admin',0);
  4. class category extends admin {
  5. private $db;
  6. public $siteid;
  7. function __construct() {
  8. parent::__construct();
  9. $this->db = pc_base::load_model('category_model');
  10. $this->siteid = $this->get_siteid();
  11. }
  12. /**
  13. * 管理栏目
  14. */
  15. public function init () {
  16. $show_pc_hash = '';
  17. $tree = pc_base::load_sys_class('tree');
  18. $models = getcache('model','commons');
  19. $sitelist = getcache('sitelist','commons');
  20. $category_items = array();
  21. foreach ($models as $modelid=>$model) {
  22. $category_items[$modelid] = getcache('category_items_'.$modelid,'commons');
  23. }
  24. $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ','&nbsp;&nbsp;&nbsp;├─ ','&nbsp;&nbsp;&nbsp;└─ ');
  25. $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
  26. $categorys = array();
  27. //读取缓存
  28. $result = getcache('category_content_'.$this->siteid,'commons');
  29. $show_detail = count($result) < 500 ? 1 : 0;
  30. $parentid = $_GET['parentid'] ? intval($_GET['parentid']) : 0;
  31. $html_root = pc_base::load_config('system','html_root');
  32. $types = array(0 => L('category_type_system'),1 => L('category_type_page'),2 => L('category_type_link'));
  33. if(!empty($result)) {
  34. foreach($result as $r) {
  35. $r['modelname'] = $models[$r['modelid']]['name'];
  36. $r['str_manage'] = '';
  37. if(!$show_detail) {
  38. if($r['parentid']!=$parentid) continue;
  39. $r['parentid'] = 0;
  40. $r['str_manage'] .= '<a href="?m=admin&c=category&a=init&parentid='.$r['catid'].'&menuid='.$_GET['menuid'].'&s='.$r['type'].'&pc_hash='.$_SESSION['pc_hash'].'">'.L('manage_sub_category').'</a> | ';
  41. }
  42. $r['str_manage'] .= '<a href="?m=admin&c=category&a=add&parentid='.$r['catid'].'&menuid='.$_GET['menuid'].'&s='.$r['type'].'&pc_hash='.$_SESSION['pc_hash'].'">'.L('add_sub_category').'</a> | ';
  43. $r['str_manage'] .= '<a href="?m=admin&c=category&a=edit&catid='.$r['catid'].'&menuid='.$_GET['menuid'].'&type='.$r['type'].'&pc_hash='.$_SESSION['pc_hash'].'">'.L('edit').'</a> | <a href="javascript:confirmurl(\'?m=admin&c=category&a=delete&catid='.$r['catid'].'&menuid='.$_GET['menuid'].'\',\''.L('confirm',array('message'=>addslashes($r['catname']))).'\')">'.L('delete').'</a> | <a href="?m=admin&c=category&a=remove&catid='.$r['catid'].'&pc_hash='.$_SESSION['pc_hash'].'">'.L('remove','','content').'</a>';
  44. $r['typename'] = $types[$r['type']];
  45. $r['display_icon'] = $r['ismenu'] ? '' : ' <img src ="'.IMG_PATH.'icon/gear_disable.png" title="'.L('not_display_in_menu').'">';
  46. if($r['type'] || $r['child']) {
  47. $r['items'] = '';
  48. } else {
  49. $r['items'] = $category_items[$r['modelid']][$r['catid']];
  50. }
  51. $r['help'] = '';
  52. $setting = string2array($r['setting']);
  53. if($r['url']) {
  54. if(preg_match('/^(http|https):\/\//', $r['url'])) {
  55. $catdir = $r['catdir'];
  56. $prefix = $r['sethtml'] ? '' : $html_root;
  57. if($this->siteid==1) {
  58. $catdir = $prefix.'/'.$r['parentdir'].$catdir;
  59. } else {
  60. $catdir = $prefix.'/'.$sitelist[$this->siteid]['dirname'].$html_root.'/'.$catdir;
  61. }
  62. if($r['type']==0 && $setting['ishtml'] && strpos($r['url'], '?')===false && substr_count($r['url'],'/')<4) $r['help'] = '<img src="'.IMG_PATH.'icon/help.png" title="'.L('tips_domain').$r['url'].'&#10;'.L('directory_binding').'&#10;'.$catdir.'/">';
  63. } else {
  64. $r['url'] = substr($sitelist[$this->siteid]['domain'],0,-1).$r['url'];
  65. }
  66. $r['url'] = "<a href='$r[url]' target='_blank'>".L('vistor')."</a>";
  67. } else {
  68. $r['url'] = "<a href='?m=admin&c=category&a=public_cache&menuid=43&module=admin'><font color='red'>".L('update_backup')."</font></a>";
  69. }
  70. $categorys[$r['catid']] = $r;
  71. }
  72. }
  73. $str = "<tr>
  74. <td align='center'><input name='listorders[\$id]' type='text' size='3' value='\$listorder' class='input-text-c'></td>
  75. <td align='center'>\$id</td>
  76. <td >\$spacer\$catname\$display_icon</td>
  77. <td>\$typename</td>
  78. <td>\$modelname</td>
  79. <td align='center'>\$items</td>
  80. <td align='center'>\$url</td>
  81. <td align='center'>\$help</td>
  82. <td align='center' >\$str_manage</td>
  83. </tr>";
  84. $tree->init($categorys);
  85. $categorys = $tree->get_tree(0, $str);
  86. include $this->admin_tpl('category_manage');
  87. }
  88. /**
  89. * 添加栏目
  90. */
  91. public function add() {
  92. if(isset($_POST['dosubmit'])) {
  93. pc_base::load_sys_func('iconv');
  94. $_POST['info']['type'] = intval($_POST['type']);
  95. if(isset($_POST['batch_add']) && empty($_POST['batch_add'])) {
  96. if($_POST['info']['catname']=='') showmessage(L('input_catname'));
  97. $_POST['info']['catname'] = safe_replace($_POST['info']['catname']);
  98. $_POST['info']['catname'] = str_replace(array('%'),'',$_POST['info']['catname']);
  99. if($_POST['info']['type']!=2) {
  100. if($_POST['info']['catdir']=='') showmessage(L('input_dirname'));
  101. if(!$this->public_check_catdir(0,$_POST['info']['catdir'])) showmessage(L('catname_have_exists'));
  102. }
  103. }
  104. $_POST['info']['siteid'] = $this->siteid;
  105. $_POST['info']['module'] = 'content';
  106. $setting = $_POST['setting'];
  107. if($_POST['info']['type']!=2) {
  108. //栏目生成静态配置
  109. if($setting['ishtml']) {
  110. $setting['category_ruleid'] = $_POST['category_html_ruleid'];
  111. } else {
  112. $setting['category_ruleid'] = $_POST['category_php_ruleid'];
  113. $_POST['info']['url'] = '';
  114. }
  115. }
  116. //内容生成静态配置
  117. if($setting['content_ishtml']) {
  118. $setting['show_ruleid'] = $_POST['show_html_ruleid'];
  119. } else {
  120. $setting['show_ruleid'] = $_POST['show_php_ruleid'];
  121. }
  122. if($setting['repeatchargedays']<1) $setting['repeatchargedays'] = 1;
  123. $_POST['info']['sethtml'] = $setting['create_to_html_root'];
  124. $_POST['info']['setting'] = array2string($setting);
  125. $end_str = $old_end = '<script type="text/javascript">window.top.art.dialog({id:"test"}).close();window.top.art.dialog({id:"test",content:\'<h2>'.L("add_success").'</h2><span style="fotn-size:16px;">'.L("following_operation").'</span><br /><ul style="fotn-size:14px;"><li><a href="?m=admin&c=category&a=public_cache&menuid=43&module=admin" target="right" onclick="window.top.art.dialog({id:\\\'test\\\'}).close()">'.L("following_operation_1").'</a></li><li><a href="'.HTTP_REFERER.'" target="right" onclick="window.top.art.dialog({id:\\\'test\\\'}).close()">'.L("following_operation_2").'</a></li></ul>\',width:"400",height:"200"});</script>';
  126. if(!isset($_POST['batch_add']) || empty($_POST['batch_add'])) {
  127. $catname = CHARSET == 'gbk' ? $_POST['info']['catname'] : iconv('utf-8','gbk',$_POST['info']['catname']);
  128. $letters = gbk_to_pinyin($catname);
  129. $_POST['info']['letter'] = strtolower(implode('', $letters));
  130. $catid = $this->db->insert($_POST['info'], true);
  131. $this->update_priv($catid, $_POST['priv_roleid']);
  132. $this->update_priv($catid, $_POST['priv_groupid'],0);
  133. } else {//批量添加
  134. $end_str = '';
  135. $batch_adds = explode("\n", $_POST['batch_add']);
  136. foreach ($batch_adds as $_v) {
  137. if(trim($_v)=='') continue;
  138. $names = explode('|', $_v);
  139. $catname = $names[0];
  140. $_POST['info']['catname'] = trim($names[0]);
  141. $letters = gbk_to_pinyin($catname);
  142. $_POST['info']['letter'] = strtolower(implode('', $letters));
  143. $_POST['info']['catdir'] = trim($names[1]) ? trim($names[1]) : trim($_POST['info']['letter']);
  144. if(!$this->public_check_catdir(0,$_POST['info']['catdir'])) {
  145. $end_str .= $end_str ? ','.$_POST['info']['catname'].'('.$_POST['info']['catdir'].')' : $_POST['info']['catname'].'('.$_POST['info']['catdir'].')';
  146. continue;
  147. }
  148. $catid = $this->db->insert($_POST['info'], true);
  149. $this->update_priv($catid, $_POST['priv_roleid']);
  150. $this->update_priv($catid, $_POST['priv_groupid'],0);
  151. }
  152. $end_str = $end_str ? L('follow_catname_have_exists').$end_str : $old_end;
  153. }
  154. $this->cache();
  155. showmessage(L('add_success').$end_str);
  156. } else {
  157. //获取站点模板信息
  158. pc_base::load_app_func('global');
  159. $template_list = template_list($this->siteid, 0);
  160. foreach ($template_list as $k=>$v) {
  161. $template_list[$v['dirname']] = $v['name'] ? $v['name'] : $v['dirname'];
  162. unset($template_list[$k]);
  163. }
  164. $show_validator = '';
  165. if(isset($_GET['parentid'])) {
  166. $parentid = $_GET['parentid'];
  167. $r = $this->db->get_one(array('catid'=>$parentid));
  168. if($r) extract($r,EXTR_SKIP);
  169. $setting = string2array($setting);
  170. }
  171. pc_base::load_sys_class('form','',0);
  172. $type = $_GET['s'];
  173. if($type==0) {
  174. $exists_model = false;
  175. $models = getcache('model','commons');
  176. foreach($models as $_m) {
  177. if($this->siteid == $_m['siteid']) {
  178. $exists_model = true;
  179. break;
  180. }
  181. }
  182. if(!$exists_model) showmessage(L('please_add_model'),'?m=content&c=sitemodel&a=init&menuid=59',5000);
  183. include $this->admin_tpl('category_add');
  184. } elseif ($type==1) {
  185. include $this->admin_tpl('category_page_add');
  186. } else {
  187. include $this->admin_tpl('category_link');
  188. }
  189. }
  190. }
  191. /**
  192. * 修改栏目
  193. */
  194. public function edit() {
  195. if(isset($_POST['dosubmit'])) {
  196. pc_base::load_sys_func('iconv');
  197. $catid = 0;
  198. $catid = intval($_POST['catid']);
  199. $setting = $_POST['setting'];
  200. //上级栏目不能是自身
  201. if($_POST['info']['parentid']==$catid){
  202. showmessage(L('operation_failure'),'?m=admin&c=category&a=init&module=admin&menuid=43');
  203. }
  204. //栏目生成静态配置
  205. if($_POST['type'] != 2) {
  206. if($setting['ishtml']) {
  207. $setting['category_ruleid'] = $_POST['category_html_ruleid'];
  208. } else {
  209. $setting['category_ruleid'] = $_POST['category_php_ruleid'];
  210. $_POST['info']['url'] = '';
  211. }
  212. }
  213. //内容生成静态配置
  214. if($setting['content_ishtml']) {
  215. $setting['show_ruleid'] = $_POST['show_html_ruleid'];
  216. } else {
  217. $setting['show_ruleid'] = $_POST['show_php_ruleid'];
  218. }
  219. if($setting['repeatchargedays']<1) $setting['repeatchargedays'] = 1;
  220. $_POST['info']['sethtml'] = $setting['create_to_html_root'];
  221. $_POST['info']['setting'] = array2string($setting);
  222. $_POST['info']['module'] = 'content';
  223. $catname = CHARSET == 'gbk' ? safe_replace($_POST['info']['catname']) : iconv('utf-8','gbk',safe_replace($_POST['info']['catname']));
  224. $catname = str_replace(array('%'),'',$catname);
  225. $letters = gbk_to_pinyin($catname);
  226. $_POST['info']['letter'] = strtolower(implode('', $letters));
  227. //应用权限设置到子栏目
  228. if($_POST['priv_child']) {
  229. $arrchildid = $this->db->get_one(array('catid'=>$catid), 'arrchildid');
  230. if(!empty($arrchildid['arrchildid'])) {
  231. $arrchildid_arr = explode(',', $arrchildid['arrchildid']);
  232. if(!empty($arrchildid_arr)) {
  233. foreach ($arrchildid_arr as $arr_v) {
  234. // 2020年8月20日10:45:54 yf
  235. $this->update_priv($arr_v, $_POST['priv_roleid']); //添加站点管理员的删除栏目权限
  236. //修改结束
  237. $this->update_priv($arr_v, $_POST['priv_groupid'], 0);
  238. }
  239. }
  240. }
  241. }
  242. //应用模板到所有子栏目
  243. if($_POST['template_child']){
  244. $this->categorys = $categorys = $this->db->select(array('siteid'=>$this->siteid,'module'=>'content'), '*', '', 'listorder ASC, catid ASC', '', 'catid');
  245. $idstr = $this->get_arrchildid($catid);
  246. if(!empty($idstr)){
  247. $sql = "select catid,setting from phpcms_category where catid in($idstr)";
  248. $this->db->query($sql);
  249. $arr = $this->db->fetch_array();
  250. if(!empty($arr)){
  251. foreach ($arr as $v){
  252. $new_setting = array2string(
  253. array_merge(string2array($v['setting']), array('category_template' => $_POST['setting']['category_template'],'list_template' => $_POST['setting']['list_template'],'show_template' => $_POST['setting']['show_template'])
  254. )
  255. );
  256. $this->db->update(array('setting'=>$new_setting), 'catid='.$v['catid']);
  257. }
  258. }
  259. }
  260. }
  261. $this->db->update($_POST['info'],array('catid'=>$catid,'siteid'=>$this->siteid));
  262. $this->update_priv($catid, $_POST['priv_roleid']);
  263. $this->update_priv($catid, $_POST['priv_groupid'],0);
  264. $this->cache();
  265. //更新附件状态
  266. if($_POST['info']['image'] && pc_base::load_config('system','attachment_stat')) {
  267. $this->attachment_db = pc_base::load_model('attachment_model');
  268. $this->attachment_db->api_update($_POST['info']['image'],'catid-'.$catid,1);
  269. }
  270. showmessage(L('operation_success').'<script type="text/javascript">window.top.art.dialog({id:"test"}).close();window.top.art.dialog({id:"test",content:\'<h2>'.L("operation_success").'</h2><span style="fotn-size:16px;">'.L("edit_following_operation").'</span><br /><ul style="fotn-size:14px;"><li><a href="?m=admin&c=category&a=public_cache&menuid=43&module=admin" target="right" onclick="window.top.art.dialog({id:\\\'test\\\'}).close()">'.L("following_operation_1").'</a></li></ul>\',width:"400",height:"200"});</script>','?m=admin&c=category&a=init&module=admin&menuid=43');
  271. } else {
  272. //获取站点模板信息
  273. pc_base::load_app_func('global');
  274. $template_list = template_list($this->siteid, 0);
  275. foreach ($template_list as $k=>$v) {
  276. $template_list[$v['dirname']] = $v['name'] ? $v['name'] : $v['dirname'];
  277. unset($template_list[$k]);
  278. }
  279. $show_validator = $catid = $r = '';
  280. $catid = intval($_GET['catid']);
  281. pc_base::load_sys_class('form','',0);
  282. $r = $this->db->get_one(array('catid'=>$catid));
  283. if($r) extract($r);
  284. $setting = string2array($setting);
  285. $this->priv_db = pc_base::load_model('category_priv_model');
  286. $this->privs = $this->priv_db->select(array('catid'=>$catid));
  287. $type = $_GET['type'];
  288. if($type==0) {
  289. include $this->admin_tpl('category_edit');
  290. } elseif ($type==1) {
  291. include $this->admin_tpl('category_page_edit');
  292. } else {
  293. include $this->admin_tpl('category_link');
  294. }
  295. }
  296. }
  297. /**
  298. * 排序
  299. */
  300. public function listorder() {
  301. if(isset($_POST['dosubmit'])) {
  302. foreach($_POST['listorders'] as $id => $listorder) {
  303. $this->db->update(array('listorder'=>$listorder),array('catid'=>$id));
  304. }
  305. $this->cache();
  306. showmessage(L('operation_success'),HTTP_REFERER);
  307. } else {
  308. showmessage(L('operation_failure'));
  309. }
  310. }
  311. /**
  312. * 删除栏目
  313. */
  314. public function delete() {
  315. $catid = intval($_GET['catid']);
  316. $categorys = getcache('category_content_'.$this->siteid,'commons');
  317. $modelid = $categorys[$catid]['modelid'];
  318. $items = getcache('category_items_'.$modelid,'commons');
  319. //if($items[$catid]) showmessage(L('category_does_not_allow_delete'));
  320. $this->delete_child($catid, $modelid);
  321. $this->db->delete(array('catid'=>$catid));
  322. if ($modelid != 0) {
  323. $this->delete_category_video($catid, $modelid);
  324. }
  325. $this->cache();
  326. showmessage(L('operation_success'),HTTP_REFERER);
  327. }
  328. /**
  329. * 递归删除栏目
  330. * @param $catid 要删除的栏目id
  331. */
  332. private function delete_child($catid, $modelid) {
  333. $catid = intval($catid);
  334. if (empty($catid)) return false;
  335. $r = $this->db->get_one(array('parentid'=>$catid));
  336. if($r) {
  337. $this->delete_child($r['catid']);
  338. $this->db->delete(array('catid'=>$r['catid']));
  339. if ($modelid != 0) {
  340. $this->delete_category_video($r['catid'], $modelid);
  341. }
  342. }
  343. return true;
  344. }
  345. /**
  346. * 删除栏目分类下的视频
  347. * @param $catid 要删除视频的栏目id
  348. */
  349. private function delete_category_video($catid, $modelid) {
  350. $content_model = pc_base::load_model('content_model');
  351. $content_model->set_model($modelid);
  352. $result = $content_model->select(array('catid'=>$catid), 'id');
  353. if (is_array($result) && !empty($result)) {
  354. foreach ($result as $key=>$val) {
  355. $content_model->delete_content($val['id'],$fileurl,$catid);
  356. }
  357. }
  358. }
  359. /**
  360. * 更新缓存
  361. */
  362. public function cache() {
  363. $categorys = array();
  364. $models = getcache('model','commons');
  365. foreach ($models as $modelid=>$model) {
  366. $datas = $this->db->select(array('modelid'=>$modelid),'catid,type,items',10000);
  367. $array = array();
  368. foreach ($datas as $r) {
  369. if($r['type']==0) $array[$r['catid']] = $r['items'];
  370. }
  371. setcache('category_items_'.$modelid, $array,'commons');
  372. }
  373. $array = array();
  374. $categorys = $this->db->select('`module`=\'content\'','catid,siteid',20000,'listorder ASC');
  375. foreach ($categorys as $r) {
  376. $array[$r['catid']] = $r['siteid'];
  377. }
  378. setcache('category_content',$array,'commons');
  379. $categorys = $this->categorys = array();
  380. $this->categorys = $this->db->select(array('siteid'=>$this->siteid, 'module'=>'content'),'*',10000,'listorder ASC');
  381. foreach($this->categorys as $r) {
  382. unset($r['module']);
  383. $setting = string2array($r['setting']);
  384. $r['create_to_html_root'] = $setting['create_to_html_root'];
  385. $r['ishtml'] = $setting['ishtml'];
  386. $r['content_ishtml'] = $setting['content_ishtml'];
  387. $r['category_ruleid'] = $setting['category_ruleid'];
  388. $r['show_ruleid'] = $setting['show_ruleid'];
  389. $r['workflowid'] = $setting['workflowid'];
  390. $r['isdomain'] = '0';
  391. if(!preg_match('/^(http|https):\/\//', $r['url'])) {
  392. $r['url'] = siteurl($r['siteid']).$r['url'];
  393. } elseif ($r['ishtml']) {
  394. $r['isdomain'] = '1';
  395. }
  396. $categorys[$r['catid']] = $r;
  397. }
  398. setcache('category_content_'.$this->siteid,$categorys,'commons');
  399. return true;
  400. }
  401. /**
  402. * 更新缓存并修复栏目
  403. */
  404. public function public_cache() {
  405. $this->repair();
  406. $this->cache();
  407. showmessage(L('operation_success'),'?m=admin&c=category&a=init&module=admin&menuid=43');
  408. }
  409. /**
  410. * 修复栏目数据
  411. */
  412. private function repair() {
  413. pc_base::load_sys_func('iconv');
  414. @set_time_limit(600);
  415. $html_root = pc_base::load_config('system','html_root');
  416. $this->categorys = $categorys = array();
  417. $this->categorys = $categorys = $this->db->select(array('siteid'=>$this->siteid,'module'=>'content'), '*', '', 'listorder ASC, catid ASC', '', 'catid');
  418. $this->get_categorys($categorys);
  419. if(is_array($this->categorys)) {
  420. foreach($this->categorys as $catid => $cat) {
  421. if($cat['type'] == 2) continue;
  422. $arrparentid = $this->get_arrparentid($catid);
  423. $setting = string2array($cat['setting']);
  424. $arrchildid = $this->get_arrchildid($catid);
  425. $child = is_numeric($arrchildid) ? 0 : 1;
  426. if($categorys[$catid]['arrparentid']!=$arrparentid || $categorys[$catid]['arrchildid']!=$arrchildid || $categorys[$catid]['child']!=$child) $this->db->update(array('arrparentid'=>$arrparentid,'arrchildid'=>$arrchildid,'child'=>$child),array('catid'=>$catid));
  427. $parentdir = $this->get_parentdir($catid);
  428. $catname = $cat['catname'];
  429. $letters = gbk_to_pinyin($catname);
  430. $letter = strtolower(implode('', $letters));
  431. $listorder = $cat['listorder'] ? $cat['listorder'] : $catid;
  432. $this->sethtml = $setting['create_to_html_root'];
  433. //检查是否生成到根目录
  434. $this->get_sethtml($catid);
  435. $sethtml = $this->sethtml ? 1 : 0;
  436. if($setting['ishtml']) {
  437. //生成静态时
  438. $url = $this->update_url($catid);
  439. if(!preg_match('/^(http|https):\/\//i', $url)) {
  440. $url = $sethtml ? '/'.$url : $html_root.'/'.$url;
  441. }
  442. } else {
  443. //不生成静态时
  444. $url = $this->update_url($catid);
  445. $url = APP_PATH.$url;
  446. }
  447. if($cat['url']!=$url) $this->db->update(array('url'=>$url), array('catid'=>$catid));
  448. if($categorys[$catid]['parentdir']!=$parentdir || $categorys[$catid]['sethtml']!=$sethtml || $categorys[$catid]['letter']!=$letter || $categorys[$catid]['listorder']!=$listorder) $this->db->update(array('parentdir'=>$parentdir,'sethtml'=>$sethtml,'letter'=>$letter,'listorder'=>$listorder), array('catid'=>$catid));
  449. }
  450. }
  451. //删除在非正常显示的栏目
  452. foreach($this->categorys as $catid => $cat) {
  453. if($cat['parentid'] != 0 && !isset($this->categorys[$cat['parentid']])) {
  454. $this->db->delete(array('catid'=>$catid));
  455. }
  456. }
  457. return true;
  458. }
  459. /**
  460. * 获取父栏目是否生成到根目录
  461. */
  462. private function get_sethtml($catid) {
  463. foreach($this->categorys as $id => $cat) {
  464. if($catid==$id) {
  465. $parentid = $cat['parentid'];
  466. if($this->categorys[$parentid]['sethtml']) {
  467. $this->sethtml = 1;
  468. }
  469. if($parentid) {
  470. $this->get_sethtml($parentid);
  471. }
  472. }
  473. }
  474. }
  475. /**
  476. * 找出子目录列表
  477. * @param array $categorys
  478. */
  479. private function get_categorys($categorys = array()) {
  480. if (is_array($categorys) && !empty($categorys)) {
  481. foreach ($categorys as $catid => $c) {
  482. $this->categorys[$catid] = $c;
  483. $result = array();
  484. foreach ($this->categorys as $_k=>$_v) {
  485. if($_v['parentid']) $result[] = $_v;
  486. }
  487. $this->get_categorys($r);
  488. }
  489. }
  490. return true;
  491. }
  492. /**
  493. * 更新栏目链接地址
  494. */
  495. private function update_url($catid) {
  496. $catid = intval($catid);
  497. if (!$catid) return false;
  498. $url = pc_base::load_app_class('url', 'content'); //调用URL实例
  499. return $url->category_url($catid);
  500. }
  501. /**
  502. *
  503. * 获取父栏目ID列表
  504. * @param integer $catid 栏目ID
  505. * @param array $arrparentid 父目录ID
  506. * @param integer $n 查找的层次
  507. */
  508. private function get_arrparentid($catid, $arrparentid = '', $n = 1) {
  509. if($n > 5 || !is_array($this->categorys) || !isset($this->categorys[$catid])) return false;
  510. $parentid = $this->categorys[$catid]['parentid'];
  511. $arrparentid = $arrparentid ? $parentid.','.$arrparentid : $parentid;
  512. if($parentid) {
  513. $arrparentid = $this->get_arrparentid($parentid, $arrparentid, ++$n);
  514. } else {
  515. $this->categorys[$catid]['arrparentid'] = $arrparentid;
  516. }
  517. $parentid = $this->categorys[$catid]['parentid'];
  518. return $arrparentid;
  519. }
  520. /**
  521. *
  522. * 获取子栏目ID列表
  523. * @param $catid 栏目ID
  524. */
  525. private function get_arrchildid($catid) {
  526. $arrchildid = $catid;
  527. if(is_array($this->categorys)) {
  528. foreach($this->categorys as $id => $cat) {
  529. if($cat['parentid'] && $id != $catid && $cat['parentid']==$catid) {
  530. $arrchildid .= ','.$this->get_arrchildid($id);
  531. }
  532. }
  533. }
  534. return $arrchildid;
  535. }
  536. /**
  537. * 获取父栏目路径
  538. * @param $catid
  539. */
  540. function get_parentdir($catid) {
  541. if($this->categorys[$catid]['parentid']==0) return '';
  542. $r = $this->categorys[$catid];
  543. $setting = string2array($r['setting']);
  544. $url = $r['url'];
  545. $arrparentid = $r['arrparentid'];
  546. unset($r);
  547. if (strpos($url, '://')===false) {
  548. if ($setting['creat_to_html_root']) {
  549. return '';
  550. } else {
  551. $arrparentid = explode(',', $arrparentid);
  552. $arrcatdir = array();
  553. foreach($arrparentid as $id) {
  554. if($id==0) continue;
  555. $arrcatdir[] = $this->categorys[$id]['catdir'];
  556. }
  557. return implode('/', $arrcatdir).'/';
  558. }
  559. } else {
  560. if ($setting['create_to_html_root']) {
  561. if (preg_match('/^((http|https):\/\/)?([^\/]+)/i', $url, $matches)) {
  562. $url = $matches[0].'/';
  563. $rs = $this->db->get_one(array('url'=>$url), '`parentdir`,`catid`');
  564. if ($catid == $rs['catid']) return '';
  565. else return $rs['parentdir'];
  566. } else {
  567. return '';
  568. }
  569. } else {
  570. $arrparentid = explode(',', $arrparentid);
  571. $arrcatdir = array();
  572. krsort($arrparentid);
  573. foreach ($arrparentid as $id) {
  574. if ($id==0) continue;
  575. $arrcatdir[] = $this->categorys[$id]['catdir'];
  576. if ($this->categorys[$id]['parentdir'] == '') break;
  577. }
  578. krsort($arrcatdir);
  579. return implode('/', $arrcatdir).'/';
  580. }
  581. }
  582. }
  583. /**
  584. * 检查目录是否存在
  585. * @param $return_method 返回方法
  586. * @param $catdir 目录
  587. */
  588. public function public_check_catdir($return_method = 1,$catdir = '') {
  589. $old_dir = '';
  590. $catdir = $catdir ? $catdir : $_GET['catdir'];
  591. $parentid = intval($_GET['parentid']);
  592. $old_dir = $_GET['old_dir'];
  593. $r = $this->db->get_one(array('siteid'=>$this->siteid,'module'=>'content','catdir'=>$catdir,'parentid'=>$parentid));
  594. if($r && $old_dir != $r['catdir']) {
  595. //目录存在
  596. if($return_method) {
  597. exit('0');
  598. } else {
  599. return false;
  600. }
  601. } else {
  602. if($return_method) {
  603. exit('1');
  604. } else {
  605. return true;
  606. }
  607. }
  608. }
  609. /**
  610. * 更新权限
  611. * @param $catid
  612. * @param $priv_datas
  613. * @param $is_admin
  614. */
  615. private function update_priv($catid,$priv_datas,$is_admin = 1) {
  616. $this->priv_db = pc_base::load_model('category_priv_model');
  617. $this->priv_db->delete(array('catid'=>$catid,'is_admin'=>$is_admin));
  618. if(is_array($priv_datas) && !empty($priv_datas)) {
  619. foreach ($priv_datas as $r) {
  620. $r = explode(',', $r);
  621. $action = $r[0];
  622. $roleid = $r[1];
  623. $this->priv_db->insert(array('catid'=>$catid,'roleid'=>$roleid,'is_admin'=>$is_admin,'action'=>$action,'siteid'=>$this->siteid));
  624. }
  625. }
  626. }
  627. /**
  628. * 检查栏目权限
  629. * @param $action 动作
  630. * @param $roleid 角色
  631. * @param $is_admin 是否为管理组
  632. */
  633. private function check_category_priv($action,$roleid,$is_admin = 1) {
  634. $checked = '';
  635. foreach ($this->privs as $priv) {
  636. if($priv['is_admin']==$is_admin && $priv['roleid']==$roleid && $priv['action']==$action) $checked = 'checked';
  637. }
  638. return $checked;
  639. }
  640. /**
  641. * 重新统计栏目信息数量
  642. */
  643. public function count_items() {
  644. $this->content_db = pc_base::load_model('content_model');
  645. $result = getcache('category_content_'.$this->siteid,'commons');
  646. foreach($result as $r) {
  647. if($r['type'] == 0) {
  648. $modelid = $r['modelid'];
  649. $this->content_db->set_model($modelid);
  650. $number = $this->content_db->count(array('catid'=>$r['catid']));
  651. $this->db->update(array('items'=>$number),array('catid'=>$r['catid']));
  652. }
  653. }
  654. showmessage(L('operation_success'),HTTP_REFERER);
  655. }
  656. /**
  657. * json方式加载模板
  658. */
  659. public function public_tpl_file_list() {
  660. $style = isset($_GET['style']) && trim($_GET['style']) ? trim($_GET['style']) : exit(0);
  661. $catid = isset($_GET['catid']) && intval($_GET['catid']) ? intval($_GET['catid']) : 0;
  662. $batch_str = isset($_GET['batch_str']) ? '['.$catid.']' : '';
  663. if ($catid) {
  664. $cat = getcache('category_content_'.$this->siteid,'commons');
  665. $cat = $cat[$catid];
  666. $cat['setting'] = string2array($cat['setting']);
  667. }
  668. pc_base::load_sys_class('form','',0);
  669. if($_GET['type']==1) {
  670. $html = array('page_template'=>form::select_template($style, 'content',(isset($cat['setting']['page_template']) && !empty($cat['setting']['page_template']) ? $cat['setting']['page_template'] : 'category'),'name="setting'.$batch_str.'[page_template]"','page'));
  671. } else {
  672. $html = array('category_template'=> form::select_template($style, 'content',(isset($cat['setting']['category_template']) && !empty($cat['setting']['category_template']) ? $cat['setting']['category_template'] : 'category'),'name="setting'.$batch_str.'[category_template]"','category'),
  673. 'list_template'=>form::select_template($style, 'content',(isset($cat['setting']['list_template']) && !empty($cat['setting']['list_template']) ? $cat['setting']['list_template'] : 'list'),'name="setting'.$batch_str.'[list_template]"','list'),
  674. 'show_template'=>form::select_template($style, 'content',(isset($cat['setting']['show_template']) && !empty($cat['setting']['show_template']) ? $cat['setting']['show_template'] : 'show'),'name="setting'.$batch_str.'[show_template]"','show')
  675. );
  676. }
  677. if ($_GET['module']) {
  678. unset($html);
  679. if ($_GET['templates']) {
  680. $templates = explode('|', $_GET['templates']);
  681. if ($_GET['id']) $id = explode('|', $_GET['id']);
  682. if (is_array($templates)) {
  683. foreach ($templates as $k => $tem) {
  684. $t = $tem.'_template';
  685. if ($id[$k]=='') $id[$k] = $tem;
  686. $html[$t] = form::select_template($style, $_GET['module'], $id[$k], 'name="'.$_GET['name'].'['.$t.']" id="'.$t.'"', $tem);
  687. }
  688. }
  689. }
  690. }
  691. if (CHARSET == 'gbk') {
  692. $html = array_iconv($html, 'gbk', 'utf-8');
  693. }
  694. echo json_encode($html);
  695. }
  696. /**
  697. * 快速进入搜索
  698. */
  699. public function public_ajax_search() {
  700. if($_GET['catname']) {
  701. if(preg_match('/([a-z]+)/i',$_GET['catname'])) {
  702. $field = 'letter';
  703. $catname = strtolower(trim($_GET['catname']));
  704. } else {
  705. $field = 'catname';
  706. $catname = trim($_GET['catname']);
  707. if (CHARSET == 'gbk') $catname = iconv('utf-8','gbk',$catname);
  708. }
  709. $result = $this->db->select("$field LIKE('$catname%') AND siteid='$this->siteid' AND child=0",'catid,type,catname,letter',10);
  710. if (CHARSET == 'gbk') {
  711. $result = array_iconv($result, 'gbk', 'utf-8');
  712. }
  713. echo json_encode($result);
  714. }
  715. }
  716. /**
  717. * json方式读取风格列表,推送部分调用
  718. */
  719. public function public_change_tpl() {
  720. pc_base::load_sys_class('form','',0);
  721. $models = getcache('model','commons');
  722. $modelid = intval($_GET['modelid']);
  723. if($_GET['modelid']) {
  724. $style = $models[$modelid]['default_style'];
  725. $category_template = $models[$modelid]['category_template'];
  726. $list_template = $models[$modelid]['list_template'];
  727. $show_template = $models[$modelid]['show_template'];
  728. $html = array(
  729. 'template_list'=> $style,
  730. 'category_template'=> form::select_template($style, 'content',$category_template,'name="setting[category_template]"','category'),
  731. 'list_template'=>form::select_template($style, 'content',$list_template,'name="setting[list_template]"','list'),
  732. 'show_template'=>form::select_template($style, 'content',$show_template,'name="setting[show_template]"','show')
  733. );
  734. if (CHARSET == 'gbk') {
  735. $html = array_iconv($html, 'gbk', 'utf-8');
  736. }
  737. echo json_encode($html);
  738. }
  739. }
  740. /**
  741. * 批量修改
  742. */
  743. public function batch_edit() {
  744. $categorys = getcache('category_content_'.$this->siteid,'commons');
  745. if(isset($_POST['dosubmit'])) {
  746. pc_base::load_sys_func('iconv');
  747. $catid = intval($_POST['catid']);
  748. $post_setting = $_POST['setting'];
  749. //栏目生成静态配置
  750. $infos = $info = array();
  751. $infos = $_POST['info'];
  752. if(empty($infos)) showmessage(L('operation_success'));
  753. $this->attachment_db = pc_base::load_model('attachment_model');
  754. foreach ($infos as $catid=>$info) {
  755. $setting = string2array($categorys[$catid]['setting']);
  756. if($_POST['type'] != 2) {
  757. if($post_setting[$catid]['ishtml']) {
  758. $setting['category_ruleid'] = $_POST['category_html_ruleid'][$catid];
  759. } else {
  760. $setting['category_ruleid'] = $_POST['category_php_ruleid'][$catid];
  761. $info['url'] = '';
  762. }
  763. }
  764. foreach($post_setting[$catid] as $_k=>$_setting) {
  765. $setting[$_k] = $_setting;
  766. }
  767. //内容生成静态配置
  768. if($post_setting[$catid]['content_ishtml']) {
  769. $setting['show_ruleid'] = $_POST['show_html_ruleid'][$catid];
  770. } else {
  771. $setting['show_ruleid'] = $_POST['show_php_ruleid'][$catid];
  772. }
  773. if($setting['repeatchargedays']<1) $setting['repeatchargedays'] = 1;
  774. $info['sethtml'] = $post_setting[$catid]['create_to_html_root'];
  775. $info['setting'] = array2string($setting);
  776. $info['module'] = 'content';
  777. $catname = CHARSET == 'gbk' ? $info['catname'] : iconv('utf-8','gbk',$info['catname']);
  778. $letters = gbk_to_pinyin($catname);
  779. $info['letter'] = strtolower(implode('', $letters));
  780. $this->db->update($info,array('catid'=>$catid,'siteid'=>$this->siteid));
  781. //更新附件状态
  782. if($info['image'] && pc_base::load_config('system','attachment_stat')) {
  783. $this->attachment_db->api_update($info['image'],'catid-'.$catid,1);
  784. }
  785. }
  786. $this->public_cache();
  787. showmessage(L('operation_success'),'?m=admin&c=category&a=init&module=admin&menuid=43');
  788. } else {
  789. if(isset($_POST['catids'])) {
  790. //获取站点模板信息
  791. pc_base::load_app_func('global');
  792. $template_list = template_list($this->siteid, 0);
  793. foreach ($template_list as $k=>$v) {
  794. $template_list[$v['dirname']] = $v['name'] ? $v['name'] : $v['dirname'];
  795. unset($template_list[$k]);
  796. }
  797. $show_validator = $show_header = '';
  798. $catid = intval($_GET['catid']);
  799. $type = $_POST['type'] ? intval($_POST['type']) : 0;
  800. pc_base::load_sys_class('form','',0);
  801. if(empty($_POST['catids'])) showmessage(L('illegal_parameters'));
  802. $batch_array = $workflows = array();
  803. foreach ($categorys as $catid=>$cat) {
  804. if($cat['type']==$type && in_array($catid, $_POST['catids'])) {
  805. $batch_array[$catid] = $cat;
  806. }
  807. }
  808. if(empty($batch_array)) showmessage(L('please_select_category'));
  809. $workflows = getcache('workflow_'.$this->siteid,'commons');
  810. if($workflows) {
  811. $workflows_datas = array();
  812. foreach($workflows as $_k=>$_v) {
  813. $workflows_datas[$_v['workflowid']] = $_v['workname'];
  814. }
  815. }
  816. if($type==1) {
  817. include $this->admin_tpl('category_batch_edit_page');
  818. } else {
  819. include $this->admin_tpl('category_batch_edit');
  820. }
  821. } else {
  822. $type = isset($_GET['select_type']) ? intval($_GET['select_type']) : 0;
  823. $tree = pc_base::load_sys_class('tree');
  824. $tree->icon = array('&nbsp;&nbsp;│ ','&nbsp;&nbsp;├─ ','&nbsp;&nbsp;└─ ');
  825. $tree->nbsp = '&nbsp;&nbsp;';
  826. $category = array();
  827. foreach($categorys as $catid=>$r) {
  828. if($this->siteid != $r['siteid'] || ($r['type']==2 && $r['child']==0)) continue;
  829. $category[$catid] = $r;
  830. }
  831. $str = "<option value='\$catid' \$selected>\$spacer \$catname</option>";
  832. $tree->init($category);
  833. $string .= $tree->get_tree(0, $str);
  834. include $this->admin_tpl('category_batch_select');
  835. }
  836. }
  837. }
  838. /**
  839. * 批量移动文章
  840. */
  841. public function remove() {
  842. $this->categorys = getcache('category_content_'.$this->siteid,'commons');
  843. $this->content_db = pc_base::load_model('content_model');
  844. if(isset($_POST['dosubmit'])) {
  845. $this->content_check_db = pc_base::load_model('content_check_model');
  846. if(!$_POST['fromid']) showmessage(L('please_input_move_source','','content'));
  847. if(!$_POST['tocatid']) showmessage(L('please_select_target_category','','content'));
  848. $tocatid = intval($_POST['tocatid']);
  849. $modelid = $this->categorys[$tocatid]['modelid'];
  850. if(!$modelid) showmessage(L('illegal_operation','','content'));
  851. $fromid = array_filter($_POST['fromid'],"is_numeric");
  852. $fromid = implode(',', $fromid);
  853. $this->content_db->set_model($modelid);
  854. $this->content_db->update(array('catid'=>$tocatid),"catid IN($fromid)");
  855. showmessage(L('operation_success'),HTTP_REFERER);
  856. } else {
  857. $show_header = '';
  858. $catid = intval($_GET['catid']);
  859. $categorys = array();
  860. $modelid = $this->categorys[$catid]['modelid'];
  861. $tree = pc_base::load_sys_class('tree');
  862. $tree->icon = array('&nbsp;&nbsp;│ ','&nbsp;&nbsp;├─ ','&nbsp;&nbsp;└─ ');
  863. $tree->nbsp = '&nbsp;&nbsp;';
  864. foreach($this->categorys as $cid=>$r) {
  865. if($this->siteid != $r['siteid'] || $r['type']) continue;
  866. if($modelid && $modelid != $r['modelid']) continue;
  867. $r['disabled'] = $r['child'] ? 'disabled' : '';
  868. $r['selected'] = $cid == $catid ? 'selected' : '';
  869. $categorys[$cid] = $r;
  870. }
  871. $str = "<option value='\$catid' \$disabled>\$spacer \$catname</option>";
  872. $tree->init($categorys);
  873. $string .= $tree->get_tree(0, $str);
  874. $str = "<option value='\$catid' \$selected>\$spacer \$catname</option>";
  875. $source_string = '';
  876. $tree->init($categorys);
  877. $source_string .= $tree->get_tree(0, $str);
  878. include $this->admin_tpl('category_remove');
  879. }
  880. }
  881. }
  882. ?>