<?php

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

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

    protected $crud;

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

        // Load language file
        $this->load->library('gettext_language');
        $this->gettext_language->load(__DIR__ . '/../language/' . $this->config->item('language') . '.po');

        // Acceess check
        if (!$this->rt_user->isAllowed('product', 'view_product')) {
            $this->admin_lib->show_denied();
        }
        if (!$this->rt_user->isAllowed('product', 'edit_product')) {
            $this->crud->unset_action('edit');
            $this->crud->unset_action('delete');
            $this->crud->unset_action('add');
        }
    }

    public function render()
    {
        $this->crud->set_table('product_manufacturer');
        $this->crud->columns('translate_name, active');
        $this->crud->fields('translate_name, active');

        $this->crud->required_fields('translate_name');

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

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

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

    protected function callbacks()
    {
        $this->crud->multilang(new multilang('translate_name', 'product_manufacturer_translate', 'name'));
        $this->crud->callback('active', 'active');

        return $this;
    }

    protected function display_as()
    {
        $this->crud->display_as('translate_name', $this->lang->line('Name'));
        $this->crud->display_as('active', $this->lang->line('Active'));

        return $this;
    }

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

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

        return $this;
    }

}
