|
|
|
<?php
|
|
|
|
|
|
|
|
namespace common\modules\blog\services;
|
|
|
|
|
|
|
|
use common\modules\blog\entities\BlogPost;
|
|
|
|
use common\modules\blog\entities\BlogTag;
|
|
|
|
use common\modules\blog\forms\BlogPostForm;
|
|
|
|
use common\modules\blog\helpers\BlogPostHelper;
|
|
|
|
use common\modules\blog\repositories\BlogCategoryRepository;
|
|
|
|
use common\modules\blog\repositories\BlogRepository;
|
|
|
|
use common\modules\blog\repositories\BlogTagRepository;
|
|
|
|
use core\entities\Meta;
|
|
|
|
use core\helpers\VideoHelper;
|
|
|
|
use core\services\TransactionManager;
|
|
|
|
use yii\base\Security;
|
|
|
|
use yii\helpers\Inflector;
|
|
|
|
|
|
|
|
class BlogPostManageService
|
|
|
|
{
|
|
|
|
private $posts;
|
|
|
|
private $categories;
|
|
|
|
private $tags;
|
|
|
|
private $transaction;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
BlogRepository $posts,
|
|
|
|
BlogCategoryRepository $categories,
|
|
|
|
BlogTagRepository $tags,
|
|
|
|
TransactionManager $transaction
|
|
|
|
)
|
|
|
|
{
|
|
|
|
$this->posts = $posts;
|
|
|
|
$this->categories = $categories;
|
|
|
|
$this->tags = $tags;
|
|
|
|
$this->transaction = $transaction;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function create(BlogPostForm $form, $type = BlogPost::TYPE_PUBLIC): BlogPost
|
|
|
|
{
|
|
|
|
$category = $this->categories->get($form->category_id);
|
|
|
|
|
|
|
|
$post = BlogPost::create(
|
|
|
|
$category->id,
|
|
|
|
$form->title,
|
|
|
|
$form->slug,
|
|
|
|
$form->description,
|
|
|
|
$form->content,
|
|
|
|
$form->published_at,
|
|
|
|
$form->video,
|
|
|
|
$type,
|
|
|
|
new Meta(
|
|
|
|
$form->meta->title,
|
|
|
|
$form->meta->description,
|
|
|
|
$form->meta->keywords
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->transaction->wrap(function () use ($post, $form) {
|
|
|
|
|
|
|
|
if (is_array($form->tags->new_tags) && !empty($form->tags->new_tags)) {
|
|
|
|
foreach ( $form->tags->new_tags as $tag_id => $tag_name ) {
|
|
|
|
if ( !$tag = $this->tags->findByName( $tag_name ) ) {
|
|
|
|
$tag = BlogTag::create( $tag_name, Inflector::slug( $tag_name, '_' ) );
|
|
|
|
$this->tags->save( $tag );
|
|
|
|
}
|
|
|
|
$post->assignTag( $tag->id );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->posts->save($post);
|
|
|
|
|
|
|
|
if ($form->image) {
|
|
|
|
$post->setImage($form->image);
|
|
|
|
}
|
|
|
|
else if ($form->video) {
|
|
|
|
$src = VideoHelper::getThumb($form->video);
|
|
|
|
//$src = 'https://i.ytimg.com/vi/' . BlogPostHelper::parseYoutubeUrl($post->video) . '/maxresdefault.jpg';
|
|
|
|
$filename = (new Security())->generateRandomString(15) . '.jpg';
|
|
|
|
copy($src, \Yii::getAlias(BlogPost::FILE_ORIGINAL_PATH . '/' . $post->id . '.jpg'));
|
|
|
|
$post->image = $filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($form->status == BlogPost::STATUS_ACTIVE) {
|
|
|
|
$post->activate();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->posts->save($post);
|
|
|
|
});
|
|
|
|
|
|
|
|
return $post;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function edit($id, BlogPostForm $form): void
|
|
|
|
{
|
|
|
|
$post = $this->posts->get($id);
|
|
|
|
BlogPostHelper::saveRevision($post);
|
|
|
|
|
|
|
|
$category = $this->categories->get($form->category_id);
|
|
|
|
|
|
|
|
$post->edit(
|
|
|
|
$category->id,
|
|
|
|
$form->title,
|
|
|
|
$form->slug,
|
|
|
|
$form->description,
|
|
|
|
$form->content,
|
|
|
|
$form->published_at,
|
|
|
|
$form->video,
|
|
|
|
$post->type,
|
|
|
|
new Meta(
|
|
|
|
$form->meta->title,
|
|
|
|
$form->meta->description,
|
|
|
|
$form->meta->keywords
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($form->image) {
|
|
|
|
$post->cleanFiles();
|
|
|
|
$post->setImage($form->image);
|
|
|
|
}
|
|
|
|
elseif ($form->video && (!$post->image || $form->reset_image)) {
|
|
|
|
$post->cleanFiles();
|
|
|
|
$src = VideoHelper::getThumb($form->video);
|
|
|
|
//$src = 'https://i.ytimg.com/vi/' . BlogPostHelper::parseYoutubeUrl($post->video) . '/maxresdefault.jpg';
|
|
|
|
$filename = (new Security())->generateRandomString(15) . '.jpg';
|
|
|
|
copy($src, \Yii::getAlias(BlogPost::FILE_ORIGINAL_PATH . '/' . $post->id . '.jpg'));
|
|
|
|
$post->image = $filename;
|
|
|
|
}
|
|
|
|
elseif ($post->image && $form->reset_image) {
|
|
|
|
$post->cleanFiles();
|
|
|
|
//Post::updateAll(['image' => null], ['id' => $post->id]);
|
|
|
|
$post->image = null;
|
|
|
|
$post->updateAttributes(['image']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->transaction->wrap(function () use ($post, $form) {
|
|
|
|
$post->revokeTags();
|
|
|
|
$this->posts->save($post);
|
|
|
|
|
|
|
|
if (is_array($form->tags->new_tags) && !empty($form->tags->new_tags)) {
|
|
|
|
foreach ( $form->tags->new_tags as $tag_id => $tag_name ) {
|
|
|
|
if ( ! $tag = $this->tags->findByName( $tag_name ) ) {
|
|
|
|
$tag = BlogTag::create( $tag_name, Inflector::slug( $tag_name, '_' ) );
|
|
|
|
$this->tags->save( $tag );
|
|
|
|
}
|
|
|
|
$post->assignTag( $tag->id );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->posts->save( $post );
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function activate($id): void
|
|
|
|
{
|
|
|
|
$post = $this->posts->get($id);
|
|
|
|
$post->activate();
|
|
|
|
$this->posts->save($post);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function draft($id): void
|
|
|
|
{
|
|
|
|
$post = $this->posts->get($id);
|
|
|
|
$post->draft();
|
|
|
|
$this->posts->save($post);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function remove($id): void
|
|
|
|
{
|
|
|
|
$post = $this->posts->get($id);
|
|
|
|
$this->posts->remove($post);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function removePreviews(): void
|
|
|
|
{
|
|
|
|
$posts = BlogPost::find()->andWhere(['type' => BlogPost::TYPE_PREVIEW])->all();
|
|
|
|
foreach ($posts as $post) {
|
|
|
|
$post->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function clearHistory(BlogPost $post): void
|
|
|
|
{
|
|
|
|
BlogPost::deleteAll(['revision_id' => $post->id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function restoreHistory($from_id, $id): void
|
|
|
|
{
|
|
|
|
$post = $this->posts->get($id);
|
|
|
|
$from = $this->posts->get($from_id);
|
|
|
|
|
|
|
|
// get from tags
|
|
|
|
$from_tags = $from->tags;
|
|
|
|
$new_tags_names = array_map(function ($item){
|
|
|
|
return $item->name;
|
|
|
|
}, $from_tags);
|
|
|
|
|
|
|
|
// clear from tags
|
|
|
|
$from->revokeTags();
|
|
|
|
$this->posts->save($from);
|
|
|
|
|
|
|
|
// remove current revision
|
|
|
|
$this->posts->remove($post);
|
|
|
|
|
|
|
|
// copy image
|
|
|
|
$path = \Yii::getAlias('@staticRoot/origin/posts');
|
|
|
|
$parts = pathinfo($from->image);
|
|
|
|
copy($path . '/' . $from->id . '.' . $parts['extension'], $path . '/' . $id . '.' . $parts['extension']);
|
|
|
|
$from->createThumbs();
|
|
|
|
|
|
|
|
$from->id = $id;
|
|
|
|
$from->type = BlogPost::TYPE_PUBLIC;
|
|
|
|
$from->revision_id = null;
|
|
|
|
|
|
|
|
foreach ($new_tags_names as $tag_name) {
|
|
|
|
if ( ! $tag = $this->tags->findByName( $tag_name ) ) {
|
|
|
|
$tag = BlogTag::create( $tag_name, Inflector::slug( $tag_name, '_' ) );
|
|
|
|
$this->tags->save( $tag );
|
|
|
|
}
|
|
|
|
$from->assignTag( $tag->id );
|
|
|
|
}
|
|
|
|
$this->posts->save($from);
|
|
|
|
|
|
|
|
// delete never revisions
|
|
|
|
BlogPost::deleteAll(['AND', ['revision_id' => $from->id], ['>', 'revision_at', $from->revision_at]]);
|
|
|
|
}
|
|
|
|
}
|