phpcms(4) V9 栏目管理

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

phpcms(4) V9 栏目管理

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

<?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;

    public $siteid;

    function __construct() 

    {

        parent::__construct(); // 调父类构造 即admin.class.php文件中类的构造函数

        $this->db = pc_base::load_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=>模型信息(模型:文章模型、下载模型、图片模型)

        {

            //当前模型id下所有栏目是否有子栏目,1-有 0-无

            $category_items[$modelid] = getcache('category_items_'.$modelid,'commons');

        }

        $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ','&nbsp;&nbsp;&nbsp;├─ ','&nbsp;&nbsp;&nbsp;└─ '); // 树状结构图

        $tree->nbsp = '&nbsp;&nbsp;&nbsp;'; // &nbsp 注意这个为空格。此句意思即三个空格。

        $categorys = array();

        //读取缓存

        $result = getcache('category_content_'.$this->siteid,'commons'); //当前站点ID下所有栏目的详细配置信息

        $show_detail = count($result) < 500 ? 1 : 0;

        $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']);

                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>";

        $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'));

                $_POST['info']['catname'] = safe_replace($_POST['info']['catname']);

                $_POST['info']['catname'] = str_replace(array('%'),'',$_POST['info']['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 

                {

                    $setting['category_ruleid'] = $_POST['category_php_ruleid'];

                    $_POST['info']['url'] = ''; //绑定域名

                }

            }

            

            //内容生成静态配置

            if($setting['content_ishtml']) //如果内容生成HTML

            {

                $setting['show_ruleid'] = $_POST['show_html_ruleid'];//内容页的url规则

            } 

            else 

            {

                $setting['show_ruleid'] = $_POST['show_php_ruleid'];

            }

            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']);

                $letters = gbk_to_pinyin($catname); //将gbk编码格式的中文目录名称转化为拼音

                $_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);

                    $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 

        {

            /*

            * 获取站点模板信息

            */

            //加载global.func.php文件

            pc_base::load_app_func('global');

 

            $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);

            }

            //加载form.class.php类库

            pc_base::load_sys_class('form','',0);

            $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;

                    }

                }

                if(!$exists_model) 

                    showmessage(L('please_add_model'),'?m=content&c=sitemodel&a=init&menuid=59',5000);

                include $this->admin_tpl('category_add'); // 模版文件category_add.tpl.php

            } 

            elseif ($type==1) 

            {

                include $this->admin_tpl('category_page_add'); // 模版文件category_page_add.tpl.php

            } 

            else 

            {

                include $this->admin_tpl('category_link');// 模版文件category_link.tpl.php

            }

        }

    }

    /**

     * 修改栏目

     */

    public function edit() 

    {

        if(isset($_POST['dosubmit'])) 

        {

            pc_base::load_sys_func('iconv');

            $catid = 0;

            $catid = intval($_POST['catid']);

            $setting = $_POST['setting'];

            //上级栏目不能是自身

            if($_POST['info']['parentid']==$catid)

            {

                showmessage(L('operation_failure'),'?m=admin&c=category&a=init&module=admin&menuid=43');

            }

            //栏目生成静态配置

            if($_POST['type'] != 2) 

            {

                if($setting['ishtml']) 

                {

                    $setting['category_ruleid'] = $_POST['category_html_ruleid'];

                } 

                else 

                {

                    $setting['category_ruleid'] = $_POST['category_php_ruleid'];

                    $_POST['info']['url'] = '';

                }

            }

            //内容生成静态配置

            if($setting['content_ishtml']) 

            {

                $setting['show_ruleid'] = $_POST['show_html_ruleid'];

            } 

            else 

            {

                $setting['show_ruleid'] = $_POST['show_php_ruleid'];

            }

            if($setting['repeatchargedays']<1) 

                $setting['repeatchargedays'] = 1;

            $_POST['info']['sethtml'] = $setting['create_to_html_root'];

            $_POST['info']['setting'] = array2string($setting);

            $_POST['info']['module'] = 'content';

            $catname = CHARSET == 'gbk' ? safe_replace($_POST['info']['catname']) : iconv('utf-8','gbk',safe_replace($_POST['info']['catname']));

            $catname = str_replace(array('%'),'',$catname);

            $letters = gbk_to_pinyin($catname);

            $_POST['info']['letter'] = strtolower(implode('', $letters));

            

            //应用权限设置到子栏目

            if($_POST['priv_child']) 

            {

                $arrchildid = $this->db->get_one(array('catid'=>$catid), 'arrchildid');

                if(!empty($arrchildid['arrchildid'])) 

                {

                    $arrchildid_arr = explode(',', $arrchildid['arrchildid']);

                    if(!empty($arrchildid_arr)) 

                    {

                        foreach ($arrchildid_arr as $arr_v) 

                        {

                            $this->update_priv($arr_v, $_POST['priv_groupid'], 0);

                        }

                    }

                }

            }

            

            //应用模版到所有子栏目

            if($_POST['template_child'])

            {

                $this->categorys = $categorys = $this->db->select(array('siteid'=>$this->siteid,'module'=>'content'), '*', '', 'listorder ASC, catid ASC', '', 'catid');

                $idstr = $this->get_arrchildid($catid);

                 if(!empty($idstr))

                 {

                    $sql = "select catid,setting from phpcms_category where catid in($idstr)";

                    $this->db->query($sql);

                    $arr = $this->db->fetch_array();

                     if(!empty($arr))

                     {

                        foreach ($arr as $v)

                        {

                            $new_setting = array2string(

                            array_merge(string2array($v['setting']), array('category_template' => $_POST['setting']['category_template'],'list_template' =>  $_POST['setting']['list_template'],'show_template' =>  $_POST['setting']['show_template'])

                                                    ));

                            $this->db->update(array('setting'=>$new_setting), 'catid='.$v['catid']);

                        }

                    }                                

                }

            }

            

            $this->db->update($_POST['info'],array('catid'=>$catid,'siteid'=>$this->siteid));

            $this->update_priv($catid, $_POST['priv_roleid']);

            $this->update_priv($catid, $_POST['priv_groupid'],0);

            $this->cache(); //更新缓存

            //更新附件状态

            if($_POST['info']['image'] && pc_base::load_config('system','attachment_stat')) 

            {

                $this->attachment_db = pc_base::load_model('attachment_model'); //加载附件数据库模型

                $this->attachment_db->api_update($_POST['info']['image'],'catid-'.$catid,1);

            }

            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');

        } 

        else 

        {

            //获取站点模板信息

            pc_base::load_app_func('global');

            $template_list = template_list($this->siteid, 0);

            foreach ($template_list as $k=>$v) 

            {

                $template_list[$v['dirname']] = $v['name'] ? $v['name'] : $v['dirname'];

                unset($template_list[$k]);

            }

            

            $show_validator = $catid = $r = '';

            $catid = intval($_GET['catid']);

            pc_base::load_sys_class('form','',0);

            $r = $this->db->get_one(array('catid'=>$catid));

            if($r) 

                extract($r);

            $setting = string2array($setting);

            

            $this->priv_db = pc_base::load_model('category_priv_model');

            $this->privs = $this->priv_db->select(array('catid'=>$catid));

            

            $type = $_GET['type'];

            // 依据类型选择模版文件 0-内部栏目、1-单网页、2-外部链接

            if($type == 0) 

            {

                include $this->admin_tpl('category_edit');

            } 

            elseif ($type == 1) 

            {

                include $this->admin_tpl('category_page_edit');

            } 

            else 

            {

                include $this->admin_tpl('category_link');

            }

        }

    }

上一篇:CodeBlocks去掉拼写检查


下一篇:c++ 自动对象