Host.php 2.55 KB
<?php

namespace app\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\Expression;

/**
 * This is the model class for table "{{%host}}".
 *
 * @property int $id
 * @property string $domain Домен
 * @property string|null $created_at Дата записи
 * @property string|null $csv_date Дата CSV
 * @property string|null $wis_date Дата проверки домена
 * @property int|null $wis_status Статус
 * @property int $tix Показатель
 */
class Host extends \yii\db\ActiveRecord
{
    const STATUS_NONE = 0; //неизвестный
    const STATUS_FREE = 1; //Свободный
    const STATUS_BUSY = 2; //Занятый


    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%host}}';
    }

    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'createdAtAttribute' => 'created_at',
                'updatedAtAttribute' => null,
                'value' => new Expression('NOW()'),
            ],
        ];

    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['domain'], 'required'],
            ['status_id', 'in', 'range' => array_keys(self::getStatus())],
            [['created_at', 'csv_date', 'wis_date'], 'safe'],
            [['wis_status', 'tix'], 'integer'],
            [['domain'], 'string', 'max' => 256],
            [['domain'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'domain' => 'Домен',
            'created_at' => 'Дата записи',
            'csv_date' => 'Дата CSV',
            'wis_date' => 'Дата проверки домена',
            'wis_status' => 'Статус',
            'tix' => 'Показатель',
        ];
    }

    /**
     * {@inheritdoc}
     * @return HostQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new HostQuery(get_called_class());
    }


    public static function getStatus($status = false)
    {
        $arr = [
            self::STATUS_NONE => 'Неизвестный',
            self::STATUS_FREE => 'Свободный',
            self::STATUS_BUSY => 'Занятый',
        ];
        if (is_numeric($status)) {
            if (array_key_exists($status, $arr)) {
                return $arr[$status];
            }
            return $status;
        }
        return $arr;
    }
}