<?php
die("access die");
$action = $_GET['action'] ? $_GET['action'] : 'manage';
qbase::checkAction( $action);
checkAdminReferer();
switch($action){
	case 'manage':
		$data = array();
		$data['channel'] = qdb::queryAlone("select count(*) as num from ".TABLEPRE."channel where link = 0 ");
		$data['channel'] = intval($data['channel']);
		$data['category'] = qdb::queryAlone("select count(*) as num from ".TABLEPRE."acategory ");
		$data['category'] = intval($data['category']);
		$data['doc'] = getDocNum();
		$manager = qdb::fetchAll("select uid, username from ".TABLEPRE."admin ");
		$data['manager'] = count($manager);
		
		$type = $_GET['type'] ? $_GET['type'] : 'm';
		$uid = urldecode(trim($_GET['uid']));
		$sql = '';
		$where = '';
		if($uid) $where = " where uid = '{$uid}' ";
		switch($type){
			case 'y':
				$sql = "select count(*) as num, y as ym  from ".TABLEPRE."admin_count {$where} group by y";
			break;
			
			case 'm':
				$sql = "select count(*) as num, m as ym  from ".TABLEPRE."admin_count {$where} group by m";
			break;
			
			case 'd':
				$sql = "select count(*) as num, d as ym from ".TABLEPRE."admin_count {$where} group by d";
			break;	
			
		}
		$max = 0;
		if($sql){
			$data['data'] = qdb::fetchAll($sql);
			foreach($data['data'] as &$v){
				$v['ym'] = dateFormat($v['ym']);
				$max = max($v['num'], $max);
			}
		}else{
			$data['data'] = array();
		}
		$max = max(1, $max);
	break;
	
	case 'category':
		$data = array();
		foreach($channel as $channelid=>$t){
			if($t['link']) continue;
			$table = "".TABLEPRE."" . $_MODULE[$t['channelselect']]['table_name'] . "_" . $channelid;
			if(empty($_MODULE[$t['channelselect']]['table_name'])) continue;			
			if(false === ($menus = cache::get('sys_category_' . $channelid))){
				$menus = qbase::get_object('category', $channelid)->cache();
			}
			$map = array();
			foreach($menus as $key=>$val){
				$data[$channelid][$key] = $val;
				if($val['child'] > 0){
					$data[$channelid][$key]['cnum'] = 0;
					$data[$channelid][$key]['wnum'] = 0;
					$map[$val['cid']] = array(0, 0);
				}else{				
					$data[$channelid][$key]['cnum'] = qdb::queryAlone("select count(*) as num from {$table} where cid = '{$val['cid']}' ");
					$data[$channelid][$key]['cnum'] = intval($data[$channelid][$key]['cnum']);
					$data[$channelid][$key]['wnum'] = qdb::queryAlone("select count(*) as num from ".TABLEPRE."recycle where cid = '{$val['cid']}' and status = 3");
					$data[$channelid][$key]['wnum'] = intval($data[$channelid][$key]['wnum']);
					$map[$val['cid']] = array($data[$channelid][$key]['cnum'], $data[$channelid][$key]['wnum']);
				}
			}
			$data[$channelid] = getCategoryDocNum($data[$channelid], $map);
		}
	break;
}

function getCategoryDocNum(&$category, &$map){
	foreach($category as &$val){
		if(empty($val['arrchildid'])) continue;
		foreach($val['arrchildid'] as $cid){
			if($cid == $val['cid']) continue;
			$val['cnum'] += $map[$cid][0];
			$val['wnum'] += $map[$cid][1];
		}
	}
	return $category;
}

function dateFormat($ym){
	return preg_replace("/([0-9]{4})([0-9]{2})([0-9]{0,2})/", "\\1-\\2-\\3", $ym);
}

function getDocNum(){
	global $channel, $_MODULE;
	$num = 0;
	foreach($channel as $id=>$val){
		if($val['link']) continue;
		$table = "".TABLEPRE."" . $_MODULE[$val['channelselect']]['table_name'] . "_" . $id;
		if(empty($_MODULE[$val['channelselect']]['table_name'])) continue;
		$n = qdb::queryAlone("select count(*) as num from {$table} ");
		$n = intval($n);
		$num += $n;
	}
	return $num;
}

include qbase::template($path . 'count');
?>
