<?php
/*
	商家黄页模板解析文件
	更新: 2012-03-11 22:23
	版本: v1.2
*/

function parse_yp_template($str)
{
	$str=preg_replace('/\{dir:yp\s+([^\}]+)\}/ie',"dc_call_user_func('get_yp_tag','\\1')",$str);
	$str=preg_replace('/\{\/dir:yp\}/i',"<?php }unset(\$_DATA); ?>",$str);

	/*
		黄页新闻
	*/
	$str=preg_replace('/\{dir:yp_news\s+([^\}]+)\}/ie',"dc_call_user_func('get_yp_news_tag','\\1')",$str);
	$str=preg_replace('/\{\/dir:yp_news\}/i',"<?php }unset(\$_DATA); ?>",$str);

	/*
		优惠促销
	*/
	$str=preg_replace('/\{dir:yp_discount\s+([^\}]+)\}/ie',"dc_call_user_func('get_yp_discount_tag','\\1')",$str);
	$str=preg_replace('/\{\/dir:yp_discount\}/i',"<?php }unset(\$_DATA); ?>",$str);

	/*
		产品
	*/
	$str=preg_replace('/\{dir:yp_product_type\s+([^\}]+)\}/ie',"dc_call_user_func('get_yp_product_type_tag','\\1')",$str);
	$str=preg_replace('/\{\/dir:yp_product_type\}/i',"<?php } \$typeid=isset(\$loopcatid)?\$dir_typeid:0;unset(\$_DATA);?>",$str); 

	$str=preg_replace('/\{dir:yp_product\s+([^\}]+)\}/ie',"dc_call_user_func('get_yp_product_tag','\\1')",$str);
	$str=preg_replace('/\{\/dir:yp_product\}/i',"<?php }unset(\$_DATA); ?>",$str);

	/*
		评论
	*/
	$str=preg_replace('/\{dir:yp_comment\s+([^\}]+)\}/ie',"dc_call_user_func('get_yp_comment_tag','\\1')",$str);
	$str=preg_replace('/\{\/dir:yp_comment\}/i',"<?php }unset(\$_DATA); ?>",$str); 

	return $str;
}

function get_yp_tag($para)
{
	/*
		设置区块默认参数
	*/
	$args=array('typeid'=>'0',
				'areaid'=>'0',
				'row'=>'10',
				'istop'=>'0',
				'orderby'=>'score',
				'orderbyway'=>'desc',
				'page'=>'0');
	foreach($para as $key => $arg)
	{
		if(isset($args[$key]))
		{
			$args[$key]=$arg;
		}
	}
	extract($args);
	$row=$row?$row:10;

	if(ISCITY)
	{
		$areaid=get_cookie('areaid');
		if($areaid)
		{
			$where=' AND `areaid`=".intval(get_cookie(\'areaid\'))."';
		}
		else if(CITY)
		{
			$where=' AND `areaid`=".intval(CITY)."';
		}
	}
	else if($areaid)
	{
		if($areaid)
		{
			$where=' AND `areaid`=".intval($areaid)."';
		}
	}
	else
	{
		$where='';
	}

	$row=intval($row)>0?intval($row):10;
	if($istop)
	{
		if($typeid)
		{
			$sql='SELECT * FROM `".DB_PRE."yp` WHERE `".DB_PRE."yp`.`typeid`=".intval('.$typeid.')." AND `".DB_PRE."yp`.`siteid`='.SITEID.' AND `".DB_PRE."yp`.`istop`=1 AND `".DB_PRE."yp`.`status`=1 '.$where.' ORDER BY `".DB_PRE."yp`.'.$orderby;
		}
		else
		{
			$sql='SELECT * FROM `".DB_PRE."yp` WHERE `".DB_PRE."yp`.`istop`=1 AND `".DB_PRE."yp`.`siteid`='.SITEID.' AND `".DB_PRE."yp`.`status`=1 '.$where.' ORDER BY `".DB_PRE."yp`.'.$orderby;
		}
	}
	else
	{
		if($typeid)
		{
			$sql='SELECT * FROM `".DB_PRE."yp` WHERE `".DB_PRE."yp`.`typeid`=".intval('.$typeid.')." AND `".DB_PRE."yp`.`siteid`='.SITEID.' AND `".DB_PRE."yp`.`status`=1 '.$where.' ORDER BY `".DB_PRE."yp`.'.$orderby;
		}
		else
		{
			$sql='SELECT * FROM `".DB_PRE."yp` WHERE `".DB_PRE."yp`.`siteid`='.SITEID.' AND `".DB_PRE."yp`.`status`=1 '.$where.' ORDER BY `".DB_PRE."yp`.'.$orderby;
		}
	}
	return '<?php $dir_page="";$_DATA=get_sql_tag_data("'.$sql.'",'.$row.','.$page.');foreach($_DATA as $no => $r)if(is_array($r)){?>';
}

