|
|
|
<?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, $config = [])
|
|
|
|
{
|
|
|
|
if ($page) {
|
|
|
|
$this->slug = $page->slug;
|
|
|
|
$this->parentId = $page->parent ? $page->parent->id : null;
|
|
|
|
|
|
|
|
$this->_page = $page;
|
|
|
|
}
|
|
|
|
parent::__construct($config);
|
|
|
|
// fill translate values
|
|
|
|
if ($page) {
|
|
|
|
foreach ( $page->translations as $translate ) {
|
|
|
|
//$meta_translate = Json::decode($translate->meta_json); // get meta
|
|
|
|
if ($translate->language == Yii::$app->params['backendDefaultLanguage']) {
|
|
|
|
$this->{'title'} = $translate->title;
|
|
|
|
$this->{'content'} = $translate->content;
|
|
|
|
// fill meta
|
|
|
|
$this->{'meta_title'} = $translate->meta_title;
|
|
|
|
$this->{'meta_description'} = $translate->meta_description;
|
|
|
|
$this->{'meta_keywords'} = $translate->meta_keywords;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->{'title' . '_' . $translate->language} = $translate->title;
|
|
|
|
$this->{'content' . '_' . $translate->language} = $translate->content;
|
|
|
|
// fill meta
|
|
|
|
$this->{'meta_title' . '_' . $translate->language} = $translate->meta_title;
|
|
|
|
$this->{'meta_description' . '_' . $translate->language} = $translate->meta_description;
|
|
|
|
$this->{'meta_keywords' . '_' . $translate->language} = $translate->meta_keywords;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public function rules(): array
|
|
|
|
{
|
|
|
|
return array_merge(
|
|
|
|
parent::rules(),
|
|
|
|
[
|
|
|
|
[['title'], 'required'],
|
|
|
|
[['parentId'], 'integer'],
|
|
|
|
[['title', 'slug', 'meta_title'], 'string', 'max' => 255],
|
|
|
|
[['content', 'meta_description', 'meta_keywords'], '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 -'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|