<?php

/**
 * This is the model class for table "user_member".
 *
 * The followings are the available columns in table 'user_member':
 * @property integer $id_member
 * @property string $create_time
 * @property string $update_time
 * @property string $visit_time
 * @property string $code
 * @property string $fullname
 * @property string $phone
 * @property string $email
 * @property string $image_identity
 * @property string $image_foto
 * @property integer $team_id
 */
class Member extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user_member';
	}

	/**
	 * @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('code, fullname, phone, email, image_identity, image_foto, image_sosmed', 'required', 'on' => 'create'),
			array('team_id', 'numerical', 'integerOnly' => true),
			array('code, phone', 'length', 'max' => 25),
			array('fullname, email, image_identity, image_foto, image_sosmed', 'length', 'max' => 255),
			array('create_time, update_time, visit_time', 'safe'),
			array('code', 'unique', 'on' => 'create'),
			array('email', 'unique', 'on' => 'create'),
			array('email', 'email', 'on' => 'create'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id_member, create_time, update_time, visit_time, code, fullname, phone, email, image_identity, image_foto, team_id', '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_member' => 'Id Member',
			'create_time' => 'Create Time',
			'update_time' => 'Update Time',
			'visit_time' => 'Visit Time',
			'code' => 'NIK e-KTP',
			'fullname' => 'Nama Lengkap',
			'phone' => 'No. Whatsapp',
			'email' => 'Alamat Email',
			'image_identity' => 'Gambar Foto KTP',
			'image_foto' => 'Gambar Pas Foto',
			'image_sosmed' => 'Gambar Bukti Follow Sosial Media',
			'team_id' => 'Team',
		);
	}

	/**
	 * 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_member', $this->id_member);
		$criteria->compare('create_time', $this->create_time, true);
		$criteria->compare('update_time', $this->update_time, true);
		$criteria->compare('visit_time', $this->visit_time, true);
		$criteria->compare('code', $this->code, true);
		$criteria->compare('fullname', $this->fullname, true);
		$criteria->compare('phone', $this->phone, true);
		$criteria->compare('email', $this->email, true);
		$criteria->compare('image_identity', $this->image_identity, true);
		$criteria->compare('image_foto', $this->image_foto, true);
		$criteria->compare('team_id', $this->team_id);

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

	public static function getCountMember()
	{
		$sql = "SELECT COUNT(id_member) as total FROM user_member WHERE team_id=" . YII::app()->user->id;
		$command = Yii::app()->db->createCommand($sql);
		return $command->queryScalar();
	}
}
