<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Description of site_menu_admin
 *
 * @author Igor Ratsun
 */
class site_menu_admin extends MX_Controller {

    public function manager() {
        // Acceess check
        if (!$this->rt_user->isAllowed('site_menu', 'edit_user_menu')) {
            $this->admin_lib->show_denied();
        }
        
        $this->lang->load('site_menu/admin');
        $this->load->model('site_menu/site_menu_model');
        $this->load->library('rtgrid/rtgrid_api');
        
        $id_site_menu_type = $this->site_menu_model->get_menu_type();

        $crud = & $this->rtgrid_api;
        $crud->set_table('site_menu');
        $crud->set_primery_key('id_site_menu');
        $crud->where('site_menu.id_site_menu_type', $id_site_menu_type);

        $crud->columns('translate_name, site_menu.parent_id, site_menu.view_order, site_menu.active');
        $crud->fields('site_menu.parent_id, site_menu.href, site_menu.view_order, site_menu.active');

        foreach ($this->lang->line('field_list') as $k => $v) {
            $crud->display_as($k, $v);
        }

        $crud->multilang(new multilang('translate_name', 'site_menu_translate', 'name'));

        $crud->hidden('id_site_menu_type', $id_site_menu_type);
        $crud->callback('view_order', 'rtgc_view_order', ['crud' => $crud]);
        $crud->callback('active', 'rtgc_active');
        $crud->callback('parent_id', 'rtgc_select', $this->site_menu_model->get_parents());
        $crud->callback_field('href', array($this->site_menu_model, 'get_pages'));

        // Add observers
        $this->load->helper('rtgrid/rtgrid_obs');
        $crud->attach(make_rtgrid_add_obs('site_menu', 'edit_user_menu'));
        $crud->attach(make_rtgrid_edit_obs('site_menu', 'edit_user_menu'));
        $crud->attach(make_rtgrid_delete_obs('site_menu', 'edit_user_menu'));
        
        $content = array(
            'content' => $crud->render(),
            'tabs' => $this->site_menu_model->get_tabs()
        );
        
        $edit = $this->uri->get_next_segment('edit');

        return array(
            'content' => (empty($edit)) ? $this->parser->parse('site_menu/admin.php', $content, TRUE) : $content['content'],
            'href' => str_replace('/' . $id_site_menu_type, '', $this->uri->uri_string()),
            'title' => $this->lang->line('module_name')
        );
    }

    public function type() {
        // Acceess check
        if (!$this->rt_user->isAllowed('site_menu', 'edit_user_menu_type')) {
            $this->admin_lib->show_denied();
        }
        $this->lang->load('site_menu/admin');
        $this->load->model('site_menu/site_menu_model');
        $this->load->library('rtgrid/rtgrid_api');

        $crud = & $this->rtgrid_api;
        $crud->set_table('site_menu_type');

        $crud->columns('name, active');
        $crud->fields('name, active_class, template,  active');

        foreach ($this->lang->line('field_list') as $k => $v) {
            $crud->display_as($k, $v);
        }

        $crud->callback('name', 'rtgc_input');
        $template = new \Rtgc_textarea(['maxlength' => '2048']);
        $template->set_field_xss_clean(false);
        $crud->callback('template', $template);
        $crud->callback('view_order', 'rtgc_view_order');
        $crud->callback('active', 'rtgc_active');

        // Add observers
        $this->load->helper('rtgrid/rtgrid_obs');
        $crud->attach(make_rtgrid_add_obs('site_menu', 'edit_user_menu_type'));
        $crud->attach(make_rtgrid_edit_obs('site_menu', 'edit_user_menu_type'));
        $crud->attach(make_rtgrid_delete_obs('site_menu', 'edit_user_menu_type'));

        return array('content' => $crud->render(), 'title' => $this->lang->line('module_name'));
    }

}
