- <?php
- //路徑:phpcms/modules/admin/category.php控制器,主要用來完成欄目相關的操作
- defined('IN_PHPCMS') or exit('No permission resources.'); //防止惡意訪問
- pc_base::load_app_class('admin','admin',0); //加載admin模塊下的admin.class.php類庫文件
- class category extends admin {
- private $db; //代表當前category_model類的實例化對象
- public $siteid;
- function __construct() {
- parent::__construct(); //父類的構造函數,即:admin.class.php類的構造函數
- $this->db = pc_base::load_model('category_model'); //加載 category_model
- $this->siteid = $this->get_siteid(); //獲取當前的站點ID,父類方法
- }
- /**
- * 管理欄目
- */
- public function init () {
- $show_pc_hash = '';
- $tree = pc_base::load_sys_class('tree'); //加載系統通用的樹型類,可以生成任何樹型結構,返回一個樹類的實例化對象
- $models = getcache('model','commons'); //獲取"caches/caches_commons/caches_data/model.cache.php"中緩存的模型相關數據
- $sitelist = getcache('sitelist','commons'); //獲取"caches/caches_commons/caches_data/sitelist.cache.php"中緩存數據
- $category_items = array(); //定義一個數組
- foreach ($models as $modelid=>$model) { //模型id=>模型信息(模型:文章模型、下載模型、圖片模型)
- //$modelid:模型id
- $category_items[$modelid] = getcache('category_items_'.$modelid,'commons');//當前模型id下所有欄目是否有子欄目,1-有 0-沒有
- }
- $tree->icon = array(' │ ',' ├─ ',' └─ ');//樹形圖標
- $tree->nbsp = ' '; //三個空格
- $categorys = array();
- //讀取緩存
- $result = getcache('category_content_'.$this->siteid,'commons');//當前站點ID下所有欄目的詳細配置信息
- $show_detail = count($result) < 500 ? 1 : 0; //如果緩存文件中的分類數量小於500,則$show_detail=1
- $parentid = $_GET['parentid'] ? intval($_GET['parentid']) : 0; //此時$parentid=0,代表頂級分類
- $html_root = pc_base::load_config('system','html_root'); //生成靜態文件的路徑 ,默認爲"/html"
- //0-內部欄目、1-<font color="blue">單網頁</font> 、2-<font color="red">外部鏈接</font>
- $types = array(0 => L('category_type_system'),1 => L('category_type_page'),2 => L('category_type_link'));
- if(!empty($result)) {
- foreach($result as $r) {
- $r['modelname'] = $models[$r['modelid']]['name']; //空、文章模型、下載模型、圖片模型
- $r['str_manage'] = '';
- if(!$show_detail) {
- if($r['parentid']!=$parentid) continue;
- $r['parentid'] = 0;
- $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> | ';
- }
- //添加子欄目的url鏈接 $r['type']:1-單網頁 0-有子欄目的分類
- $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> | ';
- //修改、刪除、批量移動 的url鏈接
- $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>';
- //0-內部欄目、1-單網頁、2-外部鏈接
- $r['typename'] = $types[$r['type']];
- //是否在導航顯示:1-顯示 2-不顯示 注意:不顯示時會出現一個不在導航顯示的小圖標
- $r['display_icon'] = $r['ismenu'] ? '' : ' <img src ="'.IMG_PATH.'icon/gear_disable.png" title="'.L('not_display_in_menu').'">';
- //如果爲單網頁或無子分類
- if($r['type'] || $r['child']) {
- $r['items'] = '';//數據量爲空
- } else {
- //數據量
- $r['items'] = $category_items[$r['modelid']][$r['catid']];
- }
- $r['help'] = '';
- //將字符串轉換爲數組:$r['setting']-添加分類時的配置項,是一個字符串
- $setting = string2array($r['setting']);
- //訪問:如, 'url' => 'http://zhencms.com/html/dajiafengfan/'
- if($r['url']) {
- if(preg_match('/^(http|https):\/\//', $r['url'])) {
- //分類目錄名稱
- $catdir = $r['catdir'];
- //是否生成到根目錄
- $prefix = $r['sethtml'] ? '' : $html_root;
- if($this->siteid==1) {
- //分類目錄:"html/父級目錄名稱/當前目錄名稱"
- $catdir = $prefix.'/'.$r['parentdir'].$catdir;
- } else {
- $catdir = $prefix.'/'.$sitelist[$this->siteid]['dirname'].$html_root.'/'.$catdir;
- }
- 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'].'
- '.L('directory_binding').'
- '.$catdir.'/">';
- } else {
- //分站點
- $r['url'] = substr($sitelist[$this->siteid]['domain'],0,-1).$r['url'];
- }
- //訪問
- $r['url'] = "<a href='$r[url]' target='_blank'>".L('vistor')."</a>";
- } else {
- $r['url'] = "<a href='?m=admin&c=category&a=public_cache&menuid=43&module=admin'><font color='red'>".L('update_backup')."</font></a>";
- }
- //分類信息
- $categorys[$r['catid']] = $r;
- }
- }
- $str = "<tr>
- <td align='center'><input name='listorders[\$id]' type='text' size='3' value='\$listorder' class='input-text-c'></td>
- <td align='center'>\$id</td>
- <td >\$spacer\$catname\$display_icon</td>
- <td>\$typename</td>
- <td>\$modelname</td>
- <td align='center'>\$items</td>
- <td align='center'>\$url</td>
- <td align='center'>\$help</td>
- <td align='center' >\$str_manage</td>
- </tr>";
- //判斷$categorys是否是一個數組
- $tree->init($categorys);
- //生成分類樹相關的html代碼
- $categorys = $tree->get_tree(0, $str);
- //模板文件
- include $this->admin_tpl('category_manage');
- }
- /**
- * 添加欄目
- */
- public function add() {
- //點擊"提交"按鈕
- if(isset($_POST['dosubmit'])) {
- pc_base::load_sys_func('iconv');
- $_POST['info']['type'] = intval($_POST['type']); //0-內部欄目 1-單網頁 2-外部鏈接
- if(isset($_POST['batch_add']) && empty($_POST['batch_add'])) { //如果不是批量添加欄目名稱
- if($_POST['info']['catname']=='') showmessage(L('input_catname')); //如果單條添加欄目名稱爲空,則給出提示
- if($_POST['info']['type']!=2) { //如果不是外部鏈接
- if($_POST['info']['catdir']=='') showmessage(L('input_dirname'));//如果英文目錄爲空,則給出提示信息
- if(!$this->public_check_catdir(0,$_POST['info']['catdir'])) showmessage(L('catname_have_exists'));
- }
- }
- $_POST['info']['siteid'] = $this->siteid; //當前站點ID
- $_POST['info']['module'] = 'content'; //content模塊
- $setting = $_POST['setting'];
- if($_POST['info']['type']!=2) { //如果不是外部鏈接
- //欄目生成靜態配置
- if($setting['ishtml']) { //如果欄目生成HTML
- $setting['category_ruleid'] = $_POST['category_html_ruleid']; //欄目頁的url規則
- } else { //如果欄目不生成HTML
- $setting['category_ruleid'] = $_POST['category_php_ruleid']; //欄目頁的url規則
- $_POST['info']['url'] = ''; //綁定域名
- }
- }
- //內容生成靜態配置
- if($setting['content_ishtml']) { //如果內容生成HTML
- $setting['show_ruleid'] = $_POST['show_html_ruleid']; //內容頁的url規則
- } else { //如果內容不生成HTML
- $setting['show_ruleid'] = $_POST['show_php_ruleid']; //內容頁的url規則
- }
- if($setting['repeatchargedays']<1) $setting['repeatchargedays'] = 1; //重複收費設置,如:一天內不重複收費
- $_POST['info']['sethtml'] = $setting['create_to_html_root']; //是否生成到根目錄
- $_POST['info']['setting'] = array2string($setting); //將這些設置項轉化爲一個字符串
- $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>';
- if(!isset($_POST['batch_add']) || empty($_POST['batch_add'])) {
- //目錄的名稱必須是gbk編碼格式
- $catname = CHARSET == 'gbk' ? $_POST['info']['catname'] : iconv('utf-8','gbk',$_POST['info']['catname']);
- //將gbk編碼格式的中文目錄名稱轉化爲拼音
- $letters = gbk_to_pinyin($catname);
- $_POST['info']['letter'] = strtolower(implode('', $letters));
- //將數據插入到category數據表,並返回剛插入的欄目id,注意:insert函數在libs/classes/model.class.php類庫中
- $catid = $this->db->insert($_POST['info'], true);
- //角色權限
- $this->update_priv($catid, $_POST['priv_roleid']);
- //會員組權限
- $this->update_priv($catid, $_POST['priv_groupid'],0);
- } else {//批量添加
- $end_str = '';
- $batch_adds = explode("\n", $_POST['batch_add']); //批量添加欄目,以\n分割多個欄目名稱
- foreach ($batch_adds as $_v) {
- if(trim($_v)=='') continue;
- $names = explode('|', $_v); //以"|"分割欄目中文名稱與欄目英文名稱
- $catname = $names[0]; //欄目中文名稱
- $_POST['info']['catname'] = trim($names[0]); //欄目中文名稱
- $letters = gbk_to_pinyin($catname); //欄目的拼音
- $_POST['info']['letter'] = strtolower(implode('', $letters));
- //如果欄目英文名稱存在,則使用英文名稱作爲英文目錄,否則使用拼音作爲英文目錄
- $_POST['info']['catdir'] = trim($names[1]) ? trim($names[1]) : trim($_POST['info']['letter']);
- //如果目錄不存在
- if(!$this->public_check_catdir(0,$_POST['info']['catdir'])) {
- $end_str .= $end_str ? ','.$_POST['info']['catname'].'('.$_POST['info']['catdir'].')' : $_POST['info']['catname'].'('.$_POST['info']['catdir'].')';
- continue;
- }
- $catid = $this->db->insert($_POST['info'], true); //插入category數據表
- $this->update_priv($catid, $_POST['priv_roleid']); //更新角色權限
- $this->update_priv($catid, $_POST['priv_groupid'],0); //更新會員組權限
- }
- $end_str = $end_str ? L('follow_catname_have_exists').$end_str : $old_end;
- }
- $this->cache(); //更新緩存
- showmessage(L('add_success').$end_str); //添加成功提示信息
- } else {
- //獲取站點模板信息
- pc_base::load_app_func('global'); //加載global.func.php文件
- $template_list = template_list($this->siteid, 0);
- foreach ($template_list as $k=>$v) {
- //$template_list['default']='默認模板'
- $template_list[$v['dirname']] = $v['name'] ? $v['name'] : $v['dirname'];
- unset($template_list[$k]);
- }
- $show_validator = '';
- if(isset($_GET['parentid'])) { //添加子欄目
- $parentid = $_GET['parentid'];
- $r = $this->db->get_one(array('catid'=>$parentid));
- if($r) extract($r,EXTR_SKIP);
- $setting = string2array($setting);
- }
- pc_base::load_sys_class('form','',0); //加載form.class.php類庫
- $type = $_GET['s']; //添加子欄目時會傳遞此參數:0-內部欄目、1-單網頁、2-外部鏈接
- if($type==0) { //0-內部欄目
- $exists_model = false; //模型是否存在的標識
- $models = getcache('model','commons'); //獲取默認的三種模型信息:文章模型、下載模型、圖片模型
- foreach($models as $_m) {
- if($this->siteid == $_m['siteid']) { //如果這默認的三種模型屬於當前站點ID
- $exists_model = true; //將模型是否存在標識賦值給true
- break; //跳出foreach循環
- }
- }
- //如果模型不存在,則給出提示信息
- if(!$exists_model) showmessage(L('please_add_model'),'?m=content&c=sitemodel&a=init&menuid=59',5000);
- //顯示模板文件
- include $this->admin_tpl('category_add');
- } elseif ($type==1) {
- include $this->admin_tpl('category_page_add');
- } else {
- include $this->admin_tpl('category_link');
- }
- }
- }
- }
來源:http://blog.csdn.net/yanhui_wei/article/details/7901334