<?php

/**
 * This is the model class for table "{{category}}".
 *
 * The followings are the available columns in table '{{category}}':
 * @property integer $id
 * @property string $category_name
 * @property string $isActive
 * @property integer $created_by
 * @property integer $updated_by
 * @property string $created_datetime
 * @property string $updated_datetime
 */

class Category extends CActiveRecord
{
    public function tableName()
    {
        return '{{category}}';
    }


    public function rules()
    {
        return array(

            // ONLY CATEGORY REQUIRED
            array('category_name', 'required'),

            // UNIQUE LIKE EXPENSE TYPE
            array('category_name', 'unique'),

            array('created_by, updated_by', 'numerical', 'integerOnly'=>true),

            array('category_name', 'length', 'max'=>255),

            array('isActive', 'length', 'max'=>1),

            array(
                'id, category_name, isActive, created_by, updated_by, created_datetime, updated_datetime',
                'safe',
                'on'=>'search'
            ),
        );
    }


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


    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'category_name' => 'Category',
            'isActive' => 'Is Active',
            'created_by' => 'Created By',
            'updated_by' => 'Updated By',
            'created_datetime' => 'Created Datetime',
            'updated_datetime' => 'Updated Datetime',
        );
    }


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

        $criteria->compare('id',$this->id);
        $criteria->compare('category_name',$this->category_name,true);
        $criteria->compare('isActive',$this->isActive,true);
        $criteria->compare('created_by',$this->created_by);
        $criteria->compare('updated_by',$this->updated_by);
        $criteria->compare('created_datetime',$this->created_datetime,true);
        $criteria->compare('updated_datetime',$this->updated_datetime,true);

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


    // LIKE getExpenseTypes()
    public static function getCategories()
    {
        $condition = 'isActive = "Y"';

        $model = self::model()->findAll(array(
            'condition' => $condition,
            'order'     => 'category_name ASC'
        ));

        return CHtml::listData(
            $model,
            'id',
            'category_name'
        );
    }


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