<?php

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

class Acl extends MX_Controller {

    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();
        }

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

        $this->ciext->load_jquery_ui();

        // Edit page
        $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_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('rtgrid/rtgrid_edit_model');
            $grid_edit = new rtgrid_edit_model();
            $grid_edit->set_table('acl_user');
            $grid_edit->set_primery_key('id_acl_user');
            $grid_edit->set_primery_key_value($id);
            if ($this->uri->get_next_segment('acl_user') == 'add') {
                $grid_edit->fields('login, password, id_acl_role, photo, email, birth_date, registered, confirmed, active');
            } else {
                $grid_edit->fields('login, id_acl_role, photo, email, birth_date, registered, confirmed, active');
            }

            $this->load->model('rtgrid/rtgrid_field_callback');
            $grid_edit->callback_field('id_acl_role', array($this->acl_model, 'callback_select_acl_role'));
            $grid_edit->callback_field('birth_date', array($this->rtgrid_field_callback, 'date'));
            $grid_edit->callback_field('registered', array($this->rtgrid_field_callback, 'date'));
            $grid_edit->callback_field('confirmed', array($this->rtgrid_field_callback, 'active'));
            $grid_edit->callback_field('active', array($this->rtgrid_field_callback, 'active'));

            $grid_edit->callback_before_edit(array($this->acl_model, 'callback_before_edit_user'));

            $grid_edit->display_as('login', $this->lang->line('login'));
            $grid_edit->display_as('password', $this->lang->line('password'));
            $grid_edit->display_as('id_acl_role', $this->lang->line('acl_role_name'));
            $grid_edit->display_as('photo', $this->lang->line('photo'));
            $grid_edit->display_as('email', $this->lang->line('email'));
            $grid_edit->display_as('birth_date', $this->lang->line('birth_date'));
            $grid_edit->display_as('registered', $this->lang->line('registered'));
            $grid_edit->display_as('confirmed', $this->lang->line('confirmed'));
            $grid_edit->display_as('active', $this->lang->line('acl_active'));

            $grid_edit->add_action(new rtgrid_edit_action('back_to_list', $this->lang->line('back_to_list'), '', '', '<a href="/' . $this->config->item('admin_page') . '/acl/acl_user" class="btn">{action_text}</a>'));

            // Add observers
            $this->load->library('ciext/observers/acl_user/acl_user_edit_observer');
            $grid_edit->attach($this->acl_user_edit_observer);

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

            $this->load->model('rtgrid/rtgrid_model');
            $grid = new rtgrid_model();
            $grid->set_table('acl_user');
            $grid->set_primery_key('id_acl_user');
            $grid->columns('acl_user.login, acl_role.acl_role_name, acl_user.active');
            $grid->join('acl_role', 'acl_role.id_acl_role=acl_user.id_acl_role', 'left');

            $grid->display_as('login', $this->lang->line('login'));
            $grid->display_as('acl_role_name', $this->lang->line('acl_role_name'));
            $grid->display_as('active', $this->lang->line('acl_active'));

            $this->load->model('rtgrid/rtgrid_column_callback');
            $grid->callback_column('active', array($this->rtgrid_column_callback, 'active'));

            if ($this->rt_user->isAllowed('acl', 'change_user_pass')) {
                $grid->add_action(new rtgrid_action('change_pass_dialog', '<a href="javascript:change_pass({primery_key});void(0);"><i class="icon-lock"></i> ' . $this->lang->line('change_pass_dialog') . '</a>'));
                $grid->add_action(new rtgrid_action('change_info', '<a href="/' . $this->config->item('admin_page') . '/mconfig/index/userinfo/change_info/user/{primery_key}"><i class="icon-user"></i> ' . $this->lang->line('change_info') . '</a>'));
            }

            if (!$this->rt_user->isAllowed('acl', 'delete_user')) {
                $grid->unset_action('delete');
                $grid->unset_top_action('delete');
            }

            if (!$this->rt_user->isAllowed('acl', 'edit_user')) {
                $grid->unset_action('edit');
            }

            if (!$this->rt_user->isAllowed('acl', 'insert_user')) {
                $grid->unset_top_action('add');
            }

            // Add observers
            $this->load->library('ciext/observers/acl_user/acl_user_delete_observer');
            $grid->attach($this->acl_user_delete_observer);

            $grid->before_action('delete', array($this->acl_model, 'before_delete_user'));

            return $this->admin_lib->show(array(
                        'content' => $grid->render() . $this->parser->parse('ciext/acl/acl_user', $this->lang->language, TRUE),
                        '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();
        }

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

        $this->ciext->load_bootstrap();

        // Edit page
        $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();
            }

            $this->load->model('rtgrid/rtgrid_edit_model');
            $grid_edit = new rtgrid_edit_model();
            $grid_edit->set_table('acl_role');
            $grid_edit->set_primery_key('id_acl_role');
            $grid_edit->set_primery_key_value($id);
            $grid_edit->fields('acl_role_name, parent_id, active');

            $this->load->model('rtgrid/rtgrid_field_callback');
            $grid_edit->callback_field('active', array($this->rtgrid_field_callback, 'active'));

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

            $grid_edit->callback_field('parent_id', array($this->acl_model, 'callback_acl_role_edit_parent_id'));

            $grid_edit->add_action(new rtgrid_edit_action('back_to_list', $this->lang->line('back_to_list'), '', '', '<a href="/' . $this->config->item('admin_page') . '/acl/acl_role" class="btn">{action_text}</a>'));

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

            $this->load->model('rtgrid/rtgrid_model');
            $grid = new rtgrid_model();
            $grid->set_table('acl_role');
            $grid->set_primery_key('id_acl_role');
            $grid->columns('acl_role_name, parent_id, active');

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

            $this->load->model('rtgrid/rtgrid_column_callback');
            $grid->callback_column('parent_id', array($this->acl_model, 'callback_acl_role_view_parent_id'));
            $grid->callback_column('active', array($this->rtgrid_column_callback, 'active'));

            $grid->before_action('delete', array($this->acl_model, 'callback_before_delete_role'));

            if (!$this->rt_user->isAllowed('acl', 'delete_role')) {
                $grid->unset_action('delete');
                $grid->unset_top_action('delete');
            }

            if (!$this->rt_user->isAllowed('acl', 'edit_role')) {
                $grid->unset_action('edit');
            }

            if (!$this->rt_user->isAllowed('acl', 'insert_role')) {
                $grid->unset_top_action('add');
            }

            return $this->admin_lib->show(array(
                        'content' => $grid->render() . $this->parser->parse('ciext/acl/acl_user', $this->lang->language, TRUE),
                        'title' => $this->lang->line('user_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()));
        }

        // Unset left menu
        $this->admin_lib->content['user_left_menu'] = '';

        $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'
        ));

        $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('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 */