Host.php 2.51 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','domain_expire'], 'safe'],
            [['wis_status', 'tix'], 'integer'],
            [['domain'], 'string', 'max' => 256],
            [['domain'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'domain' => 'Domain',
            'created_at' => 'Date',
            'csv_date' => 'Date CSV',
            'wis_date' => 'Date WHOIS',
            'wis_status' => 'Status',
            'domain_expire' => 'Date expired',
            'tix' => '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 => 'No checked',
            self::STATUS_FREE => 'FREE',
            self::STATUS_BUSY => 'BUSY',
        ];
        if (is_numeric($status)) {
            if (array_key_exists($status, $arr)) {
                return $arr[$status];
            }
            return $status;
        }
        return $arr;
    }
}