<?php

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

class admin_menu extends MX_Controller {

    private $parent_id = array(0 => '---');
    private $images_folder = '';

    function __construct() {
        parent::__construct();
        $this->load->library('ciext/admin_lib');
        
        // Set top menu active
        $this->admin_lib->content['active_menu'] = 1;

        $this->images_folder = '/' . $this->config->item('assets_folder') . '/img/modules/ciext';

        // Acceess check
        if (!$this->rt_user->isAllowed('system', 'view_amenu'))
            $this->admin_lib->show_denied();

        $this->lang->load('ciext/crud');
        $this->load->model('ciext/admin_menu_model');

        // Get all parent id. SINGLETON
        $temp = $this->db->select('id_admin_menu, name')->where('parent_id', 0)->get('admin_menu')->result_array();
        foreach ($temp as $v)
            $this->parent_id[$v['id_admin_menu']] = $v['name'];
    }

    public function index() {

        $this->load->library('rtgrid/rtgrid_api');

        $crud = & $this->rtgrid_api;
        $crud->set_table('admin_menu');
        $crud->set_primery_key('id_admin_menu');

        $crud->columns('name, page, parent_id, view_order, active');
        $crud->fields('name, page, parent_id, view_order, acl_resource, acl_action, image, active');

        $crud->display_as('name', $this->lang->line('crud_menu'));
        $crud->display_as('page', $this->lang->line('crud_page'));
        $crud->display_as('image', $this->lang->line('crud_image'));
        $crud->display_as('parent_id', $this->lang->line('crud_parent_id'));
        $crud->display_as('view_order', $this->lang->line('crud_view_order'));
        $crud->display_as('active', $this->lang->line('active'));

        $crud->callback('active', 'active');

        $crud->callback_column('page', array($this, 'callback_view_page'));
        $crud->callback_column('parent_id', array($this, 'callback_view_parent_id'));

        $crud->callback_field('page', array($this, 'callback_edit_page'));
        $crud->callback_field('parent_id', array($this, 'callback_edit_parent_id'));

        // Set crud operations
        if (!$this->rt_user->isAllowed('system', 'edit_amenu')) {
            $crud->unset_top_action('add');
        }

        if (!$this->rt_user->isAllowed('system', 'edit_amenu')) {
            $crud->unset_action('edit');
        }

        if (!$this->rt_user->isAllowed('system', 'edit_amenu')) {
            $crud->unset_top_action('delete');
            $crud->unset_action('delete');
        }

        $this->admin_lib->show(array('content' => $crud->render(), 'title' => 'Admin menu'));
    }

    /**
     * Ajax method
     * Save sort items admin-menu panel 
     */
    public function set_panel_items() {

        $result = array('success' => 'false');
        if (isset($_POST['panel_items'])) {
            $this->admin_menu_model->set_panel_items($_POST['panel_items']);
            $result['success'] = 'true';
        }

        echo json_encode($result);
    }

    /**
     * Callback function for page
     * @param integer $value
     * @return string 
     */
    function callback_view_page($value) {
        return base_url() . $value;
    }

    /**
     * Callback function for page
     * @param integer $value
     * @return string 
     */
    function callback_edit_page($name, $value) {
        return base_url() . $this->config->item('admin_page') . '/  <br /><input type="text" name="' . $name . '" value="' . $value . '" />';
    }

    /**
     * Callback function for parent_id
     * @param integer $value
     * @return string 
     */
    function callback_view_parent_id($value) {
        return '<label>' . $this->parent_id[$value] . '</label>';
    }

    /**
     * Callback function for parent_id
     * @param integer $value
     * @return string 
     */
    function callback_edit_parent_id($name, $value) {
        $res = '<select name="' . $name . '">';
        foreach ($this->parent_id as $k => $v) {
            $res .= '<option value="' . $k . '" ' . (($k == $value) ? 'selected="selected"' : '') . '>' . $v . '</option>';
        }
        return $res . '</select>';
    }

}

/* End of file admin_menu.php */
/* Location: ./application/modules/ciext/admin_menu.php */