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.

132 lines
3.2 KiB

7 years ago
<?php
namespace common\modules\pages\entities;
7 years ago
use common\modules\pages\entities\queries\PageQuery;
use core\behaviors\LanguageBehavior;
7 years ago
use paulzi\nestedsets\NestedSetsBehavior;
use core\behaviors\MetaBehavior;
use yii\behaviors\TimestampBehavior;
use yii\behaviors\SluggableBehavior;
use common\behaviors\WidgetContentBehavior;
7 years ago
use yii\db\ActiveRecord;
use core\entities\Meta;
use Yii;
use yii\helpers\Inflector;
use yii\helpers\Json;
7 years ago
/**
* @property int $id
7 years ago
* @property string $slug
* @property int $created_at
* @property int $updated_at
* @property int $tree
* @property int $lft
* @property int $rgt
* @property int $depth
* @property int $type
* @property int $revision_at
* @property int $revision_id
*
* @method ActiveRecord findTranslation(string $language)
* @method void saveTranslations($translations)
*
* @property ActiveRecord[] translations
* @property ActiveRecord[] translation
7 years ago
*
* @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;
7 years ago
public $meta;
public $_form;
public static function create($form, $slug, $type = Page::TYPE_PUBLIC): self
7 years ago
{
7 years ago
$page = new static();
$page->slug = $slug;
$page->type = $type;
$page->_form = $form;
7 years ago
return $page;
7 years ago
}
public function edit($form, $slug, $type = Page::TYPE_PUBLIC): void
7 years ago
{
$this->slug = $slug;
$this->type = $type;
$this->_form = $form;
7 years ago
}
public function getSeoTitle(): string
{
return $this->translation->meta_title;
//return $this->meta->title ?: $this->title;
//$meta = Meta::createMeta($this->meta_json);
//return $meta->title;
7 years ago
}
public static function tableName(): string
{
return '{{%pages}}';
}
public function behaviors(): array
{
return [
[
'class' => NestedSetsBehavior::class,
'treeAttribute' => 'tree',
],
TimestampBehavior::class,
WidgetContentBehavior::class,
[
'class' => SluggableBehavior::class,
'attribute' => 'title',
'ensureUnique' => true,
'preserveNonEmptyValues' => true,
],
[
'class' => LanguageBehavior::class,
'virtualClassName' => 'PagesVirtualTranslate',
'translatedLanguages' => \Yii::$app->params['translatedLanguages'],
'relativeField' => 'page_id',
'tableName' => "{{%pages_lng}}",
'attributes' => ['title', 'content', 'meta_title', 'meta_description', 'meta_keywords'],
'defaultLanguage' => \Yii::$app->params['defaultLanguage'],
],
7 years ago
];
}
public function transactions(): array
{
return [
self::SCENARIO_DEFAULT => self::OP_ALL,
];
}
public function attributeLabels() {
return [
'title' => Yii::t('pages', 'Title'),
'slug' => Yii::t('pages', 'Slug'),
'id' => Yii::t('pages', 'ID'),
'content' => Yii::t('pages', 'Content'),
7 years ago
];
}
public static function find(): PageQuery
{
return new PageQuery(static::class);
}
7 years ago
}