<?php

class User extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'thundersnarf_user':
	 * @var integer $id
	 * @var string $username
	 * @var string $password
	 * @var string $email
	 * @var string $profile
	 * @var integer $last_acess
	 * @var integer $last_ip
	 */
	 
	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_user';
	}

	/**
	 * @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('username, password, email', 'required'),
			array('last_acess', 'numerical', 'integerOnly'=>true),
			array('username, password, email', 'length', 'max'=>128),
			array('profile', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('ultimaSolicitacaoA, ultimaSolicitacaoB, id, username, password, email, profile, last_acess, last_ip', 'safe', 'on'=>'search'),
			array('password', 'passwordUsuario'),
		);
	}

	public function passwordUsuario($attribute){
		if($this->id=='' and trim($this->password)=='d41d8cd98f00b204e9800998ecf8427e'){
			$this->addError('password','Password não pode ser vazio.');
		}
	}

	/**
	 * @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(
		);
	}

        /**
	 * Prepares attributes before performing validation.
	 */
	protected function beforeValidate()
	{
            if($this->isNewRecord)
                $this->last_acess=time();
            return true;
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Id',
			'username' => 'Nome',
			'password' => 'Senha',
			'email' => 'Email',
			'profile' => 'Perfil',
			'last_acess' => 'Data de Último Acesso',
			'last_ip' => 'IP do Último Acesso',
			'ultimaSolicitacaoA' => 'Último Acesso',
		);
	}

	/**
	 * 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['User'])){
			foreach($this->getAttributes() as $key=>$value){
				$this->setAttribute($key,'');	
			}
		}else{
			$dataA = date_parse($_GET['User']['ultimaSolicitacaoA']);
			$dataB = date_parse($_GET['User']['ultimaSolicitacaoB']);
			if(checkdate($dataA['month'], $dataA['day'], $dataA['year']) AND checkdate($dataB['month'], $dataB['day'], $dataB['year']) ){
				$criteria->condition = 'last_acess 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_acess >= '.mktime('0', '0', '1', $dataA['month'], $dataA['day'], $dataA['year']).'';				
			}else if(checkdate($dataB['month'], $dataB['day'], $dataB['year'])){
				$criteria->condition = 'last_acess <= '.mktime('23', '59', '59', $dataB['month'], $dataB['day'], $dataB['year']).'';				
			}
		}

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

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

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

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

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

		$criteria->compare('last_ip',$this->last_ip);
		
		return new CActiveDataProvider('User', array(
			'criteria'=>$criteria,
			'pagination'=>array(
		      'pageSize'=> Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']),
		    ),
		));
	}
}