<?php

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

/**
 * If use without CIEXT, enable kcfinder first
 * Ex.
  @session_start();
  $_SESSION['KCFINDER'] = array();
  $_SESSION['KCFINDER']['disabled'] = false;
  $_SESSION['KCFINDER_uploadURL'] = "/assets/uploads/multimedia";
 *
 * @author Igor Ratsun
 */
class Rtgc_kcfinder_file extends \Abstract_rtgc_callback
{

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

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

    public function column($value = '')
    {
        if (empty($value)) {

            return '';
        }

        return $value;
    }

    public function field($name = '', $value = NULL)
    {
        $func_name = 'openKCFinderFile' . substr(md5($this->opt), 0, 5);
        $js = "function $func_name(field) {
                window.KCFinder = {
                    callBack: function(url) {
                        field.value = url;
                        window.KCFinder = null;
                    }
                };
                window.open('/assets/plugins/kcfinder/browse.php?type=files&dir=files/" . (!empty($this->opt['dir']) ? $this->opt['dir'] : 'public') . "', 'kcfinder_textbox',
                    'status=0, toolbar=0, location=0, menubar=0, directories=0, ' +
                    'resizable=1, scrollbars=0, width=800, height=600'
                );
            }
            $(function(){
                $('.clear-btn').click(function(){
                    $(this).parent().find('input').val('');
                    return false;
                });
            });";
        $this->CI->js_css->add_js_string($js);

        $html = '<input name="' . $name . '" type="text" readonly="readonly" onclick="' . $func_name . '(this)" placeholder="'
                . $this->CI->lang->line('Click here and select a file')
                . '" value="' . $value . '" title="' . $value . '" style="cursor:pointer" />'
                . '<a class="btn clear-btn" href="#">' . $this->CI->lang->line('Clear') . '</a>';

        return $html;
    }

}
