<?php
 /**
 *      [XCECP] (C) COLNET Inc.
 *      This is NOT a freeware, use is subject to license terms
 */
if(!defined("IN_QKWEB")){exit("Access Denied");}
function smarty_block_codemap($params, $content, &$smarty, &$repeat) {
	global $db;
	if(!isset($params['name']) || $params['name']==""){
		$params['name'] = "item";
	}
	$dataindex = md5(__FUNCTION__ . md5(serialize($params)));
	if(!isset($smarty->block_data)){$smarty->block_data=array();}
	if(!isset($smarty->block_data[$dataindex])){
		$wheres = "";
		if(isset($params['limit']) && $params['limit']!=""){		
			if($params['limit']!=="false"){			
				$limit = $params['limit'];
				if(settype($limit,"integer")){
					$limit = $limit;
				}else{
					$limit = 10;
				}
			}else{
				$limit =false;
			}
		}else{
			$limit = 10;
		}
		$pageSize = $limit;
		$smarty->assign("limit",$limit);
		$limit = "limit $limit";
		$page = getgpc("page");
		if(!$page || !settype($page,"integer") || $params['pager']!="true"){$page=1;}
		if($page && settype($page,"integer")){
			$offset = ($page-1)*$pageSize;
			$limit = "limit $offset,$pageSize";
		}
		$offset = ($page-1)*$pageSize;
		$limit = "limit $offset,$pageSize";
		if(isset($params['limit']) && $params['limit']=="false"){
			$limit = "";
		}
		if(isset($params['id']) && $params['id']!=""){
			$ids = $params['id'];
			$idArr = explode(",",$ids);
			$ids = "";
			foreach($idArr as $val){
				if($val && settype($val,"integer")!=false){
					$ids .= $val.",";
				}
			}
			if(substr($ids,-1,1)==","){$ids = substr($ids,0,strlen($ids)-1);}
			$wheres.= " and id in (".$ids.")";
		}
		if(isset($params['parentId']) && $params['parentId']!=""){
			$parentId = $params['parentId'];
			$wheres.= " and parentId = '".$parentId."'";
		}

		$sqlOrderBy = "";
		if(isset($params['orderBy']) && $params['orderBy']!=""){
			$orderbys = $params['orderBy'];
			$orderbyArr = explode(",",$orderbys);
			$orderbys = "";
			foreach($orderbyArr as $val){
				$orderArr = explode(" ",$val);
				if($orderArr[0] && isset($orderArr[1]) && in_array($orderArr[1],array("asc","desc"))){
					if($orderArr[0]!=""){
						if($sqlOrderBy==""){
							$sqlOrderBy .= $orderArr[0]." ".$orderArr[1];
						}else{
							$sqlOrderBy .= ",".$orderArr[0]." ".$orderArr[1];
						}
					}
				}
			}
		}else{
			$sqlOrderBy = " id desc";
		}


		
		$allCount = $GLOBALS['db']->getOne("select count(id) from `qk_codemap` where 1=1 $wheres");
		$smarty->assign("allCount",$allCount);
		$sql = "select * from `qk_codemap` where 1=1 $wheres order by $sqlOrderBy $limit";
		$res = $db->getAll($sql);
		$smarty->block_data[$dataindex]=$res;
	}
	if(is_array($smarty->block_data[$dataindex]) && count($smarty->block_data[$dataindex])>0){
		$item = array_shift($smarty->block_data[$dataindex]);
		$smarty->assign($params['name'], $item);
		if(count($smarty->block_data[$dataindex])==0){
			$smarty->block_data[$dataindex]=false;
		}
		$repeat= true;
	}else{
		unset($smarty->block_data[$dataindex]);
		$repeat = false;
	}
	echo $content;
}
?>