<?php

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

/**
 * CRUD model
 *
 * @author CIEXT skeleton
 */
class Filemanager_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('filemanager', 'edit_file') && !$this->rt_user->isAllowed('filemanager', 'edit_all_file')) {
            $this->admin_lib->show_denied();
        }
        $this->crud->unset_action('edit');
        $this->crud->unset_action('add');
    }

    public function render()
    {
        $this->crud->set_table('media');
        $this->crud->columns('name, type, size, is_image, image_type, created');
        $this->crud->fields('name, type, size, is_image, image_type, created');

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

        if ($this->crud->is_grid()) {
            $this->crud->before_action('delete', [$this, 'callback_before_delete']);
        }

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

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

    public function callback_before_delete($ids = [])
    {
        $db = clone $this->db;
        $q = $db->select('name, is_image')->where_in('id_media', $ids)->get('media')->result_array();
        foreach ($q as $item) {
            $old_file = './assets/uploads/multimedia/' . (!empty($item['is_image']) ? 'images/' : 'files/') . $item['name'];
            if (file_exists($old_file) && !is_dir($old_file)) {
                @unlink($old_file);
            }
        }

        return true;
    }

    protected function callbacks()
    {
        $this->crud->callback('name', $this->load->library('filemanager/crud_field_name'));
        $this->crud->callback('type', 'rtgc_input');
        $this->crud->callback('size', 'rtgc_input');
        $this->crud->callback('is_image', 'rtgc_active');
        $this->crud->callback('image_type', 'rtgc_input');
        $this->crud->callback('created', 'datetime');

        return $this;
    }

    protected function display_as()
    {
        $this->crud->display_as('name', $this->lang->line('Name'));
        $this->crud->display_as('type', $this->lang->line('Type'));
        $this->crud->display_as('size', $this->lang->line('Size, kB'));
        $this->crud->display_as('is_image', $this->lang->line('Is image'));
        $this->crud->display_as('image_type', $this->lang->line('Image type'));
        $this->crud->display_as('created', $this->lang->line('Created'));

        return $this;
    }

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

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

        return $this;
    }

}
