<?php

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

/**
 * CRUD model
 *
 * @author CIEXT skeleton
 */
class Rtform_request_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();
        }
        
        $this->crud->unset_action('add');
        $this->crud->unset_action('save');
        $this->crud->unset_action('save_and_back_to_list');
        $this->crud->unset_action('clone');
    }

    public function render()
    {
        $this->crud->set_table('rtform_request');
        $this->crud->columns('data, id_rtform, date');
        $this->crud->fields('date, data');

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

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

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

    protected function callbacks()
    {
        $this->crud->set_relation('id_rtform', 'rtform', 'alias');
        if ($this->crud->is_grid()) {
            $this->crud->callback('data', $this->load->library('rtform/callback/rtfc_column_data'));
        }
        $this->crud->callback('date', 'datetime');

        if (!$this->crud->is_grid()) {
            $this->load->model('rtform/rtform_tmodel');
            $this->load->model('rtform/rtform_request_tmodel');
            $request = $this->rtform_request_tmodel->fetch($this->uri->get_next_segment('edit'));
            $parent = $this->rtform_tmodel->fetch($request['id_rtform']);
            $this->load->model('emailmng/template_tmodel');
            $rec_tpl = $this->template_tmodel->fetch($parent['recipients_template']);
            $this->crud->callback_field('data', function ($name, $value) use ($rec_tpl) {
                $html = $rec_tpl['message'];
                foreach (json_decode(htmlspecialchars_decode($value), true) as $key => $item) {
                    $html = str_replace('[[' . $key . ']]', $item, $html);
                }

                return '<iframe srcdoc="' . htmlspecialchars($html) . '" style="width: 100%; min-height: 400px"></iframe>';
            });
        }


        return $this;
    }

    protected function display_as()
    {
        $this->crud->display_as('id_rtform', $this->lang->line('Form'));
        $this->crud->display_as('date', $this->lang->line('Date'));
        $this->crud->display_as('data', $this->lang->line('Request'));

        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('Record was added') . $moreBtn));
        $this->crud->attach(make_rtgrid_edit_obs('rtform', $action, $this->lang->line('Record was edited') . $moreBtn));
        $this->crud->attach(make_rtgrid_delete_obs('rtform', $action, $this->lang->line('Record was deleted')));

        return $this;
    }

}
