<?php

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

/**
 * CRUD model
 * 
 * @author CIEXT skeleton
 */
class crud_emailmng extends CI_Model
{

    protected $crud;

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

        $this->load->library('gettext_language');
        $this->gettext_language->load(__DIR__ . '/../language/' . $this->config->item('language') . '.po');

        // Acceess check
        if (!$this->rt_user->isAllowed('emailmng', 'view_emailmng')) {
            $this->admin_lib->show_denied();
        }
        if (!$this->rt_user->isAllowed('emailmng', 'edit_emailmng')) {
            $this->crud->unset_action('edit');
        }
        if (!$this->rt_user->isAllowed('emailmng', 'can_delete')) {
            $this->crud->unset_action('delete');
            $this->crud->unset_action('add');
            $this->crud->readonly('code');
        }
    }

    public function render()
    {
        $this->crud->set_table('emailmng');
        $this->crud->columns('code, subject, mailtype');
        $field = 'code, copy_to, hidden_copy_to, subject, message, mailtype';
        if ($this->rt_user->isAllowed('emailmng', 'can_delete')) {
            $field .= ', subject_tooltip, message_tooltip';
        }
        $this->crud->fields($field);
        $this->crud->unique_fields('code');
        $this->crud->required_fields('code');

        $this->callbacks()->display_as()->observers();
        if (!$this->rt_user->isAllowed('emailmng', 'can_delete')) {
            $this->crud->readonly('code');
        }
        
        return $this->crud->render();
    }

    protected function callbacks()
    {
        $this->crud->callback('code', 'input');
        $this->crud->callback('copy_to', 'input');
        $this->crud->callback('hidden_copy_to', 'input');
        $this->crud->callback('subject', 'input');
        $this->crud->callback('subject_tooltip', 'textarea');
        $this->crud->callback('message', 'texteditor');
        $this->crud->callback('message_tooltip', 'textarea');
        $this->crud->callback('mailtype', 'rtgc_select', $this->config->item('mailtype'));
        $this->load->model('emailmng/template_tmodel');
        $template = $this->template_tmodel->fetch((int) $this->uri->get_next_segment('edit'));
        if (!empty($template)) {
            $this->crud->tooltip('subject', $template['subject_tooltip']);
            $this->crud->tooltip('message', $template['message_tooltip']);
        }

        return $this;
    }

    protected function display_as()
    {
        $this->crud->display_as('code', $this->lang->line('Code of template'));
        $this->crud->display_as('copy_to', $this->lang->line('Send copy to'));
        $this->crud->display_as('hidden_copy_to', $this->lang->line('Send hidden copy to'));
        $this->crud->display_as('subject', $this->lang->line('Message subject'));
        $this->crud->display_as('message', $this->lang->line('Message'));
        $this->crud->display_as('mailtype', $this->lang->line('Mail type'));
        $this->crud->display_as('subject_tooltip', $this->lang->line('Subject tooltip'));
        $this->crud->display_as('message_tooltip', $this->lang->line('Message tooltip'));

        return $this;
    }

    protected function observers()
    {
        $this->load->helper('rtgrid/rtgrid_obs');
        $action = 'edit_emailmng';
        $moreBtn = ' <a href="{link}">>>></a>';

        $this->crud->attach(make_rtgrid_add_obs('emailmng', $action, $this->lang->line('Mail template was added') . $moreBtn));
        $this->crud->attach(make_rtgrid_edit_obs('emailmng', $action, $this->lang->line('Mail template was edited') . $moreBtn));
        $this->crud->attach(make_rtgrid_delete_obs('emailmng', $action, $this->lang->line('Mail template was deleted')));

        return $this;
    }

}
