phpcms v9代码分析:后台content模块content.php

//第一步: //路径:phpcms/modules/content/content.php //构造方法 public function __const

//第一步:  
//路径:phpcms/modules/content/content.php  
//构造方法  
public function __construct() {  
        parent::__construct();  
        $this->db = pc_base::load_model('content_model');//内容模型数据库操作类  
        $this->siteid = $this->get_siteid();//当前站点id  
        $this->categorys = getcache('category_content_'.$this->siteid,'commons');//当前站点下所有栏目的详细配置信息  
        //权限判断  
        if(isset($_GET['catid']) && $_SESSION['roleid'] != 1 && ROUTE_A !='pass' && strpos(ROUTE_A,'public_')===false) {  
            $catid = intval($_GET['catid']);  
            $this->priv_db = pc_base::load_model('category_priv_model');  
            $action = $this->categorys[$catid]['type']==0 ? ROUTE_A : 'init';  
            $priv_datas = $this->priv_db->get_one(array('catid'=>$catid,'is_admin'=>1,'action'=>$action));  
            if(!$priv_datas) showmessage(L('permission_to_operate'),'blank');  
        }  
    }  


//添加内容代码分析  
public function add() {  
        //点击"保存后自动关闭"或"保存并继续发表"按钮 ,几乎所有表单内容都存放在 $info[]数组中  
        if(isset($_POST['dosubmit']) || isset($_POST['dosubmit_continue'])) {  
            define('INDEX_HTML',true);  
            //栏目id  
            $catid = $_POST['info']['catid'] = intval($_POST['info']['catid']);  
            //标题  
            if(trim($_POST['info']['title'])=='') showmessage(L('title_is_empty'));  
            //当前栏目的详细配置信息  
            $category = $this->categorys[$catid];  
            //当前栏目类型:0-内部栏目  1-单网页  2-外部链接  
            if($category['type']==0) {  
                //当前栏目所属模型id:1-文章模型  2-下载模型  3-图片模型  
                $modelid = $this->categorys[$catid]['modelid'];  
                //设置模型主表及数据主表,如:news、gt_news  
                $this->db->set_model($modelid);  
                //如果该栏目设置了工作流,那么必须走工作流设定  
                //将当前栏目详细配置信息中的$category['setting']转化为数组  
                $setting = string2array($category['setting']);  
                $workflowid = $setting['workflowid'];//工作流设置  
                //$_POST['status']==99 代表发布  
                if($workflowid && $_POST['status']!=99) {  
                    //如果用户是超级管理员,那么则根据自己的设置来发布  
                    $_POST['info']['status'] = $_SESSION['roleid']==1 ? intval($_POST['status']) : 1;  
                } else {  
                    $_POST['info']['status'] = 99;//将表单提交过来的发布状态赋值给 info[]数组  
                }  
                //添加内容  
                $this->db->add_content($_POST['info']);//查看第二步:phpcms/model/content_model.class.php  
                //点击"保存后自动关闭"按钮  
                if(isset($_POST['dosubmit'])) {  
                    showmessage(L('add_success').L('2s_close'),'blank','','','function set_time() {$("#secondid").html(1);}setTimeout("set_time()", 500);setTimeout("window.close()", 1200);');  
                } else {//点击"保存并继续发表"按钮  
                    showmessage(L('add_success'),HTTP_REFERER);  
                }  
            } else {  
                //单网页  
                $this->page_db = pc_base::load_model('page_model');  
                $style_font_weight = $_POST['style_font_weight'] ? 'font-weight:'.strip_tags($_POST['style_font_weight']) : '';  
                $_POST['info']['style'] = strip_tags($_POST['style_color']).';'.$style_font_weight;  
                  
                if($_POST['edit']) {  
                    $this->page_db->update($_POST['info'],array('catid'=>$catid));  
                } else {  
                    $catid = $this->page_db->insert($_POST['info'],1);  
                }  
                $this->page_db->create_html($catid,$_POST['info']);  
                $forward = HTTP_REFERER;  
            }  
            showmessage(L('add_success'),$forward);  
        } else {//显示内容添加页模板  
            $show_header = $show_dialog = $show_validator = '';  
            //设置cookie 在附件添加处调用  
            param::set_cookie('module', 'content');  
            //栏目id  
            if(isset($_GET['catid']) && $_GET['catid']) {  
                //栏目id  
                $catid = $_GET['catid'] = intval($_GET['catid']);  
                  
                param::set_cookie('catid', $catid);  
                //当前栏目详细配置信息  
                $category = $this->categorys[$catid];  
                //当前栏目类型:0-内部栏目  1-单网页   2-外部链接  
                if($category['type']==0) {  
                    //当前栏目所属模型id  
                    $modelid = $category['modelid'];  
                    //取模型ID,依模型ID来生成对应的表单  
                    require CACHE_MODEL_PATH.'content_form.class.php';//动态生成内容添加页对应的表单  
                    $content_form = new content_form($modelid,$catid,$this->categorys);  
                    $forminfos = $content_form->get();//获取内容添加页对应表单信息  
                    $formValidator = $content_form->formValidator;  
                    //将当前栏目详细配置信息中的$category['setting']转化为数组  
                    $setting = string2array($category['setting']);  
                    //如果设置了工作流,则必须走工作流流程  
                    $workflowid = $setting['workflowid'];  
                    //获取当前站点下工作流详细配置信息  
                    $workflows = getcache('workflow_'.$this->siteid,'commons');  
                    //获取当前工作流信息:1-一级审核  2-二级审核  3-三级审核  4-四级审核  
                    $workflows = $workflows[$workflowid];  
                    $workflows_setting = string2array($workflows['setting']);  
                    $nocheck_users = $workflows_setting['nocheck_users'];  
                    $admin_username = param::get_cookie('admin_username');  
                    if(!empty($nocheck_users) && in_array($admin_username, $nocheck_users)) {  
                        $priv_status = true;  
                    } else {  
                        $priv_status = false;  
                    }  
                    //显示内容添加页面模板  
                    include $this->admin_tpl('content_add');  
                } else {  
                    //单网页  
                    $this->page_db = pc_base::load_model('page_model');  
                      
                    $r = $this->page_db->get_one(array('catid'=>$catid));  
                      
                    if($r) {  
                        extract($r);  
                        $style_arr = explode(';',$style);  
                        $style_color = $style_arr[0];  
                        $style_font_weight = $style_arr[1] ? substr($style_arr[1],12) : '';  
                    }  
                    include $this->admin_tpl('content_page');  
                }  
            } else {  
                include $this->admin_tpl('content_add');  
            }  
            header("Cache-control: private");  
        }  
    }  


