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.

95 lines
2.4 KiB

7 years ago
<?php
namespace common\modules\blog\entities;
use core\behaviors\MetaBehavior;
use yii\behaviors\SluggableBehavior;
7 years ago
use core\entities\Meta;
use yii\db\ActiveRecord;
use Yii;
/**
* @property integer $id
* @property string $name
* @property string $slug
* @property string $title
* @property string $description
* @property integer $sort
* @property string $meta_json
* @property Meta $meta
*/
class BlogCategory extends ActiveRecord
{
public $meta;
public static function create($name, $slug, $title, $description, $sort, Meta $meta): self
{
$category = new static();
$category->name = $name;
$category->slug = $slug;
$category->title = $title;
$category->description = $description;
$category->sort = $sort;
$category->meta = $meta;
return $category;
}
public function edit($name, $slug, $title, $description, $sort, Meta $meta): void
{
$this->name = $name;
$this->slug = $slug;
$this->title = $title;
$this->description = $description;
$this->sort = $sort;
$this->meta = $meta;
}
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'),
];
}
public function getSeoTitle(): string
{
return $this->meta->title ?: $this->getHeadingTile();
}
public function getHeadingTile(): string
{
return $this->title ?: $this->name;
}
public function getPostsCount(): int
{
return BlogPost::find()->where(['category_id' => $this->id])->count('*');
}
public static function tableName(): string
{
return '{{%blog_categories}}';
}
public function behaviors(): array
{
return [
MetaBehavior::class,
[
'class' => SluggableBehavior::class,
'attribute' => 'title',
'ensureUnique' => true,
'preserveNonEmptyValues' => true,
],
7 years ago
];
}
}