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.
101 lines
3.2 KiB
101 lines
3.2 KiB
<?php |
|
|
|
namespace common\modules\pages\forms; |
|
|
|
use core\components\LanguageDynamicModel; |
|
use common\modules\pages\entities\Page; |
|
use core\validators\SlugValidator; |
|
use yii\db\ActiveQuery; |
|
use yii\helpers\ArrayHelper; |
|
use Yii; |
|
|
|
class PageForm extends LanguageDynamicModel |
|
{ |
|
public $type; |
|
public $title; |
|
public $slug; |
|
public $content; |
|
public $parentId; |
|
|
|
public $meta_title; |
|
public $meta_description; |
|
public $meta_keywords; |
|
|
|
public $_page; |
|
|
|
public function __construct(Page $page = null, array $attributes = [], $config = []) |
|
{ |
|
if ($page) { |
|
$this->slug = $page->slug; |
|
$this->parentId = $page->parent ? $page->parent->id : null; |
|
|
|
$this->_page = $page; |
|
} |
|
parent::__construct($page, $attributes, $config); |
|
} |
|
|
|
public function rules(): array |
|
{ |
|
return array_merge( |
|
parent::rules(), |
|
[ |
|
[['title'], 'required'], |
|
[['parentId'], 'integer'], |
|
[['title', 'slug', 'meta_title', 'meta_keywords'], 'string', 'max' => 255], |
|
[['content', 'meta_description'], '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 array_merge( |
|
parent::attributeLabels(), |
|
[ |
|
'title' => Yii::t('pages', 'Title'), |
|
'slug' => Yii::t('pages', 'Slug'), |
|
'id' => Yii::t('pages', 'ID'), |
|
'content' => Yii::t('pages', 'Content'), |
|
'parentId' => Yii::t('pages', 'Parent Page'), |
|
'meta_title' => Yii::t('pages', 'META Title'), |
|
'meta_description' => Yii::t('pages', 'META Description'), |
|
'meta_keywords' => Yii::t('pages', 'META Keywords'), |
|
] |
|
); |
|
} |
|
|
|
public function attributeHints() |
|
{ |
|
return array_merge( |
|
parent::attributeHints(), |
|
[ |
|
'slug' => Yii::t('pages', 'SEO link will be generated automatically if not specified'), |
|
] |
|
); |
|
} |
|
|
|
public function parentsList(): array |
|
{ |
|
return ArrayHelper::map(Page::find()->andWhere(['tree' => 1])->orderBy('lft')->all(), 'id', function (Page $page) { |
|
return ($page->depth > 1 ? str_repeat('-- ', $page->depth - 1) . ' ' : '') . ($page->translation ? $page->translation->title : Yii::t('pages', '- no parent -')); |
|
}); |
|
} |
|
}
|
|
|