<?php

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

/**
 * Set multiple input for options select
 *
 * @author Olexandr Vlasiuk
 */
class Multifield_options implements Rtgc_callback
{

    protected $callback;

    public function __construct($param = [])
    {
        // Check params
        if (!empty($param['callback'])) {
            $this->callback = $param['callback'];
        }
    }

    public function column($value = '', $data = array())
    {
        return 'No data to display for grid. Only field data.';
    }

    public function field($name = '', $value = NULL)
    {
        get_instance()->js_css->parse_js('rtgrid/edit/multifield/js/multifield.js');

        $field_data = json_decode(htmlspecialchars_decode($value));
        $content['field'] = array();
        $content['alias'] = $name;
        $content['value'] = $value;
        if (!empty($field_data) && is_array($field_data)) {
            foreach ($field_data as $key => $value) {
                $content['field'][] = array(
                    'value' => (!empty($this->callback)) ? call_user_func($this->callback, $name, $value) : get_instance()->rtgrid_field_callback->default_field($name, $value),
                    'btn'   => ($key == 0) ? 'icon-plus' : 'icon-remove',
                    'class' => ($key == 0) ? 'add' : 'remove'
                );
            }
        } else {
            $content['field'][] = array(
                'value' => (!empty($this->callback)) ? call_user_func($this->callback, $name, $value) : get_instance()->rtgrid_field_callback->default_field($name, $value),
                'btn'   => 'icon-plus',
                'class' => 'add'
            );
        }

        return get_instance()->parser->parse('rtgrid/edit/multifield/multifield', $content, TRUE);
    }

}
