<?php
    
    namespace common\models\event;
    
    use artbox\core\models\Image;
    use artbox\core\models\traits\AliasableTrait;
    use artbox\webcomment\models\CommentModel;
    use yii\behaviors\TimestampBehavior;
    use yii\db\ActiveRecord;
    use artbox\core\models\Language;
    use yii\db\ActiveQuery;
    use yii\db\Query;
    use yii\helpers\Json;
    use yii2tech\ar\linkmany\LinkManyBehavior;
    use yii2tech\ar\position\PositionBehavior;
    use yii2tech\ar\variation\VariationBehavior;
    
    /**
     * This is the model class for table "blog_article".
     *
     * @property integer     $id
     * @property Image       $image
     * @property integer     $created_at
     * @property integer     $updated_at
     * @property integer     $deleted_at
     * @property integer     $sort
     * @property boolean     $status
     * @property integer     $author_id
     * @property integer     $image_id
     * @property EventLang[] $blogArticleLangs
     * @property Language[]  $languages
     * @property Event[]     $relatedBlogArticles
     * @property Event[]     $articles
     * @property Tag[]       $tags
     *  * from VariationBehavior
     * @method ActiveQuery  hasDefaultVariationRelation();
     */
    class Event extends ActiveRecord
    {
        use AliasableTrait;
        
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'event';
        }
        
        public function behaviors()
        {
            return [
                'translations'         => [
                    'class'                             => VariationBehavior::className(),
                    'variationsRelation'                => 'languages',
                    'defaultVariationRelation'          => 'language',
                    'variationOptionReferenceAttribute' => 'language_id',
                    'optionModelClass'                  => Language::className(),
                    'defaultVariationOptionReference'   => function () {
                        return Language::getCurrent()->id;
                    },
                    'optionQueryFilter'                 => function (ActiveQuery $query) {
                        $query->where(
                            [
                                'status' => true,
                            ]
                        );
                    },
                ],
                'positionBehavior'     => [
                    'class'             => PositionBehavior::className(),
                    'positionAttribute' => 'sort',
                ],
                'linkTagBehavior'      => [
                    'class'                      => LinkManyBehavior::className(),
                    'relation'                   => 'tags',
                    'relationReferenceAttribute' => 'tagIds',
                ],
                'timestamp'            => [
                    'class' => TimestampBehavior::className(),
                ],
            ];
        }
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [
                        'created_at',
                        'updated_at',
                        'deleted_at',
                        'sort',
                        'author_id',
                        'image_id',
                    ],
                    'integer',
                ],
                [
                    [ 'status' ],
                    'boolean',
                ],
                
                [
                    [
                        'tagIds',
                    ],
                    'safe',
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'id'          => 'ID',
                'image'       => 'Image',
                'created_at'  => 'Created At',
                'updated_at'  => \Yii::t('core', 'Updated At'),
                'deleted_at'  => 'Deleted At',
                'sort'        => \Yii::t('core', 'Sort'),
                'status'      => \Yii::t('core', 'Status'),
                'author_id'   => 'Author ID',
                'title'       => \Yii::t('core', 'Title'),
                'tags'        => \Yii::t('core', 'Tags'),
                
                'image_id'    => \Yii::t('core', 'Image'),
                'tagIds'      => \Yii::t('core', 'Tags'),
            ];
        }
        
        public function getLanguages()
        {
            return $this->hasMany(EventLang::className(), [ 'event_id' => 'id' ]);
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getLanguage()
        {
            return $this->hasDefaultVariationRelation();
        }
        
       
        public function getRoute()
        {
            return Json::encode(
                [
                    'event/view',
                    'id' => $this->id,
                ]
            );
        }
        
        
        
       
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getImage()
        {
            return $this->hasOne(Image::className(), [ 'id' => 'image_id' ]);
        }
        
        
        
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getTags()
        {
            return $this->hasMany(Tag::className(), [ 'id' => 'event_tag_id' ])
                        ->viaTable('event_to_tag', [ 'event_id' => 'id' ]);
        }
    }
