<?php

namespace common\modules\blog\models;

use common\modules\blog\behaviors\Autocomplete;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\db\Query;

/**
 * This is the model class for table "article_category".
 *
 * @property integer $article_category_id
 * @property integer $status
 * @property integer $sort
 * @property string $code
 * @property string $date_add
 * @property string $date_update
 * @property string $tag
 * @property integer $artucle_category_pid
 *
 * @property Article[] $articles
 * @property ArticleCategory $parent
 * @property ArticleCategory[] $articleCategories
 * @property ArticleCategoryLang[] $articleCategoryLangs
 * @property ArticleCategoryMedia[] $articleCategoryMedia
 */
class ArticleCategory extends ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'article_category';
    }

    public function behaviors()
    {
        return [
            [
                'class' => Autocomplete::className(),
                'attributes' => [
                    'translit' => ['code'],
                ]
            ]
        ];
    }
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['status', 'sort', 'article_category_pid'], 'integer'],
            [['code'], 'required'],
            [['code', 'tag'], 'string'],
            [['date_add', 'date_update'], 'safe'],
            [['status'], 'boolean'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'article_category_id' => Yii::t('app', 'ID'),
            'status' => Yii::t('app', 'Active'),
            'sort' => Yii::t('app', 'Sort'),
            'code' => Yii::t('app', 'Code'),
            'date_add' => Yii::t('app', 'Created At'),
            'date_update' => Yii::t('app', 'Updated At'),
            'tag' => Yii::t('app', 'Tags'),
            'article_category_pid' => Yii::t('app', 'Parent ID'),
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getArticles()
    {
        return $this->hasMany(Article::className(), ['article_id' => 'article_id'])->viaTable('article_to_category', ['article_category_id' => 'article_category_id']) ;
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getParent()
    {
        return $this->hasOne(ArticleCategory::className(), ['article_category_id' => 'article_category_pid']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getArticleCategories()
    {
        return $this->hasMany(ArticleCategory::className(), ['article_category_pid' => 'article_category_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getArticleCategoryLangs()
    {
        return $this->hasMany(ArticleCategoryLang::className(), ['article_category_id' => 'article_category_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getArticleCategoryMedia()
    {
        return $this->hasMany(ArticleCategoryMedia::className(), ['article_category_id' => 'article_category_id']);
    }

    public static function findArticleCategoryDropdown($id)
    {
        $query = new Query();
        return $query->select(['l.name', 'c.article_category_id'])
                        ->from(['article_category c'])
                        ->leftJoin(['article_category_lang l'], 'c.article_category_id = l.article_category_id')
                        ->where(['l.language_id' => 0, 'c.status' => 1])
                        ->andWhere(['not', ['c.article_category_id' => $id]])
                        ->indexBy('article_category_id')
                        ->column();
    }

}
