<?php

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

/**
 * CRUD model
 * 
 * @author CIEXT skeleton
 */
class CRUD_CLASS_NAME extends CI_Model
{

    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')->load(__DIR__ . '/../language/' . $this->config->item('language') . '.po');
        
        // Acceess check
        if (!$this->rt_user->isAllowed('/* module_folder */', 'view_/* module_folder */')) {
            $this->admin_lib->show_denied();
        }
        if (!$this->rt_user->isAllowed('/* module_folder */', 'edit_/* module_folder */')) {
            $this->crud->unset_action('edit');
            $this->crud->unset_action('delete');
            $this->crud->unset_action('add');
        }
    }

    public function render()
    {
        $this->crud->set_table(/* table */);
        $this->crud->columns(/* columns */);
        $this->crud->fields(/* fields */);
        /* required */
        $this->callbacks()->display_as()->observers();

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

    protected function callbacks()
    {
        /* callback */
        return $this;
    }

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

        $this->crud->attach(make_rtgrid_add_obs('/* module_folder */', $action, $this->lang->line('Record was added') . $moreBtn));
        $this->crud->attach(make_rtgrid_edit_obs('/* module_folder */', $action, $this->lang->line('Record was edited') . $moreBtn));
        $this->crud->attach(make_rtgrid_delete_obs('/* module_folder */', $action, $this->lang->line('Record was deleted')));

        return $this;
    }

}
