<?php

/**
 *--------------------------------------
 * loga:fl_list
 *--------------------------------------
 * @project		: loga
 * @author		: cblee
 * @created		: 2016-07-06
 * @copyright	: (c)2016 AsThis
 *--------------------------------------
 */
defined('PFA_PATH') or exit('Access Denied');

class TagLogaFlList {
	public function get_tagStr($a) {
		$alias = '';
		$type = 'type';
		$offset = 0;
		$row = 10;
		$key = 'k';
		$as = 'item';

		$where = "	\$where = array();\r\n";
		$where .= "	\$where['__FLINK__.f_status'] = array('EQ', 1);\r\n";

		/* category alias */
		if(isset($a['alias']) and !empty($a['alias'])) {
			if(strpos($a['alias'], '$')) {
				$where .= "	if(!empty(".substr($a['alias'], 2, -2).")) {\r\n";
				$where .= "		\$where['__FLINK__.fc_alias'] = array('EQ', '{$a['alias']}');\r\n";
				$where .= "	}\r\n";
			}
			else {
				$where .= "	\$where['__FLINK__.fc_alias'] = array('EQ', '{$a['alias']}');\r\n";
			}
			$alias = $a['alias'];
		}

		/* show 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";
			}
			elseif(strpos($a['type'], '$')) {
				$where .= "	if('text' == ".substr($a['type'], 2, -2).") {\r\n";
				$where .= "		\$where['__FLINK__.f_show_type'] = array('EQ', 0);\r\n";
				$where .= "	}\r\n";
				$where .= "	elseif('logo' == ".substr($a['type'], 2, -2).") {\r\n";
				$where .= "		\$where['__FLINK__.f_show_type'] = array('EQ', 1);\r\n";
				$where .= "	}\r\n";
			}
			$type = $a['type'];
		}

		/* limit start row */
		if(isset($a['offset']) and !empty($a['offset'])) {
			if(strpos($a['offset'], '$')) {
				$offset = "'.(!empty(".substr($a['offset'], 2, -2).") ? '{$a['offset']}' : '{$offset}').'";
			}
			else {
				$offset = $a['offset'];
			}
		}

		/* row */
		if(isset($a['row']) and !empty($a['row'])) {
			if(strpos($a['row'], '$')) {
				$row = "'.(!empty(".substr($a['row'], 2, -2).") ? '{$a['row']}' : '{$row}').'";
			}
			else {
				$row = $a['row'];
			}
		}

		/* key */
		if(isset($a['key']) and !empty($a['key'])) {
			if(strpos($a['key'], '$')) {
				$key = "'.(!empty(".substr($a['key'], 2, -2).") ? '{$a['key']}' : '{$key}').'";
			}
			else {
				$key = $a['key'];
			}
		}

		/* as */
		if(isset($a['as']) and !empty($a['as'])) {
			if(strpos($a['as'], '$')) {
				$as = "'.(!empty(".substr($a['as'], 2, -2).") ? '{$a['as']}' : '{$as}').'";
			}
			else {
				$as = $a['as'];
			}
		}

		$str = "<?php\r\n";
		$str .= "\$var_fl = LANG_NAME.'_flink_list_{$alias}_{$type}_{$offset}_{$row}';\r\n";
		$str .= "\$\$var_fl = S('_list/_'.ltrim(\$var_fl, '_'), '', null, array('temp' => TEMP_PATH.D_S.'home'));\r\n";
		$str .= "if(empty(\$\$var_fl)) {\r\n";
		$str .= $where;
		$str .= "	\$\$var_fl = M('Flink')->get_flinkList(\$where, '`f_display_order` ASC', '{$offset},{$row}');\r\n";
		/* expire */
		if(isset($a['expire']) and !empty($a['expire'])) {
			if(strpos($a['expire'], '$')) {
				$expire = "'.(!empty(".substr($a['expire'], 2, -2).") ? '{$a['expire']}' : '{$expire}').'";
			}
			else {
				$expire = $a['expire'];
			}
			$str .= "	S('_list/_'.ltrim(\$var_fl, '_'), \$\$var_fl, '{$expire}', array('temp' => TEMP_PATH.D_S.'home'));\r\n";
		}
		else {
			$str .= "	S('_list/_'.ltrim(\$var_fl, '_'), \$\$var_fl, null, array('temp' => TEMP_PATH.D_S.'home'));\r\n";
		}
		$str .= "}\r\n";
		$str .= "if(\$\$var_fl) : foreach(\$\$var_fl as \${$key} => \${$as}): \r\n";
		$str .= "?>\r\n";
		return $str;
	}

	public function get_tagEndStr() {
		$str = "<?php endforeach; endif; ?>\r\n";
		return $str;
	}
}

?>