phpcms V9 欄目管理

關於phpcms V9框架系統後臺管理之欄目管理,請參見下文的源碼分析(添加欄目和修改欄目):javascript

參照添加欄目的界面圖示,便於對源代碼的理解:php

  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     }

系統包含模塊,模塊包括欄目,欄目綁定模型,模型指定模板。html

 

Good  Good Study, Day Day Up.java

順序  選擇  循環  總結sql