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.

116 lines
3.6 KiB

7 years ago
<?php
namespace common\modules\pages\forms;
7 years ago
use core\components\LanguageDynamicModel;
use common\modules\pages\entities\Page;
7 years ago
use core\validators\SlugValidator;
use yii\db\ActiveQuery;
7 years ago
use yii\helpers\ArrayHelper;
use Yii;
class PageForm extends LanguageDynamicModel
7 years ago
{
public $type;
7 years ago
public $title;
public $slug;
public $content;
public $parentId;
public $meta_title;
public $meta_description;
public $meta_keywords;
public $_page;
7 years ago
public function __construct(Page $page = null, $config = [])
{
if ($page) {
$this->slug = $page->slug;
$this->parentId = $page->parent ? $page->parent->id : null;
7 years ago
$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;
}
};
};
7 years ago
}
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;
}],
]
);
7 years ago
}
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'),
]
);
7 years ago
}
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
7 years ago
{
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 -'));
});
7 years ago
}
}