<?php
/**
 * @package iCMS
 * @copyright 2007-2010, iDreamSoft
 * @license http://www.idreamsoft.cn iDreamSoft
 * @author coolmoo <idreamsoft@qq.com>
 */
!defined('iPATH') && exit('What are you doing?');

function iCMS_catalog($vars,&$iCMS){
	$row		= isset($vars['row'])?(int)$vars['row']:"10";
	$cacheTime	= isset($vars['time'])?(int)$vars['time']:"-1";
	$status		= isset($vars['status'])?(int)$vars['status']:"0";
	$whereSQL=" WHERE ishidden='$status'";
	if($vars['att']=='list'){
		$whereSQL.=" and `attr` in ('channel','list')";
	}elseif($vars['att']=='index'){
		$whereSQL.=" and `attr` = 'index'";
	}elseif($vars['att']=='page'){
		$whereSQL.=" and `attr` = 'page'";
	}
	if(isset($vars['mid'])){
		$whereSQL.=" and `mid` = '{$vars['mid']}'";
	}
	isset($vars['id']) && !isset($vars['type']) && $whereSQL.= getSQL($vars['id'],'id');
	isset($vars['id!']) && $whereSQL.= getSQL($vars['id!'],'id','not');
	switch ($vars['type']) {
		case "top":	
			$vars['id'] && $whereSQL.= getSQL($vars['id'],'id');
			$whereSQL.=" AND rootid='0'";
		break;
		case "subtop":	
			$vars['id'] && $whereSQL.= getSQL($vars['id'],'id');
		break;
		case "sub":	
			$whereSQL.= getSQL(getCids($vars['id']),'id');
		break;
		case "subone":	
			$whereSQL.= getSQL(getCids($vars['id'],false),'id');
		break;
		case "allsub":
			$whereSQL.= getSQL(getCids(),'id');
		break;
		case "self":
			$cParent=$iCMS->cache('catalog.parent','include/syscache',0,true);
			$whereSQL.=getSQL(getCids($cParent[$vars['id']],false),'id');
		break;
	}

	if($vars['cache']==false){
		$iCMS->config['iscache']=false;
		$rs = '';
	}else{
		$iCMS->config['iscache']=true;
		$cacheName='catalog/'.md5($whereSQL);
		$rs=$iCMS->cache($cacheName);
	}
	if(empty($rs)){
		$rs=$iCMS->db->getArray("SELECT * FROM `#iCMS@__catalog`{$whereSQL} ORDER BY `order`,`id` ASC LIMIT $row");
//echo $iCMS->db->last_query;
//$iCMS->db->last_query='explain '.$iCMS->db->last_query;
//$explain=$iCMS->db->getRow($iCMS->db->last_query);
//var_dump($explain);
		$_count=count($rs);
		for ($i=0;$i<$_count;$i++){
			$rs[$i]['url']=$iCMS->iurl($rs[$i]['attr'],$rs[$i])->href;
			$rs[$i]['link']="<a href='{$rs[$i]['url']}'>{$rs[$i]['name']}</a>";
		}
		$iCMS->cache(false)->addcache($cacheName,$rs,$cacheTime);
	}
	return $rs;
}
?>