<?php

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

/**
 * CRUD model
 *
 * @author CIEXT skeleton
 */
class Crud_slider 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('slider', 'manage_slider')) {
            $this->admin_lib->show_denied();
        }
    }

    public function render()
    {
        $this->crud->set_table('slider');
        $this->crud->columns('code, name, is_image, is_title, is_description, is_link, view_order');
        $this->crud->fields('code, name, is_image, image_w, image_h, is_title, is_description, is_link, view_order, template');

        $this->crud->required_fields('name', 'code', 'template');

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

        if (!$this->crud->is_grid()) {
            $this->crud->callback_after_edit(function ($post) {
                $ci = &get_instance();
                $page = 'mconfig/index/slider/slide/' . $post['code'];
                $parent = $ci->db->select('id_admin_menu')->where('name', 'Sliders')->get('admin_menu')->row_array();
                if (!empty($parent['id_admin_menu'])) {
                    $data = [
                        'name'         => $post['name'],
                        'page'         => $page,
                        'parent_id'    => $parent['id_admin_menu'],
                        'view_order'   => $post['view_order'],
                        'acl_resource' => 'slider',
                        'acl_action'   => 'view_slider',
                        'active'       => true,
                    ];
                    $current = $ci->db->select('id_admin_menu')->where('page', $page)->get('admin_menu')->row_array();
                    if (empty($current['id_admin_menu'])) {
                        $ci->db->insert('admin_menu', $data);
                    } else {
                        $ci->db->where('id_admin_menu', $current['id_admin_menu'])->update('admin_menu', $data);
                    }
                }
            });
        } else {
            $this->crud->before_action('delete', [$this, 'before_delete']);
        }

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

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

    public function before_delete($items = array())
    {
        $ids = array_map(function ($v) {
            return 'mconfig/index/slider/slide/' . $v;
        }, $items);

        $this->db->where_in('page', $ids)->delete('admin_menu');

        return true;
    }

    protected function callbacks()
    {
        $this->crud->callback('code', 'rtgc_edit_link');
        $this->crud->callback('name', 'rtgc_edit_link');
        $this->crud->callback('is_image', 'rtgc_active');
        $this->crud->callback('image_w', new \Rtgc_input_with_default_value(0));
        $this->crud->callback('image_h', new \Rtgc_input_with_default_value(0));
        $this->crud->callback('is_title', 'rtgc_active');
        $this->crud->callback('is_description', 'rtgc_active');
        $this->crud->callback('is_link', 'rtgc_active');
        $this->crud->callback('view_order', 'rtgc_view_order', ['crud' => $this->crud]);
        $this->crud->callback('template', new \Rtgc_input_with_default_value('slider/default'));

        return $this;
    }

    protected function display_as()
    {
        $this->crud->display_as('code', $this->lang->line('Code'));
        $this->crud->display_as('name', $this->lang->line('Name'));
        $this->crud->display_as('is_image', $this->lang->line('Show image'));
        $this->crud->display_as('image_w', $this->lang->line('Image width'));
        $this->crud->display_as('image_h', $this->lang->line('Image height'));
        $this->crud->display_as('is_title', $this->lang->line('Show title'));
        $this->crud->display_as('is_description', $this->lang->line('Show description'));
        $this->crud->display_as('is_link', $this->lang->line('Show link'));
        $this->crud->display_as('view_order', $this->lang->line('View order'));
        $this->crud->display_as('template', $this->lang->line('Template'));

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

        return $this;
    }

}
