You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.5 KiB

7 years ago
<?php
namespace common\modules\blog\entities;
use common\modules\blog\entities\queries\BlogCategoryQuery;
use core\behaviors\LanguageBehavior;
use core\behaviors\SluggableRelationBehavior;
7 years ago
use yii\db\ActiveRecord;
use Yii;
/**
* @property integer $id
* @property string $slug
* @property integer $sort
*
*
* @method ActiveRecord findTranslation(string $language)
* @method void saveTranslations($translations)
*
* @property ActiveRecord[] $translations
* @property ActiveRecord $translation
7 years ago
*/
class BlogCategory extends ActiveRecord
{
public $_form;
7 years ago
public static function create($form, $slug, $sort): self
7 years ago
{
$category = new static();
$category->slug = $slug;
$category->sort = $sort;
$category->_form = $form;
7 years ago
return $category;
}
public function edit($form, $slug, $sort): void
7 years ago
{
$this->slug = $slug;
$this->sort = $sort;
$this->_form = $form;
7 years ago
}
public function attributeLabels()
{
return [
'id' => Yii::t('blog', 'ID'),
'name' => Yii::t('blog', 'Name'),
'slug' => Yii::t('blog', 'SEO link'),
'sort' => Yii::t('blog', 'Sort'),
'title' => Yii::t('blog', 'Title'),
'description' => Yii::t('blog', 'Description'),
'meta_title' => Yii::t('blog', 'META Title'),
'meta_description' => Yii::t('blog', 'META Description'),
'meta_keywords' => Yii::t('blog', 'META Keywords'),
7 years ago
];
}
public function getSeoTitle(): string
{
return $this->translation->meta_title ?: $this->getHeadingTile();
7 years ago
}
public function getHeadingTile(): string
{
return $this->translation->title ?: $this->translation->name;
7 years ago
}
public function getPostsCount(): int
{
return BlogPost::find()->where(['category_id' => $this->id])->count('*');
7 years ago
}
public static function tableName(): string
{
return '{{%blog_categories}}';
}
public function behaviors(): array
{
return [
/*[
'class' => SluggableBehavior::class,
'attribute' => 'title',
'ensureUnique' => true,
'preserveNonEmptyValues' => true,
],*/
[
'class' => LanguageBehavior::class,
'virtualClassName' => 'BlogCategoryVirtualTranslate',
'translatedLanguages' => \Yii::$app->params['translatedLanguages'],
'relativeField' => 'blog_category_id',
'tableName' => '{{%blog_categories_lng}}',
'attributes' => [
'title',
'description',
'name',
'meta_title',
'meta_description',
'meta_keywords'
],
//'defaultLanguage' => basename(Yii::$app->getBasePath()) === 'backend' ? Yii::$app->language : Yii::$app->params['defaultLanguage'],
'defaultLanguage' => \Yii::$app->params['defaultLanguage'],
],
[
'class' => SluggableRelationBehavior::class,
'attribute' => 'name',
'relation' => 'translation',
],
7 years ago
];
}
public static function find(): BlogCategoryQuery
{
return new BlogCategoryQuery(static::class);
}
}