<?php

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

/**
 * Set url input
 *
 * @author Igor Ratsun
 */
class Rtgc_url extends \Rtgc_input
{
    public function __construct($options = [])
    {
        if (empty($options['crud']) || empty($options['field'])) {
            show_error('rtgc_url: Empty crud or field option');
        }

        $crud = &$options['crud'];
        $filed = $options['field'];
        $crud->callback_before_edit(function ($post) use ($filed) {
            $ci = &get_instance();
            $ci->load->helper('url');
            $post[$filed] = url_title($post[$filed], 'dash', TRUE);

            return $post;
        });

        $opt = [];
        foreach ($options as $key => $value) {
            if (in_array($key, ['crud', 'field'])) {
                continue;
            }
            $opt[$key] = $value;
        }
        parent::__construct($opt);
    }

}
