Job.php 2.91 KB
<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "job".
 *
 * @property integer $job_id
 * @property string $name
 * @property string $link
 * @property string $date_start
 * @property string $date_end
 * @property string $position
 * @property integer $user_id
 * @property integer $total_count
 * @property integer $complete_count
 * @property integer $current
 */
class Job extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'job';
    }


    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'slug' => [
                'class' => 'common\behaviors\Slug',
                'in_attribute' => 'name',
                'out_attribute' => 'link',
                'translit' => true
            ]
        ];
    }

    public function beforeSave($insert)
    {
        $this->date_start = \Yii::$app->formatter->asDatetime($this->date_start, 'Y-MM-d HH:mm:ss');

        if($this->date_end) {
            $this->date_end = \Yii::$app->formatter->asDatetime($this->date_end, 'Y-MM-d HH:mm:ss');
        }


        return parent::beforeSave($insert); // TODO: Change the autogenerated stub
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name'], 'required'],
            [['date_start', 'date_end'], 'safe'],
            [['user_id', 'total_count', 'complete_count', 'current'], 'integer'],
            [['name', 'link', 'position'], 'string', 'max' => 255]
        ];
    }



    public function getExpTime()
    {
        if($this->date_end && $this->date_start){
            $date = new \DateTime(date('Y-m-d H:i:s', $this->date_start));
            return \Yii::$app->formatter->asRelativeTime($date->diff(new \DateTime(date('Y-m-d H:i:s', $this->date_end))));
        } elseif($this->date_start) {
            $now = new \DateTime();
            $date = new \DateTime(date('Y-m-d H:i:s', strtotime($this->date_start)));
            return \Yii::$app->formatter->asRelativeTime($date->diff(new \DateTime()));
        } else {
            return 'неизвестна дата начала';
        }

    }


    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'job_id' => Yii::t('app', 'Job ID'),
            'name' => Yii::t('app', 'Name'),
            'link' => Yii::t('app', 'Link'),
            'date_start' => Yii::t('app', 'Date Start'),
            'date_end' => Yii::t('app', 'Date End'),
            'position' => Yii::t('app', 'Position'),
            'user_id' => Yii::t('app', 'User ID'),
            'total_count' => Yii::t('app', 'Total Count'),
            'complete_count' => Yii::t('app', 'Complete Count'),
            'current' => Yii::t('app', 'Current'),
        ];
    }
}