【phpcms-v9】前臺content模塊控制器index.php文件分析-內容詳情頁代碼分析.
- //內容頁:文章內容詳情頁、圖片詳情頁、下載詳情頁走的都是show()方法
- //路徑:phpcms/modules/content/index.php控制器
- public function show() {
- $catid = intval($_GET['catid']); //欄目id
- $id = intval($_GET['id']); //新聞id
- //如果欄目id不存在或新聞id不存在,則給出提示信息
- if(!$catid || !$id) showmessage(L('information_does_not_exist'),'blank');
- $_userid = $this->_userid; //用戶id
- $_username = $this->_username; //用戶名
- $_groupid = $this->_groupid; //用戶會員組id
- $page = intval($_GET['page']); //當前頁碼
- $page = max($page,1); //保證最小頁碼為1
- $siteids = getcache('category_content','commons'); //獲取所有欄目所對應的站點id
- $siteid = $siteids[$catid]; //獲取當前欄目的站點id
- $CATEGORYS = getcache('category_content_'.$siteid,'commons'); //獲取當前站點下所有欄目的詳細配置信息
- if(!isset($CATEGORYS[$catid]) || $CATEGORYS[$catid]['type']!=0) showmessage(L('information_does_not_exist'),'blank');
- $this->category = $CAT = $CATEGORYS[$catid]; //獲取當前欄目的詳細配置信息
- $this->category_setting = $CAT['setting'] = string2array($this->category['setting']);//當前欄目的setting配置信息
- $siteid = $GLOBALS['siteid'] = $CAT['siteid']; //當前欄目站點id
- $MODEL = getcache('model','commons'); //所有模型配置信息 1-文章模型 2-下載模型 3-圖片模型
- $modelid = $CAT['modelid']; //當前欄目所屬模型id
- //當前模型id所對應的主表名稱:文章模型-news ,下載模型-download , 圖片模型-picture
- $tablename = $this->db->table_name = $this->db->db_tablepre.$MODEL[$modelid]['tablename'];//當前模型id對應的主表名
- $r = $this->db->get_one(array('id'=>$id)); //返回的主表數據,條件:where id=$id
- if(!$r || $r['status'] != 99) showmessage(L('info_does_not_exists'),'blank');
- //當前模型id所對應的副表名
- $this->db->table_name = $tablename.'_data'; //當前模型id所對應的副表名
- $r2 = $this->db->get_one(array('id'=>$id)); //返回的副表數據,條件:where id=$id
- $rs = $r2 ? array_merge($r,$r2) : $r; //如果有返回副表數據,則將主表數據與副表數據合并后再返回,否則返回主表數據
- //再次重新賦值,以數據庫為準
- $catid = $CATEGORYS[$r['catid']]['catid']; //欄目id
- $modelid = $CATEGORYS[$catid]['modelid']; //模型id
- require_once CACHE_MODEL_PATH.'content_output.class.php';
- $content_output = new content_output($modelid,$catid,$CATEGORYS);//主要用來對查詢到的記錄做一下過濾,僅此而已
- $data = $content_output->get($rs); //參數:查詢到的數據
- //很重要的代碼
- extract($data); //從數組中將變量導入到當前的符號表
- //檢查文章會員組權限
- //閱讀權限字段的值,如果在添加內容時沒有設置閱讀權限,那么閱讀權限groupids_view字段在數據庫中的值為空,否則值為會員組id
- if($groupids_view && is_array($groupids_view)) {
- $_groupid = param::get_cookie('_groupid'); //獲取會員組id
- $_groupid = intval($_groupid); //會員組id
- if(!$_groupid) { //如果沒有登錄
- $forward = urlencode(get_url()); //跳轉網址
- //如果沒有登錄,則給出提示信息
- showmessage(L('login_website'),APP_PATH.'index.php?m=member&c=index&a=login&forward='.$forward);
- }
- //用戶登錄,但是沒有在閱讀權限的會員組中,則給出沒有權限的提示信息
- if(!in_array($_groupid,$groupids_view)) showmessage(L('no_priv'));
- } else {//如果在添加內容時沒有設置閱讀權限,則會去判斷欄目的訪問權限 //如果
- //根據欄目訪問權限判斷權限
- $_priv_data = $this->_category_priv($catid);
- if($_priv_data=='-1') {
- $forward = urlencode(get_url());
- showmessage(L('login_website'),APP_PATH.'index.php?m=member&c=index&a=login&forward='.$forward);
- } elseif($_priv_data=='-2') {
- showmessage(L('no_priv'));
- }
- }
- if(module_exists('comment')) { //判斷模塊是否安裝
- $allow_comment = isset($allow_comment) ? $allow_comment : 1;//是否允許評論,默認情況下是允許評論的
- } else {
- $allow_comment = 0; //如果評論模塊不存在,則不允許評論
- }
- //閱讀收費 類型 0-按點收費 1-按元收費
- $paytype = $rs['paytype'];
- $readpoint = $rs['readpoint']; //收費多少
- $allow_visitor = 1; //是否支付過
- //$this->category_setting['defaultchargepoint']:默認收取點數或元數 ,在添加欄目時設置
- if($readpoint || $this->category_setting['defaultchargepoint']) {//一個條件為真即可
- if(!$readpoint) {//如果在添加內容時沒有設置收費點數,則將添加欄目時設置的收費點數賦值給$readpoint
- $readpoint = $this->category_setting['defaultchargepoint']; //默認收費點數
- $paytype = $this->category_setting['paytype']; //默認支付類型 0-點 1-元
- }
- //檢查是否支付過
- $allow_visitor = self::_check_payment($catid.'_'.$id,$paytype);
- if(!$allow_visitor) {
- $http_referer = urlencode(get_url());
- $allow_visitor = sys_auth($catid.'_'.$id.'|'.$readpoint.'|'.$paytype).'&http_referer='.$http_referer;
- } else {
- $allow_visitor = 1;
- }
- }
- //最頂級欄目ID
- $arrparentid = explode(',', $CAT['arrparentid']); //所有的父級欄目id數組
- $top_parentid = $arrparentid[1] ? $arrparentid[1] : $catid; //最頂級欄目id
- //如果添加內容時未設定內容詳情模板頁,則按添加欄目時設置的內容詳情模板頁
- $template = $template ? $template : $CAT['setting']['show_template'];
- if(!$template) $template = 'show';
- //SEO
- $seo_keywords = '';
- //如果當前內容的關鍵字不為空,則以逗號連接成一個字符串,如:"我的,影子,奔跑 "
- if(!emptyempty($keywords)) $seo_keywords = implode(',',$keywords);
- $SEO = seo($siteid, $catid, $title, $description, $seo_keywords);
- define('STYLE',$CAT['setting']['template_list']);//模板風格
- if(isset($rs['paginationtype'])) {//分頁類型:0-不分頁 1-自動分頁 2-手動分頁
- $paginationtype = $rs['paginationtype'];
- $maxcharperpage = $rs['maxcharperpage'];//每頁最大顯示的字符數,默認為10000,自動分頁會生效
- }
- $pages = $titles = '';
- if($rs['paginationtype']==1) {//1-自動分頁
- //自動分頁
- if($maxcharperpage < 10) $maxcharperpage = 500;
- $contentpage = pc_base::load_app_class('contentpage'); //文章內容分頁類
- $content = $contentpage->get_data($content,$maxcharperpage);//處理并返回字符串
- }
- if($rs['paginationtype']!=0) {
- //手動分頁
- $CONTENT_POS = strpos($content, '[page]');
- if($CONTENT_POS !== false) {
- $this->url = pc_base::load_app_class('url', 'content');
- //將內容$content以[page]分割為一個數組
- //[0]=>... [1] => 全體與眾嘉賓合影[/page]... [2] => 張靜初[/page]...
- $contents = array_filter(explode('[page]', $content));//以[page]分割為一個數組
- $pagenumber = count($contents);//分割成了多少頁
- if (strpos($content, '[/page]')!==false && ($CONTENT_POS<7)) {//判斷[page]出現的位置是否在第一位
- $pagenumber--;
- }
- /**
- * $pageurls大概格式如下:
- * Array //Vevb.com
- * (
- * [1] => Array
- * (
- * [1] => http://zhencms.com/index.php?m=content&c=index&a=show&catid=18&id=8
- * [0] => http://zhencms.com/index.php?m=content&c=index&a=show&catid=18&id=8
- * )
- *
- * [2] => Array
- * (
- * [1] => http://zhencms.com/index.php?m=content&c=index&a=show&catid=18&id=8&page=2
- * [0] => http://zhencms.com/index.php?m=content&c=index&a=show&catid=18&id=8&page=2
- * )
- *
- * [3] => Array
- * (
- * [1] => http://zhencms.com/index.php?m=content&c=index&a=show&catid=18&id=8&page=3
- * [0] => http://zhencms.com/index.php?m=content&c=index&a=show&catid=18&id=8&page=3
- * )
- *
- * )
- */
- for($i=1; $i<=$pagenumber; $i++) {
- //參數1:內容id 參數2:當前頁 參數3:欄目id 參數4:添加時間
- //作用:替換urlrules重寫規則中的變量,如:index.php?m=content&c=index&a=lists&catid={$catid}|index.php?m=content&c=index&a=lists&catid={$catid}&page={$page}
- $pageurls[$i] = $this->url->show($id, $i, $catid, $rs['inputtime']);//內容頁的鏈接
- }
- $END_POS = strpos($content, '[/page]');//標題的象征
- if($END_POS !== false) {
- if($CONTENT_POS>7) {
- //如:[page]《我的影子在奔跑》開機 張靜初演隱忍母親[/page]
- $content = '[page]'.$title.'[/page]'.$content;
- }
- if(preg_match_all("|/[page/](.*)/[/page/]|U", $content, $m, PREG_PATTERN_ORDER)) {
- foreach($m[1] as $k=>$v) {
- $p = $k+1;
- $titles[$p]['title'] = strip_tags($v);
- $titles[$p]['url'] = $pageurls[$p][0];
- }
- }
- /**
- *print_r($titles)
- *Array
- * (
- * [1] => Array
- * (
- * [title] => 《我的影子在奔跑》開機 張靜初演隱忍母親
- * [url] => http://zhencms.com/index.php?m=content&c=index&a=show&catid=18&id=8
- * )
- *
- * [2] => Array
- * (
- * [title] => 全體與眾嘉賓合影
- * [url] => http://zhencms.com/index.php?m=content&c=index&a=show&catid=18&id=8&page=2
- * )
- *
- * [3] => Array
- * (
- * [title] => 張靜初
- * [url] => http://zhencms.com/index.php?m=content&c=index&a=show&catid=18&id=8&page=3
- * )
- *
- * )
- */
- }
- //當不存在 [/page]時,則使用下面分頁 只有分頁,沒有子標題時
- //參數1:總頁碼 參數2:當前頁碼 參數3:鏈接地址
- $pages = content_pages($pagenumber,$page, $pageurls);//分頁函數,路徑:phpcms/modules/content/functions/util.func.php
- //判斷[page]出現的位置是否在第一位
- if($CONTENT_POS<7) {
- $content = $contents[$page];//$contents:內容段數組 $contents[$page]:第幾頁內容
- } else {
- if ($page==1 && !emptyempty($titles)) {
- //如:《我的影子在奔跑》開機 張靜初演隱忍母親[/page]導演方剛亮
- $content = $title.'[/page]'.$contents[$page-1];
- } else {
- //如:全體與眾嘉賓合影[/page]全體與眾嘉賓合影
- $content = $contents[$page-1];
- }
- }
- /**
- * echo $content;
- * 如:全體與眾嘉賓合影[/page]全體與眾嘉賓合影
- */
- if($titles) {//子標題
- list($title, $content) = explode('[/page]', $content);//$title-子標題 $content-內容
- $content = trim($content);//內容
- if(strpos($content,'</p>')===0) {
- $content = '<p>'.$content;//左補齊
- }
- if(stripos($content,'<p>')===0) {
- $content = $content.'</p>';//右補齊
- }
- }
- }
- }
- $this->db->table_name = $tablename;//主表
- //上一篇
- $previous_page = $this->db->get_one("`catid` = '$catid' AND `id`<'$id' AND `status`=99",'*','id DESC');
- //下一篇
- $next_page = $this->db->get_one("`catid`= '$catid' AND `id`>'$id' AND `status`=99");
- if(emptyempty($previous_page)) {//如果 上一篇 為空 ,則顯示 第一頁
- $previous_page = array('title'=>L('first_page'), 'thumb'=>IMG_PATH.'nopic_small.gif', 'url'=>'javascript:alert(/''.L('first_page').'/');');
- }
- if(emptyempty($next_page)) {//如果 下一篇 為空,則顯示 最后一頁
- $next_page = array('title'=>L('last_page'), 'thumb'=>IMG_PATH.'nopic_small.gif', 'url'=>'javascript:alert(/''.L('last_page').'/');');
- }
- include template('content',$template);//顯示content模塊的$template模板
- }
新聞熱點
疑難解答