<?php

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

    protected $_options = array(
        array(
            'name' => 'order',
            'type' => 'text',
            'label' => 'News Order',
            'value' => 'id DESC',
        ),
        array(
            'name' => 'limit',
            'type' => 'text',
            'label' => 'Number Of Items to Display',
            'value' => 7,
        ),
    );

    public function run() {

        if (isset($this->_options['order']) && isset($this->_options['limit'])) {
            $criteria = new CDbCriteria();
            if (intval($this->_options['limit'])) {
                $criteria->limit = $this->_options['limit'] * 1;
            }

            $criteria->order = $this->_options['order'];
            $newslist = Article::model()->findAll($criteria);

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

            if (isset($this->_options['layout']) && $this->getViewFile('newslist/' . $this->_options['layout'])) {
                $this->layout = $this->_options['layout'];
            } else {
                echo '<!-- LAYOUT NOT FOUND -->';
            }
            echo $this->render("newslist/{$this->layout}", array('newslist' => $newslist,), true);
        } else {
            echo '<!-- PARAMS NOT SET WELL -->';
        }
    }

}
