首页 > CMS > PhpCMS > 正文

【phpcms-v9】后台content模块的content.php控制器文件分析-后台添

2024-09-10 07:15:45
字体:
来源:转载
供稿:网友

【phpcms-v9】后台content模块的content.php控制器文件分析-后台添加内容代码分析.

第一步,路径:phpcms/modules/content/content.php:

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

 

添加内容代码分析:

  1. public function add() {   
  2.         //点击"保存后自动关闭"或"保存并继续发表"按钮 ,几乎所有表单内容都存放在 $info[]数组中   
  3.         if(isset($_POST['dosubmit']) || isset($_POST['dosubmit_continue'])) {   
  4.             define('INDEX_HTML',true);   
  5.             //栏目id   
  6.             $catid = $_POST['info']['catid'] = intval($_POST['info']['catid']);   
  7.             //标题   
  8.             if(trim($_POST['info']['title'])=='') showmessage(L('title_is_empty'));   
  9.             //当前栏目的详细配置信息   
  10.             $category = $this->categorys[$catid];   
  11.             //当前栏目类型:0-内部栏目  1-单网页  2-外部链接   
  12.             if($category['type']==0) {   
  13.                 //当前栏目所属模型id:1-文章模型  2-下载模型  3-图片模型   
  14.                 $modelid = $this->categorys[$catid]['modelid'];   
  15.                 //设置模型主表及数据主表,如:news、gt_news   
  16.                 $this->db->set_model($modelid);   
  17.                 //如果该栏目设置了工作流,那么必须走工作流设定   
  18.                 //将当前栏目详细配置信息中的$category['setting']转化为数组   
  19.                 $setting = string2array($category['setting']);   
  20.                 $workflowid = $setting['workflowid'];//工作流设置   
  21.                 //$_POST['status']==99 代表发布   
  22.                 if($workflowid && $_POST['status']!=99) {   
  23.                     //如果用户是超级管理员,那么则根据自己的设置来发布   
  24.                     $_POST['info']['status'] = $_SESSION['roleid']==1 ? intval($_POST['status']) : 1;   
  25.                 } else {   
  26.                     $_POST['info']['status'] = 99;//将表单提交过来的发布状态赋值给 info[]数组   
  27.                 }   
  28.                 //添加内容   
  29.                 $this->db->add_content($_POST['info']);//查看第二步:phpcms/model/content_model.class.php   
  30.                 //点击"保存后自动关闭"按钮   
  31.                 if(isset($_POST['dosubmit'])) {   
  32.                     showmessage(L('add_success').L('2s_close'),'blank','','','function set_time() {$("#secondid").html(1);}setTimeout("set_time()", 500);setTimeout("window.close()", 1200);');   
  33.                 } else {//点击"保存并继续发表"按钮   
  34.                     showmessage(L('add_success'),HTTP_REFERER);   
  35.                 }   
  36.             } else {   
  37.                 //单网页   
  38.                 $this->page_db = pc_base::load_model('page_model');   
  39.                 $style_font_weight = $_POST['style_font_weight'] ? 'font-weight:'.strip_tags($_POST['style_font_weight']) : '';   
  40.                 $_POST['info']['style'] = strip_tags($_POST['style_color']).';'.$style_font_weight;   
  41.                    
  42.                 if($_POST['edit']) {   
  43.                     $this->page_db->update($_POST['info'],array('catid'=>$catid));   
  44.                 } else {   
  45.                     $catid = $this->page_db->insert($_POST['info'],1);   
  46.                 }   
  47.                 $this->page_db->create_html($catid,$_POST['info']);   
  48.                 $forward = HTTP_REFERER;   
  49.             }   
  50.             showmessage(L('add_success'),$forward);   
  51.         } else {//显示内容添加页模板   
  52.             $show_header = $show_dialog = $show_validator = '';   
  53.             //设置cookie 在附件添加处调用   
  54.             param::set_cookie('module''content');   
  55.             //栏目id   
  56.             if(isset($_GET['catid']) && $_GET['catid']) {   
  57.                 //栏目id   
  58.                 $catid = $_GET['catid'] = intval($_GET['catid']);   
  59.                    
  60.                 param::set_cookie('catid'$catid);   
  61.                 //当前栏目详细配置信息   
  62.                 $category = $this->categorys[$catid];   
  63.                 //当前栏目类型:0-内部栏目  1-单网页   2-外部链接   
  64.                 if($category['type']==0) {   
  65.                     //当前栏目所属模型id   
  66.                     $modelid = $category['modelid'];   
  67.                     //取模型ID,依模型ID来生成对应的表单   
  68.                     require CACHE_MODEL_PATH.'content_form.class.php';//动态生成内容添加页对应的表单   
  69.                     $content_form = new content_form($modelid,$catid,$this->categorys);   
  70.                     $forminfos = $content_form->get();//获取内容添加页对应表单信息   
  71.                     $formValidator = $content_form->formValidator;   
  72.                     //将当前栏目详细配置信息中的$category['setting']转化为数组   
  73.                     $setting = string2array($category['setting']);   
  74.                     //如果设置了工作流,则必须走工作流流程   
  75.                     $workflowid = $setting['workflowid'];   
  76.                     //获取当前站点下工作流详细配置信息   
  77.                     $workflows = getcache('workflow_'.$this->siteid,'commons');   
  78.                     //获取当前工作流信息:1-一级审核  2-二级审核  3-三级审核  4-四级审核   
  79.                     $workflows = $workflows[$workflowid];   
  80.                     $workflows_setting = string2array($workflows['setting']);   
  81.                     $nocheck_users = $workflows_setting['nocheck_users'];   
  82.                     $admin_username = param::get_cookie('admin_username');   
  83.                     if(!emptyempty($nocheck_users) && in_array($admin_username$nocheck_users)) {   
  84.                         $priv_status = true;   
  85.                     } else {   
  86.                         $priv_status = false;   
  87.                     }   
  88.                     //显示内容添加页面模板   
  89.                     include $this->admin_tpl('content_add');   
  90.                 } else {   
  91.                     //单网页   
  92.                     $this->page_db = pc_base::load_model('page_model');   
  93.                        
  94.                     $r = $this->page_db->get_one(array('catid'=>$catid));   
  95.                        
  96.                     if($r) {   
  97.                         extract($r);   
  98.                         $style_arr = explode(';',$style);   
  99.                         $style_color = $style_arr[0];   
  100.                         $style_font_weight = $style_arr[1] ? substr($style_arr[1],12) : '';   
  101.                     }   
  102.                     include $this->admin_tpl('content_page');   
  103.                 }   
  104.             } else {   
  105.                 include $this->admin_tpl('content_add');   
  106.             }   
  107.             header("Cache-control: private");   
  108.         }   
  109.     } 

 

