<?php

/**
 * This is the model class for table "ref_department".
 *
 * The followings are the available columns in table 'ref_department':
 * @property integer $id_department
 * @property string $name
 * @property string $leader
 * @property string $address
 * @property string $email
 * @property string $image
 * @property string $website
 * @property integer $views
 * @property integer $status
 * @property integer $type
 * @property integer $created_id
 * @property string $created_date
 * @property integer $update_id
 * @property string $update_date
 */
class Department extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'ref_department';
	}

	/**
	 * @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('views, status, type, created_id, update_id', 'numerical', 'integerOnly'=>true),
			array('name, image', 'length', 'max'=>255),
			array('leader, website', 'length', 'max'=>100),
			array('email', 'length', 'max'=>50),
			array('address, created_date, update_date', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id_department, name, leader, address, email, image, website, views, status, type, created_id, created_date, update_id, update_date', '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(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_department' => 'Id Department',
			'name' => 'Nama Unit Kerja',
			'leader' => 'Nama Pimpinan',
			'address' => 'Alamat',
			'website' => 'Situs Website',
			'email' => 'ALamat Email',
			'status' => 'Status',
			'type' => 'Type',
			'created_id' => 'Created',
			'created_date' => 'Created Date',
			'update_id' => 'Update',
			'update_date' => 'Update Date',
		);
	}


	/**
	 * 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_department',$this->id_department);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('leader',$this->leader,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('image',$this->image,true);
		$criteria->compare('website',$this->website,true);
		$criteria->compare('views',$this->views);
		$criteria->compare('status',$this->status);
		$criteria->compare('type',$this->type);
		$criteria->compare('created_id',$this->created_id);
		$criteria->compare('created_date',$this->created_date,true);
		$criteria->compare('update_id',$this->update_id);
		$criteria->compare('update_date',$this->update_date,true);

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

	/**
	 * @return CDbConnection the database connection used for this class
	 */

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

	public static function getAll(){
		$sql = "SELECT * from ref_department WHERE status=1 AND type=1 OR type=3 ORDER BY name";
		$command = YII::app()->db_ppid->createCommand($sql);
		return $command->queryAll();
	}

	public function getName($id){
		$data = self::model()->findBypk($id);
		if($data===null){
			return "-";
		}else{
			return $data->name;
		}
	}

	public function type($data){
		if($data==1){
			return "Perangkat Daerah";
		}elseif($data==2){
			return "BLUD";
		}elseif($data==3){
			return "BUMD";
		}elseif($data==3){
			return "Lainnya";
		}else{
			return "-";
		}
	}


	// public static function rankDepartment(){

	// 	$sql = "SELECT
	// 	b.name as name,
	// 	count( id_innovation ) as total
	// 	FROM
	// 	innovation AS a
	// 	LEFT JOIN ref_department AS b ON b.id_department = a.id_department
	// 	GROUP BY
	// 	b.name
	// 	ORDER BY
	// 	count( id_innovation ) DESC
	// 	";
	// 	$command = YII::app()->db_ppid->createCommand($sql);
	// 	return $command->queryAll();
	// }

	public static function rankDepartment(){

		$sql = "SELECT
		id_department
		FROM
		innovation
		ORDER BY
		id_department DESC
		";
		$command = YII::app()->db->createCommand($sql);
		return $command->queryAll();
	}

	public static function totalDepartment($id){

		$sql = "SELECT
		count(id_innovation) as total
		FROM
		innovation
		WHERE
		id_department =".$id;
		$command = YII::app()->db->createCommand($sql);
		return $command->queryScalar();
	}
}
