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.
117 lines
2.7 KiB
117 lines
2.7 KiB
<?php |
|
|
|
namespace common\modules\pages\entities; |
|
|
|
use common\modules\pages\entities\queries\PageQuery; |
|
use paulzi\nestedsets\NestedSetsBehavior; |
|
use core\behaviors\MetaBehavior; |
|
use yii\behaviors\TimestampBehavior; |
|
use yii\behaviors\SluggableBehavior; |
|
use common\behaviors\WidgetContentBehavior; |
|
use yii\db\ActiveRecord; |
|
use core\entities\Meta; |
|
use Yii; |
|
use yii\helpers\Inflector; |
|
|
|
/** |
|
* @property int $id |
|
* @property string $title |
|
* @property string $slug |
|
* @property string $content |
|
* @property int $created_at |
|
* @property int $updated_at |
|
* @property string $meta_json |
|
* @property int $tree |
|
* @property int $lft |
|
* @property int $rgt |
|
* @property int $depth |
|
* @property int $type |
|
* @property int $revision_at |
|
* @property int $revision_id |
|
* @property Meta $meta |
|
* |
|
* @property Page $parent |
|
* @property Page[] $parents |
|
* @property Page[] $children |
|
* @property Page $prev |
|
* @property Page $next |
|
* @mixin NestedSetsBehavior |
|
*/ |
|
class Page extends ActiveRecord |
|
{ |
|
const TYPE_PUBLIC = 0; |
|
const TYPE_REVISION = 1; |
|
const TYPE_PREVIEW = 2; |
|
|
|
public $meta; |
|
|
|
public static function create($title, $slug, $content, $type = Page::TYPE_PUBLIC, Meta $meta): self |
|
{ |
|
$page = new static(); |
|
$page->title = $title; |
|
$page->slug = $slug; |
|
$page->content = $content; |
|
$page->meta = $meta; |
|
$page->type = $type; |
|
return $page; |
|
} |
|
|
|
public function edit($title, $slug, $content, $type = Page::TYPE_PUBLIC, Meta $meta): void |
|
{ |
|
$this->title = $title; |
|
$this->slug = $slug; |
|
$this->content = $content; |
|
$this->meta = $meta; |
|
$this->type = $type; |
|
} |
|
|
|
public function getSeoTitle(): string |
|
{ |
|
return $this->meta->title ?: $this->title; |
|
} |
|
|
|
public static function tableName(): string |
|
{ |
|
return '{{%pages}}'; |
|
} |
|
|
|
public function behaviors(): array |
|
{ |
|
return [ |
|
MetaBehavior::class, |
|
[ |
|
'class' => NestedSetsBehavior::class, |
|
'treeAttribute' => 'tree', |
|
], |
|
TimestampBehavior::class, |
|
WidgetContentBehavior::class, |
|
[ |
|
'class' => SluggableBehavior::class, |
|
'attribute' => 'title', |
|
'ensureUnique' => true, |
|
'preserveNonEmptyValues' => true, |
|
], |
|
]; |
|
} |
|
|
|
public function transactions(): array |
|
{ |
|
return [ |
|
self::SCENARIO_DEFAULT => self::OP_ALL, |
|
]; |
|
} |
|
|
|
public function attributeLabels() { |
|
return [ |
|
'title' => Yii::t('page', 'Title'), |
|
'slug' => Yii::t('page', 'Slug'), |
|
'id' => Yii::t('page', 'ID'), |
|
'content' => Yii::t('page', 'Content'), |
|
]; |
|
} |
|
|
|
public static function find(): PageQuery |
|
{ |
|
return new PageQuery(static::class); |
|
} |
|
} |