<?php
$catid = isset($_GET['catid']) ? intval($_GET['catid']) : '';
$areaid = isset($_GET['areaid']) ? intval($_GET['areaid']) : '';
$posttime = isset($_GET['posttime']) ? intval($_GET['posttime']) : '';
$timestamp = time();

$cat_list   = get_categories_tree($catid);

if(empty($catid)){
	define(CURSCRIPT,'category');
	include mymps_tpl('category_all');
	exit;
}
if(!$cat = get_cat_info($catid)){
	errormsg('您所指定的栏目不存在或者已被删除！');
}

if($cat['parentid'] == 0){
	define(CURSCRIPT,'category');
	
	$perpage = $mobile_settings['mobiletopicperpage'] ? $mobile_settings['mobiletopicperpage'] : 10;
	$page = isset($_GET['page']) ? intval($_GET['page']) : '';
	$page = empty($page) ? 1 : $page;
	
	$parent = $db -> getRow("SELECT catid,catname FROM `{$db_mymps}category` WHERE catid = '$cat[parentid]'");
	$rows_num = $db -> getOne("SELECT COUNT(a.id) FROM `{$db_mymps}information` AS a WHERE a.info_level >0 {$cate_limit}{$area_limit}{$posttime_limit}");
	$totalpage = ceil($rows_num/$perpage);
	$num = intval($page-1)*$perpage;
	$param = setparams(array('mod','catid','areaid'));
	if(is_array($cat_list[$catid][children])){
		foreach($cat_list[$catid][children] as $k => $v){
			$cat_children .= $v['catid'].',';
		}
		$cat_children = substr($cat_children,0,-1);
	} else {
		$cat_children = $catid;
	}
	$info_list = $db -> getAll("SELECT a.* FROM `{$db_mymps}information` AS a WHERE a.info_level > 0 AND a.catid IN ($cat_children) ORDER BY a.upgrade_type DESC,a.begintime DESC LIMIT $num,$perpage");
	
	include mymps_tpl('category_cat');
} else{
	include MYMPS_INC.'/global.php';
	define(CURSCRIPT,'list');
	$allow_identifier  = allow_identifier();
	$allow_identifier  = $allow_identifier[$cat['modid']]['identifier'];
	$allow_identifier  = is_array($allow_identifier)?$allow_identifier:array();
	$allow_identifiers = array_merge(array('catid','areaid'),$allow_identifier);

	$extra_in  = '';
	$extra_num = 0;
	$extra_uri = '';
	$extra_arg = array();
	$sql = "SELECT infoid,count(*) AS num FROM `{$db_mymps}info_extra` WHERE 0";
	foreach ($$_request as $key => $val){
		if(in_array($key,$allow_identifier) && !empty($$key)){
			$val = intval($val);
			$extra_num++;
			$sql .= " OR (1 ";
			$sql .= empty($val) ? "AND name = '$key'" : " AND name = '$key' AND value = '$val' ";
			$extra_arg["$key"] = $val;
			$sql .= ')';
		}
	}
	if ($extra_num > 0){
		$sql .= " GROUP BY infoid HAVING num = '$extra_num'";
		$row = $db->getCol($sql);
		if (count($row)){
			$extra_in = " AND " . create_in($row, 'a.id');
		}else{
			$extra_in = " AND 0 ";
		}
	} else {
		$sql = '';
	}
	
	$mymps_extra_model = mod_identifier();
	$mymps_extra_model = $mymps_extra_model[$cat['modid']];
	$mymps_extra_model = is_array($mymps_extra_model) ? $mymps_extra_model : array();

	foreach($mymps_extra_model as $key => $val){
		if(is_array($val['list'])){
			foreach($val['list'] as $k => $v){
				$mymps_extra_model[$key]['list'][$k]['uri'] = 'index.php?mod=category&';
				foreach($allow_identifiers as $keys){
					if($v['identifier'] == $keys){
						$mymps_extra_model[$key]['list'][$k]['uri'] .= $v['id'] ? $keys.'='.$v[id].'&' : '';
					} else {
						$mymps_extra_model[$key]['list'][$k]['uri'] .= $$keys ? $keys.'='.$$keys.'&' : '';
					}
				}
				$mymps_extra_model[$key]['list'][$k]['uri'] = substr($mymps_extra_model[$key]['list'][$k]['uri'],0,-1);
				if($v[id] == $$v[identifier]){
					$mymps_extra_model[$key]['list'][$k]['select'] .= 1;
				} else{
					$mymps_extra_model[$key]['list'][$k]['select'] .= 0;
				}
			}
		}
	}
	$shaixuan = $mymps_extra_model;

	$area_list = cat_list('area',0,0,false,1);
	$area_list = array_merge(array('0'=>array('areaid'=>'','areaname'=>'不限')),$area_list);
	foreach($area_list as $key => $val){
		$area_list[$key]['uri'] = 'index.php?mod=category&';
		foreach($allow_identifiers as $keys){
			if($keys == 'areaid'){
				$area_list[$key]['uri'] .= $val['areaid'] ? $keys.'='.$val[areaid].'&' : '';
			} else {
				$area_list[$key]['uri'] .= $$keys ? $keys.'='.$$keys.'&' : '';
			}
		}
		$area_list[$key]['uri'] = substr($area_list[$key]['uri'],0,-1);
		$area_list[$key]['select'] = $val['areaid'] == $areaid ? 1 : 0 ;
	}
	
	$perpage = $mobile_settings['mobiletopicperpage'] ? $mobile_settings['mobiletopicperpage'] : 10;
	$page = isset($_GET['page']) ? intval($_GET['page']) : '';
	$page = empty($page) ? 1 : $page;
	
	$cat_list   = get_categories_tree($catid);
	$cate_limit = " AND ".get_children($catid);
	$area_limit = " AND ".get_area_children($areaid);
	
	$parent = $db -> getRow("SELECT catid,catname FROM `{$db_mymps}category` WHERE catid = '$cat[parentid]'");
	$rows_num = $db -> getOne("SELECT COUNT(a.id) FROM `{$db_mymps}information` AS a WHERE a.info_level > 0 {$cate_limit}{$area_limit}{$posttime_limit}{$extra_in}");
	$totalpage = ceil($rows_num/$perpage);
	$num = intval($page-1)*$perpage;
	$param = setparams(array('mod','catid','areaid'));
	$idin 		= NULL;
	$idin 		= get_page_idin("id","SELECT a.id FROM `{$db_mymps}information` AS a WHERE a.info_level > 0 {$extra_in}{$cate_limit}{$area_limit}{$orderby}",$mymps_global['cfg_list_page_line'] ? $mymps_global['cfg_list_page_line'] : 15);
	$infolist = $idin ? $db -> getAll("SELECT a.* FROM `{$db_mymps}information` AS a WHERE a.info_level > 0 {$cate_limit}{$area_limit}{$posttime_limit}{$extra_in} ORDER BY a.upgrade_type_list DESC,a.begintime DESC LIMIT $num,$perpage") : array();
	foreach($infolist as $k => $row){
		$arr['id']              = $row['id'];
		$arr['title']           = $row['title'];
		$arr['hit']             = $row['hit'];
		$arr['img_path']        = $row['img_path'];
		$arr['img_count']       = $row['img_count'];
		$arr['upgrade_type']    = $row['upgrade_type'];
		$arr['upgrade_type_list']    = $row['upgrade_type_list'];
		$arr['userid']          = $row['userid'];
		$arr['contact_who']     = $row['contact_who'];
		$arr['content']	        = clear_html($row['content']);
		$arr['begintime']       = get_format_time($row['begintime']);
		$info_list[$row['id']]	= $arr;
		$ids .= $row['id'].',';
	}	
	
	$infoid 	= substr($ids,0,-1);
	$ids 		= NULL;

	if($info_list){
		$sql = "SELECT c.infoid,c.name,c.value,d.optionid,d.title,d.type,d.rules FROM {$db_mymps}info_extra as c LEFT JOIN {$db_mymps}info_typeoptions as d on d.identifier = c.name WHERE c.infoid IN ($infoid)";
		$res = $db->query($sql);
		while($row=$db->fetchrow($res)){
			$optionarray = get_extraoption_value(array('type'=>$row['type'],'rules'=>$row['rules']),$row['value']);
			$arr[$row['infoid']][$row['name']] = $optionarray['value'];
		}
		foreach($info_list as $key => $info){
			$info_list[$key] = is_array($arr[$key]) ? array_merge($info,$arr[$key]) : $info;
		}
	}

	include mymps_tpl('category_list');
}
?>