<?php

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

    protected $_options = array(
        array(
            'name' => 'order',
            'label' => 'Order',
            'value' => 'create_datetime desc',
            'type' => 'dropdownlist',
            'items' => array(
                'id DESC' => 'ID Descending',
                'id ASC' => 'ID Ascending',
                'title ASC' => 'Title Descending',
                'title DESC' => 'Title Ascending',
                'create_datetime ASC' => 'Date Ascending',
                'create_datetime DESC' => 'Date Descending',
            ),
        ),
        array(
            'name' => 'exclude',
            'label' => 'Excluded Categories',
            'value' => '',
            'multiple' => true,
            'type' => 'dropdownlist',
            'items' => array(),
        ),
        array(
            'name' => 'include',
            'label' => 'Included Categories',
            'value' => '',
            'multiple' => true,
            'type' => 'dropdownlist',
            'items' => array(),
        ),
        array(
            'name' => 'pagesize',
            'label' => 'Page Size',
            'value' => 10,
        ),
    );
    protected $_categories;

    public function run() {

        $config = array();
        $criteria = new CDbCriteria();


        $criteria->scopes = array('published', 'filterLanguage');


        if (isset($this->_options['exclude']) && is_array($this->_options['exclude']) && count($this->_options['exclude'])) {


            $criteria->together = true;
            $criteria->with = array('categories' => array('select' => 'id'));
            $criteria->addNotInCondition('categories.id', $this->_options['exclude']);
        }

        if (isset($this->_options['include']) && is_array($this->_options['include']) && count($this->_options['include'])) {


            $criteria->together = true;
            $criteria->with = array('categories' => array('select' => 'id'));
            $criteria->addInCondition('categories.id', $this->_options['include']);
        }

        if (isset($this->_options['order'])) {
            $criteria->order = "t.featured DESC, t." . ltrim($this->_options['order'], "t.");
        }

        $config['criteria'] = $criteria;
        $config['pagination'] = array(
            'pageSize' => isset($this->_options['pagesize']) ? intval($this->_options['pagesize']) : 10,
        );

        return $config;
    }

    public function getOptions() {

        $options = parent::getOptions();

        if ($this->_categories === NULL) {
            $this->_categories = CHtml::listData(Category::model()->findAll(), 'id', 'title');
        }
        foreach ($options as &$option) {
            if (isset($option['name']) && $option['name'] === 'exclude') {
                if (is_array($this->_categories)) {
                    $option['items'] = $this->_categories;
                }
            }
        }
        foreach ($options as &$option) {
            if (isset($option['name']) && $option['name'] === 'include') {
                if (is_array($this->_categories)) {
                    $option['items'] = $this->_categories;
                }
            }
        }


        return $options;
    }

}
