<?php
    
    namespace common\models\slider;
    
    use artbox\core\models\Image;
    use artbox\core\models\Language;
    use yii\db\ActiveQuery;
    use yii\db\ActiveRecord;
    use yii2tech\ar\position\PositionBehavior;
    use yii2tech\ar\variation\VariationBehavior;
    
    /**
     * This is the model class for table "slide".
     *
     * @property integer     $id
     * @property boolean     $status
     * @property integer     $sort
     * @property SlideLang[] $slideLangs
     * * from VariationBehavior
     * @method ActiveQuery  hasDefaultVariationRelation();
     */
    class Slide extends ActiveRecord
    {
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'slide';
        }
        
        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',
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                
                [
                    [
                        'sort',
                    ],
                    'integer',
                ],
                [
                    [ 'status' ],
                    'boolean',
                ],
                [
                    [ 'background_id' ],
                    'integer',
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'id'     => \Yii::t('core', 'ID'),
                'status' => \Yii::t('core', 'Status'),
                'sort'   => \Yii::t('core', 'Sort'),
                'title'  => \Yii::t('core', 'Title'),
                'link'   => \Yii::t('core', 'Link'),
                'background_id' => \Yii::t('core', 'Background'),
            ];
        }
        
        public function getLanguages()
        {
            return $this->hasMany(SlideLang::className(), [ 'slide_id' => 'id' ]);
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getLanguage()
        {
            return $this->hasDefaultVariationRelation();
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getSlideLangs()
        {
            return $this->hasMany(SlideLang::className(), [ 'slide_id' => 'id' ])
                        ->inverseOf('slide');
        }
    
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getBackground()
        {
            return $this->hasOne(Image::className(), [ 'id' => 'background_id' ]);
        }
    }
