<?php
	include_once dirname(__FILE__).'/link.class.php';

	$linkobj=new link();

	function link_typename($typeid=0)
	{
		global $linkobj;
		return $linkobj->typename(intval($typeid));
	}

	function parse_link_template($str)
	{
		$str=preg_replace('/\{niubi:flink\s+([^\}]+)\}/ie',"la_call_user_func('get_flink_tag','\\1')",$str);
		$str=preg_replace('/\{\/niubi:flink\}/i',"<?php }unset(\$_DATA); ?>",$str); 
		return $str;
	}

	function get_flink_tag($para)
	{
		/*
			设置区块默认参数
		*/
		$args=array(
					'typeid'=>'0',
					'withlogo'=>'-1',
					'istop'=>'-1',
					'limit'=>'0',
					'row'=>'24');
		foreach($para as $key => $arg)
		{
			if(isset($args[$key]))
			{
				$args[$key]=$arg;
			}
		}
		extract($args);
		$row=intval($row)>0?intval($row):24;
		return '<?php $_DATA=get_flink_data('.$typeid.','.$withlogo.','.$istop.','.$limit.','.$row.');foreach($_DATA as $no => $r)if(is_array($r)){?>';
	}	

	function get_flink_data($typeid=0,$withlogo=-1,$istop=-1,$limit=0,$row=24)
	{
		global $cacheobj,$db,$siteidobj,$conobj;
	
		$result=$_DATA=array();
		$typeid=intval($typeid)?intval($typeid):0;
		$withlogo=intval($withlogo)>-1?intval($withlogo):-1;
		$istop=intval($istop)>-1?intval($istop):-1;
		$row=intval($row)?intval($row):24;

		if(SITETYPE && $_SESSION['siteid'])
		{
			$where='siteid IN('.implode(',',$siteidobj->getchildid($_SESSION['siteid'])).')';
		}
		else
		{
			$where='1';
		}
		
		$where.=$typeid?' AND typeid='.$typeid:'';

		$where.=$withlogo>-1?($withlogo==1?' AND image!=\'\'':' AND image==\'\''):'';

		$where.=$istop>-1?' AND istop='.$istop:'';
		
		$sql="SELECT * FROM `".LA_PRE."link` WHERE $where ORDER BY `".LA_PRE."link`.`id` ASC LIMIT $limit,$row";

		$data=$cacheobj->get($sql);
		if(!$data)
		{
			$data=$db->fetch_all($sql);
			if($data)
			{
				$cacheobj->set($sql,$data);
			}
		}
		return $data;
	}
?>
