【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(!empty($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
				 *	(
				 *	    [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 && !empty($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(empty($previous_page)) {//如果 上一篇 为空 ,则显示 第一页
			$previous_page = array('title'=>L('first_page'), 'thumb'=>IMG_PATH.'nopic_small.gif', 'url'=>'javascript:alert(\''.L('first_page').'\');');
		}

		if(empty($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模板
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值