<?php

/**
 *--------------------------------------
 * uwa标签
 *--------------------------------------
 * @project		: uwa
 * @author		: cblee
 * @created		: 2012-10-25
 * @copyright	: (c)2012 AsThis
 *--------------------------------------
 */

class TagUwa
{
	public function parse_tag($params)
	{
		$params = explode(' ', $params, 2);
		/* 获取标签 */
		$tag = $params[0];

		/* 获取参数 */
		if(isset($params[1]))
		{
			$params[1] = $params[1].' ';
			preg_match_all('/.*?(\s*.*?=.*?[\"|\'].*?[\"|\']\s).*?/si', $params[1], $arr);
			$a = array();
			if(isset($arr[1]) and !empty($arr[1]))
			{
				foreach($arr[1] as $v)
				{
					$t = explode('=', trim(str_replace(array('"',"'"), '', $v)));
					$a = array_merge($a, array(trim($t[0]) => trim($t[1])));
				}
			}
		}

		$str = '';
		if('ad' == $tag) /* 广告 */
		{
			/* 解析id */
			if(isset($a['id']) and !empty($a['id']))
			{
				$str = "<?php\r\n";
				$str .= "\$_asi_{$a['id']} = M('AdSpace')->get_spaceInfo('{$a['id']}');\r\n";
				$str .= "if(!empty(\$_asi_{$a['id']}) and 1 == \$_asi_{$a['id']}['as_status'])\r\n";
				$str .= "{\r\n";
				$str .= "	\$_al_{$a['id']} = M('Ad')->get_adList('{$a['id']}', true);\r\n";
				$str .= "	if(!empty(\$_al_{$a['id']}))\r\n";
				$str .= "	{\r\n";
				$str .= "		\$_asi_{$a['id']}['ad'] = \$_al_{$a['id']};\r\n";
				$str .= "		\$this->assign('_ASI', \$_asi_{$a['id']});\r\n";
				$str .= "		\$ad = \$this->fetch('home/clip/ad_tag');\r\n";
				$str .= "		echo \$ad;\r\n";
				$str .= "	}\r\n";
				$str .= "	else\r\n";
				$str .= "	{\r\n";
				$str .= "		echo \$_asi_{$a['id']}['as_default'];\r\n";
				$str .= "	}\r\n";
				$str .= "}\r\n";
				$str .= "?>\r\n";
			}
		}
		elseif('menu' == $tag) /* 菜单 */
		{
			if(isset($a['alias']) and !empty($a['alias']))
			{
				$alias = $a['alias'];
				$str = "<?php\r\n";
				$str .= "\$_menu_{$alias} = M('Menu')->get_menuList('{$alias}');\r\n";
				$str .= "if(is_array(\$_menu_{$alias})) : foreach(\$_menu_{$alias} as \$k => \$m): \r\n";
				$str .= "?>\r\n";
			}
		}
		elseif('fl_list' == $tag) /* 链接 */
		{
			$where = "	\$where = array();\r\n";
			$where .= "	\$where['__FLINK__.f_status'] = array('EQ', 1);\r\n";
			/* 分类ID */
			$cid = 'cid';
			if(isset($a['cid']) and !empty($a['cid']))
			{
				$where .= "	\$where['__FLINK__.flink_category_id'] = array('IN', '{$a['cid']}');\r\n";
				$cid = $a['cid'];
			}
			/* 显示类型 */
			$type = 'type';
			if(isset($a['type']) and!empty($a['type']))
			{
				if('text' == $a['type'])
				{
					$where .= "	\$where['__FLINK__.f_show_type'] = array('EQ', 0);\r\n";

				}
				elseif('logo' == $a['type'])
				{
					$where .= "	\$where['__FLINK__.f_show_type'] = array('EQ', 1);\r\n";
				}
				$type = $a['type'];
			}
			/* 条数 */
			if(isset($a['row']) and !empty($a['row']))
			{
				$limit = $a['row'];
				$row = $a['row'];
			}
			else
			{
				$limit = 10;
				$row = 10;
			}
			$str = "<?php\r\n";
			$str .= "\$_flink_list_{$cid}_{$type}_{$row} = F('~list/~flink_list_{$cid}_{$type}_{$row}');\r\n";
			$str .= "if(empty(\$_flink_list_{$cid}_{$type}_{$row}))\r\n";
			$str .= "{\r\n";
			$str .= $where;
			$str .= "	\$_flink_list_{$cid}_{$type}_{$row} = M('Flink')->get_flinkList(\$where, '`f_display_order` ASC', '{$limit}');\r\n";
			$str .= "	F('~list/~flink_list_{$cid}_{$type}_{$row}', \$_flink_list_{$cid}_{$type}_{$row});\r\n";
			$str .= "}\r\n";
			$str .= "if(\$_flink_list_{$cid}_{$type}_{$row}) : foreach(\$_flink_list_{$cid}_{$type}_{$row} as \$k => \$item): \r\n";
			$str .= "?>\r\n";
		}
		elseif('gb_list' == $tag) /* 留言 */
		{
			$where = "	\$where = array();\r\n";
			$where .= "	\$where['__GUESTBOOK__.g_status'] = array('EQ', 1);\r\n";
			/* 条数 */
			if(isset($a['row']) and !empty($a['row']))
			{
				$limit = $a['row'];
				$row = $a['row'];
			}
			else
			{
				$limit = 10;
				$row = 10;
			}
			$str = "<?php\r\n";
			$str .= "\$_gb_list_{$row} = S('~list/~gb_list_{$row}');\r\n";
			$str .= "if(empty(\$_gb_list_{$row}))\r\n";
			$str .= "{\r\n";
			$str .= $where;
			$str .= "	\$_gb_list_{$row} = M('Guestbook')->get_guestbookList(\$where, '`g_add_time` DESC', '{$limit}');\r\n";
			$str .= "	S('~list/~gb_list_{$row}', \$_gb_list_{$row});\r\n";
			$str .= "}\r\n";
			$str .= "if(\$_gb_list_{$row}) : foreach(\$_gb_list_{$row} as \$k => \$item): \r\n";
			$str .= "?>\r\n";
		}
		elseif('sp_list' == $tag) /* 单页 */
		{
			/* 条数 */
			if(isset($a['row']) and !empty($a['row']))
			{
				$limit = $a['row'];
				$row = $a['row'];
			}
			else
			{
				$limit = 10;
				$row = 10;
			}
			$str = "<?php\r\n";
			$str .= "\$_sp_list_{$row} = F('~list/~sp_list_{$row}');\r\n";
			$str .= "if(empty(\$_sp_list_{$row}))\r\n";
			$str .= "{\r\n";
			$str .= "	\$_sp_list_{$row} = M('SinglePage')->get_singlePageList('', '`sp_display_order` ASC', '{$limit}');\r\n";
			$str .= "	F('~list/~sp_list_{$row}', \$_sp_list_{$row});\r\n";
			$str .= "}\r\n";
			$str .= "if(\$_sp_list_{$row}) : foreach(\$_sp_list_{$row} as \$item): \r\n";
			$str .= "?>\r\n";
		}
		elseif('a_list' == $tag) /* 档案 */
		{
			$where = "	\$where = array();\r\n";
			$where .= "	\$where['__ARCHIVE__.a_status'] = array('EQ', 1);\r\n";
			/* 频道ID */
			$cid = 'cid';
			$issub = 'no';
			if(!isset($a['cid']) or empty($a['cid']))
			{
				if(isset($a['issub']) and 'yes' == $a['issub']) /* 作为子循环 */
				{
					$cid = "C('AC_ID')";
					$issub = 'yes';
				}
				else
				{
					$cid = "(isset(\$AC_ID) ? \$AC_ID : 0)";
				}
			}
			elseif('all' == $a['cid'])
			{
				$cid = "'0'";
			}
			else
			{
				$cid = "'".$a['cid']."'";
			}
			if(strpos($cid, ','))
			{
				$where .= "	\$where['__ARCHIVE__.archive_channel_id'] = array('IN', {$cid});\r\n";
			}
			elseif('all' != $a['cid'])
			{
				$where .= "\$_aci = M('ArchiveChannel')->get_channelInfo({$cid});\r\n";
				$where .= "C('AM_ID', \$_aci['archive_model_id']);\r\n";
				/* 频道信息 */
				$where .= "\$_ACL = M('ArchiveChannel')->get_channelList(0, {$cid});\r\n";
				$where .= "\$act = new ATree(\$_ACL, array('archive_channel_id', 'ac_parent_id', 'ac_sub_channel'), {$cid});\r\n";
				$where .= "	\$where['__ARCHIVE__.archive_channel_id'] = array('IN', implode(',', \$act->get_leafid({$cid})));\r\n";
			}

			/* 档案flag */
			$flag = 'flag';
			if(isset($a['flag']) and !empty($a['flag']))
			{
				$where .= "	\$where['__ARCHIVE__.af_alias'] = array('INSET', '{$a['flag']}');\r\n";
				$flag = $a['flag'];
			}
			/* 时间 */
			$days = 'days';
			if(isset($a['days']) and !empty($a['days']))
			{
				$where .= "	\$where['__ARCHIVE__.a_edit_time'] = array('GT', time() - 86400*{$a['days']});\r\n";
				$days = $a['days'];
			}
			/* 关键词 */
			$keywords = 'keywords';
			if(isset($a['keywords']) and !empty($a['keywords']))
			{
				$where .= "	\$where['__ARCHIVE__.a_keywords'] = array('LIKE', '%{$a['keywords']}%');\r\n";
				$pyc = get_instance('Pinyin');
				$keywords = $pyc->get_pinyin($a['keywords'], 'utf-8');
			}
			/* 排序 */
			$orderby = 'orderby';
			$order = '`a_rank` DESC, `a_add_time` DESC';
			if(isset($a['orderby']) and !empty($a['orderby']))
			{
				$order = "`{$a['orderby']}`";
				if(isset($a['order']) and !empty($a['order']))
				{
					$order .= " {$a['order']}";
					$orderby = $a['orderby'].'_'.$a['order'];
				}
				else
				{
					$orderby = $a['orderby'].'_order';
				}
			}
			/* 条数 */
			if(isset($a['row']) and !empty($a['row']))
			{
				$limit = $a['row'];
				$row = $a['row'];
			}
			else
			{
				$limit = 10;
				$row = 10;
			}
			/* 标题长度 */
			$titleLenStr = '';
			if(isset($a['titlelen']) and !empty($a['titlelen']))
			{
				$titleLenStr = "	\$item['a_title'] = AString::utf8_substr(\$item['a_title'], '{$a['titlelen']}')\r\n";
			}
			$str = "<?php\r\n";
			$str .= "\$var_a = '_a_list_'.{$cid}.'_{$issub}_{$flag}_{$days}_{$keywords}_{$orderby}_{$row}';\r\n";
			$str .= "\$\$var_a = S('~list/~'.ltrim(\$var_a, '_'));\r\n";
			$str .= "if(empty(\$\$var_a))\r\n";
			$str .= "{\r\n";
			$str .= $where;
			$str .= "	\$\$var_a = M('Archive')->get_archiveList(\$where, '{$order}', '{$limit}', C('AM_ID'));\r\n";
			$str .= "	S('~list/~'.ltrim(\$var_a, '_'), \$\$var_a);\r\n";
			$str .= "}\r\n";
			$str .= "if(\$\$var_a) : foreach(\$\$var_a as \$k => \$item): \r\n";
			$str .= $titleLenStr;
			$str .= "?>\r\n";
		}
		elseif('ac_list' == $tag) /* 档案频道 */
		{
			/* 频道ID */
			$cid = 'cid';
			$issub = 'no';
			if(!isset($a['cid']) or empty($a['cid']))
			{
				if(isset($a['issub']) and 'yes' == $a['issub']) /* 作为子循环 */
				{
					$cid = "C('AC_ID')";
					$issub = 'yes';
				}
				else
				{
					$cid = "(isset(\$AC_ID) ? \$AC_ID : 0)";
				}
			}
			elseif('all' == $a['cid'])
			{
				$cid = "'0'";
			}
			else
			{
				$cid = "'".$a['cid']."'";
			}
			/* 条数 */
			if(isset($a['row']) and !empty($a['row']))
			{
				$limit = $a['row'];
				$row = $a['row'];
			}
			else
			{
				$limit = 10;
				$row = 10;
			}
			$str = "<?php\r\n";
			$str .= "\$var_ac = '_ac_list_'.{$cid}.'_{$issub}_{$row}';\r\n";
			$str .= "\$\$var_ac = S('~list/~'.ltrim(\$var_ac, '_'));\r\n";
			$str .= "if(empty(\$\$var_ac))\r\n";
			$str .= "{\r\n";
			$str .= "	\$_ACL = M('ArchiveChannel')->get_channelList(0, {$cid}, '{$limit}');\r\n";
			$str .= "	\$act = new ATree(\$_ACL, array('archive_channel_id', 'ac_parent_id', 'ac_sub_channel'), {$cid});\r\n";
			$str .= "	\$\$var_ac = \$act->get_leaf({$cid});\r\n";
			$str .= "	S('~list/~'.ltrim(\$var_ac, '_'), \$\$var_ac);\r\n";
			$str .= "}\r\n";
			$str .= "if(\$\$var_ac) : foreach(\$\$var_ac as \$key => \$channel): \r\n";
			$str .= "C('AC_ID', \$channel['archive_channel_id']);\r\n";
			$str .= "?>\r\n";
		}

		return $str;
	}

	public function parse_tag_end($params)
	{
		/* 获取标签 */
		$tag = $params;

		$str = '';
		if('ad' == $tag)
		{}
		elseif('menu' == $tag)
		{
			$str = "<?php endforeach; endif; ?>\r\n";
		}
		elseif('fl_list' == $tag)
		{
			$str = "<?php endforeach; endif; ?>\r\n";
		}
		elseif('gb_list' == $tag)
		{
			$str = "<?php endforeach; endif; ?>\r\n";
		}
		elseif('sp_list' == $tag)
		{
			$str = "<?php endforeach; endif; ?>\r\n";
		}
		elseif('a_list' == $tag)
		{
			$str = "<?php endforeach; endif; ?>\r\n";
		}
		elseif('ac_list' == $tag)
		{
			$str = "<?php C('AC_ID', null); endforeach; endif; ?>\r\n";
		}

		return $str;
	}
}

?>