<?php

/**
 *--------------------------------------
 * 菜单
 *--------------------------------------
 * @project		: uwa
 * @author		: cblee
 * @created		: 2012-10-12
 * @copyright	: (c)2012 AsThis
 *--------------------------------------
 */

class MenuCtrlr extends ManageCtrlr
{
	public function list_menu()
	{
		$_MSL = M('MenuSpace')->get_spaceList();
		if(empty($_MSL))
		{
			$this->error(L('ADD_MENU_SPACE_FIRST'), Url::U('menu_space/add_space'));
		}
		$this->assign('_MSL', $_MSL);

		/* 菜单展位 */
		$msAlias = ARequest::get('ms_alias') ? ARequest::get('ms_alias') : '';
		$_ML = M('Menu')->get_menuList($msAlias);
		$this->assign('_ML', $_ML);

		$this->display();
	}

	public function add_menu()
	{
		/* 菜单展位 */
		$_MSL = M('MenuSpace')->get_spaceList();
		if(empty($_MSL))
		{
			$this->error(L('ADD_MENU_SPACE_FIRST'), Url::U('menu_space/add_space'));
		}
		$this->assign('_MSL', $_MSL);

		/* 控制器列表 */
		$_CL = get_ctrlrList(LIB_CTRLR_PATH.D_S.'Home');
		$this->assign('_CL', $_CL);

		$this->display();
	}
	public function add_menu_do()
	{
		$data = ARequest::get();
		$result = M('Menu')->add_menu($data);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('MENU').': '.$result['error'], 0);
			$this->error($result['error'], Url::U('menu/list_menu'));
		}
		M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('MENU').': MENU_ID['.$result['data'].']');
		$this->success(L('ADD_SUCCESS'), Url::U('menu/list_menu'));
	}

	public function edit_menu()
	{
		$menuId = ARequest::get('menu_id');

		$_MI = M('Menu')->get_menuInfo($menuId);
		if(empty($_MI))
		{
			$this->error(L('ITEM_NOT_EXIST'), Url::U('menu/list_menu'));
		}
		$this->assign('_MI', $_MI);

		/* 菜单展位 */
		$_MSL = M('MenuSpace')->get_spaceList();
		if(empty($_MSL))
		{
			$this->error(L('ADD_MENU_SPACE_FIRST'), Url::U('menu_space/add_space'));
		}
		$this->assign('_MSL', $_MSL);

		/* 菜单列表 */
		$_ML = M('Menu')->get_menuList($_MI['ms_alias']);
		$this->assign('_ML', $_ML);

		/* 控制器列表 */
		$_CL = get_ctrlrList(LIB_CTRLR_PATH.D_S.'Home');
		$this->assign('_CL', $_CL);

		$this->display();
	}
	public function edit_menu_do()
	{
		$data = ARequest::get();
		$result = M('Menu')->edit_menu($data);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('MENU').': MENU_ID['.$data['menu_id'].']'.$result['error'], 0);
			$this->error($result['error'], Url::U('menu/list_menu'));
		}
		M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('MENU').': MENU_ID['.$data['menu_id'].']');
		$this->success(L('EDIT_SUCCESS'), Url::U('menu/list_menu'));
	}

	public function update_menu_do()
	{
		$menuId = ARequest::get('menu_id');
		$_L_ID = is_array($menuId) ? implode(',', $menuId) : $menuId; /* 日志记录ID */

		if(is_array($menuId) && empty($menuId))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('MENU').': MENU_ID['.$menuId.']'.L('ITEM_NOT_EXIST'), 0);
			$this->error(L('ITEM_NOT_EXIST'), Url::U('menu/list_menu'));
		}

		$mDisplayOrder = ARequest::get('m_display_order');
		$mName = ARequest::get('m_name');
		$data = array();
		foreach($menuId as $k => $id)
		{
			$data['menu_id'] = $id;
			$data['m_display_order'] = $mDisplayOrder[$k];
			$data['m_name'] = $mName[$k];
			$result = M('Menu')->edit_menu($data);
			if(!empty($result['error']))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('MENU').': MENU_ID['.$data['menu_id'].']'.$result['error'], 0);
				$this->error($result['error'], Url::U('menu/list_menu'));
			}
		}

		/* 记录日志 */
		M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('MENU').': MENU_ID['.$_L_ID.']');
		$this->success(L('EDIT_SUCCESS'), Url::U('menu/list_menu'));
	}

	public function delete_menu_do()
	{
		$menuId  = ARequest::get('menu_id');
		$_L_ID = is_array($menuId) ? implode(',', $menuId) : $menuId; /* 日志记录ID */

		if(is_array($menuId ))
		{
			foreach($menuId as $menuId)
			{
				$result = M('Menu')->delete_menu($menuId);
				if(!empty($result['error']))
				{
					M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('MENU').': MENU_ID['.$menuId.']'.$result['error'], 0);
					$this->error($result['error'], Url::U('menu/list_menu'));
				}
			}
		}
		else
		{
			$result = M('Menu')->delete_menu($menuId);
			if(!empty($result['error']))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('MENU').': MENU_ID['.$menuId.']'.$result['error'], 0);
				$this->error($result['error'], Url::U('menu/list_menu'));
			}
		}

		/* 记录日志 */
		M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('MENU').': MENU_ID['.$_L_ID.']');
		$this->success(L('DELETE_SUCCESS'), Url::U('menu/list_menu'));
	}

	/* 返回控制器的相关操作 */
	public function get_actnList()
	{
		$ctrlr = ARequest::get('ctrlr');
		$file = LIB_CTRLR_PATH.D_S.'Home'.D_S.$ctrlr.'Ctrlr.class.php';
		$actnList = get_fileActnList($file);
		$this->ajax_return($actnList);
	}

	/* 返回展位的顶级菜单列表 */
	public function get_menuList()
	{
		$msAlias = ARequest::get('ms_alias');
		$_ML = M('Menu')->get_menuList($msAlias);
		$this->ajax_return($_ML);
	}
}

?>