//第二步:  
//路径:phpcms/model/content_model.class.php  内容模型数据库操作类  
        public $table_name = '';  
    public $category = '';  
    public function __construct() {  
        $this->db_config = pc_base::load_config('database');  
        $this->db_setting = 'default';  
        parent::__construct();  
        $this->url = pc_base::load_app_class('url', 'content');  
        $this->siteid = get_siteid();  
    }  
    public function set_model($modelid) {  
        $this->model = getcache('model', 'commons');//获取所有模型的详细配置信息  
        $this->modelid = $modelid;//当前模型id  
        $this->table_name = $this->db_tablepre.$this->model[$modelid]['tablename'];//当前模型的主表名,带前缀:如,gt_news  
        $this->model_tablename = $this->model[$modelid]['tablename'];//当前模型主表名,不带前缀,如:news  
    }  
    /**  
     * 添加内容  
     *   
     * @param $data 表单提交过来的数据  
     * @param $isimport 是否为外部接口导入  
     */  
    public function add_content($data,$isimport = 0) {  
        //返回经addslashes处理过的字符串或数组  
        if($isimport) $data = new_addslashes($data);  
        $this->search_db = pc_base::load_model('search_model');  
        $modelid = $this->modelid;//当前模型id  
        require_once CACHE_MODEL_PATH.'content_input.class.php';  
                require_once CACHE_MODEL_PATH.'content_update.class.php';  
        $content_input = new content_input($this->modelid);  
        $inputinfo = $content_input->get($data,$isimport);  
        //系统字段信息,存储在主表  
        $systeminfo = $inputinfo['system'];  
        //非系统字段信息,存储在副表  
        $modelinfo = $inputinfo['model'];  
        //发布时间不为空且不是数字  
        if($data['inputtime'] && !is_numeric($data['inputtime'])) {  
            //将发布时间转换为时间戳,归类为系统字段信息  
            $systeminfo['inputtime'] = strtotime($data['inputtime']);  
        } elseif(!$data['inputtime']) {//发布时间为空,则将系统时间戳信息赋值给$systeminfo['inputtime']  
            $systeminfo['inputtime'] = SYS_TIME;  
        } else {  
            $systeminfo['inputtime'] = $data['inputtime'];  
        }  
          
        //读取模型字段配置中,关于日期配置格式,来组合日期数据  
        $this->fields = getcache('model_field_'.$modelid,'model');//当前模型字段详细配置信息  
        $setting = string2array($this->fields['inputtime']['setting']);//关于日期的设置,转换为数组格式  
        /**  
         *  'setting' => 'array (  
         *        \'fieldtype\' => \'int\',  整型  
         *        \'format\' => \'Y-m-d H:i:s\', 时间格式  
         *        \'defaulttype\' => \'0\',  
         *  )',  
         */  
        extract($setting);  
        if($fieldtype=='date') {  
            $systeminfo['inputtime'] = date('Y-m-d');  
        }elseif($fieldtype=='datetime'){  
            $systeminfo['inputtime'] = date('Y-m-d H:i:s');  
        }  
        //更新时间  
        if($data['updatetime'] && !is_numeric($data['updatetime'])) {  
            $systeminfo['updatetime'] = strtotime($data['updatetime']);  
        } elseif(!$data['updatetime']) {  
            $systeminfo['updatetime'] = SYS_TIME;  
        } else {  
            $systeminfo['updatetime'] = $data['updatetime'];  
        }  
        //用户名  
        $systeminfo['username'] = $data['username'] ? $data['username'] : param::get_cookie('admin_username');  
        //系统添加  
        $systeminfo['sysadd'] = defined('IN_ADMIN') ? 1 : 0;  
          
        //自动提取摘要  
        if(isset($_POST['add_introduce']) && $systeminfo['description'] == '' && isset($modelinfo['content'])) {  
            $content = stripslashes($modelinfo['content']);//内容  
            $introcude_length = intval($_POST['introcude_length']);//自动截取内容长度  
            //如果自动截取的内容中含有[page]等字符,则将其替换为空  
            $systeminfo['description'] = str_cut(str_replace(array("\r\n","\t",'[page]','[/page]','“','”',' '), '', strip_tags($content)),$introcude_length);  
            //摘要  
            $inputinfo['system']['description'] = $systeminfo['description'] = addslashes($systeminfo['description']);  
        }  
        //自动提取缩略图  
        if(isset($_POST['auto_thumb']) && $systeminfo['thumb'] == '' && isset($modelinfo['content'])) {  
            $content = $content ? $content : stripslashes($modelinfo['content']);  
            $auto_thumb_no = intval($_POST['auto_thumb_no'])-1;//将内容中第几张图片作为标题图片  
            if(preg_match_all("/(src)=([\"|']?)([^ \"'>]+\.(gif|jpg|jpeg|bmp|png))\\2/i", $content, $matches)) {  
                $systeminfo['thumb'] = $matches[3][$auto_thumb_no];//缩略图  
            }  
        }  
        //主表  
        $tablename = $this->table_name = $this->db_tablepre.$this->model_tablename;  
        //系统字段信息存入主表,并返回刚插入记录的id  
        $id = $modelinfo['id'] = $this->insert($systeminfo,true);//参数2-是否返回插入的id  
        $this->update($systeminfo,array('id'=>$id));  
        //更新URL地址  
        if($data['islink']==1) {//转向链接  
            $urls[0] = $_POST['linkurl'];  
        } else {  
            $urls = $this->url->show($id, 0, $systeminfo['catid'], $systeminfo['inputtime'], $data['prefix'],$inputinfo,'add');  
        }  
        $this->table_name = $tablename;//主表  
        $this->update(array('url'=>$urls[0]),array('id'=>$id));  
        //附属表  
        $this->table_name = $this->table_name.'_data';  
        //将非系统字段信息的值存入到附属表中  
        $this->insert($modelinfo);  
          
        //添加统计  
        $this->hits_db = pc_base::load_model('hits_model');//gt_hits表-统计表  
        $hitsid = 'c-'.$modelid.'-'.$id;//统计表的id组成  
        //统计信息入库  
        $this->hits_db->insert(array('hitsid'=>$hitsid,'catid'=>$systeminfo['catid'],'updatetime'=>SYS_TIME));  
          
        //更新到全站搜索  
        $this->search_api($id,$inputinfo);  
          
        //更新栏目统计数据,如:栏目下文章的数据量  
        $this->update_category_items($systeminfo['catid'],'add',1);  
          
        //调用 update  
        $content_update = new content_update($this->modelid,$id);  
        //合并后,调用update  
        $merge_data = array_merge($systeminfo,$modelinfo);  
        $merge_data['posids'] = $data['posids'];//推荐位  
        $content_update->update($merge_data);  
          
        //发布到审核列表中  
        if(!defined('IN_ADMIN') || $data['status']!=99) {  
            $this->content_check_db = pc_base::load_model('content_check_model');//gt_content_check表  
            $check_data = array(  
                'checkid'=>'c-'.$id.'-'.$modelid,  
                'catid'=>$systeminfo['catid'],  
                'siteid'=>$this->siteid,  
                'title'=>$systeminfo['title'],  
                'username'=>$systeminfo['username'],  
                'inputtime'=>$systeminfo['inputtime'],  
                'status'=>$data['status'],  
                );  
            $this->content_check_db->insert($check_data);  
        }  
        //END发布到审核列表中  
        if(!$isimport) {  
            $html = pc_base::load_app_class('html', 'content');  
            if($urls['content_ishtml'] && $data['status']==99) $html->show($urls[1],$urls['data']);  
            $catid = $systeminfo['catid'];  
        }  
        //发布到其他栏目  
        if($id && isset($_POST['othor_catid']) && is_array($_POST['othor_catid'])) {  
            $linkurl = $urls[0];  
            $r = $this->get_one(array('id'=>$id));  
            foreach ($_POST['othor_catid'] as $cid=>$_v) {  
                $this->set_catid($cid);//设置catid 所在的模型数据库  
                $mid = $this->category[$cid]['modelid'];//模型id  
                if($modelid==$mid) {  
                    //相同模型的栏目插入新的数据  
                    $inputinfo['system']['catid'] = $systeminfo['catid'] = $cid; //新的栏目id  
                    $newid = $modelinfo['id'] = $this->insert($systeminfo,true); //系统字段信息插入到主表中并返回插入的id  
                    $this->table_name = $tablename.'_data';//附表数据入库  
                    $this->insert($modelinfo);  
                    if($data['islink']==1) {//转向链接  
                        $urls = $_POST['linkurl'];//转向链接  
                    } else {  
                        $urls = $this->url->show($newid, 0, $cid, $systeminfo['inputtime'], $data['prefix'],$inputinfo,'add');  
                    }  
                    $this->table_name = $tablename;  
                    $this->update(array('url'=>$urls[0]),array('id'=>$newid));  
                    //发布到审核列表中  
                    if($data['status']!=99) {  
                        $check_data = array(  
                            'checkid'=>'c-'.$newid.'-'.$mid,  
                            'catid'=>$cid,  
                            'siteid'=>$this->siteid,  
                            'title'=>$systeminfo['title'],  
                            'username'=>$systeminfo['username'],  
                            'inputtime'=>$systeminfo['inputtime'],  
                            'status'=>1,  
                            );  
                        $this->content_check_db->insert($check_data);  
                    }  
                    if($urls['content_ishtml'] && $data['status']==99) $html->show($urls[1],$urls['data']);  
                } else {  
                    //不同模型插入转向链接地址  
                    $newid = $this->insert(  
                    array('title'=>$systeminfo['title'],  
                        'style'=>$systeminfo['style'],  
                        'thumb'=>$systeminfo['thumb'],  
                        'keywords'=>$systeminfo['keywords'],  
                        'description'=>$systeminfo['description'],  
                        'status'=>$systeminfo['status'],  
                        'catid'=>$cid,'url'=>$linkurl,  
                        'sysadd'=>1,  
                        'username'=>$systeminfo['username'],  
                        'inputtime'=>$systeminfo['inputtime'],  
                        'updatetime'=>$systeminfo['updatetime'],  
                        'islink'=>1  
                    ),true);  
                    $this->table_name = $this->table_name.'_data';  
                    $this->insert(array('id'=>$newid));  
                    //发布到审核列表中  
                    if($data['status']!=99) {  
                        $check_data = array(  
                            'checkid'=>'c-'.$newid.'-'.$mid,  
                            'catid'=>$systeminfo['catid'],  
                            'siteid'=>$this->siteid,  
                            'title'=>$systeminfo['title'],  
                            'username'=>$systeminfo['username'],  
                            'inputtime'=>$systeminfo['inputtime'],  
                            'status'=>1,  
                            );  
                        $this->content_check_db->insert($check_data);  
                    }  
                }  
                $hitsid = 'c-'.$mid.'-'.$newid;  
                $this->hits_db->insert(array('hitsid'=>$hitsid,'catid'=>$cid,'updatetime'=>SYS_TIME));  
            }  
        }  
        //END 发布到其他栏目  
        //更新附件状态  
        if(pc_base::load_config('system','attachment_stat')) {  
            $this->attachment_db = pc_base::load_model('attachment_model');  
            $this->attachment_db->api_update('','c-'.$systeminfo['catid'].'-'.$id,2);  
        }  
        //生成静态  
        if(!$isimport && $data['status']==99) {  
            //在添加和修改内容处定义了 INDEX_HTML  
            if(defined('INDEX_HTML')) $html->index();  
            if(defined('RELATION_HTML')) $html->create_relation_html($catid);  
        }  
        return $id;//返回刚插入的记录id  
    }  

\