<?php

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

class TagLogaAcList {
	public function get_tagStr($a) {
		$cid = 'cid';
		$not = 'not';
		$pid = 'pid';
		$alias = '';
		$row = 100;
		$key = 'key';
		$as = 'category';

		$where = "	\$where = array();\r\n";
		/* direct read category id */
		if(isset($a['cid']) and !empty($a['cid'])) {
			if(strpos($a['cid'], ',')) {
				$where .= "	\$where['__ARTICLE_CATEGORY__.article_category_id'][] = array('IN', '{$a['cid']}');\r\n";
				$cid = str_replace(',', 'loga', $a['cid']);
			}
			else {
				if(strpos($a['cid'], '$')) {
					$where .= "	if(!empty(".substr($a['cid'], 2, -2).")) {\r\n";
					$where .= "		\$where['__ARTICLE_CATEGORY__.article_category_id'][] = array('IN', '{$a['cid']}');\r\n";
					$where .= "	}\r\n";
				}
				else {
					$where .= "	\$where['__ARTICLE_CATEGORY__.article_category_id'][] = array('IN', '{$a['cid']}');\r\n";
				}
				$cid = $a['cid'];
			}
		}

		/* exclude id */
		if(isset($a['not']) and !empty($a['not'])) {
			if(strpos($a['not'], ',')) {
				$where .= "	\$where['__ARTICLE_CATEGORY__.article_category_id'][] = array('NOT IN', '{$a['not']}');\r\n";
				$not = str_replace(',', 'uwanot', $a['not']);
			}
			else {
				if(strpos($a['not'], '$')) {
					$where .= "	if(!empty(".substr($a['not'], 2, -2).")) {\r\n";
					$where .= "		\$where['__ARTICLE_CATEGORY__.article_category_id'][] = array('NOT IN', '{$a['not']}');\r\n";
					$where .= "	}\r\n";
				}
				else {
					$where .= "	\$where['__ARTICLE_CATEGORY__.article_category_id'][] = array('NOT IN', '{$a['not']}');\r\n";
				}
				$not = $a['not'];
			}
		}

		/* parent category id */
		if(isset($a['pid']) and 'all' == $a['pid']) {
			$pid = 'all';
		}
		else {
			if(isset($a['pid']) and !empty($a['pid'])) {
				if(strpos($a['pid'], '$')) {
					$pid = "'.(!empty(".substr($a['pid'], 2, -2).") ? '{$a['pid']}' : 0).'";
				}
				else {
					$pid = $a['pid'];
				}
			}
			else {
				$pid = "'.(isset(\$AC_ID) ? \$AC_ID : 0).'";
			}
			$where .= "	\$where['__ARTICLE_CATEGORY__.ac_parent_id'] = array('EQ', '{$pid}');\r\n";
		}

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

		/* 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_acl = LANG_NAME.'_ac_list_{$cid}_{$not}_{$pid}_{$alias}_{$row}';\r\n";
		$str .= "\$\$var_acl = S('_list/_'.ltrim(\$var_acl, '_'), '', null, array('temp' => TEMP_PATH.D_S.'home'));\r\n";
		$str .= "if(empty(\$\$var_acl)) {\r\n";
		$str .= $where;
		$str .= "	\$\$var_acl = M('ArticleCategory')->get_categoryList('{$alias}', 1, \$where);\r\n";
		$str .= "	if(0 < '{$row}') {\r\n";
		$str .= "		\$\$var_acl = array_slice(\$\$var_acl, 0, '{$row}');\r\n";
		$str .= "	}\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_acl, '_'), \$\$var_acl, '{$expire}', array('temp' => TEMP_PATH.D_S.'home'));\r\n";
		}
		else {
			$str .= "	S('_list/_'.ltrim(\$var_acl, '_'), \$\$var_acl, null, array('temp' => TEMP_PATH.D_S.'home'));\r\n";
		}
		$str .= "}\r\n";
		$str .= "if(\$\$var_acl) : foreach(\$\$var_acl as \${$key} => \${$as}): \r\n";
		$str .= "C('AC_ID', \${$as}['article_category_id']);\r\n";
		$str .= "?>\r\n";
		return $str;
	}

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

?>