第二步,路径:phpcms/model/content_model.class.php内容模型数据库操作类:

  1. public $table_name = '';   
  2.     public $category = '';   
  3.     public function __construct() {   
  4.         $this->db_config = pc_base::load_config('database');   
  5.         $this->db_setting = 'default';   
  6.         parent::__construct();   
  7.         $this->url = pc_base::load_app_class('url''content');   
  8.         $this->siteid = get_siteid();   
  9.     }   
  10.     public function set_model($modelid) {   
  11.         $this->model = getcache('model''commons');//获取所有模型的详细配置信息   
  12.         $this->modelid = $modelid;//当前模型id   
  13.         $this->table_name = $this->db_tablepre.$this->model[$modelid]['tablename'];//当前模型的主表名,带前缀:如,gt_news   
  14.         $this->model_tablename = $this->model[$modelid]['tablename'];//当前模型主表名,不带前缀,如:news   
  15.     }   
  16.     /**   
  17.      * 添加内容   
  18.      *    
  19.      * @param $data 表单提交过来的数据   
  20.      * @param $isimport 是否为外部接口导入   
  21.      */   
  22.     public function add_content($data,$isimport = 0) {   
  23.         //返回经addslashes处理过的字符串或数组   
  24.         if($isimport$data = new_addslashes($data);   
  25.         $this->search_db = pc_base::load_model('search_model');   
  26.         $modelid = $this->modelid;//当前模型id   
  27.         require_once CACHE_MODEL_PATH.'content_input.class.php';   
  28.                 require_once CACHE_MODEL_PATH.'content_update.class.php';   
  29.         $content_input = new content_input($this->modelid);   
  30.         $inputinfo = $content_input->get($data,$isimport);   
  31.         //系统字段信息,存储在主表   
  32.         $systeminfo = $inputinfo['system'];   
  33.         //非系统字段信息,存储在副表   
  34.         $modelinfo = $inputinfo['model'];   
  35.         //发布时间不为空且不是数字   
  36.         if($data['inputtime'] && !is_numeric($data['inputtime'])) {   
  37.             //将发布时间转换为时间戳,归类为系统字段信息   
  38.             $systeminfo['inputtime'] = strtotime($data['inputtime']);   
  39.         } elseif(!$data['inputtime']) {//发布时间为空,则将系统时间戳信息赋值给$systeminfo['inputtime']   
  40.             $systeminfo['inputtime'] = SYS_TIME;   
  41.         } else {   
  42.             $systeminfo['inputtime'] = $data['inputtime'];   
  43.         }   
  44.            
  45.         //读取模型字段配置中,关于日期配置格式,来组合日期数据   
  46.         $this->fields = getcache('model_field_'.$modelid,'model');//当前模型字段详细配置信息   
  47.         $setting = string2array($this->fields['inputtime']['setting']);//关于日期的设置,转换为数组格式   
  48.         /**   
  49.          *  'setting' => 'array (   
  50.          *        /'fieldtype/' => /'int/',  整型   
  51.          *        /'format/' => /'Y-m-d H:i:s/', 时间格式   
  52.          *        /'defaulttype/' => /'0/',   
  53.          *  )',   
  54.          */   
  55.         extract($setting);   
  56.         if($fieldtype=='date') {   
  57.             $systeminfo['inputtime'] = date('Y-m-d');   
  58.         }elseif($fieldtype=='datetime'){   
  59.             $systeminfo['inputtime'] = date('Y-m-d H:i:s');   
  60.         }   
  61.         //更新时间   
  62.         if($data['updatetime'] && !is_numeric($data['updatetime'])) {   
  63.             $systeminfo['updatetime'] = strtotime($data['updatetime']);   
  64.         } elseif(!$data['updatetime']) {   
  65.             $systeminfo['updatetime'] = SYS_TIME;   
  66.         } else {   
  67.             $systeminfo['updatetime'] = $data['updatetime'];   
  68.         }   
  69.         //用户名   
  70.         $systeminfo['username'] = $data['username'] ? $data['username'] : param::get_cookie('admin_username');   
  71.         //系统添加   
  72.         $systeminfo['sysadd'] = defined('IN_ADMIN') ? 1 : 0;   
  73.            
  74.         //自动提取摘要   
  75.         if(isset($_POST['add_introduce']) && $systeminfo['description'] == '' && isset($modelinfo['content'])) {   
  76.             $content = stripslashes($modelinfo['content']);//内容   
  77.             $introcude_length = intval($_POST['introcude_length']);//自动截取内容长度   
  78.             //如果自动截取的内容中含有[page]等字符,则将其替换为空   
  79.             $systeminfo['description'] = str_cut(str_replace(array("/r/n","/t",'[page]','[/page]','“','”',' '), ''strip_tags($content)),$introcude_length);   
  80.             //摘要   
  81.             $inputinfo['system']['description'] = $systeminfo['description'] = addslashes($systeminfo['description']);   
  82.         }   
  83.         //自动提取缩略图   
  84.         if(isset($_POST['auto_thumb']) && $systeminfo['thumb'] == '' && isset($modelinfo['content'])) {   
  85.             $content = $content ? $content : stripslashes($modelinfo['content']);   
  86.             $auto_thumb_no = intval($_POST['auto_thumb_no'])-1;//将内容中第几张图片作为标题图片   
  87.             if(preg_match_all("/(src)=([/"|']?)([^ /"'>]+/.(gif|jpg|jpeg|bmp|png))//2/i"$content$matches)) {   
  88.                 $systeminfo['thumb'] = $matches[3][$auto_thumb_no];//缩略图  
  89.             }   
  90.         }   
  91.         //主表   
  92.         $tablename = $this->table_name = $this->db_tablepre.$this->model_tablename;   
  93.         //系统字段信息存入主表,并返回刚插入记录的id   
  94.         $id = $modelinfo['id'] = $this->insert($systeminfo,true);//参数2-是否返回插入的id   
  95.         $this->update($systeminfo,array('id'=>$id));   
  96.         //更新URL地址   
  97.         if($data['islink']==1) {//转向链接   
  98.             $urls[0] = $_POST['linkurl'];   
  99.         } else {   
  100.             $urls = $this->url->show($id, 0, $systeminfo['catid'], $systeminfo['inputtime'], $data['prefix'],$inputinfo,'add');   
  101.         }   
  102.         $this->table_name = $tablename;//主表   
  103.         $this->update(array('url'=>$urls[0]),array('id'=>$id));   
  104.         //附属表   
  105.         $this->table_name = $this->table_name.'_data';   
  106.         //将非系统字段信息的值存入到附属表中   
  107.         $this->insert($modelinfo);   
  108.            
  109.         //添加统计   
  110.         $this->hits_db = pc_base::load_model('hits_model');//gt_hits表-统计表   
  111.         $hitsid = 'c-'.$modelid.'-'.$id;//统计表的id组成   
  112.         //统计信息入库   
  113.         $this->hits_db->insert(array('hitsid'=>$hitsid,'catid'=>$systeminfo['catid'],'updatetime'=>SYS_TIME));   
  114.            
  115.         //更新到全站搜索   
  116.         $this->search_api($id,$inputinfo);   
  117.            
  118.         //更新栏目统计数据,如:栏目下文章的数据量   
  119.         $this->update_category_items($systeminfo['catid'],'add',1);   
  120.            
  121.         //调用 update   
  122.         $content_update = new content_update($this->modelid,$id);   
  123.         //合并后,调用update   
  124.         $merge_data = array_merge($systeminfo,$modelinfo);   
  125.         $merge_data['posids'] = $data['posids'];//推荐位   
  126.         $content_update->update($merge_data);   
  127.            
  128.         //发布到审核列表中   
  129.         if(!defined('IN_ADMIN') || $data['status']!=99) {   
  130.             $this->content_check_db = pc_base::load_model('content_check_model');//gt_content_check表   
  131.             $check_data = array(   
  132.                 'checkid'=>'c-'.$id.'-'.$modelid,   
  133.                 'catid'=>$systeminfo['catid'],   
  134.                 'siteid'=>$this->siteid,   
  135.                 'title'=>$systeminfo['title'],   
  136.                 'username'=>$systeminfo['username'],   
  137.                 'inputtime'=>$systeminfo['inputtime'],   
  138.                 'status'=>$data['status'],   
  139.                 );   
  140.             $this->content_check_db->insert($check_data);   
  141.         }   
  142.         //END发布到审核列表中   
  143.         if(!$isimport) {   
  144.             $html = pc_base::load_app_class('html''content');   
  145.             if($urls['content_ishtml'] && $data['status']==99) $html->show($urls[1],$urls['data']);   
  146.             $catid = $systeminfo['catid'];   
  147.         }   
  148.         //发布到其他栏目   
  149.         if($id && isset($_POST['othor_catid']) && is_array($_POST['othor_catid'])) {   
  150.             $linkurl = $urls[0];   
  151.             $r = $this->get_one(array('id'=>$id));   
  152.             foreach ($_POST['othor_catid'as $cid=>$_v) {   
  153.                 $this->set_catid($cid);//设置catid 所在的模型数据库   
  154.                 $mid = $this->category[$cid]['modelid'];//模型id   
  155.                 if($modelid==$mid) {   
  156.                     //相同模型的栏目插入新的数据   
  157.                     $inputinfo['system']['catid'] = $systeminfo['catid'] = $cid//新的栏目id   
  158.                     $newid = $modelinfo['id'] = $this->insert($systeminfo,true); //系统字段信息插入到主表中并返回插入的id   
  159.                     $this->table_name = $tablename.'_data';//附表数据入库   
  160.                     $this->insert($modelinfo);   
  161.                     if($data['islink']==1) {//转向链接   
  162.                         $urls = $_POST['linkurl'];//转向链接   
  163.                     } else {   
  164.                         $urls = $this->url->show($newid, 0, $cid$systeminfo['inputtime'], $data['prefix'],$inputinfo,'add');   
  165.                     }   
  166.                     $this->table_name = $tablename;   
  167.                     $this->update(array('url'=>$urls[0]),array('id'=>$newid));   
  168.                     //发布到审核列表中   
  169.                     if($data['status']!=99) {   
  170.                         $check_data = array(   
  171.                             'checkid'=>'c-'.$newid.'-'.$mid,   
  172.                             'catid'=>$cid,   
  173.                             'siteid'=>$this->siteid,   
  174.                             'title'=>$systeminfo['title'],   
  175.                             'username'=>$systeminfo['username'],   
  176.                             'inputtime'=>$systeminfo['inputtime'],   
  177.                             'status'=>1,   
  178.                             );   
  179.                         $this->content_check_db->insert($check_data);   
  180.                     }    //Vevb.com 
  181.                     if($urls['content_ishtml'] && $data['status']==99) $html->show($urls[1],$urls['data']);   
  182.                 } else {   
  183.                     //不同模型插入转向链接地址   
  184.                     $newid = $this->insert(   
  185.                     array('title'=>$systeminfo['title'],   
  186.                         'style'=>$systeminfo['style'],   
  187.                         'thumb'=>$systeminfo['thumb'],   
  188.                         'keywords'=>$systeminfo['keywords'],   
  189.                         'description'=>$systeminfo['description'],   
  190.                         'status'=>$systeminfo['status'],   
  191.                         'catid'=>$cid,'url'=>$linkurl,   
  192.                         'sysadd'=>1,   
  193.                         'username'=>$systeminfo['username'],   
  194.                         'inputtime'=>$systeminfo['inputtime'],   
  195.                         'updatetime'=>$systeminfo['updatetime'],   
  196.                         'islink'=>1   
  197.                     ),true);   
  198.                     $this->table_name = $this->table_name.'_data';   
  199.                     $this->insert(array('id'=>$newid));   
  200.                     //发布到审核列表中   
  201.                     if($data['status']!=99) {   
  202.                         $check_data = array(   
  203.                             'checkid'=>'c-'.$newid.'-'.$mid,   
  204.                             'catid'=>$systeminfo['catid'],   
  205.                             'siteid'=>$this->siteid,   
  206.                             'title'=>$systeminfo['title'],   
  207.                             'username'=>$systeminfo['username'],   
  208.                             'inputtime'=>$systeminfo['inputtime'],   
  209.                             'status'=>1,   
  210.                             );   
  211.                         $this->content_check_db->insert($check_data);   
  212.                     }   
  213.                 }   
  214.                 $hitsid = 'c-'.$mid.'-'.$newid;   
  215.                 $this->hits_db->insert(array('hitsid'=>$hitsid,'catid'=>$cid,'updatetime'=>SYS_TIME));   
  216.             }   
  217.         }   
  218.         //END 发布到其他栏目   
  219.         //更新附件状态   
  220.         if(pc_base::load_config('system','attachment_stat')) {   
  221.             $this->attachment_db = pc_base::load_model('attachment_model');  
  222.             $this->attachment_db->api_update('','c-'.$systeminfo['catid'].'-'.$id,2);   
  223.         }   
  224.         //生成静态   
  225.         if(!$isimport && $data['status']==99) {   
  226.             //在添加和修改内容处定义了 INDEX_HTML   
  227.             if(defined('INDEX_HTML')) $html->index();   
  228.             if(defined('RELATION_HTML')) $html->create_relation_html($catid);   
  229.         }   
  230.         return $id;//返回刚插入的记录id   
  231.     }   

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表