<?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_article($params, $content, $smarty, &$repeat)
{ 
	
  	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;
		if($offset<1){$offset=0;}
		$limit = "limit $offset,$pageSize";
		if($params['limit']=="false"){
			$limit = "";
		}
		$fields="";
		if(isset($params['fields']) && $params['fields']!=""){
			$fields = $params['fields'];
		}

		if(isset($params['catId']) && $params['catId']!="" && preg_match("/^([0-9|,]+)$/",$params['catId'])){
			$catId = $params['catId'];
		}else{
			$catId = false;
		}
		if($catId){
			$wheres = " and catId in (select id from qk_article_cat where id in (".$catId.") or parentId in (".$catId."))";
		}
		if(isset($params['id'])){
			if($params['id']!="" && settype($params['id'],"integer")){
				$id = $params['id'];
			}else if($params['id']==''){
				$id = 0;
			}
		}else{
			$id = false;
		}
		if($id===0 || $id){
			$wheres = " and id='".$id."'";
		}
		if(isset($params['key']) && $params['key']!=""){
			$key = $params['key'];
		}
		if(isset($params['search']) && $params['search']=='true'){
			if(!$key){$key = getgpc("key");}
			if($key){$wheres .= " and title like '%$key%'";}
		}
		if(isset($params['iStatus']) && is_numeric($params['iStatus'])){
			$wheres .= " and iStatus='".$params['iStatus']."'";
		}
		if(isset($params['thumb'])){
			$fields = ',thumb';
			if($params['thumb']=="true"){
				$wheres .= " and thumbId<>0";
			}else if($params['thumb']=="false"){
				$wheres .= " and thumbId=0";
			}
		}

		if(!$_SESSION['adminId']){ $wheres.=" and iStatus=1"; }

		if(isset($params['orderBy']) && $params['orderBy']!=""){
			$orderBy  = $params['orderBy'];
		}else{
			$orderBy = "sortId asc,id desc";
		}
		if(defined('SITE_ID') && !$GLOBALS['siteConf']['share']){$wheres .= " and (siteId='".SITE_ID."' or siteId=0)";}
		$sql = "select count(id) from qk_article where 1=1 $wheres";
		$allCount = $GLOBALS['db']->getOne($sql);
		if($params['pager']=="true"){
			$fileds=",nearby";
			$smarty->assign("allCount",$allCount);
		}
		$fieldsExt='';
		if($fields){
			$fieldsArrAll=explode(',',$fields);
			if(!in_array('id',$fieldsArrAll)){$fieldsArrAll[]='id';}
			if(!in_array('title',$fieldsArrAll)){$fieldsArrAll[]='title';}
			if(!in_array('shortTitle',$fieldsArrAll)){$fieldsArrAll[]='shortTitle';}
			if(!in_array('seoUrl',$fieldsArrAll)){$fieldsArrAll[]='seoUrl';}
			if(!in_array('iStatus',$fieldsArrAll)){$fieldsArrAll[]='iStatus';}
			if(!in_array('sortId',$fieldsArrAll)){$fieldsArrAll[]='sortId';}
			if(!in_array('thumbId',$fieldsArrAll)){$fieldsArrAll[]='thumbId';}
			if(!in_array('catId',$fieldsArrAll)){$fieldsArrAll[]='catId';}
			if(!in_array('keywords',$fieldsArrAll)){$fieldsArrAll[]='keywords';}
			if(!in_array('description',$fieldsArrAll)){$fieldsArrAll[]='description';}
			if(!in_array('posttime',$fieldsArrAll)){$fieldsArrAll[]='posttime';}
			if(!in_array('externalLink',$fieldsArrAll)){$fieldsArrAll[]='externalLink';}
			
			$fieldsArr=array();
			$fieldsExtArr = array();
			foreach($fieldsArrAll as $field){
				if(in_array($field,array('id','title','shortTitle','seoUrl','iStatus','sortId','thumbId','catId','keywords','description','posttime','content','userId','externalLink','from','fromUrl','template','siteId'))){
					$fieldsArr[] = $field;
				}else{
					$fieldsExtArr[] = $field;
				}
			}
			$fields = implode(',',$fieldsArr);
			$fieldsSql=str_replace(',','`,`',$fields);$fieldsSql='`'.$fieldsSql.'`';
			$fieldsExt = implode(',',$fieldsExtArr);
		}else{
			$fields = " id,title,shortTitle,seoUrl,iStatus,sortId,thumbId,catId,keywords,description,posttime,externalLink";
			$fieldsSql = $fields;
			$fieldsArr=explode(',',$fields);//print_r($fieldsArr);
		}
		$sql = "select $fieldsSql from qk_article where 1=1 $wheres order by $orderBy $limit";
		$res = $GLOBALS['db']->getAll($sql);
		$i = 0;
		load_class("article");
		$am = new ArticleModel();
		if($fields)$fieldsArr = explode(',',$fields);
		foreach($res as $k=>$v){
			$res[$i] = $am->getInfoExt($v,"url,$fieldsExt");
			if(!$res[$i]['shortTitle']){$res[$i]['shortTitle']=$res[$i]['title'];}
			$res[$i]['index'] = $i;
			$res[$i]['total'] = $allCount;
			$i++;
		}
		$smarty->block_data[$dataindex]=$res;
	}
	if(is_array($smarty->block_data[$dataindex]) && $smarty->block_data[$dataindex]){
		$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;
}
?>