<?php

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

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

    protected $crud;
    protected $parent_id;

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

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

        $this->config->load('post/config');
    }

    public function render()
    {
        $this->crud->set_table('post');
        $this->crud->columns('thumbnail, translate_title, rtp.active');
        $this->crud->fields('thumbnail, id_resource_type_page');

        $this->crud->required_fields('thumbnail', 'translate_title', 'translate_content');

        if ($this->crud->is_grid()) {
            $this->crud->join('resource r', 'r.id_item=post.id_resource_type_page AND r.id_resource_type=3', 'left');
            $this->crud->where('r.parent_id', $this->get_parent_id());
            $this->crud->join('resource_type_page rtp', 'rtp.id_resource_type_page=post.id_resource_type_page', 'left');
        }

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

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

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

    protected function callbacks()
    {
        $this->crud->set_image_upload(new image_upload(
            'thumbnail',
            $this->config->item('post_thumbnail_path'),
            $this->config->item('post_thumbnail_width'),
            $this->config->item('post_thumbnail_height')
        ));
        $this->crud->multilang(new multilang('translate_title', 'post_translate', 'title', function($name, $value){
            return (new \Rtgc_edit_link())->field($name, $value);
        }));
        $this->crud->multilang(new multilang('translate_content', 'post_translate', 'content', function($name, $value){
            return (new \Rtgc_texteditor())->field($name, $value);
        }));
        $this->crud->multilang(new multilang('translate_short_desc', 'post_translate', 'short_desc', function($name, $value){
            return (new \Rtgc_textarea())->field($name, $value);
        }));
        $this->crud->callback('translate_title', 'rtgc_edit_link');
        $this->crud->callback('active', 'rtgc_active');
        $this->load->library('resmng/respage_rtgrid', [
            'crud'                           => $this->crud,
            'alias_field'                    => 'translate_title',
            'id_resource_type_page_template' => $this->config->item('post_id_resource_type_page_template'),
            'parent_id'                      => $this->get_parent_id(),
        ]);

        return $this;
    }

    protected function display_as()
    {
        $this->crud->display_as('thumbnail', $this->lang->line('Thumbnail'));
        $this->crud->display_as('translate_title', $this->lang->line('Title'));
        $this->crud->display_as('translate_short_desc', $this->lang->line('Short description'));
        $this->crud->display_as('translate_content', $this->lang->line('Content'));
        $this->crud->display_as('active', $this->lang->line('Published'));
        $this->crud->display_as('id_resource_type_page', $this->lang->line('Page'));

        return $this;
    }

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

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

        return $this;
    }

    protected function get_parent_id()
    {
        if (empty($this->parent_id)) {
            $db = clone $this->db;
            $q = $db
                ->select('r.id_resource')
                ->join('resource r', 'r.id_item=c.id_resource_type_page AND r.id_resource_type=3', 'left')
                ->where('c.id_post_category', (int)$this->uri->get_next_segment('posts'))
                ->get('post_category c')
                ->row_array();
            $this->parent_id = !empty($q['id_resource']) ? $q['id_resource'] : $this->config->item('post_category_parent_id');
        }

        return $this->parent_id;
    }

}
