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.

117 lines
3.5 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\MetaForm;
use core\validators\SlugValidator;
use yii\helpers\ArrayHelper;
use yii\web\UploadedFile;
use Yii;
/**
* @property MetaForm $meta
* @property BlogTagForm $tags
*/
class BlogPostForm extends CompositeForm
{
public $category_id;
public $title;
public $description;
public $content;
public $image;
public $video;
public $published_at;
public $slug;
public $reset_image;
public $status;
7 years ago
public $_post;
public function __construct(BlogPost $post = null, $config = [])
{
if ($post) {
$this->category_id = $post->category_id;
$this->title = $post->title;
$this->description = $post->description;
$this->content = $post->content;
$this->video = $post->video;
$this->published_at = $post->published_at;
$this->slug = $post->slug;
$this->meta = new MetaForm($post->meta);
$this->tags = new BlogTagForm($post);
$this->status = $post->status;
7 years ago
$this->_post = $post;
} else {
$this->meta = new MetaForm();
$this->tags = new BlogTagForm();
$this->status = 0;
7 years ago
}
parent::__construct($config);
}
public function rules(): array
{
return [
[['category_id', 'title'], 'required'],
[['title', 'video'], 'string', 'max' => 255],
[['category_id', 'status'], 'integer'],
7 years ago
[['description', 'content'], 'string'],
[['image'], 'image'],
['reset_image', 'boolean'],
['published_at', 'safe'],
['slug', SlugValidator::class],
[['slug'], 'unique', 'targetClass' => BlogPost::class, 'filter' => $this->_post ? ['<>', 'id', $this->_post->id] : null],
];
}
public function attributeLabels()
{
return [
'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'),
'meta_json' => Yii::t('blog', 'Meta Json'),
'comments_count' => Yii::t('blog', 'Comments Count'),
'views' => Yii::t('blog', 'Views'),
'slug' => Yii::t('blog', 'Slug'),
'reset_image' => Yii::t('blog', 'Reset Image'),
];
}
public function attributeHints() {
return [
'published_at' => Yii::t('blog', 'The article will be published after the specified date if its status is not a draft'),
];
}
public function categoriesList(): array
7 years ago
{
return ArrayHelper::map(BlogCategory::find()->orderBy('sort')->asArray()->all(), 'id', 'name');
}
protected function internalForms(): array
{
return ['meta', 'tags'];
}
public function beforeValidate(): bool
{
if (parent::beforeValidate()) {
$this->image = UploadedFile::getInstance($this, 'image');
$this->published_at = strtotime($this->published_at);
return true;
}
return false;
}
}