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

来源:互联网 发布:markdownpad for mac 编辑:程序博客网 时间:2024/04/29 04:33
[html] view plain copy
 print?
  1. //第一步:  
  2. //路径:phpcms/modules/content/content.php  
  3. //构造方法  
  4. public function __construct() {  
  5.         parent::__construct();  
  6.         $this->db = pc_base::load_model('content_model');//内容模型数据库操作类  
  7.         $this->siteid = $this->get_siteid();//当前站点id  
  8.         $this->categorys = getcache('category_content_'.$this->siteid,'commons');//当前站点下所有栏目的详细配置信息  
  9.         //权限判断  
  10.         if(isset($_GET['catid']) && $_SESSION['roleid'] != 1 && ROUTE_A !='pass' && strpos(ROUTE_A,'public_')===false) {  
  11.             $catid = intval($_GET['catid']);  
  12.             $this->priv_db = pc_base::load_model('category_priv_model');  
  13.             $action = $this->categorys[$catid]['type']==0 ? ROUTE_A : 'init';  
  14.             $priv_datas = $this->priv_db->get_one(array('catid'=>$catid,'is_admin'=>1,'action'=>$action));  
  15.             if(!$priv_datas) showmessage(L('permission_to_operate'),'blank');  
  16.         }  
  17.     }  

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

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

0 0
原创粉丝点击