<?php

/**
 *--------------------------------------
 * 档案模型
 *--------------------------------------
 * @project		: uwa
 * @author		: cblee
 * @created		: 2012-10-1
 * @copyright	: (c)2012 AsThis
 *--------------------------------------
 */

class ArchiveModelModl extends Modl
{
	/* 获取模型列表 $amStatus状态 $used是否有相关频道 */
	public function get_modelList($amStatus = true, $used = false)
	{
		$_AML = F('~aml');
		if(empty($_AML))
		{
			$_AML = $this->select();
			F('~aml', $_AML);
		}
		if(!empty($_AML) and $amStatus)
		{
			foreach($_AML as $k => $model)
			{
				if(1 != $model['am_status'])
				{
					unset($_AML[$k]);
				}
			}
		}
		if(!empty($_AML) and $used)
		{
			foreach($_AML as $k => $model)
			{
				$_c = M('ArchiveChannel')->where('archive_model_id = \''.$model['archive_model_id'].'\'')->select();
				if(empty($_c))
				{
					unset($_AML[$k]);
				}
			}
		}
		return $_AML;
	}

	public function get_modelInfo($archiveModelId)
	{
		$_AMI = F('~ami/~am_'.$archiveModelId);
		if(empty($_AMI))
		{
			$_AMI = $this->where('archive_model_id = \''.$archiveModelId.'\'')->find();
			if(!empty($_AMI))
			{
				$at = get_instance('ATag', 'field');
				$at->tags = array();
				$at->parse_content($_AMI['am_fieldset']);
				$_AMI['am_field'] = $at->tags;
			}
			F('~ami/~am_'.$archiveModelId, $_AMI); /* 缓存模型信息 */
		}
		return $_AMI;
	}

	public function get_modelFieldInfo($archiveModelId, $fName)
	{
		$_AMI = $this->where('archive_model_id = \''.$archiveModelId.'\'')->find();
		if(!empty($_AMI))
		{
			$at = get_instance('ATag', 'field');
			$at->tags = array();
			$at->parse_content($_AMI['am_fieldset']);
			$_AMF = $at->get_tag($fName);
			return $_AMF;
		}
		return null;
	}

	public function add_model($data)
	{
		$result = array('data' => '', 'error' => '');
		$tables = $this->db->get_tables();
		$addonTable = C('DB.PREFIX').$data['am_addon_table'].C('DB.SUFFIX');
		if(in_array($addonTable, $tables))
		{
			$result['error'] = L('TABLE_EXIST').':'.$addonTable;
			return $result;
		}

		$_t_sql = "CREATE TABLE `{$addonTable}` (`archive_id` int UNSIGNED NOT NULL , PRIMARY KEY (`archive_id`), KEY `archive_id` (`archive_id`)) ";
		$_t_sql .= "DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci ";
		$_t_sql .= "COMMENT='{$data['am_name']}';";
		if(false === $this->execute($_t_sql))
		{
			$result['error'] = L('ADD_TABLE_FAILED');
			return $result;
		}

		$_t_id = $this->insert($data);
		if(false === $_t_id)
		{
			$result['error'] = L('ADD_FAILED');
			return $result;
		}
		$result['data'] = $_t_id;

		F('~aml', null);

		return $result;
	}

	public function edit_model($data)
	{
		$result = array('data' => '', 'error' => '');

		if(false === $this->update($data))
		{
			$result['error'] = L('EDIT_MODEL_FAILED');
			return $result;
		}

		F('~ami/~am_'.$data['archive_model_id'], null); /* 删除模型缓存 */
		F('~aml', null);

		/* 删除频道缓存 */
		$_t_acl = M('ArchiveChannel')->field('archive_channel_id')->where('`archive_model_id` = '.$data['archive_model_id'])->select();
		if(!empty($_t_acl))
		{
			foreach($_t_acl as $ac)
			{
				F('~aci/~ac_'.$ac['archive_channel_id'], null);
			}
			F('~acl', null);
		}

		return $result;
	}

