<?php

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

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

    protected $crud;

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

        // Acceess check
        if (!$this->rt_user->isAllowed('logger', 'view_logger')) {
            $this->admin_lib->show_denied();
        }

        $this->crud->unset_action('save');
        $this->crud->unset_action('save_and_back_to_list');
        $this->crud->unset_action('add');
        $this->crud->unset_action('add_and_next');
    }

    public function render()
    {
        $this->crud->set_table('logger');
        $this->crud->columns('key, value, date, ip');
        $this->crud->fields('key, value, date, ip');

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

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

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

    protected function callbacks()
    {
        $this->crud->callback('key', 'rtgc_edit_link');
        $this->crud->callback('ip', 'input');
        $this->crud->callback('date', 'datetime');
        $this->crud->callback('value', 'textarea');
        $this->crud->callback_field('value', function($n, $v){
            return '<pre>' . $v . '</pre>';
        });

        return $this;
    }

    protected function display_as()
    {
        $this->crud->display_as('key', $this->lang->line('Key'));
        $this->crud->display_as('ip', $this->lang->line('IP'));
        $this->crud->display_as('date', $this->lang->line('Date'));
        $this->crud->display_as('value', $this->lang->line('Value'));

        return $this;
    }

}
