<?php

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

class Acl_user_crud extends \Ciext\Model\AbstractEventModel
{

    protected $crud;

    public function __construct()
    {
        parent::__construct();
        $this->load->library('rtgrid/rtgrid_api');
        $this->crud = new rtgrid_api();

        // Acceess check
        if (!$this->rt_user->isAllowed('acl', 'delete_user')) {
            $this->crud->unset_action('delete');
        }
        if (!$this->rt_user->isAllowed('acl', 'edit_user')) {
            $this->crud->unset_action('edit');
        }
        if (!$this->rt_user->isAllowed('acl', 'insert_user')) {
            $this->crud->unset_action('add');
        }
    }

    public function render()
    {
        $this->crud->set_table('acl_user');
        $this->crud->columns('login, id_acl_role, acl_user.active');
        $this->crud->fields('photo, login, password, id_acl_role, email, birth_date, registered, id_lan, confirmed, active');

        $this->callbacks()->display_as()->observers();

        // Notify
        $this->notify(__FUNCTION__, ['crud' => $this->crud]);

        return $this->crud->render();
    }

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

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

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

        if (!$this->crud->is_grid()) {
            $this->crud->callback_before_edit(array($this->acl_model, 'callback_before_edit_user'));
        } else {
            $this->crud->before_action('delete', array($this->acl_model, 'before_delete_user'));
        }

        return $this;
    }

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

        return $this;
    }

    protected function observers()
    {
        if (!$this->crud->is_grid()) {
            $this->crud->attach($this->load->library('ciext/observers/acl_user/acl_user_edit_observer'));
        } else {
            $this->crud->attach($this->load->library('ciext/observers/acl_user/acl_user_delete_observer'));
        }

        return $this;
    }

}
