<?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.models.php
 * 摘　　要：频道模型函数
 * 适用范围：PHPWMS 1.0
 * 版　　本：1.0
 *------------------------------------------------------------------
 * Script Written By phpwms项目组
 * 最后更新： 2009-09-24 phpwms 由PHP金南曦网络技术有限公司整理发布
 *******************************************************************/
if(!defined('IN_SYS'))exit('ph188.com');
class models_WMS extends base {
	function init(){
		parent::init();
	}
	function wmsAction(){
		$this->_getList();
	}
	private function _getList(){
		$channel_id = to_int($this->_parseItem[0]);/*频道ID 不是模型ID*/
		$cate_id = to_int($this->_parseItem[1]);/*分类ID*/
		$url = get_self();
		/*如果分类为空,提取频道下的所有分类包括文章*/
		$action = $this->_parseModels['action'];
		if(empty($action))header("location:$url");
		if(empty($channel_id)){
			$c = get_channels();
			$t = array();
			foreach ($c as $j){
				if($j['en_name']==$action){
					$channel_id = $j['channel_id'];
					$t = $j;
					break;
				}
			}
			if(empty($channel_id))header("location:$url");
			$c = $t;
		}else{
			$c = 	get_channels('',$channel_id);
			if(is_mulit_array($c))header("location:$url");
		}
		$channel_tag = $c;
		$channel_name  = $c['channel_name'];
		$navs[] = array('link'=>building_url('models',$action,array($c['channel_id'])),'name'=>$channel_name);
		$display = $this->_parseModels['action'].'/';
		$cate_data = $categorys  = get_category('',$channel_id);/*获得频道下的所有分类数据*/
		if(empty($cate_data))$this->_exception('参数错误');
		/*列表频道下的所有一级分类*/
		if(empty($cate_id)){
			if(!empty($c['out_link'])){
				$out_link = $c['out_link'];
				header("location:$out_link");
				exit();
			}
				if(empty($categorys)){			
					$assign_data = array();
				}else{
					$show_num = to_int($c['display_num']);
					$show_num = empty($show_num)?10:$show_num;
					/*提取频道下的所有子分类*/
					foreach ($categorys as $k=>$t){
							$article_ids = get_category_cache_content_index($t['category_id']);
							if(!empty($article_ids)){
							//获得要显示的文章ID
								$a_id[$t['category_id']] =  (isset($article_ids[$show_num-1])?array_slice($article_ids,0,($show_num-1)):$article_ids);
							}else{
								unset($categorys[$k]);
							}
							unset($article_ids);
					}
					$all_article_ids  = join(',',!empty($a_id)?mulitarray_to_array($a_id,FALSE):array());
					if(!empty($all_article_ids)){
						$sql = "SELECT 
							`c_id`,`passwords`,`short_title`,`short_title`,`long_title`,`short_content`,`is_hot`,
							`thumb`,`is_pic`,`is_new` ,`is_top`,`is_recycle`,`post_time`,`edit_time`,`hits`,`down_times`,
							`post_user`,`out_link`,`channel_model`,`channel_id`,`category_id`,`source`,
							`ip`,`type_id`,`seo`,`is_index_slide`,`big_pic`,`attach_size`,`attach_link_1`,`attach_link_2`,
							`attach_link_3`,`top_news` , `product_price` 
						FROM ##__contents WHERE `c_id` in ($all_article_ids) ORDER BY `c_id` DESC ;";
						$d = $this->db->fetch($sql);
						$article_data = format_content($d) ;
						$data = array_group_by($article_data,'category_id');
						unset($article_data);
						foreach ($categorys as $k=>&$j){
							if(!empty($data[$j['category_id']])){
								$j['more_link'] = building_url('models',$this->_parseModels['action'],array($channel_id,$j['category_id']));
								$j['article_data'] = $data[$j['category_id']];
							}else{
								unset($categorys[$k]);
							}
						}
						unset($data);
					}
					$assign_data = $categorys;
					unset($categorys);
				}
				$self_config_chanel_tpl = $c['channel_tpl_name'];
				if(!empty($self_config_chanel_tpl)){
					unset($c);
					if(!is_file(TEMPLATE_MAIN_PATH.$this->_tpl_name.'/'.$self_config_chanel_tpl)){
						$display .= $action.'_channel.php';	
					}else{
						$display  = $self_config_chanel_tpl;
					}
				}else{
					$display .= $action.'_channel.php';	
				}
				$seo_data = array(
					'title'=>$channel_name,
				);
				$this->setSeo($seo_data);
			}
		/*处理分类*/
		if(!empty($cate_id)){
			$cate_exits  = array();
			foreach ($cate_data as $v){
				if($v['category_id']==$cate_id)$cate_exits = $v;
			}
			unset($cate_data);
			if(empty($cate_exits))$this->_exception('无可用分类');
			$cate_name = $cate_exits['category_name'];
			if(is_post()){
				if($_POST['pass']==$cate_exits['category_pass']){
					$_SESSION['pub_category_logined_'.$cate_id] = 'ok';
				}
				$cookies_url = $_COOKIE['re_url'];
				$url = $_SERVER['HTTP_REFERER'];
				$url = empty($cookies_url)?$url:$cookies_url;
				header("location:$url");
				exit();
			}
			$seo = unserialize_deep($cate_exits['seo_info']);
			$seo_data = array(
				'title'=>$cate_exits['category_name'],
				'description'=>$cate_exits['desc'],
				'keywords'=>$seo['kew_words'],
			);
			$this->setSeo($seo_data);
			unset($seo_data);
			$pass = $cate_exits['category_pass'];
			$is_logined = $_SESSION['pub_category_logined_'.$cate_id];
			if(!empty($pass) && $is_logined !='ok'){
				$this->assign(array(
					'has_pass'=>TRUE,
					'pass_url'=>building_url('models',$action,array($channel_id,$cate_id)),
				));
			}else{
				$this->assign(array(
					'has_pass'=>FALSE,
				));
				$is_logined = 'ok';
			}
			$navs[] = array('name'=>$cate_exits['category_name']);
			if($is_logined=='ok'){
				$cate_ids = join(',',array_merge(array($cate_id),get_cate_son($cate_id,FALSE,array('category_id'))));/*包括自身*/
				$select_total_data = unserialize_deep($cate_exits['cfg']);
				$perpage = to_int($select_total_data['show_number']);
					unset($select_total_data);
					$perpage = empty($perpage) || !is_numeric($perpage)?20:$perpage;
					$sec_sql = "SELECT C.`c_id`,C.`passwords`,C.`short_title`,C.`short_title`,C.`long_title`,C.`short_content`,C.`is_hot`,
							C.`thumb`,C.`is_pic`,C.`is_new` ,C.`is_top`,C.`is_recycle`,C.`post_time`,C.`edit_time`,C.`hits`,C.`down_times`,
							C.`post_user`,C.`out_link`,C.`channel_model`,C.`channel_id`,C.`category_id`,C.`source`,
							C.`ip`,C.`type_id`,C.`seo`,C.`is_index_slide`,C.`big_pic`,C.`attach_size`,C.`attach_link_1`,C.`attach_link_2`,
							C.`attach_link_3`,C.`top_news` ,C. `product_price` FROM
											 ##__contents AS C 
											 WHERE C.`channel_id` IN($channel_id)  AND C.`is_show` IN(1)
											 AND C.`is_recycle` IN(0) AND C.`category_id` IN ($cate_ids)
											 ORDER BY C.`c_id` DESC  ";
					$total_sql = "SELECT COUNT(*) AS T FROM
								 ##__contents AS C 
								 WHERE C.`channel_id` = '$channel_id'  AND C.`is_show` IN(1)
								 AND C.`is_recycle` IN(0) AND C.`category_id` IN ($cate_ids)
								 ORDER BY C.`c_id` DESC;";
					$total = $this->db->fetch($total_sql,TRUE);
					$total = $total['T'];
					$url = building_url('models',$action,array($channel_id,$cate_id));
					if($total>0){
						$assign_data = page_for_db_extend($total,$sec_sql,$url,$perpage);
						unset($url);
						$assign_data['data'] = format_content($assign_data['data'],$action);
					}
				$self_config_tpl_name  = $cate_exits['tpl_name'];
				unset($cate_exits);
			}
			if(!empty($self_config_tpl_name)){
				if(!is_file(TEMPLATE_MAIN_PATH.$this->_tpl_name.'/'.$self_config_tpl_name)){
					$display .= $action.'_category_list.php';
				}else{
						$display = $self_config_tpl_name;
				}
				
			}else{
				$display .= $action.'_category_list.php';
			}
		}/*分类处理结束*/
		$this->_setNav($navs);
		$this->assign(array(
			'category_name'=>$cate_name,
			'channel_id'=>$channel_id,
			'category_id'=>to_int($this->_parseItem['1']),
			'data'=>$assign_data
		));
		//$display = empty($model_name) ? $display : $model_name . '/' . $model_name . '_category_list.php' ;
		if(empty($display)){
			echo $display;
		}
		return $this->display($display);
	}
}
?>