HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux dev1 5.15.83-1-pve #1 SMP PVE 5.15.83-1 (2022-12-15T00:00Z) x86_64
User: safarimaris (1000)
PHP: 7.2.34-54+ubuntu22.04.1+deb.sury.org+1
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,
Upload Files
File: /home/safarimaris/home/safarimaris/common/models/Tour.php
<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "tour".
 *
 * @property integer $id
 * @property integer $entityId
 * @property string $startDate
 * @property string $ym
 * @property integer $isOpenDate
 * @property integer $duration
 * @property string $name
 * @property string $price
 * @property string $salePrice
 * @property string $spaces
 * @property string $productId
 * @property integer $isSpecial
 * @property integer $isCompatriot
 * @property string $notice
 * @property integer $isExternal
 * @property integer $isLock
 * @property integer $isInstructor
 * @property integer $isDelete
 *
 * @property Entity $entity
 */
class Tour extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'tour';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['entityId', 'duration', 'name', 'price', 'spaces'], 'required'],
            [['entityId', 'isOpenDate', 'duration', 'isSpecial', 'isCompatriot', 'isExternal', 'isLock', 'ym', 'isInstructor', 'isDelete', 'spaces', 'productId'], 'integer'],
            [['startDate', 'isExternal', 'isLock'], 'safe'],
            [['notice'], 'string'],
            ['spaces', 'validateSpaces'],
            [['name', 'price', 'salePrice'], 'string', 'max' => 255],
            [['entityId'], 'exist', 'skipOnError' => true, 'targetClass' => Entity::className(), 'targetAttribute' => ['entityId' => 'id']],
        ];
    }

    public function validateSpaces($attribute)
    {
        if (isset($this->entity->boat->capacity) && $this->entity->boat->capacity < $this->spaces) {
            $this->addError($attribute, sprintf(\Yii::t('app', 'Can`t be bigger than %s'), $this->entity->boat->capacity));
        }
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('model', 'ID'),
            'entityId' => Yii::t('model', 'Entity ID'),
            'startDate' => Yii::t('model', 'Start Date'),
            'isOpenDate' => Yii::t('model', 'Is Open Date'),
            'duration' => Yii::t('model', 'Duration'),
            'name' => Yii::t('model', 'Name'),
            'price' => Yii::t('model', 'Price'),
            'salePrice' => Yii::t('model', 'Strike Price'),
            'spaces' => Yii::t('model', 'Spaces'),
            'productId' => Yii::t('model', 'Product'),
            'isSpecial' => Yii::t('model', 'Is Special'),
            'isCompatriot' => Yii::t('model', 'Is Compatriot'),
            'notice' => Yii::t('model', 'Notice'),
            'tourOpen' => Yii::t('model', 'Tour Open'),
            'isExternal' => Yii::t('model', 'is External'),
            'isLock' => Yii::t('model', 'is Lock'),
            'isInstructor' => Yii::t('model', 'Is Instructor'),
            'isDelete' => Yii::t('model', 'Is deleted'),
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getEntity()
    {
        return $this->hasOne(Entity::className(), ['id' => 'entityId']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getProduct()
    {
        return $this->hasOne(Product::className(), ['id' => 'productId']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTourOpens()
    {
        return $this->hasMany(TourOpen::className(), ['tourId' => 'id']);
    }

    public function findExtended()
    {
        $model = TourOpen::find()->where(['tourId' => $this->id])->one();
        return $model;

    }

    public function showOpenMonth()
    {
        $list = [];
        foreach ($this->tourOpens as $item) {
            $attributeLabels = $item->attributeLabels();
            foreach ($item as $fld => $vl) {

                if ($vl === 1 && strstr($fld, 'm') !== false) {
                    array_push($list, $attributeLabels[$fld]);
                }
            }
        }
        return join(', ', $list);
    }

    /*
     * save ym
     * */
    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            $this->ym = date("Ym", strtotime($this->startDate));
            return true;
        }
        return false;
    }
}