<?php

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

class MemberModelModl extends Modl
{
	/* 获取模型列表 $mmStatus状态 */
	public function get_modelList($mmStatus = true)
	{
		$_MML = F('~mml');
		if(empty($_MML))
		{
			$_MML =  $this->select();
			F('~mml', $_MML);
		}
		if(!empty($_MML) and $mmStatus)
		{
			foreach($_MML as $k => $model)
			{
				if(1 != $model['mm_status'])
				{
					unset($_MML[$k]);
				}
			}
		}
		return $_MML;
	}

	public function get_modelInfo($memberModelId)
	{
		$_MMI = F('~mmi/~mm_'.$memberModelId);
		if(empty($_MMI))
		{
			$_MMI = $this->where('member_model_id = \''.$memberModelId.'\'')->find();
			F('~mmi/~mm_'.$memberModelId, $_MMI);
		}
		if(!empty($_MMI))
		{
			$at = get_instance('ATag', 'field');
			$at->tags = array();
			$at->parse_content($_MMI['mm_fieldset']);
			$_MMI['mm_field'] = $at->tags;
			return $_MMI;
		}
		return null;
	}

	public function get_modelFieldInfo($memberModelId, $fName)
	{
		$_MMI = $this->where('member_model_id = \''.$memberModelId.'\'')->find();
		if(!empty($_MMI))
		{
			$at = get_instance('ATag', 'field');
			$at->tags = array();
			$at->parse_content($_MMI['mm_fieldset']);
			$_MMF = $at->get_tag($fName);
			return $_MMF;
		}
		return null;
	}

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

		$_t_sql = "CREATE TABLE `{$addonTable}` (`member_id` int UNSIGNED NOT NULL , PRIMARY KEY (`member_id`), KEY `member_id` (`member_id`)) ";
		$_t_sql .= "DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci ";
		$_t_sql .= "COMMENT='{$data['mm_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('~mml', 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('~mmi/~mm_'.$data['member_model_id'], null);
		F('~mml', null);

		return $result;
	}

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

		$_MMI = $this->where('member_model_id = \''.$memberModelId.'\'')->find();
		if(empty($_MMI))
		{
			$result['error'] = L('ITEM_NOT_EXIST');
			return $result;
		}
		if(0 == $_MMI['mm_type'])
		{
			$result['error'] = L('SYSTEM_MODEL_IS_LOCKED');
			return $result;
		}

		$addonTable = C('DB.PREFIX').$_MMI['mm_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($memberModelId))
		{
			$result['error'] = L('EDIT_MODEL_FAILED');
			return $result;
		}

		F('~mmi/~mm_'.$memberModelId, null); /* 删除模型缓存 */
		F('~mml', null);

		return $result;
	}

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

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

		/* 处理附加表 */
		load('field#func');
		$addonTable = C('DB.PREFIX').$_MMI['mm_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($_MMI['mm_addon_table'], 1))->flush();

		$at = get_instance('ATag', 'field');
		$at->tags = array();
		$at->parse_content($_MMI['mm_fieldset']);

		unset($field[key($field)]['f_name']); unset($field[key($field)]['member_model_id']);
		$at->add_tag($field);
		$data['member_model_id'] = $memberModelId;
		$data['mm_fieldset'] = $at->deparse_tag($at->tags);
		if(false === $this->update($data))
		{
			$result['error'] = L('UPDATE_MODEL_FIELDSET_FAILED');
			return $result;
		}

		F('~mmi/~mm_'.$memberModelId, null); /* 删除模型缓存 */
		F('~mml', null);

		return $result;
	}

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

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

		/* 处理附加表 */
		load('field#func');
		$addonTable = C('DB.PREFIX').$_MMI['mm_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($_MMI['mm_addon_table'], 1))->flush();

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

		F('~mmi/~mm_'.$memberModelId, null); /* 删除模型缓存 */
		F('~mml', null);

		return $result;
	}

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

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

		/* 处理附加表 */
		$addonTable = C('DB.PREFIX').$_MMI['mm_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($_MMI['mm_addon_table'], 1))->flush();

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

		F('~mmi/~mm_'.$memberModelId, null); /* 删除模型缓存 */
		F('~mml', null);

		return $result;
	}

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

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

		/* 处理附加表 */
		load('field#func');
		$at = get_instance('ATag', 'field');
		$at->tags = array();
		$at->parse_content($_MMI['mm_fieldset']);
		$addonTable = strtoupper($_MMI['mm_addon_table']);
		$_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($_MMI['mm_addon_table'], 1))->flush();

		F('~mml', null);

		return $result;
	}

}


?>