<?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 Ip extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'thunder_ip':
	 * @var integer $id
	 * @var string $ip
	 * @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_ip';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// will receive user inputs.
		return array(
			array('last_requested, cache_static, cache_dynamic, traffic_static, traffic_dynamic, hit_static, hit_dynamic, miss_static, miss_dynamic', 'numerical', 'integerOnly'=>true),
			array('ip', 'length', 'max'=>15),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('requested, ultimaSolicitacaoA, ultimaSolicitacaoB, id, ip, 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(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Id',
			'ip' => 'IP',
			'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ático',
			'hit_dynamic' => 'Hit Dinâmico',
			'miss_static' => 'Miss Estático',
			'miss_dynamic' => 'Miss Dinâmico',
			'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['Ip'])){
			foreach($this->getAttributes() as $key=>$value){
				$this->setAttribute($key,'');	
			}
		}else{
			$dataA = date_parse($_GET['Ip']['ultimaSolicitacaoA']);
			$dataB = date_parse($_GET['Ip']['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']).'';				
			}
		}
		
		$criteria->compare('id',$this->id);

		$criteria->compare('ip',$this->ip,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('Ip', array(
			'criteria'=>$criteria,
    		'sort'=>$sort,
			'pagination'=>array(
		      'pageSize'=> Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']),
		    ),
		));
	}
}