<?php

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

class Acl extends \Ciext\Controller\AbstractAdminController
{

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

        $this->lang->load('ciext/acl');
        $this->load->model('ciext/acl_model');
    }

    public function acl_user()
    {
        // Acceess check
        if (!$this->rt_user->isAllowed('acl', 'view_user')) {
            $this->admin_lib->show_denied();
        }
        $id = intval($this->uri->get_next_segment('edit'));
        if ($this->uri->get_next_segment('edit') == 'add' || !empty($id)) {
            if (!$this->rt_user->isAllowed('acl', 'edit_user') && !empty($id)) {
                $this->admin_lib->show_denied();
            }
            if (!$this->rt_user->isAllowed('acl', 'insert_user') && $this->uri->get_next_segment('acl_user') == 'add') {
                $this->admin_lib->show_denied();
            }
        }

        $this->load->model('ciext/acl_user_crud');

        return $this->admin_lib->show(array(
                    'content' => $this->acl_user_crud->render(),
                    'title'   => $this->lang->line('user_title')
        ));
    }

    public function acl_change_pass()
    {
        if (($this->rt_user->isAllowed('acl', 'change_user_pass')) && $this->rt_user->change_pass($_POST['id'], $_POST['pass'])) {
            $this->load->helper('ciext/observer');
            $this->lang->load('ciext/observers/acl_user');
            $user_name = $this->db->select('login')->where('id_acl_user', intval($this->input->post('id')))->get('acl_user')->row();
            if (isset($user_name->login)) {
                $save = new saveObserver();
                $save->set_access('acl', 'edit_user');
                $save->set_text(sprintf($this->lang->line('Change pass'), $user_name->login));
                $save->save();
            }

            echo $this->lang->line('pass_changed');
        } else {
            echo $this->lang->line('pass_fail');
        }
    }

    public function acl_role()
    {
        // Acceess check
        if (!$this->rt_user->isAllowed('acl', 'view_role')) {
            $this->admin_lib->show_denied();
        }
        $id = intval($this->uri->get_next_segment('edit'));
        if ($this->uri->get_next_segment('edit') == 'add' || !empty($id)) {
            // Acceess check
            if (!$this->rt_user->isAllowed('acl', 'edit_role') && !empty($id)) {
                $this->admin_lib->show_denied();
            }
            if (!$this->rt_user->isAllowed('acl', 'insert_role') && $this->uri->get_next_segment('edit') == 'add') {
                $this->admin_lib->show_denied();
            }
        }

        // Set top menu active
        $this->admin_lib->content['active_menu'] = 2;

        $this->ciext->load_bootstrap();

        $this->load->library('rtgrid/rtgrid_api');
        $crud = new rtgrid_api();
        $crud->set_table('acl_role');

        $crud->columns('acl_role_name, parent_id, active');
        $crud->fields('acl_role_name, parent_id, active');

        $crud->callback('active', new \Rtgc_active());
        $crud->callback_column('parent_id', array($this->acl_model, 'callback_acl_role_view_parent_id'));
        $crud->callback_field('parent_id', array($this->acl_model, 'callback_acl_role_edit_parent_id'));

        $crud->display_as('acl_role_name', $this->lang->line('acl_role_name'));
        $crud->display_as('parent_id', $this->lang->line('acl_role_parent_id'));
        $crud->display_as('active', $this->lang->line('acl_active'));

        if ($crud->is_grid()) {
            $crud->before_action('delete', array($this->acl_model, 'callback_before_delete_role'));
        }

        if (!$this->rt_user->isAllowed('acl', 'delete_role')) {
            $crud->unset_action('delete');
            $crud->unset_top_action('delete');
        }
        if (!$this->rt_user->isAllowed('acl', 'edit_role')) {
            $crud->unset_action('edit');
        }
        if (!$this->rt_user->isAllowed('acl', 'insert_role')) {
            $crud->unset_top_action('add');
        }

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

    private function acl_rule_save()
    {
        $this->load->language('ciext/acl');
        // Prepare data
        $id_acl_role = intval($this->input->post('id_acl_role'));

        $data = array(
            'status'         => '0',
            'status_message' => $this->lang->line('fail')
        );

        if (!empty($id_acl_role)) {
            $this->load->model('ciext/acl_model');
            $this->load->library('ciext/observers/acl_user/acl_rule_edit_subject');
            $this->load->library('ciext/observers/acl_user/acl_rule_edit_observer');

            // Add observers
            $this->acl_rule_edit_subject->attach($this->acl_rule_edit_observer);

            $saved = ($this->acl_model->delete_rules($id_acl_role) > 0) ? TRUE : FALSE;

            // Prepare rules for remove
            $this->acl_model->delete_rules($id_acl_role);

            foreach ($_POST as $k => $v) {
                $post = explode('_', $k);
                if (isset($post[0]) && $post[0] == 'resource') {
                    foreach ($v as $value) {
                        if ($this->acl_model->add_rule($id_acl_role, $post[1], $value)) {
                            $this->acl_rule_edit_subject->set_data($id_acl_role, $post[1], $value);
                        }
                    }
                }
            }

            $this->acl_rule_edit_subject->notify();

            if ($this->acl_model->save_rules($id_acl_role)) {
                $data['status'] = '1';
                $data['status_message'] = $this->lang->line('success');
            }
        }

        return $data;
    }

    public function acl_rule()
    {

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

        // Ajax action
        if (!empty($_POST)) {
            die(json_encode($this->acl_rule_save()));
        }

        // Set top menu active
        $this->admin_lib->content['active_menu'] = 2;

        $this->ciext->load_bootstrap();
        $this->ciext->load_jquery_ui();
        $this->js_css->add_js(base_url() . $this->config->item('assets_folder') . '/plugins/multiselect/js/ui.multiselect.js');
        $this->js_css->add_css(array(
            base_url() . $this->config->item('assets_folder') . '/plugins/multiselect/css/common.css',
            base_url() . $this->config->item('assets_folder') . '/plugins/multiselect/css/ui.multiselect.css'
        ));
        
        $locale_file = $this->config->item('assets_folder') . '/plugins/multiselect/js/locale/ui-multiselect-' . $this->config->item('language_short'). '.js';
        if (file_exists(FCPATH . $locale_file)) {
            $this->js_css->add_js('/' . $locale_file);
        }
        $this->js_css->add_js_string('$(function(){$(\'.multiselect\').css(\'width\', \'600px\').css(\'height\', \'200px\').multiselect();});');

        $this->load->model('ciext/acl_model');
        $this->load->helper('form');

        $roles = $this->acl_model->get_roles();
        $resources = $this->acl_model->get_acl_resources();

        $content = array();
        foreach ($roles as $id => $role) {
            $res = array();
            foreach ($resources as $resource) {
                $options = array();
                $selected = array();
                $rule = $this->acl_model->get_actions($role['id_acl_role'], $resource['id_acl_resource']);
                foreach ($rule as $option) {
                    if ($this->rt_user->isAllowed($resource['acl_resource_name'], $option['acl_action_name'])) {
                        $options[$option['id_acl_action']] = $option['acl_action_alias'];
                        if (!empty($option['id_acl_rule'])) {
                            $selected[] = $option['id_acl_action'];
                        }
                    }
                }
                if (!empty($options)) {
                    $res[] = array(
                        'resource_name'   => $resource['acl_resource_alias'],
                        'resource_select' => form_multiselect('resource_' . $resource['id_acl_resource'] . '[]', $options, $selected, 'class="multiselect"')
                    );
                }
            }
            $content['roles'][] = array(
                'role' => $role['acl_role_name'],
                'id'   => $id
            );
            $content['data'][] = array(
                'id'         => $id,
                'form_open'  => $this->acl_model->jq_action_form_open('', array(), array('id_acl_role' => $role['id_acl_role'])),
                'res'        => $res,
                'form_close' => form_close()
            );
        }

        if (empty($content)) {
            $content['roles'] = array();
            $content['data'] = array();
        }

        $show = $this->parser->parse('ciext/acl/acl_rule', array_merge($content, $this->lang->language), TRUE);

        $this->admin_lib->show(array(
            'content' => $show,
            'title'   => $this->lang->line('rule_title')
        ));
    }

}

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