	public function delete_model($archiveModelId)
	{
		$result = array('data' => '', 'error' => '');

		$_AMI = $this->where('archive_model_id = \''.$archiveModelId.'\'')->find();
		if(empty($_AMI))
		{
			$result['error'] = L('ITEM_NOT_EXIST');
			return $result;
		}
		if(0 == $_AMI['am_type'])
		{
			$result['error'] = L('SYSTEM_MODEL_IS_LOCKED');
			return $result;
		}

		$addonTable = C('DB.PREFIX').$_AMI['am_addon_table'].C('DB.SUFFIX');
		$_t_sql = "DROP TABLE `{$addonTable}`;";
		if(false === $this->execute($_t_sql))
		{
			$result['error'] = L('DROP_TABLE_FAILED');
			return $result;
		}

		if(false === $this->delete($archiveModelId))
		{
			$result['error'] = L('EDIT_MODEL_FAILED');
			return $result;
		}

		F('~ami/~am_'.$archiveModelId, null); /* 删除模型缓存 */
		F('~aml', null);

		return $result;
	}

	public function add_modelField($archiveModelId, $field)
	{
		$result = array('data' => '', 'error' => '');

		$_AMI = $this->where('archive_model_id = \''.$archiveModelId.'\'')->find();
		if(empty($_AMI))
		{
			$result['error'] = L('ITEM_NOT_EXIST');
			return $result;
		}

		/* 处理附加表 */
		load('field#func');
		$addonTable = C('DB.PREFIX').$_AMI['am_addon_table'].C('DB.SUFFIX');
		$fieldMakeSQL = get_fieldMakeSQL($field[key($field)]['f_type'], key($field), $field[key($field)]['f_default'], $field[key($field)]['f_length'], $field[key($field)]['f_item_name']);
		$_t_sql = 'ALTER TABLE `'.$addonTable.'` ADD COLUMN '.$fieldMakeSQL.';';
		if(false === $this->execute($_t_sql))
		{
			$result['error'] = L('ADD_FIELD_FAILED');
			return $result;
		}
		/*更新结构缓存*/
		M(parse_name($_AMI['am_addon_table'], 1))->flush();

		$at = get_instance('ATag', 'field');
		$at->tags = array();
		$at->parse_content($_AMI['am_fieldset']);
		unset($field[key($field)]['f_name']); unset($field[key($field)]['archive_model_id']);
		$at->add_tag($field);
		$data['archive_model_id'] = $archiveModelId;
		$data['am_fieldset'] = $at->deparse_tag($at->tags);
		if(false === $this->update($data))
		{
			$result['error'] = L('UPDATE_MODEL_FIELDSET_FAILED');
			return $result;
		}

		F('~ami/~am_'.$archiveModelId, null); /* 删除模型缓存 */
		F('~aml', null);

		/* 删除频道缓存 */
		$_t_acl = M('ArchiveChannel')->field('archive_channel_id')->where('`archive_model_id` = '.$archiveModelId)->select();
		if(!empty($_t_acl))
		{
			foreach($_t_acl as $ac)
			{
				F('~aci/~ac_'.$ac['archive_channel_id'], null);
			}
			F('~acl', null);
		}

		return $result;
	}

	public function edit_modelField($archiveModelId, $field)
	{
		$result = array('data' => '', 'error' => '');

		$_AMI = $this->where('archive_model_id = \''.$archiveModelId.'\'')->find();
		if(empty($_AMI))
		{
			$result['error'] = L('ITEM_NOT_EXIST');
			return $result;
		}

		/* 处理附加表 */
		load('field#func');
		$addonTable = C('DB.PREFIX').$_AMI['am_addon_table'].C('DB.SUFFIX');
		$fieldMakeSQL = get_fieldMakeSQL($field[key($field)]['f_type'], key($field), $field[key($field)]['f_default'], $field[key($field)]['f_length'], $field[key($field)]['f_item_name']);
		$_t_sql = 'ALTER TABLE `'.$addonTable.'` MODIFY COLUMN '.$fieldMakeSQL.';';
		if(false === $this->execute($_t_sql))
		{
			$result['error'] = L('EDIT_FIELD_FAILED');
			return $result;
		}
		/*更新结构缓存*/
		M(parse_name($_AMI['am_addon_table'], 1))->flush();

		$at = get_instance('ATag', 'field');
		$at->tags = array();
		$at->parse_content($_AMI['am_fieldset']);
		unset($field[key($field)]['f_name']); unset($field[key($field)]['archive_model_id']);
		$at->add_tag($field);
		$data['archive_model_id'] = $archiveModelId;
		$data['am_fieldset'] = $at->deparse_tag($at->tags);
		if(false === $this->update($data))
		{
			$result['error'] = L('UPDATE_MODEL_FIELDSET_FAILED');
			return $result;
		}

		F('~ami/~am_'.$archiveModelId, null); /* 删除模型缓存 */
		F('~aml', null);

		/* 删除频道缓存 */
		$_t_acl = M('ArchiveChannel')->field('archive_channel_id')->where('`archive_model_id` = '.$archiveModelId)->select();
		if(!empty($_t_acl))
		{
			foreach($_t_acl as $ac)
			{
				F('~aci/~ac_'.$ac['archive_channel_id'], null);
			}
			F('~acl', null);
		}

		return $result;
	}

