<?php
/**
 * ThunderSnarf
 *
 * @author Ricardo H.F. Sette P. <ricardohfsp@gmail.com>
 * @link http://www.freebsdbrasil.com.br/
 * @copyright Copyright &copy; 2010 - FreeBSD Brasil LTDA
 * @license licença GNU/GPL v2.0 http://www.gnu.org/licenses/gpl-2.0.html
 */
?>
<?php

class Domain extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'thunder_domain':
	 * @var integer $id
	 * @var string $domain
	 * @var string $createdate
	 * @var integer $cache_static
	 * @var integer $cache_dynamic
	 * @var integer $traffic_static
	 * @var integer $traffic_dynamic
	 * @var integer $hit_static
	 * @var integer $hit_dynamic
	 * @var integer $miss_static
	 * @var integer $miss_dynamic
	 */
	
	public $ultimaSolicitacaoA;
	public $ultimaSolicitacaoB;

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'thundersnarf_domain';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('domain, createdate', 'required'),
			array('last_requested, cache_static, cache_dynamic, traffic_static, traffic_dynamic, hit_static, hit_dynamic, miss_static, miss_dynamic', 'numerical', 'integerOnly'=>true),
			array('domain', 'length', 'max'=>300),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('requested, ultimaSolicitacaoA, ultimaSolicitacaoB, id, domain, cache_static, cache_dynamic, traffic_static, traffic_dynamic, hit_static, hit_dynamic, miss_static, miss_dynamic', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'files' => array(self::HAS_MANY, 'File', 'domain_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Id',
			'domain' => 'Domínio',
			'createdate' => 'Primeira Solicitação',
			'ultimaSolicitacaoA' => 'Última Solicitação',
			'ultimaSolicitacaoB' => 'Última Solicitação',
			'cache_static' => 'Cache Estático',
			'cache_dynamic' => 'Cache Dinâmico',
			'traffic_static' => 'Internet Estática',
			'traffic_dynamic' => 'Internet Dinâmica',
			'hit_static' => 'Hit Estática',
			'hit_dynamic' => 'Hit Dinâmica',
			'miss_static' => 'Miss Estática',
			'miss_dynamic' => 'Miss Dinâmica',
			'last_requested' => 'Última Solicitação',
			'requested' => 'Número de Requisições',
		);
	}
	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		if (isset($_GET['pageSize'])) {
		    Yii::app()->user->setState('pageSize',(int)$_GET['pageSize']);
		    unset($_GET['pageSize']);
		}
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		if(!isset($_GET['Domain'])){
			foreach($this->getAttributes() as $key=>$value){
				$this->setAttribute($key,'');	
			}
		}else{
			$dataA = date_parse($_GET['Domain']['ultimaSolicitacaoA']);
			$dataB = date_parse($_GET['Domain']['ultimaSolicitacaoB']);
			if(checkdate($dataA['month'], $dataA['day'], $dataA['year']) AND checkdate($dataB['month'], $dataB['day'], $dataB['year']) ){
				$criteria->condition = 'last_requested BETWEEN '.mktime('0', '0', '1', $dataA['month'], $dataA['day'], $dataA['year']).' AND '.mktime('23', '59', '59', $dataB['month'], $dataB['day'], $dataB['year']).'';
			}else if(checkdate($dataA['month'], $dataA['day'], $dataA['year'])){
				$criteria->condition = 'last_requested >= '.mktime('0', '0', '1', $dataA['month'], $dataA['day'], $dataA['year']).'';				
			}else if(checkdate($dataB['month'], $dataB['day'], $dataB['year'])){
				$criteria->condition = 'last_requested <= '.mktime('23', '59', '59', $dataB['month'], $dataB['day'], $dataB['year']).'';				
			}
		}
		
		if(isset($_GET["tipoTrafego"]) and $_GET["tipoTrafego"]=="P"){
			foreach(Yii::app()->user->getState('thunderplugin',array("vazio")) as $key=>$value){
				$criteria->addSearchCondition('domain',$value,false,'OR','=');
			}
		}
		
		$criteria->compare('id',$this->id);

		$criteria->compare('domain',$this->domain,true);

		$criteria->compare('requested',$this->requested);

		$criteria->compare('cache_static',$this->cache_static);

		$criteria->compare('cache_dynamic',$this->cache_dynamic);

		$criteria->compare('traffic_static',$this->traffic_static);

		$criteria->compare('traffic_dynamic',$this->traffic_dynamic);

		$criteria->compare('hit_static',$this->hit_static);

		$criteria->compare('hit_dynamic',$this->hit_dynamic);

		$criteria->compare('miss_static',$this->miss_static);

		$criteria->compare('miss_dynamic',$this->miss_dynamic);
		
		$sort = new CSort();
		if(isset($_GET["tipoTrafego"]) and $_GET["tipoTrafego"]=="E"){
			$sort->defaultOrder = 'cache_static DESC';
			if(!isset($_GET['sort'])) $_GET['sort'] = 'cache_static.desc';
		}else if(isset($_GET["tipoTrafego"]) and $_GET["tipoTrafego"]=="D"){
			$sort->defaultOrder = 'cache_dynamic DESC';
			if(!isset($_GET['sort'])) $_GET['sort'] = 'cache_dynamic.desc';
		}else{
			$sort->defaultOrder = 'requested DESC';
			if(!isset($_GET['sort'])) $_GET['sort'] = 'requested.desc';
		}
		
		return new CActiveDataProvider('Domain', array(
			'criteria'=>$criteria,
			'sort'=>$sort,
			'pagination'=>array(
		      'pageSize'=> Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']),
		    ),
		));
	}
}