<?php

/**
 * This is the model class for table "sidcai_tipoacceso".
 *
 * The followings are the available columns in table 'sidcai_tipoacceso':
 * @property integer $tipo_codigo_pk
 * @property string $tipo_descripcion
 * @property string $tipo_habilitado
 * @property string $tipo_visible
 *
 * The followings are the available model relations:
 * @property SidcaiOpcion[] $sidcaiOpcions
 * @property SidcaiOpcionPerfil[] $sidcaiOpcionPerfils
 * @property SidcaiPerfil[] $sidcaiPerfils
 * @property SidcaiUsuario[] $sidcaiUsuarios
 */
class SidcaiTipoacceso extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'sidcai_tipoacceso';
	}

	/**
	 * @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('tipo_codigo_pk, tipo_descripcion', 'required'),
			array('tipo_codigo_pk', 'numerical', 'integerOnly'=>true),
			array('tipo_habilitado, tipo_visible', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('tipo_codigo_pk, tipo_descripcion, tipo_habilitado, tipo_visible', '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(
			'sidcaiOpcions' => array(self::HAS_MANY, 'SidcaiOpcion', 'tipo_codigo_fk'),
			'sidcaiOpcionPerfils' => array(self::HAS_MANY, 'SidcaiOpcionPerfil', 'tipo_codigo_fk'),
			'sidcaiPerfils' => array(self::HAS_MANY, 'SidcaiPerfil', 'tipo_codigo_fk'),
			'sidcaiUsuarios' => array(self::HAS_MANY, 'SidcaiUsuario', 'tipo_codigo_fk'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'tipo_codigo_pk' => 'Tipo Codigo Pk',
			'tipo_descripcion' => 'Tipo Descripcion',
			'tipo_habilitado' => 'Tipo Habilitado',
			'tipo_visible' => 'Tipo Visible',
		);
	}

	/**
	 * 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('tipo_codigo_pk',$this->tipo_codigo_pk);
		$criteria->compare('tipo_descripcion',$this->tipo_descripcion,true);
		$criteria->compare('tipo_habilitado',$this->tipo_habilitado,true);
		$criteria->compare('tipo_visible',$this->tipo_visible,true);

		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 SidcaiTipoacceso the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public static function getAcceso(){
		$accesos = SidcaiTipoacceso::model()->findAll('tipo_habilitado = :tipo_habilitado', array(':tipo_habilitado' => true));

		return CHtml::listData($accesos, 'tipo_codigo_pk', 'tipo_descripcion');
	}
}
