|
|
|
<?php
|
|
|
|
|
|
|
|
namespace common\modules\pages\forms;
|
|
|
|
|
|
|
|
use core\forms\CompositeForm;
|
|
|
|
use core\forms\MetaForm;
|
|
|
|
use common\modules\pages\entities\Page;
|
|
|
|
use core\validators\SlugValidator;
|
|
|
|
use yii\db\ActiveQuery;
|
|
|
|
use yii\helpers\ArrayHelper;
|
|
|
|
use Yii;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property MetaForm $meta;
|
|
|
|
*/
|
|
|
|
class PageForm extends CompositeForm
|
|
|
|
{
|
|
|
|
public $type;
|
|
|
|
|
|
|
|
public $title;
|
|
|
|
public $slug;
|
|
|
|
public $content;
|
|
|
|
public $parentId;
|
|
|
|
|
|
|
|
public $_page;
|
|
|
|
|
|
|
|
public function __construct(Page $page = null, $config = [])
|
|
|
|
{
|
|
|
|
if ($page) {
|
|
|
|
$this->title = $page->title;
|
|
|
|
$this->slug = $page->slug;
|
|
|
|
$this->content = $page->content;
|
|
|
|
$this->parentId = $page->parent ? $page->parent->id : null;
|
|
|
|
$this->meta = new MetaForm($page->meta);
|
|
|
|
$this->_page = $page;
|
|
|
|
} else {
|
|
|
|
$this->meta = new MetaForm();
|
|
|
|
}
|
|
|
|
parent::__construct($config);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function rules(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[['title'], 'required'],
|
|
|
|
[['parentId'], 'integer'],
|
|
|
|
[['title', 'slug'], 'string', 'max' => 255],
|
|
|
|
[['content'], 'string'],
|
|
|
|
['slug', SlugValidator::class],
|
|
|
|
[['slug'], 'unique', 'targetClass' => Page::class, 'filter' => function (ActiveQuery $query) {
|
|
|
|
if ($this->type != Page::TYPE_PUBLIC) {
|
|
|
|
$query->andWhere($this->type . '=' . Page::TYPE_PUBLIC);
|
|
|
|
}
|
|
|
|
|
|
|
|
$query->andWhere(['type' => Page::TYPE_PUBLIC]);
|
|
|
|
if ($this->_page) {
|
|
|
|
$query->andWhere(['<>', 'id', $this->_page->id]);
|
|
|
|
}
|
|
|
|
return $query;
|
|
|
|
}],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function attributeLabels() {
|
|
|
|
return [
|
|
|
|
'title' => Yii::t('page', 'Title'),
|
|
|
|
'slug' => Yii::t('page', 'Slug'),
|
|
|
|
'id' => Yii::t('page', 'ID'),
|
|
|
|
'content' => Yii::t('page', 'Content'),
|
|
|
|
'parentId' => Yii::t('page', 'Parent Page'),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function attributeHints() {
|
|
|
|
return [
|
|
|
|
'slug' => Yii::t('page', 'SEO link will be generated automatically if not specified'),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function parentsList(): array
|
|
|
|
{
|
|
|
|
return ArrayHelper::map(Page::find()->andWhere(['tree' => 1])->orderBy('lft')->asArray()->all(), 'id', function (array $page) {
|
|
|
|
return ($page['depth'] > 1 ? str_repeat('-- ', $page['depth'] - 1) . ' ' : '') . $page['title'];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function internalForms(): array
|
|
|
|
{
|
|
|
|
return ['meta'];
|
|
|
|
}
|
|
|
|
}
|