<?php
// 模块控制器
class ModuleAction extends BaseAction {
	protected function _initializeMore() {
		$this -> obj = M('module');
	}
	public function defaults() {
		$page = intval(R('page', 'GET'));
		$data = $this -> obj -> order('orderid,id') -> page($page) -> select();
		$pages = $this -> obj -> pages();
		if (is_array($data)) {
			$tableInDb = array();
			foreach($data as $k => $v) {
				if (!isset($tableInDb[$v['dir']])) {
					$tableInDb[$v['dir']] = $this -> obj -> setDbConfigDir($v['dir']) -> db() -> getTables(C('DB_NAME', 1, $v['dir']));
				}
				$data[$k]['useconfig'] = in_array(C('DB_PREFIX', 1, $v['dir']) . $v['name'] . '_config', $tableInDb[$v['dir']])?1:0;
			}
		}
		$this -> assign('data', $data);
		$this -> assign('pages', $pages);
		$this -> display('module');
	}
	public function add() {
		if ($this -> isSubmit) {
			$msg = $this -> obj -> save()?L('OPERATE_SUCCESS'):$this -> obj -> error;
			$this -> showMsg($msg);
		}
		$this -> assign('data', array('id' => 0, 'name' => '', 'title' => '', 'dir' => '', 'description' => '', 'tables' => '', 'usecategory' => 0));
		$this -> display('module');
	}
	public function edit() {
		if ($this -> isSubmit) {
			$msg = $this -> obj -> save()?L('OPERATE_SUCCESS'):$this -> obj -> error;
			$this -> showMsg($msg, $this -> forward);
		}
		$data = $this -> obj -> find(intval(R('id')));
		$data['iscomment'] = M('access') -> where(array('control' => 'comment', 'module' => $data['name'])) -> count();
		$this -> assign('data', $data);
		$this -> display('module');
	}
	public function del() {
		$msg = $this -> obj -> del(R('id'))?L('OPERATE_SUCCESS'):L('OPERATE_FAIL');
		$this -> showMsg($msg);
	}
	public function state() {
		$msg = M('module') -> create(array('id' => R('id', 'get'), 'state' => R('val', 'get'))) -> update()?L('OPERATE_SUCCESS'):L('OPERATE_FAIL');
		$this -> showMsg($msg);
	}
	public function order() {
		$selarray = explode(',', $_POST['selids']);
		foreach($selarray as $id) {
			$this -> obj -> where("id=$id") -> create(array('orderid' => $_POST['orderid'][$id])) -> update();
		}
		$this -> showMsg(L('OPERATE_SUCCESS'), $_SERVER['HTTP_REFERER']);
	}
	public function config() {
		$id = intval(R('id'));
		$module = M('module') -> find($id);
		$configObj = M('config');
		if ($this -> isSubmit) {
			$config = R('info', 'post');
			$default = R('default', 'post');	//默认设置,例如:复选框在没有勾选的情况下无值,可以通过这里指定 key1=value1|key2=value2
			if (!empty($default)) {
				$tmp = explode('|', $default);
				foreach($tmp as $tmp1) {
					$tmp2 = explode('=', $tmp1);
					if (!isset($config[$tmp2[0]])) $config[$tmp2[0]] = $tmp2[1];
				}
			}
			if ($configObj -> save($config, $module['name'])) {
				$this -> showMsg(L('OPERATE_SUCCESS'));
			} else {
				$msg = $configObj -> error ? $configObj -> error : L('OPERATE_FAIL');
				$this -> showMsg($msg);
			}
		}
		$this -> assign('module', $module);
		$this -> assign('data', $configObj -> get($module['name']));
		$this -> display($module['name'] . '_config');
	}
	// 析构函数
	public function __destruct() {
		M('module') -> cache();
	}
}
