Job.php 6.36 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 [

            ];
        }

        public function beforeSave($insert)
        {
            if($this->date_start) {
                $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);
        }

        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [ 'name' ],
                    'required',
                ],
                [
                    [
                        'date_start',
                        'date_end',
                    ],
                    'safe',
                ],
                [
                    [
                        'user_id',
                        'current',
                    ],
                    'integer',
                ],
                [
                    [
                        'name',
                        'link',
                        'position',
                        'achievement',
                    ],
                    'string',
                    'max' => 255,
                ],
                [
                    [
                        'link',
                    ],
                    'match',
                    'pattern' => '/^(?:https?:\/\/)?(?:w{3}\.)?'.preg_quote($_SERVER['HTTP_HOST']).'\/company\/\w+\/\d+$/i',
                ],
                [
                    [
                        'total_count',
                        'complete_count',
                    ],
                    'integer',
                    'min' => 0,
                ],
                [
                    [
                        'complete_count',
                    ],
                    'compare',
                    'compareAttribute' => 'total_count',
                    'operator' => '<=',
                ]
            ];
        }

        public function getExpTime()
        {
            if(!empty($this->date_start) && !empty($this->date_end)) {
                $result = '';
                $start = new \DateTime($this->date_start);
                $end =  new \DateTime($this->date_end);
                $interval = $end->diff($start);
                if($interval->y > 0) {
                    if($interval->y == 1) {
                        $result .= $interval->y.' год';
                    } elseif($interval->y > 1 && $interval->y <= 4) {
                        $result .= $interval->y.' года';
                    } else {
                        $result .= $interval->y.' лет';
                    }
                }
                if($interval->m > 0) {
                    if(!empty($result)) {
                        $result .= ', ';
                    }
                    if($interval->m == 1) {
                        $result .= $interval->m.' месяц';
                    } elseif($interval->m > 1 && $interval->m <= 4) {
                        $result .= $interval->m.' месяца';
                    } else {
                        $result .= $interval->m.' месяцев';
                    }
                }
                if(empty($result) && $interval->d > 0) {
                    $result = 'Меньше месяца';
                }
            } elseif(!empty($this->date_start)) {
                $result = '';
                $start = new \DateTime($this->date_start);
                $end =  new \DateTime();
                $interval = $end->diff($start);
                if($interval->y > 0) {
                    if($interval->y == 1) {
                        $result .= $interval->y.' год';
                    } elseif($interval->y > 1 && $interval->y <= 4) {
                        $result .= $interval->y.' года';
                    } else {
                        $result .= $interval->y.' лет';
                    }
                }
                if($interval->m > 0) {
                    if(!empty($result)) {
                        $result .= ', ';
                    }
                    if($interval->m == 1) {
                        $result .= $interval->m.' месяц';
                    } elseif($interval->m > 1 && $interval->m <= 4) {
                        $result .= $interval->m.' месяца';
                    } else {
                        $result .= $interval->m.' месяцев';
                    }
                }
                if(empty($result) && $interval->d > 0) {
                    $result = 'Меньше месяца';
                }
            } else {
                return false;
            }
            return $result;
        }


    /**
     * @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'),
            'achievement' => Yii::t('app', 'achievement'),
        ];
    }
}