	public function delete_modelField($archiveModelId, $fName)
	{
		$result = array('data' => '', 'error' => '');

		$_AMI = $this->where('archive_model_id = \''.$archiveModelId.'\'')->find();
		if(empty($_AMI))
		{
			$result['error'] = L('ITEM_NOT_EXIST');
			return $result;
		}

		/* 处理附加表 */
		$addonTable = C('DB.PREFIX').$_AMI['am_addon_table'].C('DB.SUFFIX');
		$_t_sql = 'ALTER TABLE `'.$addonTable.'` DROP COLUMN '.$fName.';';
		if(false === $this->execute($_t_sql))
		{
			$result['error'] = L('DROP_FIELD_FAILED');
			return $result;
		}
		/*更新结构缓存*/
		M(parse_name($_AMI['am_addon_table'], 1))->flush();

		$at = get_instance('ATag', 'field');
		$at->tags = array();
		$at->parse_content($_AMI['am_fieldset']);
		$at->delete_tag($fName);
		$data['archive_model_id'] = $archiveModelId;
		$data['am_fieldset'] = $at->deparse_tag($at->tags);
		if(false === $this->update($data))
		{
			$result['error'] = L('UPDATE_MODEL_FIELDSET_FAILED');
			return $result;
		}

		F('~ami/~am_'.$archiveModelId, null); /* 删除模型缓存 */
		F('~aml', null);

		/* 删除频道缓存 */
		$_t_acl = M('ArchiveChannel')->field('archive_channel_id')->where('`archive_model_id` = '.$archiveModelId)->select();
		if(!empty($_t_acl))
		{
			foreach($_t_acl as $ac)
			{
				F('~aci/~ac_'.$ac['archive_channel_id'], null);
			}
			F('~acl', null);
		}

		return $result;
	}

	public function import_model($modelConfig)
	{
		$result = array('data' => '', 'error' => '');
		$_AMI = unserialize(ACrypt::decrypt($modelConfig));
		if(!isset($_AMI['am_addon_table']) or empty($_AMI['am_addon_table']))
		{
			$result['error'] = L('CONFIG_ERROR');
			return $result;
		}

		$result = $this->add_model($_AMI);
		if(!empty($result['error']))
		{
			$result['error'] = $result['error'];
			return $result;
		}

		/* 处理附加表 */
		load('field#func');
		$at = get_instance('ATag', 'field');
		$at->tags = array();
		$at->parse_content($_AMI['am_fieldset']);
		$addonTable = C('DB.PREFIX').$_AMI['am_addon_table'].C('DB.SUFFIX');
		$_t_sql = 'ALTER TABLE `'.$addonTable.'` ';
		foreach($at->tags as $tag => $params)
		{
			$_t_sql .= 'ADD COLUMN '.get_fieldMakeSQL($params['f_type'], $tag, $params['f_default'], $params['f_length'], $params['f_item_name']).',';
		}
		$_t_sql = rtrim($_t_sql, ',').';';
		if(false === $this->execute($_t_sql))
		{
			$result['error'] = L('ADD_FIELD_FAILED');
			return $result;
		}
		/*更新结构缓存*/
		M(parse_name($_AMI['am_addon_table'], 1))->flush();

		F('~aml', null);

		return $result;
	}

}


?>