function get_yp_news_tag($para)
{
	/*
		设置区块默认参数
	*/
	$args=array('ypid'=>'0',
				'areaid'=>'0',
				'row'=>'10',
				'page'=>'0');
	foreach($para as $key => $arg)
	{
		if(isset($args[$key]))
		{
			$args[$key]=$arg;
		}
	}
	extract($args);
	$row=$row?$row:10;

	if($ypid)
	{
		$where=' AND 1';

		if(ISCITY)
		{
			$areaid=get_cookie('areaid');
			if($areaid)
			{
				$where.=' AND `areaid`=".intval(get_cookie(\'areaid\'))."';
			}
			else if(CITY)
			{
				$where.=' AND `areaid`=".intval(CITY)."';
			}
		}
		else if($areaid)
		{
			if(is_numeric($areaid))
			{
				$where.=' AND `areaid`='.intval($areaid);
			}
			else
			{
				$where.=' AND `areaid`=".intval($areaid)."';
			}
		}
		else
		{
			$where.='';
		}

		$sql='SELECT * FROM `".DB_PRE."yp_news` WHERE `".DB_PRE."yp_news`.`siteid`='.SITEID.' AND `".DB_PRE."yp_news`.`ypid`=".intval('.$ypid.')."'.$where.' ORDER BY `".DB_PRE."yp_news`.`id` DESC';	
	}
	else
	{
		if(ISCITY)
		{
			$areaid=get_cookie('areaid');
			if($areaid)
			{
				$where=' WHERE `".DB_PRE."yp_news`.`siteid`='.SITEID.' AND `areaid`=".intval(get_cookie(\'areaid\'))."';
			}
			else if(CITY)
			{
				$where=' WHERE `".DB_PRE."yp_news`.`siteid`='.SITEID.' AND `areaid`=".intval(CITY)."';
			}
		}
		else if($areaid)
		{
			if(is_numeric($areaid))
			{
				$where=' WHERE `".DB_PRE."yp_news`.`siteid`='.SITEID.' AND `areaid`='.intval($areaid);
			}
			else
			{
				$where=' WHERE `".DB_PRE."yp_news`.`siteid`='.SITEID.' AND `areaid`=".intval($areaid)."';
			}
		}
		else
		{
			$where=' WHERE 1';
		}

		$where.=' AND 1';

		$sql="SELECT * FROM `\".DB_PRE.\"yp_news` ".$where." ORDER BY `\".DB_PRE.\"yp_news`.`id` DESC";
	}

	return '<?php $dir_page="";$_DATA=get_sql_tag_data("'.$sql.'",'.$row.','.$page.');foreach($_DATA as $no => $r)if(is_array($r)){?>';
}

