<?php

 class Comentarios extends GxActiveRecord {

	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	public function tableName() {
		return 'comentarios';
	}

	public static function label($n = 1) {
		return Yii::t('app', 'Comentário|Comentários', $n);
	}

	public static function representingColumn() {
		return 'CMT_DATA';
	}

	public function behaviors() {
		return array('datetimeI18NBehavior' => array('class' => 'ext.DateTimeI18NBehavior'));
	}

	public function rules() {
		return array(
			array('CMT_AUTOR, CMT_DATA, CMT_OBS', 'required'),
			array('CMT_AUTOR', 'numerical', 'integerOnly' => true),
			array('CMT_ID, CMT_AUTOR, CMT_DATA, CMT_OBS', 'safe', 'on' => 'search'),
		);
	}

	public function relations() {
		return array(
			'Autor' => array(self::BELONGS_TO, 'Funcionarios', 'CMT_AUTOR'),
		);
	}

	public function pivotModels() {
		return array(
		);
	}

	public function attributeLabels() {
		return array(
			'CMT_ID'     => 'Código',
			'CMT_AUTOR'  => 'Autor',
			'CMT_DATA'   => 'Data',
			'CMT_OBS'    => 'Comentário',
			'Autor'      => 'Autor',
		);
	}

	public function beforeSave() {
		parent::beforeSave();

		if (strpos($this->CMT_DATA, '/') !== false) {
			$this->CMT_DATA = Util::formatDataHoraMysql($this->CMT_DATA);
		}
		return true;
	}
	public function beforeValidate() {
		parent::beforeValidate();
		if($this->isNewRecord) {
			$this->CMT_AUTOR = User::getId();
			$this->CMT_DATA = Date('d/m/Y H:i:s');
		}
		return true;
	}


	public function search() {
		$criteria = new CDbCriteria;

		$criteria->compare('CMT_ID', $this->CMT_ID);
		$criteria->compare('CMT_AUTOR', $this->CMT_AUTOR);
		$criteria->compare('CMT_DATA', $this->CMT_DATA);
		$criteria->compare('CMT_OBS', $this->CMT_OBS, true);
		$criteria->compare('CMT_FOREIGNKEY', $this->CMT_FOREIGNKEY);

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

}