<?php 
/*******************************************************************
 * 版权协议：2009-2019
 * 版权所有: PHP-北京金南曦网络技术有限公司旗下PHPWMS，PHP商城系统技术支持中心http://www.php188.net，并保留所有权利。 
* PHP商城系统网址: http://www.php188.com
 * PHPWMS系统网址：http://www.phpwms.com
 * 销售电话：010-87875088
 * 销售电话：010-87875188 
*------------------------------------------------------------------
 * 这不是一个完全免费软件！您只能在不用于商业目的的前提下对程序代码进
 * 行修改和使用；不允许对程序代码以任何形式任何目的的再发布。
 *------------------------------------------------------------------
 * 文件名称：phpwms.content.php
 * 摘　　要：频道模型函数
 * 适用范围：PHPWMS 1.0
 * 版　　本：1.0
 *------------------------------------------------------------------
 * Script Written By phpwms项目组
 * 最后更新： 2009-09-24 phpwms 由PHP金南曦网络技术有限公司整理发布
 *******************************************************************/
if(!defined('IN_SYS'))exit('access die');
class content_WMS extends base {
	private $_model_name = null;
	protected function init(){
		parent::init();	
	}
	private function _getNavByContentId($content_data){
		$category_id = $content_data['category_id'];
		$cate_data = get_category('','',$category_id);
		$channel_id = $cate_data['channel_id'];
		if(empty($channel_id))$this->_exception('',TRUE);
		$action = get_action($channel_id);
		//频道
		$nav[] = array('name'=>$cate_data['channel_name'],'link'=>building_url('models',$action,array($channel_id)));
		//所属分类
		if(!class_exists('WMSTree'))require PUB_CLASS.'phpwms.tree.class.php';
		$tree = new WMSTree();
		$parent_trees = $tree->get_parent(get_category('',$channel_id),'category_id','parent_id',$category_id,array('category_name','category_id'));
		if(!empty($parent_trees)){
			foreach ($parent_trees as $k=>$t){
				$parent_trees[$k]['name']= $t['category_name'];
				$parent_trees[$k]['link'] = building_url('models',$action,array($channel_id,$t['category_id']));
				unset($parent_trees[$k]['category_name']);
				unset($parent_trees[$k]['category_id']);
			}
		}
		$nav = array_merge($nav,$parent_trees);
		$nav[] = array('name'=>$content_data['long_title']);
		unset($parent_trees);
		return $nav;
	}
	function wmsAction(){
		$id = to_int($this->_parseItem[0]);
		if(empty($id))$this->_exception('参数错误',TRUE);
		$check_data = $this->db->fetch("SELECT * FROM ##__contents WHERE `c_id` = '$id' order by c_id limit 0,1;",TRUE);
		if(empty($check_data))$this->_exception('无可用数据');
		if(!empty($check_data)){
			$channel_model = $check_data['channel_model'];
			// 上一页下一页
			$all = $this->db->fetch("
			(SELECT  `c_id`,`long_title` as title FROM `##__contents` WHERE `c_id`<'$id' AND  `channel_model` = '$channel_model'  ORDER BY `c_id` DESC  LIMIT 0,1  )
				UNION ALL 
			(SELECT  `c_id`,`long_title` as title FROM `##__contents` WHERE `c_id`>'$id' AND  `channel_model` = '$channel_model' ORDER BY `c_id` ASC  LIMIT 0,1 )
			 
			");
			if(!empty($all)){
				$last = !empty($all[0])?array('name'=>$all[0]['title'],'link'=>building_url('content','',array($all[0]['c_id']))):array();
				$next = !empty($all[1])?array('name'=>$all[1]['title'],'link'=>building_url('content','',array($all[1]['c_id']))):array();
			}else{
				$next = array();
				$last = array();
			}
		}
		$action = get_action($check_data['channel_model']);
		$out_link = $check_data['out_link'];
		if(!empty($out_link)){
			@header("location:$out_link");
			exit();
		}
		$cate_data = get_category('','',$check_data['category_id']);
		$this->_model_name = $cate_data['model_name'];
		if($cate_data['category_pass'] && $_SESSION['pub_category_logined_'.$cate_data['category_id']]!='ok'){
			//跳转到URL
			setcookie('re_url',$_SERVER['REQUEST_URI'],time()+300,'/');
			$url = building_url('models',$action,array($check_data['channel_id'],$cate_data['category_id']));
			header("location:".$url);
			exit();
		}
		$this->db->query("UPDATE ##__contents SET `hits` = hits+1 where `c_id` IN ($id) order by c_id ;");
		$check_data = filter_site_url_decode($check_data);
		$check_data['seo'] = unserialize_deep($check_data['seo']);
		$url = building_url('content','',array($id));
		$check_data['content'] = pages_for_array(array_filter(explode('[-page-]',$check_data['content'])),1,'',$url);
		$seo_data = array(
			'title'=>$check_data['long_title'],
			'description'=>$check_data['seo']['description'],
			'keywords'=>$check_data['seo']['keywords'],
		);
		$this->setSeo($seo_data);
		$navs = $this->_getNavByContentId($check_data);
		$this->_setNav($navs);
		$guest_book_set = unserialize_deep(get_cache(array('phpwms_guestbook')));
			//若$guest_book_set['is_need_check'] 为1表示显示
		$ADD = $guest_book_set['is_need_check']=='1'?" AND `hide` = '0' ":'';
		$comment_total_data = $this->db->fetch("SELECT COUNT(*) AS T FROM ##__user_request WHERE `c_id` = '$id'  $ADD ORDER BY `id` limit 0,1;",TRUE);
		$e_id = to_int($check_data['type_id']);
		if(!empty($e_id)){
				$extend_data = $this->db->fetch("SELECT * FROM ##__extend_attr WHERE `attr_type_id` = '$e_id' order by attr_id ;");
				$attrs = array();
				if(!empty($id)){
					$attrs = $this->db->fetch("SELECT * FROM ##__extend_type_attr_bind  WHERE `article_id` = '$id'  order by `b_id`");
					$attrs = array_group_by($attrs,'attr_id');
				}
				$attr_keys = array_keys($attrs);
				foreach ($extend_data as $k=>&$d){
					$jd = $attrs[$d['attr_id']][0];
					if(!empty($jd['value']) && in_array($d['attr_id'],$attr_keys)){
						$d = array_merge($d,$attrs[$d['attr_id']][0]);
					}else{
						unset($extend_data[$k]);
					}
				}
		}
		$this->assign(array(
			'data'=>$check_data,
			'extend_data'=>$extend_data,//扩展类型数据
			'next'=>$next,
			'last'=>$last,
			'cat'=>$cate_data['link_cat_data'],
			'category_id'=>$check_data['category_id'],
			'channel_id'=>$check_data['channel_id'],
			'curent_link'=>building_url('content','',array($id)),
			'comment'=>array(
				'comment_post_url'=>building_url('member@comment', 'comment', array($id)),
				'num'=>$comment_total_data['T'],
				'link'=>building_url('member@comment','',array($id))
			)
		));
		/**判断是否设置了模版名称，没有则默认。有则调用设置的模版**/
		$model = empty($this->_model_name) ? get_action($check_data['channel_id']) : $this->_model_name;
		unset($check_data);
		$model = empty($model)?'article':$model;
		$f = $model.'/'.$model.'_content.php';
		$this->display($f);
	}
} 
?>