<?php
if (!defined('MOD')) die("access die");
$cid = intval($_GET['cid']);
$ordering = intval($_GET['ordering']);
$filter = intval($_GET['filter']);
$filters = array($filter);
$status =  intval($_GET['status']);
$pageno = max(intval($_GET['pageno']), 1);
$offset = ($pageno - 1) * $pagesize;
$where = " ";
if ($cid > 0) {
	purviewCheck($cid, 'category');
	$arrchildid = qdb::QueryAlone("select arrchildid from ".TABLEPRE."acategory where cid='$cid' limit 1");
	if($arrchildid) $where .= " and a.cid IN(" . $arrchildid . ")  ";
	$strall = str_replace("<option value=\"{$cid}-", "<option selected value=\"{$cid}-", $menuTree);
} else {
	if ($admin_info['groupid'] < 3 || ($admin_info['groupid'] == 3 && in_array($channelid, $manage_channel))) {
		$where .= "  ";
	} else {
		$where .= " and a.cid IN(" . implode(',', $manage_category[$channelid]) . ")   ";
	} 
} 
if ($filter > 0) 	$where .= " and a.filter like '%{$filter}%' ";
if (isset($_GET['is_comend']) && intval($_GET['is_comend']) > 0) $where .= " and a.is_comend > 0";
if ($status == 0){
	$where = " where 1=1 " . $where;
	if ($_GET['f'] == 1) {
		$type = $_GET['type'];
		if (!in_array($type, array('subject', 'editor', 'description', 'id'))) $type = 'subject';
		$keyword = $_GET['keyword'];
		$keyword = trim(urldecode($keyword));
		if($keyword){
			if($type == 'id'){
				$keyword = str_replace(array('，',' '), ',', $keyword);
				$key_arr = explode(",", $keyword);
				$ids = array();
				foreach($key_arr as $val){
					$val = intval($val);
					if($val > 0){
						$ids[] = $val;
					}
				}
				if(!empty($ids)){
					$ids_string = implode(",", $ids);
					$where .= " and a.id IN({$ids_string})  " ;
				}else{
					$where .= " and (a.subject LIKE '%" . addslashes("$keyword") . "%')";
				}
			}else{
				$where .= " and (a.{$type} LIKE '%" . addslashes("$keyword") . "%')";
			}
		}
	}
	if(isset($_GET['param']) && !empty($_GET['param']) && is_array($_GET['param'])){
		$temp = qdb::fetchAll("SHOW FIELDS FROM  ".TABLEPRE."{$mod}_{$channelid} ");
		$mapping = array();
		foreach($temp as $val){
			$mapping[$val['Field']] = strpos($val['Type'], 'int') !== false ? 1 : 0;
		}
		$fields = array_keys($mapping);
		foreach($_GET['param'] as $key=>$val){
			if(!in_array($key, $fields)) continue;
			$val = trim(urldecode($val));
			if($mapping[$key]) $val = intval($val);
			if($val > -1) $where .= " and a.{$key} = '{$val}' ";
		}
	}
	$total = qdb::QueryAlone("SELECT COUNT(*) FROM ".TABLEPRE."{$mod}_{$channelid} as a " . $where);
	$where .= " order by a.id " .($ordering ? 'asc' : 'desc' ). " limit " . $offset . " , " . $pagesize;
	$query = "select a.*, c.title, c.ciddir, c.pur_browse, c.html, c.category_html  from ".TABLEPRE."{$mod}_{$channelid} as a left join ".TABLEPRE."acategory as c on a.cid=c.cid  " . $where;
	$data = qdb::fetchAll($query);
	$published = true;
}else{
	$where = " where a.channelid = '{$channelid}' and a.status = '{$status}'  " . $where;
	$total = qdb::QueryAlone("SELECT COUNT(*) FROM ".TABLEPRE."recycle as a " . $where);
	$where .= " order by a.id " .($ordering ? 'asc' : 'desc' ). " limit " . $offset . " , " . $pagesize;
	$query = "select a.data, c.title, c.ciddir, c.pur_browse, c.html, c.category_html from ".TABLEPRE."recycle as a left join ".TABLEPRE."acategory as c on a.cid=c.cid  " . $where;
	$data = array();
	$temp = qdb::fetchAll($query);
	foreach($temp as $val){
		$tp = unserialize($val['data']);
		if(empty($tp)) continue;
		unset($val['data']);
		$data[] = array_merge($val, $tp);
	}
	$published = false;
}

foreach ($data as &$item) {
	if ($_GET['f'] == 1 && $keyword) $item['subject'] = eregi_replace($keyword, '<font color=red>' . $keyword . '</font>', $item['subject']);
	$iif = !($item['html'] || $item['pur_browse'] || $item['pay']);
	if ($published){
		$item['ym'] = qbase::GetHtmlDirType($channelid, $item['id'], $item['ciddir'], $item['dateline'], $item['filename'], $item['ver'], $iif);
		$html_file = R_P .'/'. $channeldir . "/" . qbase::cutHtmlUrl($channelid, $item['ym']);
		if ($item['pur_browse'] == 1 || $item['html'] == 1) {
			$item['des'] = '已生成';
			$item['alt'] = '所属栏目为认证栏目,不用生成';
			$item['html'] = 1;
		} else if ($item['pay'] > 0) {
			$item['des'] = '已生成';
			$item['alt'] = '该' . $channeltitle . '有阅读权限设置,不用生成';
			$item['html'] = 1;
		} else if (is_file($html_file) && filemtime($html_file) >= $item['edit_time']) {
			$item['des'] = "<a href='javascript:htmlCreate({$item['id']},1," . (isset($item['page']) ? $item['page'] : 1) . ");'><FONT COLOR=#3300CC id='html_{$item['id']}'>已生成</FONT></a>";
			$item['alt'] = '该' . $channeltitle . '已经生成静态页面';
			$item['html'] = 1;
		} else {
			$item['des'] = "<a href='javascript:htmlCreate({$item['id']},1," . (isset($item['page']) ? $item['page'] : 1) . ");'><FONT COLOR=#3300CC id='html_{$item['id']}'>待生成</FONT></a>";
			$item['alt'] = '该' . $channeltitle . '尚未生成静态页面';
			$item['html'] = 0;
		} 
	}else{
		$item['ym'] = qbase::GetHtmlDirType($channelid, $item['id'], $item['ciddir'], $item['dateline'], $item['filename'], $item['ver'], 0);
		$item['des'] = "<FONT COLOR=#3300CC id='html_{$item['id']}'>不能生成</FONT>";
		$item['alt'] = '该' . $channeltitle . '尚未发布,不能生成html';
		$item['html'] = 0;
	}
	$item['oldtime'] = $item['dateline'];
	$item['dateline'] = qbase::formatTime('Y/m/d', $item['dateline']);
	$item['pagemore'] = $item['page'] + 1;
} 
unset($item);
if(AJAX){
	$data= array("total"=>$total, 'data'=>$data);
	echo qbase::xmlPrint(qbase::jsonencode($data));
}else{
	include qbase::template($path . $mod . '/manage');
}
exit();
?>
