<?php

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

/**
 * Set input
 *
 * @author Igor Ratsun
 */
class Rtgc_input extends \Abstract_rtgc_callback implements \Rtgc_filtrable
{

    protected $options = [
        'type'      => 'text',
        'maxlength' => '255',
    ];

    public function __construct($opt = [])
    {
        if (is_array($opt)) {
            $this->options = array_merge($this->options, $opt);
        }
    }

    public function column($value = '', $data = array())
    {
        return $value;
    }

    public function field($name = '', $value = NULL)
    {
        $data = $this->options;
        $data['name'] = $name;
        $data['value'] = $value;
        
        $attr_html = '';
        foreach ($data as $key => $val) {
            $attr_html .= $key . '="' . $val . '" ';
        }

        return '<input ' . trim($attr_html) . ' />';
    }

    public function filter($name, $value)
    {
        return $this->field($name, $value);
    }

    public function filter_by($column, $value)
    {
        if (mb_strlen($value) !== 0) {
            $ci = &get_instance();
            if ($ci->db->dbdriver == 'postgre') {
                $ci->db->where('"' . $column . '"' . '::TEXT LIKE \'%' . $value . '%\'', '', false);
            } else {
                $ci->db->like($column, $value);
            }
        }
    }

}