function get_yp_discount_tag($para)
{
	/*
		设置区块默认参数
	*/
	$args=array('ypid'=>'0',
				'withthumb'=>'0',
				'areaid'=>'0',
				'row'=>'10',
				'page'=>'0');
	foreach($para as $key => $arg)
	{
		if(isset($args[$key]))
		{
			$args[$key]=$arg;
		}
	}
	extract($args);
	$row=$row?$row:10;
	
	$conditionthumb=$withthumb?' thumb!=\'\' ':'1';
	if($ypid)
	{
		$where=' AND 1';

		if(ISCITY)
		{
			$areaid=get_cookie('areaid');
			if($areaid)
			{
				$where.=' AND `areaid`=".intval(get_cookie(\'areaid\'))."';
			}
			else if(CITY)
			{
				$where.=' AND `areaid`=".intval(CITY)."';
			}
		}
		else if($areaid)
		{
			if(is_numeric($areaid))
			{
				$where.=' AND `areaid`='.intval($areaid);
			}
			else
			{
				$where.=' AND `areaid`=".intval($areaid)."';
			}
		}
		else
		{
			$where.='';
		}

		$sql='SELECT * FROM `".DB_PRE."yp_discount` WHERE `".DB_PRE."yp_discount`.`siteid`='.SITEID.' AND '.$conditionthumb.' AND `".DB_PRE."yp_discount`.`ypid`=".intval('.$ypid.')."'.$where.' ORDER BY `".DB_PRE."yp_discount`.`id` DESC';	
	}
	else
	{
		if(ISCITY)
		{
			$areaid=get_cookie('areaid');
			if($areaid)
			{
				$where=' WHERE `".DB_PRE."yp_discount`.`siteid`='.SITEID.' AND `areaid`=".intval(get_cookie(\'areaid\'))."';
			}
			else if(CITY)
			{
				$where=' WHERE `".DB_PRE."yp_discount`.`siteid`='.SITEID.' AND `areaid`=".intval(CITY)."';
			}
		}
		else if($areaid)
		{
			if(is_numeric($areaid))
			{
				$where=' WHERE '.$conditionthumb.' AND `".DB_PRE."yp_discount`.`siteid`='.SITEID.' AND `areaid`='.intval($areaid);
			}
			else
			{
				$where=' WHERE '.$conditionthumb.' AND `".DB_PRE."yp_discount`.`siteid`='.SITEID.' AND `areaid`=".intval($areaid)."';
			}
		}
		else
		{
			$where='WHERE '.$conditionthumb;
		}
		$where.=' AND 1';

		$sql="SELECT * FROM `\".DB_PRE.\"yp_discount` ".$where." ORDER BY `\".DB_PRE.\"yp_discount`.`id` DESC";
	}

	return '<?php $dir_page="";$_DATA=get_sql_tag_data("'.$sql.'",'.$row.','.$page.');foreach($_DATA as $no => $r)if(is_array($r)){?>';
}

function get_yp_product_type_tag($para)
{
	/*
		设置区块默认参数
	*/
	$args=array('ypid'=>'0',
				'parentid'=>'0',
				'row'=>'10',
				'page'=>0);
	foreach($para as $key => $arg)
	{
		if(isset($args[$key]))
		{
			$args[$key]=$arg;
		}
	}
	extract($args);
	$row=$row?$row:10;
	$sql='SELECT * FROM `".DB_PRE."yp_producttype` WHERE `".DB_PRE."yp_producttype`.`ypid`=".intval('.$ypid.')." AND `".DB_PRE."yp_producttype`.`parentid`=".intval('.$parentid.')."';
	return '<?php $dir_typeid=$typeid;$dir_page="";$_DATA=get_sql_tag_data("'.$sql.'",'.$row.','.$page.');$loopcatid=$typeid;foreach($_DATA as $no => $r)if(is_array($r)){$typeid=$r["id"];?>';
}

