<?php

/**
 * This is the model class for table "nota".
 *
 * The followings are the available columns in table 'nota':
 * @property integer $id
 * @property string $titulo
 * @property string $contenido
 * @property string $fecha
 * @property integer $id_usuario
 */
class Nota extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'nota';
	}

	/**
	 * @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('titulo', 'required'),
			array('id_usuario', 'numerical', 'integerOnly'=>true),
                    array('id_categoria', 'numerical', 'integerOnly'=>true),
			array('titulo', 'length', 'max'=>255),
                    	//array('imagen',  'file', 'types'=>'jpg, gif, png'),
                    array('imagen', 'file', 'allowEmpty'=>true, 'types'=>'jpg, gif, png'),
                   // array('imagen', 'file', 'allowEmpty'=>true,'types'=>'jpg, gif, png','on'=>'update'),
			array('contenido, fecha, imagen', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, titulo, contenido, fecha, id_usuario, id_categoria', '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(
                    'usuario' => array(self::BELONGS_TO, 'CrugeStoredUser', 'id_usuario'),
                     'categoria' => array(self::BELONGS_TO, 'Categoria', 'id_categoria'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'titulo' => 'Titulo',
			'contenido' => 'Contenido',
			'fecha' => 'Fecha',
			'id_usuario' => 'Id Usuario',
                    'id_categoria' => 'Id Categoria',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('titulo',$this->titulo,true);
		$criteria->compare('contenido',$this->contenido,true);
		$criteria->compare('fecha',$this->fecha,true);
		$criteria->compare('id_usuario',$this->id_usuario);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Nota the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
        
        public function beforeSave() {
    if ($this->isNewRecord){
        $this->fecha = new CDbExpression('NOW()');
        $this->id_usuario = Yii::app()->user->id;
         
    }
    
 
    return parent::beforeSave();
}
protected function afterFind(){
         $this->fecha= Yii::app()->dateFormatter->formatDateTime(
                             CDateTimeParser::parse($this->fecha, 'yyyy-MM-dd H:i:s'),'medium');
         

        //$this->hora_registro = Yii::app()->dateFormatter->formatDateTime($this->hora_registro, false, 'medium');
         return true;
}
}
