<?php

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

class crud_acl_user extends rtgrid_api
{

    public function __construct()
    {
        parent::__construct();

        $ci = & get_instance();

        // Access check
        $this->access_check($ci);

        $this->set_table('acl_user');

        $this->columns('login, id_acl_role, acl_user.active');
        $this->fields('photo, login, password, id_acl_role, email, birth_date, registered, id_lan, confirmed, active');

        $this->set_image_upload(new image_upload('photo', '/assets/uploads/userinfo/', 256, 256, TRUE));
        $this->set_relation('id_acl_role', 'acl_role', 'acl_role_name');
        $this->set_relation('id_lan', 'lan', 'name', array('lan.active' => TRUE));

        $this->multilang(new multilang('translate_name', 'acl_user_translate', 'name'));
        $this->multilang(new multilang('translate_surname', 'acl_user_translate', 'surname'));

        // Callback
        $this->callback('login', 'rtgc_edit_link');
        $this->callback('password', 'rtgc_password');
        $this->callback('birth_date', 'date');
        $this->callback('registered', 'date');
        $this->callback('confirmed', 'active');
        $this->callback('active', 'active');

        // Display as
        if ($ci->uri->get_next_segment('edit') != 'add') {
            $this->display_as('password', $ci->lang->line('New password'));
        } else {
            $this->display_as('password', $ci->lang->line('password'));
        }
        $this->display_as('login', $ci->lang->line('login'));
        $this->display_as('id_acl_role', $ci->lang->line('acl_role_name'));
        $this->display_as('birth_date', $ci->lang->line('birth_date'));
        $this->display_as('registered', $ci->lang->line('registered'));
        $this->display_as('id_lan', $ci->lang->line('Default language'));
        $this->display_as('confirmed', $ci->lang->line('confirmed'));
        $this->display_as('active', $ci->lang->line('acl_active'));
        $this->display_as('translate_name', $ci->lang->line('Name'));
        $this->display_as('translate_surname', $ci->lang->line('Surname'));

        if (!$this->is_grid()) {
            $this->callback_before_edit(array($ci->acl_model, 'callback_before_edit_user'));
            // Add observers
            $ci->load->library('ciext/observers/acl_user/acl_user_edit_observer');
            $this->attach($ci->acl_user_edit_observer);
        } else {
            $this->before_action('delete', array($ci->acl_model, 'before_delete_user'));
            // Add observers
            $ci->load->library('ciext/observers/acl_user/acl_user_delete_observer');
            $this->attach($ci->acl_user_delete_observer);
        }
    }

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

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

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

}
