当前位置:首页 > CMS教程 > phpcms > 列表

【phpcms-v9】category.php文件分析-添加栏目

发布:smiling 来源: PHP粉丝网  添加日期:2014-10-22 17:01:40 浏览: 评论:0 
  1. <?php   
  2. //路径:phpcms/modules/admin/category.php控制器,主要用来完成栏目相关的操作   
  3. defined('IN_PHPCMS'or exit('No permission resources.');           //防止恶意访问   
  4. pc_base::load_app_class('admin','admin',0);                         //加载admin模块下的admin.class.php类库文件   
  5.    
  6. class category extends admin {   
  7.     private $db;                                                    //代表当前category_model类的实例化对象   
  8.     public $siteid;   
  9.     function __construct() {   
  10.         parent::__construct();                                      //父类的构造函数,即:admin.class.php类的构造函数   
  11.         $this->db = pc_base::load_model('category_model');           //加载 category_model   
  12.         $this->siteid = $this->get_siteid();                      //获取当前的站点ID,父类方法   
  13.     }   
  14.     /**   
  15.      * 管理栏目   
  16.      */   
  17.     public function init () {   
  18.         $show_pc_hash = '';   
  19.         $tree = pc_base::load_sys_class('tree');            //加载系统通用的树型类,可以生成任何树型结构,返回一个树类的实例化对象   
  20.         $models = getcache('model','commons');              //获取"caches/caches_commons/caches_data/model.cache.php"中缓存的模型相关数据   
  21.         $sitelist = getcache('sitelist','commons');         //获取"caches/caches_commons/caches_data/sitelist.cache.php"中缓存数据   
  22.         $category_items = array();                          //定义一个数组   
  23.         foreach ($models as $modelid=>$model) {              //模型id=>模型信息(模型:文章模型、下载模型、图片模型)   
  24.             //$modelid:模型id   
  25.             $category_items[$modelid] = getcache('category_items_'.$modelid,'commons');//当前模型id下所有栏目是否有子栏目,1-有 0-没有   
  26.         }   
  27.         $tree->icon = array('   │ ','   ├─ ','   └─ ');//树形图标   
  28.         $tree->nbsp = '   ';                                                          //三个空格   
  29.         $categorys = array();   
  30.         //读取缓存   
  31.         $result = getcache('category_content_'.$this->siteid,'commons');//当前站点ID下所有栏目的详细配置信息   
  32.         $show_detail = count($result) < 500 ? 1 : 0;                 //如果缓存文件中的分类数量小于500,则$show_detail=1   
  33.         $parentid = $_GET['parentid'] ? intval($_GET['parentid']) : 0;  //此时$parentid=0,代表顶级分类   
  34.         $html_root = pc_base::load_config('system','html_root');        //生成静态文件的路径 ,默认为"/html"   
  35.         //0-内部栏目、1-<font color="blue">单网页</font> 、2-<font color="red">外部链接</font>   
  36.         $types = array(0 => L('category_type_system'),1 => L('category_type_page'),2 => L('category_type_link'));   
  37.         if(!emptyempty($result)) {   
  38.             foreach($result as $r) {   
  39.                 $r['modelname'] = $models[$r['modelid']]['name'];       //空、文章模型、下载模型、图片模型   
  40.                 $r['str_manage'] = '';   
  41.                 if(!$show_detail) {   
  42.                     if($r['parentid']!=$parentidcontinue;   
  43.                     $r['parentid'] = 0;   
  44.                     $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> | ';   
  45.                 }   
  46.                 //添加子栏目的url链接       $r['type']:1-单网页  0-有子栏目的分类   
  47.                 $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> | ';   
  48.                 //修改、删除、批量移动 的url链接   
  49.                 $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>';   
  50.                 //0-内部栏目、1-单网页、2-外部链接   
  51.                 $r['typename'] = $types[$r['type']];   
  52.                 //是否在导航显示:1-显示   2-不显示    注意:不显示时会出现一个不在导航显示的小图标   
  53.                 $r['display_icon'] = $r['ismenu'] ? '' : ' <img src ="'.IMG_PATH.'icon/gear_disable.png" title="'.L('not_display_in_menu').'">';   
  54.                 //如果为单网页或无子分类   
  55.                 if($r['type'] || $r['child']) {   
  56.                     $r['items'] = '';//数据量为空   
  57.                 } else {   
  58.                     //数据量   
  59.                     $r['items'] = $category_items[$r['modelid']][$r['catid']];   
  60.                 }   
  61.                 $r['help'] = '';   
  62.                 //将字符串转换为数组:$r['setting']-添加分类时的配置项,是一个字符串   
  63.                 $setting = string2array($r['setting']);   
  64.                 //访问:如, 'url' => 'http://zhencms.com/html/dajiafengfan/'   
  65.                 if($r['url']) {   
  66.                     if(preg_match('/^(http|https):\/\//'$r['url'])) {   
  67.                         //分类目录名称   
  68.                         $catdir = $r['catdir'];   
  69.                         //是否生成到根目录   
  70.                         $prefix = $r['sethtml'] ? '' : $html_root;   
  71.                         if($this->siteid==1) {   
  72.                             //分类目录:"html/父级目录名称/当前目录名称"   
  73.                             $catdir = $prefix.'/'.$r['parentdir'].$catdir;   
  74.                         } else {   
  75.                             $catdir = $prefix.'/'.$sitelist[$this->siteid]['dirname'].$html_root.'/'.$catdir;   
  76.                         }   
  77.                         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'].'   
  78. '.L('directory_binding').'   
  79. '.$catdir.'/">';   
  80.                     } else {   
  81.                         //分站点   
  82.                         $r['url'] = substr($sitelist[$this->siteid]['domain'],0,-1).$r['url'];   
  83.                     }   
  84.                     //访问   
  85.                     $r['url'] = "<a href='$r[url]' target='_blank'>".L('vistor')."</a>";   
  86.                 } else {   
  87.                     $r['url'] = "<a href='?m=admin&c=category&a=public_cache&menuid=43&module=admin'><font color='red'>".L('update_backup')."</font></a>";   
  88.                 }   
  89.                 //分类信息   
  90.                 $categorys[$r['catid']] = $r;   
  91.             }   
  92.         }   
  93.         $str  = "<tr>   
  94.                     <td align='center'><input name='listorders[\$id]' type='text' size='3' value='\$listorder' class='input-text-c'></td>   
  95.                     <td align='center'>\$id</td>   
  96.                     <td >\$spacer\$catname\$display_icon</td>   
  97.                     <td>\$typename</td>   
  98.                     <td>\$modelname</td>   
  99.                     <td align='center'>\$items</td>   
  100.                     <td align='center'>\$url</td>   
  101.                     <td align='center'>\$help</td>   
  102.                     <td align='center' >\$str_manage</td>   
  103.                 </tr>";   
  104.         //判断$categorys是否是一个数组   
  105.         $tree->init($categorys);   
  106.         //生成分类树相关的html代码   
  107.         $categorys = $tree->get_tree(0, $str);   
  108.         //模板文件   
  109.         include $this->admin_tpl('category_manage');   
  110.     }   
  111.     /**   
  112.      * 添加栏目   
  113.      */   
  114.     public function add() {   
  115.         //点击"提交"按钮   
  116.         if(isset($_POST['dosubmit'])) {   
  117.             pc_base::load_sys_func('iconv');   
  118.             $_POST['info']['type'] = intval($_POST['type']);                        //0-内部栏目 1-单网页  2-外部链接   
  119.                
  120.             if(isset($_POST['batch_add']) && emptyempty($_POST['batch_add'])) {          //如果不是批量添加栏目名称   
  121.                 if($_POST['info']['catname']=='') showmessage(L('input_catname'));  //如果单条添加栏目名称为空,则给出提示   
  122.                 if($_POST['info']['type']!=2) {                                     //如果不是外部链接   
  123.                     if($_POST['info']['catdir']=='') showmessage(L('input_dirname'));//如果英文目录为空,则给出提示信息   
  124.                     if(!$this->public_check_catdir(0,$_POST['info']['catdir'])) showmessage(L('catname_have_exists'));   
  125.                 }   
  126.             }   
  127.                
  128.             $_POST['info']['siteid'] = $this->siteid;                                //当前站点ID   
  129.             $_POST['info']['module'] = 'content';                                   //content模块   
  130.             $setting = $_POST['setting'];   
  131.             if($_POST['info']['type']!=2) {                                         //如果不是外部链接   
  132.                 //栏目生成静态配置   
  133.                 if($setting['ishtml']) {                                            //如果栏目生成HTML   
  134.                     $setting['category_ruleid'] = $_POST['category_html_ruleid'];   //栏目页的url规则   
  135.                 } else {                                                            //如果栏目不生成HTML   
  136.                     $setting['category_ruleid'] = $_POST['category_php_ruleid'];    //栏目页的url规则   
  137.                     $_POST['info']['url'] = '';                                     //绑定域名   
  138.                 }   
  139.             }   
  140.                
  141.             //内容生成静态配置   
  142.             if($setting['content_ishtml']) {                                        //如果内容生成HTML   
  143.                 $setting['show_ruleid'] = $_POST['show_html_ruleid'];               //内容页的url规则   
  144.             } else {                                                                //如果内容不生成HTML   
  145.                 $setting['show_ruleid'] = $_POST['show_php_ruleid'];                //内容页的url规则   
  146.             }   
  147.             if($setting['repeatchargedays']<1) $setting['repeatchargedays'] = 1; //重复收费设置,如:一天内不重复收费   
  148.             $_POST['info']['sethtml'] = $setting['create_to_html_root'];            //是否生成到根目录   
  149.             $_POST['info']['setting'] = array2string($setting);                     //将这些设置项转化为一个字符串   
  150.                
  151.             $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>';   
  152.             if(!isset($_POST['batch_add']) || emptyempty($_POST['batch_add'])) {   
  153.                 //目录的名称必须是gbk编码格式   
  154.                 $catname = CHARSET == 'gbk' ? $_POST['info']['catname'] : iconv('utf-8','gbk',$_POST['info']['catname']);   
  155.                 //将gbk编码格式的中文目录名称转化为拼音   
  156.                 $letters = gbk_to_pinyin($catname);   
  157.                 $_POST['info']['letter'] = strtolower(implode(''$letters));   
  158.                 //将数据插入到category数据表,并返回刚插入的栏目id,注意:insert函数在libs/classes/model.class.php类库中   
  159.                 $catid = $this->db->insert($_POST['info'], true);   
  160.                 //角色权限   
  161.                 $this->update_priv($catid$_POST['priv_roleid']);   
  162.                 //会员组权限   
  163.                 $this->update_priv($catid$_POST['priv_groupid'],0);   
  164.             } else {//批量添加   
  165.                 $end_str = '';   
  166.                 $batch_adds = explode("\n"$_POST['batch_add']);                   //批量添加栏目,以\n分割多个栏目名称   
  167.                 foreach ($batch_adds as $_v) {   
  168.                     if(trim($_v)==''continue;   
  169.                     $names = explode('|'$_v);                                     //以"|"分割栏目中文名称与栏目英文名称   
  170.                     $catname = $names[0];                                           //栏目中文名称   
  171.                     $_POST['info']['catname'] = trim($names[0]);                    //栏目中文名称   
  172.                     $letters = gbk_to_pinyin($catname);                             //栏目的拼音   
  173.                     $_POST['info']['letter'] = strtolower(implode(''$letters));   
  174.                     //如果栏目英文名称存在,则使用英文名称作为英文目录,否则使用拼音作为英文目录   
  175.                     $_POST['info']['catdir'] = trim($names[1]) ? trim($names[1]) : trim($_POST['info']['letter']);   
  176.                     //如果目录不存在   
  177.                     if(!$this->public_check_catdir(0,$_POST['info']['catdir'])) {   
  178.                         $end_str .= $end_str ? ','.$_POST['info']['catname'].'('.$_POST['info']['catdir'].')' : $_POST['info']['catname'].'('.$_POST['info']['catdir'].')';   
  179.                         continue;   
  180.                     }   
  181.                     $catid = $this->db->insert($_POST['info'], true);             //插入category数据表   
  182.                     $this->update_priv($catid$_POST['priv_roleid']);               //更新角色权限   
  183.                     $this->update_priv($catid$_POST['priv_groupid'],0);            //更新会员组权限   
  184.                 }   
  185.                 $end_str = $end_str ? L('follow_catname_have_exists').$end_str : $old_end;   
  186.             }   
  187.             $this->cache();                                                          //更新缓存   
  188.             showmessage(L('add_success').$end_str);                                 //添加成功提示信息   
  189.         } else {   
  190.             //获取站点模板信息   
  191.             pc_base::load_app_func('global');                   //加载global.func.php文件   
  192.    
  193.             $template_list = template_list($this->siteid, 0);   
  194.             foreach ($template_list as $k=>$v) {   
  195.                 //$template_list['default']='默认模板'   
  196.                 $template_list[$v['dirname']] = $v['name'] ? $v['name'] : $v['dirname'];   
  197.                 unset($template_list[$k]);   
  198.             }   
  199.             $show_validator = '';   
  200.             if(isset($_GET['parentid'])) {                      //添加子栏目   
  201.                 $parentid = $_GET['parentid'];   
  202.                 $r = $this->db->get_one(array('catid'=>$parentid));   
  203.                 if($r) extract($r,EXTR_SKIP);   
  204.                 $setting = string2array($setting);   
  205.             }   
  206.                
  207.             pc_base::load_sys_class('form','',0);               //加载form.class.php类库   
  208.             $type = $_GET['s'];                                 //添加子栏目时会传递此参数:0-内部栏目、1-单网页、2-外部链接   
  209.             if($type==0) {                                      //0-内部栏目   
  210.                 $exists_model = false;                          //模型是否存在的标识   
  211.                 $models = getcache('model','commons');          //获取默认的三种模型信息:文章模型、下载模型、图片模型   
  212.                 foreach($models as $_m) {   
  213.                     if($this->siteid == $_m['siteid']) {     //如果这默认的三种模型属于当前站点ID   
  214.                         $exists_model = true;                   //将模型是否存在标识赋值给true   
  215.                         break;                                  //跳出foreach循环   
  216.                     }   
  217.                 }   
  218.                 //如果模型不存在,则给出提示信息   
  219.                 if(!$exists_model) showmessage(L('please_add_model'),'?m=content&c=sitemodel&a=init&menuid=59',5000);   
  220.                 //显示模板文件   
  221.                 include $this->admin_tpl('category_add');   
  222.             } elseif ($type==1) {   
  223.                 include $this->admin_tpl('category_page_add');   
  224.             } else {   //phpfensi.com 
  225.                 include $this->admin_tpl('category_link');   
  226.             }   
  227.         }   
  228.     }   
  229. }  
  230. ?> 

Tags: phpcms添加栏目 category php

分享到: