<?php

/**
 * This is the model class for table "tbl_candidatos_documentos".
 *
 * The followings are the available columns in table 'tbl_candidatos_documentos':
 * @property integer $id
 * @property integer $candidato_id
 * @property datetime $fecha
 * @property string $tipo
 * @property string $nombre_fichero 
 *
 * The followings are the available model relations:
 * @property Candidatos $candidato
 */
class CandidatoDocumento extends CActiveRecord
{
        public $url;
    
        const TIPO_INTERNO='Interno';
        const TIPO_COMERCIAL='Comercial';    
    
        /**
         * Devuelve la lista de tipos de documento.
         * @return array lista de tipos
         */
        public function getOpcionesTipo() {
            return array(
                self::TIPO_INTERNO => 'Interno',
                self::TIPO_COMERCIAL => 'Comercial'
            );
        }   
        
        public function getReadableFileSize($retstring = null) {
                // adapted from code at http://aidanlister.com/repos/v/function.size_readable.php
                $sizes = array('bytes', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');

                if ($retstring === null) { $retstring = '%01.2f %s'; }

                $lastsizestring = end($sizes);

                foreach ($sizes as $sizestring) {
                        if ($this->size < 1024) { break; }
                        if ($sizestring != $lastsizestring) { $this->size /= 1024; }
                }
                if ($sizestring == $sizes[0]) { $retstring = '%01d %s'; } // Bytes aren't normally fractional
                return sprintf($retstring, $this->size, $sizestring);
        }       
        
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return CandidatoDocumento 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_candidatos_documentos';
	}

	/**
	 * @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('candidato_id', 'required'),
			array('candidato_id', 'numerical', 'integerOnly'=>true),
			array('tipo, nombre_fichero', 'length', 'max'=>255),
			array('fecha', 'safe'),
                    
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, candidato_id, fecha, tipo, nombre_fichero', 'safe', 'on'=>'search'),
		);
	}
        
        
        protected function afterConstruct() {
            // Valores por defecto
            $this->tipo = self::TIPO_INTERNO;
            $this->fecha = date('Y-m-d H:i:s', time());
            parent::afterConstruct();
        }        
        
        
	/**
	 * @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(
			'candidato' => array(self::BELONGS_TO, 'Candidatos', 'candidato_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'candidato_id' => 'Candidato',
			'fecha' => 'Fecha',
			'tipo' => 'Tipo',
			'nombre_fichero' => 'Nombre',
                        'file' => 'Fichero',
		);
	}

	/**
	 * 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->compare('id',$this->id);
		$criteria->compare('candidato_id',$this->candidato_id);
		$criteria->compare('fecha',$this->fecha,true);
		$criteria->compare('tipo',$this->tipo,true);
		$criteria->compare('nombre_fichero',$this->nombre_fichero,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        public function behaviors() { 
            return array( 
                'LoggableBehavior'=> 'application.modules.auditTrail.behaviors.LoggableBehavior', 
            ); 
        }
}