<?php

/**
 * Description of ShortCodeWidget
 *
 * @author Mohammed Osman  <osmancode@gmail.com>
 */
class ShortCodeWidget extends CWidget {

    /**
     * text: activeTextField
     * hidden: activeHiddenField
     * password: activePasswordField
     * textarea: activeTextArea
     * file: activeFileField
     * radio: activeRadioButton
     * checkbox: activeCheckBox
     * listbox: activeListBox
     * dropdownlist: activeDropDownList
     * checkboxlist: activeCheckBoxList
     * radiolist: activeRadioButtonList
     * url: activeUrlField
     * email: activeEmailField
     * number: activeNumberField
     * range: activeRangeField
     * date: activeDateField
     * time: activeTimeField
     * datetime: activeDateTimeField
     * datetimelocal: activeDateTimeLocalField
     * week: activeWeekField
     * color: activeColorField
     * tel: activeTelField
     * search: activeSearchField
     * @var array
     */
    protected $_options = array();
    public $layout = 'default';
    public $modelClass;
    public $shortCode;

    public function getOptions() {
        if ($this->modelClass && class_exists($this->modelClass)) {
            foreach ($this->_options as &$option) {
                if (isset($option['name']) && $option['name'] === 'model_id') {
                    $items = CActiveRecord::model($this->modelClass)->findAll();
                    $items = CHtml::listData($items, 'id', CActiveRecord::model($this->modelClass)->hasAttribute('title') ? 'title' : 'id');
                    if ($items && count($items)) {
                        $option['items'] = $items;
                        $option['type'] = 'dropdownlist';
                        $option['empty'] = '';
                    }
                }
            }
        }

        return $this->_options;
    }

    public function setOptions($userOptions) {
        $defaultOptions = $this->array_column($this->_options, 'name', 'value', 'options');
        $this->_options = array_merge($defaultOptions, $userOptions);
        if (is_array($defaultOptions)) {
            foreach ($defaultOptions as $option) {
                if (isset($option['name'])) {
                    $key = $option['name'];
                    $defaultValue = NULL;
                    if (isset($option['value'])) {
                        $defaultValue = $option['value'];
                    } else {
                        if (isset($option['options']) && count($option['options'])) {
                            $defaultValue = array_keys($option['options']);
                        } else {
                            
                        }
                    }

                    if (in_array($key, $userOptions)) {
                        $defaultValue = $userOptions[$key];
                    }

//		$this->_options[$option['name']] = '';
                }
            }
        }
    }

    public function init() {
        if (isset($this->_options['layout']) && $this->getViewFile($this->shortCode . '/' . $this->_options['layout'])) {
            $this->layout = $this->_options['layout'];
        }


        if (isset($this->_options['model_class']) && class_exists($this->_options['model_class'])) {
            $this->modelClass = $this->_options['model_class'];
        }
    }

    public function run() {
        if (isset($this->_options['model_id'])) {
            $slider = CActiveRecord::model($this->modelClass)->findByPk($this->_options['model_id']);
            if ($slider === NULL) {
                echo '<!-- NOT FOUND -->';
                return;
            }
            echo $this->render("{$this->shortCode}/{$this->layout}", array(
                'slider' => $slider,
                    ), true);
        } else {
            echo '<!--PARAM NOT SET-->';
        }
    }

    /**
     * returns the values from a single column of the array, identified by the column_key. Optionally, you may provide an index_key to index the values in the returned array by the values from the index_key column in the input array.
     * @param array $input A multi-dimensional array (record set) from which to pull a column of values.
     * @param type $indexKey The column to use as the index/keys for the returned array. This value may be the integer key of the column, or it may be the string key name.
     * @param type $columnKey The column of values to return. This value may be the integer key of the column you wish to retrieve, or it may be the string key name for an associative array. It may also be NULL to return complete arrays (useful together with index_key to reindex the array).
     * @param type $fallbackColumnKey
     * @param type $fallbackValue
     * @return array Returns an array of values representing a single column from the input array. 
     */
    private function array_column(array $input, $indexKey = null, $columnKey = NULL, $fallbackColumnKey = false, $fallbackValue = NULL) {
        $result = array();

        if (null === $indexKey) {
            if (null === $columnKey) {
                // trigger_error('What are you doing? Use array_values() instead!', E_USER_NOTICE);
                $result = array_values($input);
            } else {
                foreach ($input as $row) {
                    if (isset($row[$columnKey])) {
                        $result[] = $row[$columnKey];
                    } elseif (isset($row[$fallbackColumnKey])) {
                        $result[] = $row[$fallbackColumnKey];
                    } else {
                        $result[] = $fallbackValue;
                    }
                }
            }
        } else {
            if (null === $columnKey) {
                foreach ($input as $row) {
                    $result[$row[$indexKey]] = $row;
                }
            } else {
                foreach ($input as $row) {
                    if (isset($row[$columnKey])) {
                        $result[$row[$indexKey]] = $row[$columnKey];
                    } elseif (isset($row[$fallbackColumnKey])) {
                        $result[$row[$indexKey]] = $row[$fallbackColumnKey];
                    } else {
                        $result[$row[$indexKey]] = $fallbackValue;
                    }

                    if (is_array($result[$row[$indexKey]])) {
                        $result[$row[$indexKey]] = array_keys($result[$row[$indexKey]]);
                    }
                }
            }
        }

        return $result;
    }

}
