<?php
// 后台用户模型
class UserModel extends Model {
	public function modelSave() {
		$this -> setTable('member_model');
		$info = $_POST['info'];
		$info['id'] = intval($info['id']);
		if (!$info['id']) {
			$info['id'] = $this -> getId('id');
			$isAdd = true;
		} else $isAdd = false;
		// 名称有效性
		$info['name'] = trim($info['name']);
		if (empty($info['name'])) {
			$this -> error = L('NAME_EMPTY');
			return false;
		}
		if ($isAdd) {
			if ($info['tablename']) {
				$tableInDb = $this -> db() -> getTables(C('DB_NAME', 1));
				if (in_array($info['tablename'], $tableInDb)) show_error($info['tablename'] . L('TABLE_IS_EXIST'));
			}
			$f = JXCMS_ROOT . 'member/Lib/Install/' . $info['tablename'] . '.sql';
			if (file_exists($f)) {
				M('sql') -> execute(file_get_contents($f));
			}
		}
		$this -> create($info);
		return $isAdd?$this -> add():$this -> update();
	}
	public function modelDel($id) {
		$id = intval($id);
		$data = $this -> setTable('member_model') -> find($id);
		$f = JXCMS_ROOT . 'member/Lib/Uninstall/' . $data['tablename'] . '.sql';
		if (file_exists($f)) {
			M('sql') -> execute(file_get_contents($f));
		}
		return $this -> delete($id);
	}
	public function cache() {
		$data = $this -> table('member_model') -> order(array('orderid ASC', 'id ASC')) -> select();
		if (is_array($data)) {
			$array = array();
			foreach($data as $tmp) {
				$array[$tmp['id']] = $tmp;
			}
			Cache :: write('member_model', $array);
		}
	}
}
