<?php
    
    namespace common\models\blog;
    
    use artbox\core\models\Image;
    use artbox\core\models\traits\AliasableTrait;
    use yii\db\ActiveQuery;
    use yii\db\ActiveRecord;
    use artbox\core\models\Language;
    use yii\helpers\Json;
    use yii2tech\ar\position\PositionBehavior;
    use yii2tech\ar\variation\VariationBehavior;
    
    /**
     * This is the model class for table "blog_category".
     *
     * @property integer        $id
     * @property integer        $sort
     * @property integer        $parent_id
     * @property boolean        $status
     * @property Article[]        $articles
     * @property CategoryLang[] $blogCategoryLangs
     * @property Language[]     $languages
     * @property Category       $parent
     * @property Image          $image
     * @property integer        $image_id
     *  * from VariationBehavior
     * @method ActiveQuery  hasDefaultVariationRelation();
     */
    class Category extends ActiveRecord
    {
        use AliasableTrait;
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'blog_category';
        }
        
        /**
         * @inheritdoc
         */
        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',
                        'parent_id',
                        'image_id',
                    ],
                    'integer',
                ],
                [
                    [ 'status' ],
                    'boolean',
                ],
                [
                    [ 'parent_id' ],
                    'default',
                    'value' => 0,
                ],
            ];
        }
        
        /**
         * @return string
         */
        public function getRoute()
        {
            return Json::encode(
                [
                    'blog/category',
                    'id' => $this->id,
                ]
            );
        }
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'id'        => 'ID',
                'image'     => 'Image',
                'parent_id' => \Yii::t('core', 'Parent ID'),
                'parent'    => \Yii::t('core', 'Parent ID'),
                'sort'      => \Yii::t('core', 'Sort'),
                'status'    => \Yii::t('core', 'Status'),
                'title'     => \Yii::t('core', 'Title'),
                'image_id'  => \Yii::t('core', 'Image'),
            ];
        }
        
        public function getLanguages()
        {
            return $this->hasMany(CategoryLang::className(), [ 'blog_category_id' => 'id' ]);
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getLanguage()
        {
            return $this->hasDefaultVariationRelation();
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getArticles()
        {
            return $this->hasMany(Article::className(), [ 'id' => 'blog_article_id' ])
                        ->viaTable('blog_article_to_category', [ 'blog_category_id' => 'id' ]);
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getParent()
        {
            return $this->hasOne(Category::className(), [ 'id' => 'parent_id' ]);
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getImage()
        {
            return $this->hasOne(Image::className(), [ 'id' => 'image_id' ]);
        }
        
    }