function get_yp_product_tag($para)
{
	/*
		设置区块默认参数
	*/
	$args=array('ypid'=>'0',
				'typeid'=>'0',
				'withthumb'=>'0',
				'areaid'=>'0',
				'row'=>'10',
				'page'=>'0');
	foreach($para as $key => $arg)
	{
		if(isset($args[$key]))
		{
			$args[$key]=$arg;
		}
	}
	extract($args);
	$row=$row?$row:10;
	$conditionthumb=$withthumb?' thumb!=\'\' ':'1';

	if($ypid)
	{
		if($typeid)
		{
			if(is_numeric($typeid))
			{
				$where=' AND `typeid`='.intval($typeid);
			}	
			else
			{
				$where=' AND `typeid`=".intval($typeid)."';
			}
		}

		if(ISCITY)
		{
			$areaid=get_cookie('areaid');
			if($areaid)
			{
				$where.=' AND `areaid`=".intval(get_cookie(\'areaid\'))."';
			}
			else if(CITY)
			{
				$where.=' AND `areaid`=".intval(CITY)."';
			}
		}
		else if($areaid)
		{
			if(is_numeric($areaid))
			{
				$where.=' AND `areaid`='.intval($areaid);
			}
			else
			{
				$where.=' AND `areaid`=".intval($areaid)."';
			}
		}
		else
		{
			$where.='';
		}

		$sql='SELECT * FROM `".DB_PRE."yp_product` WHERE `".DB_PRE."yp_product`.`siteid`='.SITEID.' AND `".DB_PRE."yp_product`.`ypid`=".intval('.$ypid.')."'.$where.' AND '.$conditionthumb.' ORDER BY `".DB_PRE."yp_product`.`id` DESC';	
	}
	else
	{
		if(ISCITY)
		{
			$areaid=get_cookie('areaid');
			if($areaid)
			{
				$where=' WHERE `".DB_PRE."yp_product`.`siteid`='.SITEID.' AND `areaid`=".intval(get_cookie(\'areaid\'))."';
			}
			else if(CITY)
			{
				$where=' WHERE `".DB_PRE."yp_product`.`siteid`='.SITEID.' AND `areaid`=".intval(CITY)."';
			}
		}
		else if($areaid)
		{
			if(is_numeric($areaid))
			{
				$where=' WHERE `".DB_PRE."yp_product`.`siteid`='.SITEID.' AND `areaid`='.intval($areaid);
			}
			else
			{
				$where=' WHERE `".DB_PRE."yp_product`.`siteid`='.SITEID.' AND `areaid`=".intval($areaid)."';
			}
		}
		else
		{
			$where=' WHERE 1';
		}

		if($typeid)
		{
			if(is_numeric($typeid))
			{
				$where.=' AND `typeid`='.intval($typeid);
			}
			else
			{
				$where.=' AND `typeid`=".intval($typeid)."';
			}
		}

		$sql="SELECT * FROM `\".DB_PRE.\"yp_product` ".$where." AND ".$conditionthumb." ORDER BY `\".DB_PRE.\"yp_product`.`id` DESC";
	}

	return '<?php $dir_page="";$_DATA=get_sql_tag_data("'.$sql.'",'.$row.','.$page.');foreach($_DATA as $no => $r)if(is_array($r)){?>';
}

function get_yp_comment_tag($para)
{
	/*
		设置区块默认参数
	*/
	$args=array('ypid'=>'0',
				'areaid'=>'0',
				'row'=>'10',
				'page'=>'0');
	foreach($para as $key => $arg)
	{
		if(isset($args[$key]))
		{
			$args[$key]=$arg;
		}
	}
	extract($args);
	$row=$row?$row:10;

	if(ISCITY)
	{
		$areaid=get_cookie('areaid');
		if($areaid)
		{
			$where=' AND `areaid`=".intval(get_cookie(\'areaid\'))."';
		}
		else if(CITY)
		{
			$where=' AND `areaid`=".intval(CITY)."';
		}
	}
	else if($areaid)
	{
		if(is_numeric($areaid))
		{
			$where=' WHERE `areaid`='.intval($areaid);
		}
		else
		{
			$where=' WHERE `areaid`=".intval($areaid)."';
		}
	}
	else
	{
		$where='';
	}
	
	$row=$row?$row:10;
	if($ypid)
	{
		$sql='SELECT * FROM `".DB_PRE."yp_comment` WHERE `".DB_PRE."yp_comment`.`siteid`='.SITEID.' AND `".DB_PRE."yp_comment`.`status`=1 AND `".DB_PRE."yp_comment`.`ypid`=".intval('.$ypid.')." '.$where.' ORDER BY `".DB_PRE."yp_comment`.`id` DESC';	
	}
	else
	{
		$sql="SELECT * FROM `\".DB_PRE.\"yp_comment` WHERE `".DB_PRE."yp_comment`.`siteid`=".SITEID." AND `\".DB_PRE.\"yp_comment`.`status`=1 ".$where." ORDER BY `\".DB_PRE.\"yp_comment`.`id` DESC";
	}

	return '<?php $dir_page="";$_DATA=get_sql_tag_data("'.$sql.'",'.$row.','.$page.');foreach($_DATA as $no => $r)if(is_array($r)){?>';
}
?>