<?php

namespace common\modules\blog\models;

use common\modules\blog\behaviors\Autocomplete;
use Yii;

/**
 * This is the model class for table "article_category_lang".
 *
 * @property integer $article_category_language_id
 * @property integer $language_id
 * @property integer $article_category_id
 * @property string $text
 * @property string $preview
 * @property string $seo_url
 * @property string $name
 * @property string $meta_title
 * @property string $meta_descr
 * @property string $meta_keyword
 * @property string $h1_tag
 * @property string $tag
 *
 * @property ArticleCategory $category
 * @property Language $lang
 */
class ArticleCategoryLang extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'article_category_lang';
    }

    public function behaviors()
    {
        return [
            [
                'class' => Autocomplete::className(),
                'attributes' => [
                    'repeat' => [['preview', 'text', false, 5, true, '...']],
                ]
            ]
        ];
    }
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['language_id', 'article_category_id'], 'integer'],
            [['text', 'name'], 'required'],
            [['text', 'preview', 'seo_url', 'name', 'meta_title', 'meta_descr', 'meta_keyword', 'h1_tag', 'tag'], 'string'],
            ['seo_url', function($attribute, $params) {
                $pattern = "/^[a-zA-Z\d_-]+$/";
                if(!preg_match($pattern, $this->$attribute)) {
                    $this->addError($attribute, Yii::t('app', "Pattern doesn't match."));
                }
            }]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'article_category_language_id' => Yii::t('app', 'ID'),
            'language_id' => Yii::t('app', 'Lang ID'),
            'article_category_id' => Yii::t('app', 'Category ID'),
            'text' => Yii::t('app', 'Text'),
            'preview' => Yii::t('app', 'Preview'),
            'seo_url' => Yii::t('app', 'Seo Url'),
            'name' => Yii::t('app', 'Name'),
            'meta_title' => Yii::t('app', 'Meta Title'),
            'meta_descr' => Yii::t('app', 'Meta Descr'),
            'meta_keyword' => Yii::t('app', 'Meta Keywords'),
            'h1_tag' => Yii::t('app', 'H1 Tag'),
            'tag' => Yii::t('app', 'Tags'),
        ];
    }

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

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getLang()
    {
        return $this->hasOne(Language::className(), ['language_id' => 'language_id']);
    }
}
