<?php

/**
 * This is the model base class for the table "noticias".
 * DO NOT MODIFY THIS FILE! It is automatically generated by giix.
 * If any changes are necessary, you must set or override the required
 * property or method in class "Noticias".
 *
 * Columns in table "noticias" available as properties of the model,
 * followed by relations of table "noticias" available as properties of the model.
 *
 * @property integer $NTS_IDNOTICIA
 * @property string $NTS_ASSUNTO
 * @property string $NTS_DATA
 * @property string $NTS_VALIDADE
 * @property string $NTS_DESCRICAO
 * @property integer $NTS_IDFUNCIONARIO
 *
 * @property Funcionarios $Autor
 */
class Noticias extends GxActiveRecord {

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

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

	public static function label($n = 1) {
		return Yii::t('app', 'Notícia|Notícias', $n);
	}

	public static function representingColumn() {
		return 'NTS_ASSUNTO';
	}
	
	public function behaviors() {
    	return array('datetimeI18NBehavior' => array('class' => 'ext.DateTimeI18NBehavior'));
	}
	
	public function rules() {
		return array(
			array('NTS_IDFUNCIONARIO', 'numerical', 'integerOnly'=>true),
			array('NTS_ASSUNTO,NTS_VALIDADE,NTS_DESCRICAO', 'required'),
			array('NTS_ASSUNTO', 'length', 'max'=>50),
			array('NTS_DATA, NTS_VALIDADE, NTS_DESCRICAO', 'safe'),
			array('NTS_ASSUNTO, NTS_DATA, NTS_VALIDADE, NTS_DESCRICAO, NTS_IDFUNCIONARIO', 'default', 'setOnEmpty' => true, 'value' => null),
			array('NTS_IDNOTICIA, NTS_ASSUNTO, NTS_DATA, NTS_VALIDADE, NTS_DESCRICAO, NTS_IDFUNCIONARIO', 'safe', 'on'=>'search'),
			array('NTS_DATA','default', 'value'=>date('d/m/Y'), 'setOnEmpty'=>false,'on'=>'insert'),
			array('NTS_IDFUNCIONARIO','default', 'value'=>User::getId(), 'setOnEmpty'=>false,'on'=>'insert'),

		);
	}

	public function relations() {
		return array(
			'Autor' => array(self::BELONGS_TO, 'Funcionarios', 'NTS_IDFUNCIONARIO', 'joinType'=>'LEFT JOIN'),
		);
	}

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

	public function attributeLabels() {
		return array(
			'NTS_IDNOTICIA' 	=> 'Código',
			'NTS_ASSUNTO' 		=> 'Assunto',
			'NTS_DATA' 			=> 'Data',
			'NTS_VALIDADE' 		=> 'Data de validade',
			'NTS_DESCRICAO' 	=> 'Descrição',
			'NTS_IDFUNCIONARIO' => 'Autor',
			'Autor' 			=> 'Autor',
		);
	}

	public function search() {
		$criteria = new CDbCriteria;
		
		$dtInicio = (empty($this->NTS_DATA)) ? '2001-01-01' : date('Y-m-d', CDateTimeParser::parse($this->NTS_DATA,'dd/MM/yyyy'));
		$dtFinal = (empty($this->NTS_VALIDADE)) ? '2050-12-12' : date('Y-m-d', CDateTimeParser::parse($this->NTS_VALIDADE,'dd/MM/yyyy'));
		$criteria->addBetweenCondition('NTS_DATA', $dtInicio, $dtFinal);
		
		$criteria->compare('NTS_IDNOTICIA', $this->NTS_IDNOTICIA);
		$criteria->compare('NTS_ASSUNTO', utf8_decode($this->NTS_ASSUNTO), true);
		$criteria->compare('NTS_DESCRICAO', utf8_decode($this->NTS_DESCRICAO), true);
		$criteria->compare('NTS_IDFUNCIONARIO', $this->NTS_IDFUNCIONARIO);

	 	$criteria->with = array(
        	'Autor' => array('joinType' => 'LEFT JOIN'),
        );
        $criteria->group = "t.NTS_IDNOTICIA";
        $sort = new CSort;
        $sort->defaultOrder = 'NTS_DATA DESC';
        $sort->attributes = array(
            'Autor'=>array(
              'asc'=>'Autor.FUN_NOME',
              'desc'=>'Autor.FUN_NOME desc',
            ),
            '*'
        );

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