<?php

/**
 * This is the model class for table "tbl_usuarios".
 *
 * The followings are the available columns in table 'tbl_usuarios':
 * @property integer $id
 * @property integer $id_empresa
 * @property string $estado
 * @property string $email
 * @property string $nombre
 * @property string $apellidos
 * @property string $password
 * @property string $tipo
 * @property string $titulo
 * @property text $descripcion
 * @property string $localidad
 * @property string $telefono
 * @property string $last_login_time
 * @property string $clave_seguridad
 *
 * The followings are the available model relations:
 * @property Empresa $empresa
 */
class Usuario extends CActiveRecord {

    const ESTADO_NOACTIVO = 0;
    const ESTADO_ACTIVO = 1;
    const ESTADO_DENEGADO = 2;

    //public $password_repeat;
    //public $empresa;

    public function getEstaActivo() {
        return ($this->estado == self::ESTADO_ACTIVO);
    }
    
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Usuario 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 'tbl_usuarios';
    }

    /**
     * @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('email, nombre, apellidos, titulo, localidad, telefono', 'length', 'max' => 255),
            array('email', 'required'),
            array('email', 'email'),
            array('email', 'unique'),
            
            array('password', 'required', 'on' => 'insert'),
            
            array('id, id_empresa, email, nombre, apellidos, titulo, localidad, telefono', '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(
                'empresa' => array(self::HAS_ONE, 'Empresa', 'id'),
        );
    }
    
    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'email' => 'Email',
            'estado' => 'Estado',
            'password' => 'Contraseña',
            'password_repeat' => 'Confirmar contraseña',
            'last_login_time' => 'Último acceso',
            'nombre' => 'Nombre',
            'apellidos' => 'Apellidos',
            'titulo' => 'Titulo',
            'localidad' => 'Localidad',
            'telefono' => 'Teléfono',
            'descripcion' => 'Descripción',
        );
    }

    public function scopes() {
        return array(
            'activo' => array(
                'condition' => 'estado=' . self::ESTADO_ACTIVO
            ),
            'noactivo' => array(
                'condition' => 'estado=' . self::ESTADO_NOACTIVO
            ),
            'denegado' => array(
                'condition' => 'estado=' . self::ESTADO_DENEGADO
            )
        );
    }

    /**
     * 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() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;
        //$criteria->with = array('empresa');
        //$criteria->together = true;

        $criteria->compare('t.email', $this->email, true);
        $criteria->compare('t.id_empresa', $this->id_empresa, true);
        $criteria->compare('t.nombre', $this->nombre, true);
        $criteria->compare('t.apellidos', $this->apellidos, true);
        $criteria->compare('t.last_login_time', $this->last_login_time, true);

        $sort = new CSort;
        $sort->defaultOrder = 't.apellidos, t.nombre ASC';

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                    'sort' => $sort,
                ));
    }

    public function encrypt($value) {
        return md5($value);
    }

}
