<?php

namespace common\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\Query;
use common\components\Helper;

/**
 * This is the model class for table "key".
 *
 * @property integer $id_key
 * @property string $number
 * @property integer $status
 * @property integer $type
 * @property string $created_at
 * @property string $updated_at
 */
class Key extends \yii\db\ActiveRecord
{
	const STATUS_DELETED = 0;
	const STATUS_ACTIVE = 10;
	const TYPE_NORMAL = 10;
	const TYPE_DEFAULT = self::TYPE_NORMAL;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'key';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['status', 'type'], 'integer'], //csak szám
            //[['created_at', 'updated_at'], 'required'],//kötelezőek
            //[['created_at', 'updated_at'], 'safe'], //bármi lehet
            [['number'], 'string', 'max' => 255],
            [['rfid_key'], 'string', 'max' => 25],
            [['number' ], 'unique'],
        ];
    }
    
    
    public function behaviors()
    {
     return [
		   [ 'class' => TimestampBehavior::className(), //mentés előtt kitölti a save methódus meghívása előtt kitölti a created... mezőket
			 'value' => function(){ return date('Y-m-d H:i:s' ); }
		   ]
       ];
    }

    /**
     * @inheritdoc   Minden modelnak van egy Attribut labels függvénye ami modelhez tartalmazza a fordítások
     */
    public function attributeLabels()
    {
        return [
            'id_key' => Yii::t('common/key', 'Id Key'),
            'number' => Yii::t('common/key', 'Number'),
            'rfid_key' => Yii::t('common/key', 'RFID key'),
            'status' => Yii::t('common/key', 'Status'),
            'type' => Yii::t('common/key', 'Type'),
            'created_at' => Yii::t('common/key', 'Created At'),
            'updated_at' => Yii::t('common/key', 'Updated At'),
        ];
    }
    
    static function statuses() {
		return [
			self::STATUS_ACTIVE => Yii::t('common/key', 'Active'),	// t - translate  a key a kategoria common/messages/hu/common/key.php mappa   (létre kell hozni a fájlt)
			self::STATUS_DELETED => Yii::t('common/key', 'Inactive'),	
		];	
    }
    
    public static function types(){
		return [
			self::TYPE_NORMAL => Yii::t('common/key', 'Key'),	
		];
	}
	
	public function getHumanType(){
		$result = "";
		$types = self::types();
		if ( array_key_exists($this->type, $types) ){
			$result = $types[$this->type];
		}
		return $result;
	}
	

	
	/**
	 * @param yii\db\Query $query
	 * */
	public static function addKeyCondition($query, $number , $fieldNumber = "key.number",$fieldRfid = "key.rfid_key"){
		$number = Helper::fixAsciiChars($number);
		$query->andWhere( ['or' , [$fieldNumber =>$number ],[$fieldRfid =>$number ] ]);
	}
	/**
	 * @param yii\db\Query $query
	 * */
	public static function addKeyConditionOptional($query, $number , $fieldNumber = "key.number",$fieldRfid = "key.rfid_key"){
		$number = Helper::fixAsciiChars($number);
		$query->andFilterWhere( ['or' , [$fieldNumber =>$number ],[$fieldRfid =>$number ] ]);
	}
	
}
