<?php

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

/**
 * Description of rtgc_select
 *
 * @author Roman Ratsun
 */
class Rtgc_select extends \Abstract_rtgc_callback implements \Rtgc_filtrable
{

    protected $CI = NULL;
    protected $extra_data = array();

    public function __construct($extra_data = array())
    {
        $this->CI = &get_instance();

        // set extra data
        $this->set_extra_data($extra_data);
    }

    public function column($value = '', $data = array())
    {
        return (!empty($this->get_extra_data($value))) ? $this->get_extra_data($value) : '';
    }

    public function field($name = '', $value = NULL)
    {
        $this->CI->load->helper('form');

        return form_dropdown($name, $this->extra_data, $value);
    }

    public function filter($name, $value)
    {
        $extra_data = $this->extra_data;
        $this->extra_data = ['0' => $this->CI->lang->line('All')];
        foreach ($extra_data as $key => $value) {
            $this->extra_data[$key] = $value;
        }

        $res = $this->field($name, $value);

        $this->extra_data = $extra_data;

        return $res;
    }

    public function filter_by($search_name, $value)
    {
        if (!empty($value)) {
            $this->CI->db->where($search_name, $value);
        }
    }

    /**
     * Set extra data.
     *
     * @param array $extra_data
     */
    protected function set_extra_data($extra_data)
    {
        $this->extra_data = (array) $extra_data;
    }

    /**
     * Get extra data.
     *
     * @param integer $key
     * @return string|array
     */
    protected function get_extra_data($key = '')
    {
        return (!empty($this->extra_data[$key])) ? $this->extra_data[$key] : '';
    }

}
