<?php
if (!defined('IN_CONTEXT')) die('access violation error!');

class ModMenu extends Module {
	
	protected $_filters = array(
        'check_admin' => ''
    );
	
    public function admin_list() {
        $this->_layout = 'content';
        
        $curr_locale = trim(SessionHolder::get('_LOCALE'));
        $mod_locale = trim(SessionHolder::get('mod_menu/_LOCALE', $curr_locale));
        $lang_sw = trim(ParamHolder::get('lang_sw', $mod_locale));
        SessionHolder::set('mod_menu/_LOCALE', $lang_sw);
        
        $menu_data =& 
            Pager::pageByObject('Menu', "s_locale=?", array($lang_sw), 
                "ORDER BY `id` DESC");
        
        $this->assign('menus', $menu_data['data']);
        $this->assign('pager', $menu_data['pager']);
        
        $this->assign('lang_sw', $lang_sw);
        $this->assign('langs', Toolkit::loadAllLangs());
    }
    public function admin_delete() {
        
        $menu_id = trim(ParamHolder::get('menu_id', '0'));
        if (intval($menu_id) == 0) {
            $this->assign('json', Toolkit::jsonERR(__('Invalid ID!')));
            return '_result';
        }
        try {
            $curr_menu = new Menu($menu_id);
            $curr_menu->delete();
            
            $db = MysqlConnection::get();
            $db->query('DELETE FROM es_menu_items WHERE menu_id = ?',array($menu_id));
            
        } catch (Exception $ex) {
            $this->assign('json', Toolkit::jsonERR($ex->getMessage()));
            return ('_result');
        }
        
        $this->assign('json', Toolkit::jsonOK());
        return '_result';
    }
    public function admin_add() {
        $this->_layout = 'content';
        
        $curr_locale = trim(SessionHolder::get('_LOCALE'));
        $mod_locale = trim(SessionHolder::get('mod_menu/_LOCALE', $curr_locale));
        
        $this->assign('menu_title', __('New Menu'));
        $this->assign('next_action', 'admin_create');

        $this->assign('mod_locale', $mod_locale);
        $this->assign('langs', Toolkit::loadAllLangs());
        $this->assign('roles', Toolkit::loadAllRoles());
        
        return '_form';
    }
    public function admin_create() {
        
        $menu_info =& ParamHolder::get('menu', array());
        if (sizeof($menu_info) <= 0) {
            $this->assign('json', Toolkit::jsonERR(__('Missing menu information!')));
            return '_result';
        }
        $is_member_only = ParamHolder::get('ismemonly', '0');
        try {
            // Re-arrange publish status
            if ($menu_info['published'] == '1') {
                $menu_info['published'] = '1';
            } else {
                $menu_info['published'] = '0';
            }
            $menu_info['for_roles'] = ACL::explainAccess(intval($is_member_only));
            
            // Data operation
            $o_menu = new Menu();
            $o_menu->set($menu_info);
            $o_menu->save();
        } catch (Exception $ex) {
            $this->assign('json', Toolkit::jsonERR($ex->getMessage()));
            return '_result';
        }
        
        $this->assign('json', Toolkit::jsonOK(array('forward' => Html::uriquery('mod_menu', 'admin_list'))));
        return '_result';
    }
    public function admin_edit() {
        $this->_layout = 'content';
        
        $menu_id = ParamHolder::get('menu_id', '0');
        if (intval($menu_id) == 0) {
            $this->assign('json', Toolkit::jsonERR(__('Invalid ID!')));
            return '_error';
        }
        
        try {
            $curr_menu = new Menu($menu_id);
            $this->assign('curr_menu', $curr_menu);

        } catch (Exception $ex) {
            $this->assign('json', Toolkit::jsonERR($ex->getMessage()));
            return '_error';
        }
        
        $this->assign('menu_title', __('Edit Menu'));
        $this->assign('next_action', 'admin_update');
        
        $this->assign('langs', Toolkit::loadAllLangs());
        $this->assign('roles', Toolkit::loadAllRoles());
        
        return '_form';
    }
    public function admin_update() {
        $flag = false;
        $menu_info =& ParamHolder::get('menu', array());
        if (sizeof($menu_info) <= 0) {
            $this->assign('json', Toolkit::jsonERR(__('Missing menu information!')));
            return '_result';
        }
        $is_member_only = ParamHolder::get('ismemonly', '0');
        try {
            // Re-arrange publish status
            if ($menu_info['published'] == '1') {
                $menu_info['published'] = '1';
                $flag = true;
            } else {
                $menu_info['published'] = '0';
                $flag = false;
            }
            $menu_info['for_roles'] = ACL::explainAccess(intval($is_member_only));
            
            // Data operation
            $o_menu = new Menu($menu_info['id']);
            $o_menu->set($menu_info);
            $o_menu->save();
            
            $o_mi = new MenuItem();
            $menu_items = $o_mi->findAll("menu_id=?",array($menu_info['id']));
       	 	if(!empty($menu_items))
            {
            	foreach($menu_items as $k => $v)
            	{
            		$menu_items_info['published'] = ($flag) ? '1' : '0';
            		$menu_items_info['for_roles'] = $menu_info['for_roles'];
            		$obj_menu_items = new MenuItem($v->id);
            		$obj_menu_items->set($menu_items_info);
            		$obj_menu_items->save();
            	}
            }
        } catch (Exception $ex) {
            $this->assign('json', Toolkit::jsonERR($ex->getMessage()));
            return '_result';
        }
        
        $this->assign('json', Toolkit::jsonOK(array('forward' => Html::uriquery('mod_menu', 'admin_list'))));
        return '_result';
    }
}
?>