<?php

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

    protected $_options = array(
        array(
            'name' => 'tabber_id',
            'type' => 'text',
            'label' => 'Tabber ID',
            'value' => 0,
        ),
        array(
            'name' => 'direction',
            'label' => 'Tabber Direction',
            'type' => 'dropdownlist',
            'items' => array('vertical' => 'Vertical', 'horizontal' => 'Horizontal'),
            'options' => array(
                'horizontal' => array('selected' => 'selected'),
            ),
        ),
    );

    public function run() {

        if (isset($this->_options['tabber_id'])) {

            $tabber = Tabber::model()->findByPk($this->_options['tabber_id']);

            if ($tabber === NULL) {
                echo '<!-- NOT FOUND -->';
                return;
            }

            if (isset($this->_options['layout']) && $this->getViewFile('ftabber/' . $this->_options['layout'])) {
                $this->layout = $this->_options['layout'];
            } else {
                echo '<!-- LAYOUT NOT FOUND -->';
            }


            $tabs = array();
            for ($index = 0; $index < count($tabber->tabberTabs); $index++) {
                $tab = $tabber->tabberTabs[$index];

                $id = "tab-{$index}";
                $tabs[$id] = array(
                    'title' => $tab->title,
                    'content' => ShortCodeManager::parse($tab->content),
                );
            }

            $htmlOptions = array();
            if (isset($this->_options['direction']))
                $htmlOptions['class'] = "yiiTab direction-{$this->_options['direction']}";

            $this->widget('ext.widgets.FTabView', array(
                'tabs' => $tabs,
                'htmlOptions' => $htmlOptions,
            ));
        } else {
            echo '<!--PARAM NOT SET-->';
        }
    }

}
