<?php

/**
 *--------------------------------------
 * 需检查操作权限的 Ajax
 *--------------------------------------
 * @project		: uwa
 * @author		: cblee
 * @created		: 2014-01-05
 * @copyright	: (c)2014 AsThis
 *--------------------------------------
 */

class AjaxCtrlr extends ManageCtrlr
{

	/* 获取频道的模型ID */
	public function get_model_id()
	{
		$archiveChannelId = ARequest::get('archive_channel_id');
		$_ACI = M('ArchiveChannel')->find($archiveChannelId);
		if(!empty($_ACI))
		{
			$this->ajax_return(1, $_ACI['archive_model_id']);
		}
		$this->ajax_return(0);
	}

	/* 获取频道的html 目录 */
	public function get_html_dir()
	{
		$archiveChannelId = ARequest::get('archive_channel_id');
		$_ACI = M('ArchiveChannel')->find($archiveChannelId);
		if(!empty($_ACI))
		{
			$this->ajax_return(1, $_ACI['ac_html_dir']);
		}
		else
		{
			$_o = M('Option')->get_option('core');
			$this->ajax_return(1, '{uwa_path}/'.trim($_o['html_path'], '/'));
		}
	}

	/* 获取指定模型ID的频道列表 */
	public function get_channel_list()
	{
		$archiveModelId = ARequest::get('archive_model_id');
		$_ACL = M('ArchiveChannel')->get_myChannelList($archiveModelId);

		$act = new ATree($_ACL, array('archive_channel_id', 'ac_parent_id', 'ac_sub_channel'));
		$_ACLStr = $act->get_leafStr(0, "<option value='\$archive_channel_id'>\$spacer\$ac_name</option>\r\n", $acParentId, "<option value='\$archive_channel_id' selected='selected'>\$spacer\$ac_name</option>\r\n");

		$this->ajax_return(1, '<option value="0">'.L('TOP_CHANNEL')."</option>\r\n".$_ACLStr);
	}

	/* 获取模型默认显示模板 */
	public function get_default_show_template()
	{
		$archiveModelId = ARequest::get('archive_model_id');
		$info = M('ArchiveModel')->field('`ac_tpl_index_default`, `ac_tpl_list_default`, `ac_tpl_archive_default`')->where('`archive_model_id` = '.$archiveModelId)->select();
		if(!empty($info))
		{
			$tpls['ac_tpl_index'] = $info[0]['ac_tpl_index_default'];
			$tpls['ac_tpl_list'] = $info[0]['ac_tpl_list_default'];
			$tpls['ac_tpl_archive'] = $info[0]['ac_tpl_archive_default'];
			$this->ajax_return(1, $tpls);
		}
		$this->ajax_return(0);
	}

	/* 检查模型别名 */
	public function check_model_alias()
	{
		$type = ARequest::get('type');
		$alias = ARequest::get('alias');
		if('archive' == strtolower($type))
		{
			$_AL = M('ArchiveModel')->field('am_alias')->select();
			foreach($_AL as $a)
			{
				if($alias == $a['am_alias'])
				{
					$this->ajax_return(0);
				}
			}
		}
		elseif('member' == strtolower($type))
		{
			$_AL = M('MemberModel')->field('mm_alias')->select();
			foreach($_AL as $a)
			{
				if($alias == $a['mm_alias'])
				{
					$this->ajax_return(0);
				}
			}
		}
		$this->ajax_return(1);
	}

	/* 获取附加表 */
	public function check_addon_table()
	{
		$addonTable = ARequest::get('addon_table');
		$tables = M()->query("SHOW TABLES FROM `".C('DB.NAME')."`");
		foreach($tables as $table)
		{
			if(C('DB.PREFIX').$addonTable == $table['Tables_in_'.C('DB.NAME')])
			{
				$this->ajax_return(0);
			}
		}
		$this->ajax_return(1);
	}

}

?>