<?php

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

/**
 * CRUD model
 * 
 * @author CIEXT skeleton
 */
class Rtform_crud extends \Ciext\Model\AbstractEventModel
{

    protected $crud;

    public function __construct()
    {
        parent::__construct();
        $this->load->library('rtgrid/rtgrid_api');
        $this->crud = new rtgrid_api();
        
        // Access check
        if (!$this->rt_user->isAllowed('rtform', 'view_rtform')) {
            $this->admin_lib->show_denied();
        }
        if (!$this->rt_user->isAllowed('rtform', 'edit_rtform')) {
            $this->crud->unset_action('edit');
            $this->crud->unset_action('delete');
            $this->crud->unset_action('add');
        }
    }

    public function render()
    {
        $this->crud->set_table('rtform');
        $this->crud->columns('rtform.alias, rtform.code, recipients');
        $this->crud->fields('alias, code, recipients, recipients_template, reply_template, success_message, form_class');
        
        $this->crud->required_fields('alias', 'code', 'recipients', 'recipients_template', 'form_class');

        $this->crud->unique_fields('code');

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

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

    protected function callbacks()
    {
        $this->crud->callback('alias', 'rtgc_edit_link');
        $this->crud->callback('code', 'rtgc_edit_link');
        $this->crud->callback('recipients', new \Rtgc_textarea(['rows' => 3]));
        $this->crud->set_relation('recipients_template', 'emailmng', 'code');
        $this->crud->set_relation('reply_template', 'emailmng', 'code');
        $this->crud->callback('success_message', new \Rtgc_textarea(['rows' => 3]));
        $this->crud->callback('form_class', new \Rtgc_input(['class' => 'input-xxlarge']));

        return $this;
    }

    protected function display_as()
    {
        $this->crud->display_as('alias', $this->lang->line('Alias'));
        $this->crud->display_as('code', $this->lang->line('Code'));
        $this->crud->display_as('recipients', $this->lang->line('Recipients'));
        $this->crud->display_as('recipients_template', $this->lang->line('Email template for recipients'));
        $this->crud->display_as('reply_template', $this->lang->line('Email template for auto reply'));
        $this->crud->display_as('success_message', $this->lang->line('Success message'));
        $this->crud->display_as('form_class', $this->lang->line('Path to form class'));

        $this->crud->tooltip('alias', $this->lang->line('Human form name. For example: Contact form on main page'));
        $this->crud->tooltip('code', $this->lang->line('Machine name. For example: contact_form_on_main'));
        $this->crud->tooltip('recipients', $this->lang->line('For example: <code>webmaster@example.com</code> or <code>sales@example.com, support@example.com</code>. To specify multiple recipients, separate each email address with a comma'));
        $this->crud->tooltip('reply_template', $this->lang->line('Will send reply if exist <code>input with name `email` </code> in form'));
        $this->crud->tooltip('form_class', $this->lang->line('Must be available by <code>$this->load>library(FORM_PATH)</code> and be instance of class <code>\Ciext\Form\AbstractForm</code>'));

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

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

        return $this;
    }

}
