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( $form, $category->id, $form->slug, $form->published_at, $form->video, $type ); $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(clone $post); //$this->saveRevision(clone $post); //print_r($post->translations); die; $category = $this->categories->get($form->category_id); $post->edit( $form, $category->id, $form->slug, $form->published_at, $form->video, $post->type ); if ($form->image) { $post->cleanFiles(); $post->setImage($form->image); } elseif ($form->video && (!$post->image || $form->reset_image)) { $post->cleanFiles(); $src = VideoHelper::getThumb($form->video); $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->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); // Remove revisions $this->clearHistory($post); $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): int { $post = $this->posts->get($id); $from = $this->posts->get($from_id); // update revision id BlogPost::updateAll(['revision_id' => $from->id], ['revision_id' => $post->id]); $this->posts->remove($post); $from->revision_id = null; $from->type = BlogPost::TYPE_PUBLIC; $this->posts->save($from); // delete never revisions BlogPost::deleteAll(['AND', ['revision_id' => $from->id], ['>', 'revision_at', $from->revision_at]]); return $from->id; } public function restoreHistory2($from_id, $id): void { $post = $this->posts->get($id); $from = $this->posts->get($from_id); $post->category_id = $from->category_id; $post->published_at = $from->published_at; $post->created_at = $from->created_at; $post->updated_at = $from->updated_at; $post->video = $from->video; $post->revision_at = $from->revision_at; $post->status = $from->status; $post->slug = $from->slug; $this->posts->save($post); // remove distance translation foreach ( $post->translations as $translate ) { /* @var $translate ActiveRecord */ $translate->delete(); } // move source translation foreach ( $from->translations as $translate ) { /* @var $translate ActiveRecord */ $translate->blog_post_id = $post->id; $translate->save(); } // 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]]); } /*public function saveRevision2(BlogPost $model) { if (!$model->revision_id) { $revision = clone $model; $revision->id = null; $revision->isNewRecord = true; $revision->revision_at = $revision->updated_at; $revision->revision_id = $model->id; $revision->type = BlogPost::TYPE_REVISION; $revision->_form = new BlogPostForm($model); $revision->save(); // tags foreach ($model->tags as $tag) { $revision->assignTag($tag->id); } $revision->save(); if ($model->image) { $path = Yii::getAlias( '@staticRoot/origin/posts' ); $parts = pathinfo( $model->image ); copy( $path . '/' . $model->id . '.' . $parts['extension'], $path . '/' . $revision->id . '.' . $parts['extension'] ); } } }*/ /*public function saveRevision(BlogPost $model) { if (!$model->revision_id) { $blogForm = new BlogPostForm($model); $blog = BlogPost::create( $blogForm, $blogForm->category_id, $blogForm->slug, $blogForm->published_at, $blogForm->video, BlogPost::TYPE_REVISION ); //$blog->id = null; //$blog->isNewRecord = true; $blog->revision_at = $model->updated_at; $blog->revision_id = $model->id; $this->transaction->wrap(function () use ($blog, $model) { foreach ( $model->tags as $tag ) { $blog->assignTag( $tag->id ); } $this->posts->save($blog); //print_r($blog->translations); die; //print_r($model->translations); die; if ($model->image) { $path = Yii::getAlias( '@staticRoot/origin/posts' ); $parts = pathinfo( $model->image ); copy( $path . '/' . $model->id . '.' . $parts['extension'], $path . '/' . $blog->id . '.' . $parts['extension'] ); } }); } }*/ }