<?php

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

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

    protected $crud;
    protected $slider;

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

        // Access check
        if (!$this->rt_user->isAllowed('slider', 'view_slider')) {
            $this->admin_lib->show_denied();
        }
        if (!$this->rt_user->isAllowed('slider', 'edit_slider')) {
            $this->crud->unset_action('edit');
            $this->crud->unset_action('delete');
            $this->crud->unset_action('add');
        }

        $this->config->load('slider/config');
        $this->load->model('slider/slider_tmodel');
        $this->slider = $this->slider_tmodel->fetch_by_code($this->uri->get_next_segment('slide'));
        if (empty($this->slider)) {
            show_404();
        }
    }

    public function render()
    {
        $this->crud->set_table('slider_slide');

        $fields = [];
        foreach (['is_image', 'is_title', 'is_description', 'is_link', 'is_yt_video'] as $field) {
            if (!empty($this->slider[$field])) {
                $fields[] = str_replace('is_', '', $field);
            }
        }
        $this->crud->columns(trim(implode(', ', $fields) . ', slider_slide.view_order, active', ', '));
        $this->crud->fields(trim(implode(', ', $fields) . ', view_order, active', ', '));

        $this->crud->where('slider_slide.id_slider', $this->slider['id_slider']);
        $this->crud->hidden('id_slider', $this->slider['id_slider']);

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

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

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

    protected function callbacks()
    {
        $this->crud->set_relation('id_slider', 'slider', 'name');
        $this->crud->callback('view_order', 'rtgc_view_order', ['crud' => $this->crud]);
        $this->crud->callback('active', 'rtgc_active');

        if (!empty($this->slider['is_image'])) {
            $this->crud->set_image_upload(new image_upload('image', $this->config->item('slider_image_path'), $this->slider['image_w'], $this->slider['image_h']));
        }
        if (!empty($this->slider['is_title'])) {
            $this->crud->callback('title', ($this->crud->is_grid() ? 'rtgc_edit_link' : new \Rtgc_textarea(['rows' => 2, 'maxlength' => 255])));
        }
        if (!empty($this->slider['is_description'])) {
            $this->crud->callback('description', new \Rtgc_textarea());
        }
        if (!empty($this->slider['is_link'])) {
            $this->crud->callback('link', new \Rtgc_textarea(['rows' => 2, 'maxlength' => 255]));
        }
        if (!empty($this->slider['is_yt_video'])) {
            $this->crud->callback('yt_video', new \Rtgc_yt_video());
        }

        return $this;
    }

    protected function display_as()
    {
        $this->crud->display_as('id_slider', $this->lang->line('Slider'));
        $this->crud->display_as('image', $this->lang->line('Image'));
        $this->crud->display_as('title', $this->lang->line('Title'));
        $this->crud->display_as('description', $this->lang->line('Description'));
        $this->crud->display_as('link', $this->lang->line('Link'));
        $this->crud->display_as('yt_video', $this->lang->line('YouTube video'));
        $this->crud->display_as('view_order', $this->lang->line('View order'));
        $this->crud->display_as('active', $this->lang->line('Active'));

        return $this;
    }

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

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

        return $this;
    }

}
