<?php

namespace common\modules\blog\models;

use common\models\ActiveRecordRule;
use common\models\Media;
use common\models\User;
use common\modules\blog\behaviors\Autocomplete;
use Yii;
use yii\db\Query;

/**
 * This is the model class for table "article".
 *
 * @property integer $article_id
 * @property integer $sort
 * @property string $date_add
 * @property string $date_update
 * @property string $code
 * @property integer $user_id
 * @property string $tag
 * @property integer $article_pid
 * @property integer $status
 * @property integer $comment
 * @property integer $vote
 *
 * @property Article $parent
 * @property Article[] $articles
 * @property User $user
 * @property ArticleLang[] $articleLangs
 * @property ArticleMedia[] $articleMedia
 * @property ArticleToCategory[] $articleToCategories
 * @property Media[] $media
 */
class Article extends ActiveRecordRule
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'article';
    }

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

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'article_id' => Yii::t('app', 'ID'),
            'sort' => Yii::t('app', 'Sort'),
            'date_add' => Yii::t('app', 'Create At'),
            'date_update' => Yii::t('app', 'Update At'),
            'code' => Yii::t('app', 'Code'),
            'user_id' => Yii::t('app', 'Author'),
            'tag' => Yii::t('app', 'Tags'),
            'article_pid' => Yii::t('app', 'Parent ID'),
            'status' => Yii::t('app', 'Active'),
            'comment' => Yii::t('app', 'Comments'),
            'vote' => Yii::t('app', 'Voting'),
        ];
    }

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

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

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

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

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

    public function getMedia()
    {
        return $this->hasMany(Media::className(), ['article_id' => 'media_id'])->via('articleMedia');
    }
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getArticleToCategories()
    {
        return $this->hasMany(ArticleToCategory::className(), ['article_id' => 'article_id']);
    }

    public function getArticleCategories()
    {
        return $this->hasMany(ArticleCategory::className(), ['article_category_id' => 'article_category_id'])->viaTable('article_to_category', ['article_id' => 'article_category_id']);
    }

    public static function findArticleDropdown($id)
    {
        $query = new Query();
        return $query->select(['l.name', 'a.article_id'])
            ->from(['article a'])
            ->leftJoin(['article_lang l'], 'a.article_id = l.article_id')
            ->where(['l.language_id' => 0, 'a.status' => 1])
            ->andWhere(['not', ['a.article_id' => $id]])
            ->indexBy('article_id')
            ->column();
    }

    public function getArticleCategoriesArray()
    {
        return $this->getArticleToCategories()->select('article_category_id')->column();
    }

}
