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.

144 lines
4.3 KiB

7 years ago
<?php
namespace common\modules\blog\forms;
use common\modules\blog\entities\BlogCategory;
use common\modules\blog\entities\BlogPost;
use core\forms\CompositeForm;
use core\forms\CompositeLanguageForm;
7 years ago
use core\forms\MetaForm;
use core\validators\SlugValidator;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
7 years ago
use yii\helpers\ArrayHelper;
use yii\web\UploadedFile;
use Yii;
/**
* @property MetaForm $meta
* @property BlogTagForm $tags
*/
class BlogPostForm extends CompositeLanguageForm
7 years ago
{
public $type;
7 years ago
public $category_id;
public $title;
public $description;
public $content;
public $image;
public $video;
public $published_at;
public $slug;
public $reset_image;
public $status;
public $meta_title;
public $meta_description;
public $meta_keywords;
7 years ago
public $_post;
public function __construct(BlogPost $post = null, array $attributes = [], $config = [])
7 years ago
{
if ($post) {
$this->category_id = $post->category_id;
$this->video = $post->video;
$this->published_at = $post->published_at;
$this->slug = $post->slug;
$this->tags = new BlogTagForm($post);
$this->status = $post->status;
7 years ago
$this->_post = $post;
} else {
$this->tags = new BlogTagForm();
$this->status = 0;
7 years ago
}
parent::__construct($post, $attributes, $config);
7 years ago
}
public function rules(): array
{
return array_merge(
parent::rules(),
[
[['category_id', 'title'], 'required'],
[['title', 'video', 'meta_title', 'meta_keywords'], 'string', 'max' => 255],
[['category_id', 'status'], 'integer'],
[['description', 'content', 'meta_description'], 'string'],
[['image'], 'image'],
['reset_image', 'boolean'],
['published_at', 'safe'],
['slug', SlugValidator::class],
[['slug'], 'unique', 'targetClass' => BlogPost::class, 'filter' => function (ActiveQuery $query) {
if ($this->type != BlogPost::TYPE_PUBLIC) {
$query->andWhere($this->type . '=' . BlogPost::TYPE_PUBLIC);
}
$query->andWhere(['type' => BlogPost::TYPE_PUBLIC]);
if ($this->_post) {
$query->andWhere(['<>', 'id', $this->_post->id]);
}
return $query;
}],
]
);
7 years ago
}
public function attributeLabels()
{
return array_merge(
parent::attributeLabels(),
[
'id' => Yii::t('blog', 'ID'),
'category_id' => Yii::t('blog', 'Category'),
'published_at' => Yii::t('blog', 'Published At'),
'created_at' => Yii::t('blog', 'Created At'),
'updated_at' => Yii::t('blog', 'Updated At'),
'title' => Yii::t('blog', 'Title'),
'description' => Yii::t('blog', 'Description'),
'content' => Yii::t('blog', 'Content'),
'image' => Yii::t('blog', 'Image'),
'video' => Yii::t('blog', 'Video'),
'status' => Yii::t('blog', 'Status'),
'comments_count' => Yii::t('blog', 'Comments Count'),
'views' => Yii::t('blog', 'Views'),
'slug' => Yii::t('blog', 'Slug'),
'reset_image' => Yii::t('blog', 'Reset Image'),
'meta_title' => Yii::t('blog', 'META Title'),
'meta_description' => Yii::t('blog', 'META Description'),
'meta_keywords' => Yii::t('blog', 'META Keywords'),
]
);
7 years ago
}
public function attributeHints() {
return array_merge(
parent::attributeHints(),
[
'published_at' => Yii::t('blog', 'The article will be published after the specified date if its status is not a draft'),
'slug' => Yii::t('pages', 'SEO link will be generated automatically if not specified'),
]
);
}
public function categoriesList(): array
7 years ago
{
return ArrayHelper::map(BlogCategory::find()->orderBy('sort')->all(), 'id', function (BlogCategory $category) {
return $category->translation->name;
});
7 years ago
}
protected function internalForms(): array
{
return ['tags'];
7 years ago
}
public function beforeValidate(): bool
{
if (parent::beforeValidate()) {
$this->image = UploadedFile::getInstance($this, 'image');
$this->published_at = strtotime($this->published_at);
return true;
}
return false;
}
}