<?php

/* @var $this WidgetController */
/* @var $model WidgetInstance */
/* @var $form CActiveForm */
?>
<?php

$userOptions = $model->getOptions();


foreach ($model->getDefaultOptions() as $defaultOptions):
    $options = array(
        'name' => false,
        'hint' => false,
        'label' => false,
        'type' => 'text',
        'items' => array(),
    );
    foreach ($options as $key => &$value) {
        if (isset($defaultOptions[$key])) {
            $value = $defaultOptions[$key];
            unset($defaultOptions[$key]);
        }
    }
    /**
     * render
     */
    $method = CFormInputElement::$coreTypes[$options['type']];
    $method = $method . 'ControlGroup';
?>
    <?php

    if ($options['label']) {
        $defaultOptions['label'] = Yii::t('widget', $options['label']);
    } else {
        $defaultOptions['label'] = $options['name'];
    }
    $userValue = false;
    if (isset($options['name']) && isset($userOptions[$options['name']])) {
        $userValue = $userOptions[$options['name']];
    }
    if (strpos($method, 'List') !== false) {
        if ($userValue !== false) {
            if (is_array($userValue)) {
                $defaultOptions['options'] = array();
                foreach ($userValue as $v) {
                    $defaultOptions['options'][$v] = array('selected' => true);
                }
            } else {
                $defaultOptions['options'] = array($userValue => array('selected' => true));
            }
        }
        echo TbHtml::$method($model, 'options[' . $options['name'] . ']', $options['items'], $defaultOptions);
    } else {
        if ($userValue !== false) {
            $defaultOptions['value'] = $userValue;
        }
        echo TbHtml::$method($model, 'options[' . $options['name'] . ']', $defaultOptions);
    }
    ?>
<?php endforeach; ?>
