phpcms V9 栏目管理

关于phpcms V9框架系统后台管理之栏目管理,请参见下文的源码分析(添加栏目和修改栏目):

参照添加栏目的界面图示,便于对源代码的理解:

  1 <?php
  2 
  3 // 文件路径:phpcms/modules/admin/category.php 控制器,主要用来控制栏目相关的操作
  4 
  5 defined('IN_PHPCMS') or exit('No permission resources.');
  6 pc_base::load_app_class('admin','admin',0); // 加载admin模块下的admin.class.php类库文件
  7 
  8 class category extends admin 
  9 {
 10     private $db;
 11     public $siteid;
 12     function __construct() 
 13     {
 14         parent::__construct(); // 调父类构造 即admin.class.php文件中类的构造函数
 15         $this->db = pc_base::load_model('category_model'); // 加载模型数据库
 16         $this->siteid = $this->get_siteid(); // 调用父类方法获取当前站点ID
 17     }
 18     /**
 19      * 管理栏目
 20      */
 21     public function init () 
 22     {
 23         $show_pc_hash = '';
 24         $tree = pc_base::load_sys_class('tree'); //加载树型类,可以生产任何的树型结构,返回一个树型的实例化对象。
 25         $models = getcache('model','commons');  //获取"caches/caches_commons/caches_data/model.cache.php"中缓存的模型相关数据  
 26         $sitelist = getcache('sitelist','commons');//获取"caches/caches_commons/caches_data/sitelist.cache.php"中缓存数据  
 27         $category_items = array(); // 定义数组
 28         foreach ($models as $modelid=>$model) //模型id=>模型信息(模型:文章模型、下载模型、图片模型)
 29         {
 30             //当前模型id下所有栏目是否有子栏目,1-有 0-无
 31             $category_items[$modelid] = getcache('category_items_'.$modelid,'commons');
 32         }
 33         $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ','&nbsp;&nbsp;&nbsp;├─ ','&nbsp;&nbsp;&nbsp;└─ '); // 树状结构图
 34         $tree->nbsp = '&nbsp;&nbsp;&nbsp;'; // &nbsp 注意这个为空格。此句意思即三个空格。
 35         $categorys = array();
 36         //读取缓存
 37         $result = getcache('category_content_'.$this->siteid,'commons'); //当前站点ID下所有栏目的详细配置信息
 38         $show_detail = count($result) < 500 ? 1 : 0;
 39         $parentid = $_GET['parentid'] ? intval($_GET['parentid']) : 0; //此时$parentid=0,代表顶级分类
 40         $html_root = pc_base::load_config('system','html_root'); //生成静态文件的路径 ,默认为"/html" 
 41         //0-内部栏目、1-<font color="blue">单网页</font> 、2-<font color="red">外部链接</font> 
 42         $types = array(0 => L('category_type_system'),1 => L('category_type_page'),2 => L('category_type_link'));
 43         if(!empty($result)) 
 44         {
 45             foreach($result as $r) 
 46             {
 47                 $r['modelname'] = $models[$r['modelid']]['name']; //空、文章模型、下载模型、图片模型
 48                 $r['str_manage'] = '';
 49                 if(!$show_detail) 
 50                 {
 51                     if($r['parentid']!=$parentid) 
 52                         continue;
 53                     $r['parentid'] = 0;
 54                     $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> | ';
 55                 }
 56                 //添加子栏目的url链接       $r['type']:1-单网页  0-有子栏目的分类
 57                 $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> | ';
 58                 //修改、删除、批量移动 的url链接
 59                 $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>';
 60                 //0-内部栏目、1-单网页、2-外部链接
 61                 $r['typename'] = $types[$r['type']];
 62                 //是否在导航显示:1-显示   2-不显示    注意:不显示时会出现一个不在导航显示的小图标 
 63                 $r['display_icon'] = $r['ismenu'] ? '' : ' <img src ="'.IMG_PATH.'icon/gear_disable.png" title="'.L('not_display_in_menu').'">';
 64                 //如果为单网页或无子分类
 65                 if($r['type'] || $r['child']) 
 66                 {
 67                     $r['items'] = ''; // 数据量
 68                 } 
 69                 else 
 70                 {
 71                     $r['items'] = $category_items[$r['modelid']][$r['catid']];
 72                 }
 73                 $r['help'] = '';
 74                 //将字符串转换为数组:$r['setting']-添加分类时的配置项,是一个字符串
 75                 $setting = string2array($r['setting']);
 76                 if($r['url']) 
 77                 {
 78                     if(preg_match('/^(http|https):\/\//', $r['url'])) 
 79                     {
 80                         //分类目录名称
 81                         $catdir = $r['catdir'];
 82                         //是否生成到根目录
 83                         $prefix = $r['sethtml'] ? '' : $html_root;
 84                         if($this->siteid==1) 
 85                         {
 86                             //分类目录:"html/父级目录名称/当前目录名称"
 87                             $catdir = $prefix.'/'.$r['parentdir'].$catdir;
 88                         } 
 89                         else 
 90                         {
 91                             $catdir = $prefix.'/'.$sitelist[$this->siteid]['dirname'].$html_root.'/'.$catdir;
 92                         }
 93                         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.'/">';
 94                     } 
 95                     else 
 96                     { // 分站点
 97                         $r['url'] = substr($sitelist[$this->siteid]['domain'],0,-1).$r['url'];
 98                     }
 99                     // 访问
100                     $r['url'] = "<a href='$r[url]' target='_blank'>".L('vistor')."</a>";
101                 } 
102                 else 
103                 {
104                     $r['url'] = "<a href='?m=admin&c=category&a=public_cache&menuid=43&module=admin'><font color='red'>".L('update_backup')."</font></a>";
105                 }
106                 // 分类信息
107                 $categorys[$r['catid']] = $r;
108             }
109         }
110         $str  = "<tr>
111                     <td align='center'><input name='listorders[\$id]' type='text' size='3' value='\$listorder' class='input-text-c'></td>
112                     <td align='center'>\$id</td>
113                     <td >\$spacer\$catname\$display_icon</td>
114                     <td>\$typename</td>
115                     <td>\$modelname</td>
116                     <td align='center'>\$items</td>
117                     <td align='center'>\$url</td>
118                     <td align='center'>\$help</td>
119                     <td align='center' >\$str_manage</td>
120                 </tr>";
121         $tree->init($categorys);
122         //生成分类树相关的html代码
123         $categorys = $tree->get_tree(0, $str);
124         //模版文件 
125         include $this->admin_tpl('category_manage');
126     }
127     
128     /**
129      * 添加栏目
130      */
131     public function add() 
132     {
133         // 点击”提交“按钮
134         if(isset($_POST['dosubmit'])) 
135         {
136             pc_base::load_sys_func('iconv');
137             $_POST['info']['type'] = intval($_POST['type']); //0-内部栏目 1-单网页  2-外部链接
138             
139             //如果不是批量添加栏目名称
140             if(isset($_POST['batch_add']) && empty($_POST['batch_add'])) 
141             {
142                 if($_POST['info']['catname']=='') //如果单条添加栏目名称为空,则给出提示
143                     showmessage(L('input_catname'));
144                 $_POST['info']['catname'] = safe_replace($_POST['info']['catname']);
145                 $_POST['info']['catname'] = str_replace(array('%'),'',$_POST['info']['catname']);
146                 if($_POST['info']['type'] != 2) //如果不是外部链接 
147                 {    //如果英文目录为空,则给出提示信息
148                     if($_POST['info']['catdir']=='') 
149                         showmessage(L('input_dirname'));
150                     if(!$this->public_check_catdir(0,$_POST['info']['catdir'])) showmessage(L('catname_have_exists'));
151                 }
152             }
153             
154             $_POST['info']['siteid'] = $this->siteid; //当前站点ID
155             $_POST['info']['module'] = 'content'; //content模块
156             $setting = $_POST['setting'];
157             if($_POST['info']['type']!=2)  //如果不是外部链接
158             {
159                 //栏目生成静态配置
160                 if($setting['ishtml']) //如果栏目生成HTML
161                 {
162                     $setting['category_ruleid'] = $_POST['category_html_ruleid']; //栏目页的url规则
163                 } 
164                 else 
165                 {
166                     $setting['category_ruleid'] = $_POST['category_php_ruleid'];
167                     $_POST['info']['url'] = ''; //绑定域名
168                 }
169             }
170             
171             //内容生成静态配置
172             if($setting['content_ishtml']) //如果内容生成HTML
173             {
174                 $setting['show_ruleid'] = $_POST['show_html_ruleid'];//内容页的url规则
175             } 
176             else 
177             {
178                 $setting['show_ruleid'] = $_POST['show_php_ruleid'];
179             }
180             if($setting['repeatchargedays']<1)   //重复收费设置,如:一天内不重复收费
181                 $setting['repeatchargedays'] = 1;
182             $_POST['info']['sethtml'] = $setting['create_to_html_root']; //是否生成到根目录
183             $_POST['info']['setting'] = array2string($setting); //将这些设置项转化为一个字符串
184             
185             $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>';
186             if(!isset($_POST['batch_add']) || empty($_POST['batch_add'])) 
187             {
188                 //目录的名称必须是gbk编码格式
189                 $catname = CHARSET == 'gbk' ? $_POST['info']['catname'] : iconv('utf-8','gbk',$_POST['info']['catname']);
190                 $letters = gbk_to_pinyin($catname); //将gbk编码格式的中文目录名称转化为拼音
191                 $_POST['info']['letter'] = strtolower(implode('', $letters));
192                 //将数据插入到category数据表,并返回刚插入的栏目id,注意:insert函数在libs/classes/model.class.php类库中
193                 $catid = $this->db->insert($_POST['info'], true);
194                 //更新角色权限
195                 $this->update_priv($catid, $_POST['priv_roleid']);
196                 //更新会员组权限
197                 $this->update_priv($catid, $_POST['priv_groupid'],0);
198             } 
199             else 
200             {//批量添加
201                 $end_str = '';
202                 $batch_adds = explode("\n", $_POST['batch_add']); //批量添加栏目,以\n分割多个栏目名称
203                 foreach ($batch_adds as $_v) 
204                 {
205                     if(trim($_v)=='') 
206                         continue;
207                     $names = explode('|', $_v); //以"|"分割栏目中文名称与栏目英文名称
208                     $catname = $names[0];
209                     $_POST['info']['catname'] = trim($names[0]); //栏目中文名称
210                     $letters = gbk_to_pinyin($catname); //栏目的拼音
211                     $_POST['info']['letter'] = strtolower(implode('', $letters));
212                     //如果栏目英文名称存在,则使用英文名称作为英文目录,否则使用拼音作为英文目录
213                     $_POST['info']['catdir'] = trim($names[1]) ? trim($names[1]) : trim($_POST['info']['letter']);
214                     // 若不存在目录
215                     if(!$this->public_check_catdir(0,$_POST['info']['catdir'])) 
216                     {
217                         $end_str .= $end_str ? ','.$_POST['info']['catname'].'('.$_POST['info']['catdir'].')' : $_POST['info']['catname'].'('.$_POST['info']['catdir'].')';
218                         continue;
219                     }
220                     $catid = $this->db->insert($_POST['info'], true);
221                     $this->update_priv($catid, $_POST['priv_roleid']);
222                     $this->update_priv($catid, $_POST['priv_groupid'],0);
223                 }
224                 $end_str = $end_str ? L('follow_catname_have_exists').$end_str : $old_end;
225             }
226             $this->cache(); //更新缓存
227             showmessage(L('add_success').$end_str); //添加成功提示信息
228         } 
229         else 
230         {
231             /*
232             * 获取站点模板信息
233             */
234             //加载global.func.php文件
235             pc_base::load_app_func('global');
236 
237             $template_list = template_list($this->siteid, 0);
238             foreach ($template_list as $k=>$v) 
239             {
240                 //$template_list['default']='默认模板'
241                 $template_list[$v['dirname']] = $v['name'] ? $v['name'] : $v['dirname'];
242                 unset($template_list[$k]);
243             }
244             $show_validator = '';
245             if(isset($_GET['parentid']))  //添加子栏目
246             {
247                 $parentid = $_GET['parentid'];
248                 $r = $this->db->get_one(array('catid'=>$parentid));
249                 if($r) extract($r,EXTR_SKIP);
250                 $setting = string2array($setting);
251             }
252             //加载form.class.php类库
253             pc_base::load_sys_class('form','',0);
254             $type = $_GET['s']; //添加子栏目时会传递此参数:0-内部栏目、1-单网页、2-外部链接
255             if($type == 0)   //0-内部栏目
256             {
257                 $exists_model = false; //模型是否存在的标识
258                 $models = getcache('model','commons');      //获取默认的三种模型信息:文章模型、下载模型、图片模型
259                 foreach($models as $_m) 
260                 {
261                     if($this->siteid == $_m['siteid']) //如果这默认的三种模型属于当前站点ID
262                     {
263                         $exists_model = true; //将模型是否存在标识赋值给true
264                         break;
265                     }
266                 }
267                 if(!$exists_model) 
268                     showmessage(L('please_add_model'),'?m=content&c=sitemodel&a=init&menuid=59',5000);
269                 include $this->admin_tpl('category_add'); // 模版文件category_add.tpl.php
270             } 
271             elseif ($type==1) 
272             {
273                 include $this->admin_tpl('category_page_add'); // 模版文件category_page_add.tpl.php
274             } 
275             else 
276             {
277                 include $this->admin_tpl('category_link');// 模版文件category_link.tpl.php
278             }
279         }
280     }
281     /**
282      * 修改栏目
283      */
284     public function edit() 
285     {
286         if(isset($_POST['dosubmit'])) 
287         {
288             pc_base::load_sys_func('iconv');
289             $catid = 0;
290             $catid = intval($_POST['catid']);
291             $setting = $_POST['setting'];
292             //上级栏目不能是自身
293             if($_POST['info']['parentid']==$catid)
294             {
295                 showmessage(L('operation_failure'),'?m=admin&c=category&a=init&module=admin&menuid=43');
296             }
297             //栏目生成静态配置
298             if($_POST['type'] != 2) 
299             {
300                 if($setting['ishtml']) 
301                 {
302                     $setting['category_ruleid'] = $_POST['category_html_ruleid'];
303                 } 
304                 else 
305                 {
306                     $setting['category_ruleid'] = $_POST['category_php_ruleid'];
307                     $_POST['info']['url'] = '';
308                 }
309             }
310             //内容生成静态配置
311             if($setting['content_ishtml']) 
312             {
313                 $setting['show_ruleid'] = $_POST['show_html_ruleid'];
314             } 
315             else 
316             {
317                 $setting['show_ruleid'] = $_POST['show_php_ruleid'];
318             }
319             if($setting['repeatchargedays']<1) 
320                 $setting['repeatchargedays'] = 1;
321             $_POST['info']['sethtml'] = $setting['create_to_html_root'];
322             $_POST['info']['setting'] = array2string($setting);
323             $_POST['info']['module'] = 'content';
324             $catname = CHARSET == 'gbk' ? safe_replace($_POST['info']['catname']) : iconv('utf-8','gbk',safe_replace($_POST['info']['catname']));
325             $catname = str_replace(array('%'),'',$catname);
326             $letters = gbk_to_pinyin($catname);
327             $_POST['info']['letter'] = strtolower(implode('', $letters));
328             
329             //应用权限设置到子栏目
330             if($_POST['priv_child']) 
331             {
332                 $arrchildid = $this->db->get_one(array('catid'=>$catid), 'arrchildid');
333                 if(!empty($arrchildid['arrchildid'])) 
334                 {
335                     $arrchildid_arr = explode(',', $arrchildid['arrchildid']);
336                     if(!empty($arrchildid_arr)) 
337                     {
338                         foreach ($arrchildid_arr as $arr_v) 
339                         {
340                             $this->update_priv($arr_v, $_POST['priv_groupid'], 0);
341                         }
342                     }
343                 }
344             }
345             
346             //应用模版到所有子栏目
347             if($_POST['template_child'])
348             {
349                 $this->categorys = $categorys = $this->db->select(array('siteid'=>$this->siteid,'module'=>'content'), '*', '', 'listorder ASC, catid ASC', '', 'catid');
350                 $idstr = $this->get_arrchildid($catid);
351                  if(!empty($idstr))
352                  {
353                     $sql = "select catid,setting from phpcms_category where catid in($idstr)";
354                     $this->db->query($sql);
355                     $arr = $this->db->fetch_array();
356                      if(!empty($arr))
357                      {
358                         foreach ($arr as $v)
359                         {
360                             $new_setting = array2string(
361                             array_merge(string2array($v['setting']), array('category_template' => $_POST['setting']['category_template'],'list_template' =>  $_POST['setting']['list_template'],'show_template' =>  $_POST['setting']['show_template'])
362                                                     ));
363                             $this->db->update(array('setting'=>$new_setting), 'catid='.$v['catid']);
364                         }
365                     }                                
366                 }
367             }
368             
369             $this->db->update($_POST['info'],array('catid'=>$catid,'siteid'=>$this->siteid));
370             $this->update_priv($catid, $_POST['priv_roleid']);
371             $this->update_priv($catid, $_POST['priv_groupid'],0);
372             $this->cache(); //更新缓存
373             //更新附件状态
374             if($_POST['info']['image'] && pc_base::load_config('system','attachment_stat')) 
375             {
376                 $this->attachment_db = pc_base::load_model('attachment_model'); //加载附件数据库模型
377                 $this->attachment_db->api_update($_POST['info']['image'],'catid-'.$catid,1);
378             }
379             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');
380         } 
381         else 
382         {
383             //获取站点模板信息
384             pc_base::load_app_func('global');
385             $template_list = template_list($this->siteid, 0);
386             foreach ($template_list as $k=>$v) 
387             {
388                 $template_list[$v['dirname']] = $v['name'] ? $v['name'] : $v['dirname'];
389                 unset($template_list[$k]);
390             }
391             
392             $show_validator = $catid = $r = '';
393             $catid = intval($_GET['catid']);
394             pc_base::load_sys_class('form','',0);
395             $r = $this->db->get_one(array('catid'=>$catid));
396             if($r) 
397                 extract($r);
398             $setting = string2array($setting);
399             
400             $this->priv_db = pc_base::load_model('category_priv_model');
401             $this->privs = $this->priv_db->select(array('catid'=>$catid));
402             
403             $type = $_GET['type'];
404             // 依据类型选择模版文件 0-内部栏目、1-单网页、2-外部链接
405             if($type == 0) 
406             {
407                 include $this->admin_tpl('category_edit');
408             } 
409             elseif ($type == 1) 
410             {
411                 include $this->admin_tpl('category_page_edit');
412             } 
413             else 
414             {
415                 include $this->admin_tpl('category_link');
416             }
417         }
418     }

系统包含模块,模块包括栏目,栏目绑定模型,模型指定模板。

 

Good  Good Study, Day Day Up.

顺序  选择  循环  总结

posted @ 2016-01-05 20:29  kaizenly  阅读(3865)  评论(0编辑  收藏  举报
打赏