REFACTORING #1880 @1h: move content actions into interactions

This commit is contained in:
Michal Kváček
2016-10-25 20:13:12 +02:00
parent 000352052d
commit 8cb06bf641
4 changed files with 138 additions and 105 deletions
@@ -2,23 +2,37 @@
namespace App\CMS\Interactions\Admin\Content;
use App\CMS\Contracts\Interaction;
use App\Http\Requests\Request;
use App\CMS\Enumerations\ContentStatuses;
class ChangeStatus implements Interaction {
public function handle(Request $data, $parameters)
class ChangeStatus implements Interaction
{
/**
* Change status of content page
*
* @param array $data
* @param array $parameters
*
* @return mixed|void
*/
public function handle($data, $parameters)
{
list ($page) = $parameters;
$page->status = $data['status'];
$page->save();
}
/**
* Check if status is valid
*
* @param array $parameters
*
* @return array
*/
public function getValidationRules($parameters = [])
{
$statuses = ContentStatuses::getWithTranslations();
// check if given status is valid
if (!isset($statuses[$status])) {
return redirect()
->route('content.overview')
->with('error', trans('admin/contents.invalidStatus'));
}
}
public function getValidationRules($parameters)
{
// TODO: Implement getValidationRules() method.
return ['status' => 'in:' . implode(',', array_keys($statuses))];
}
}
@@ -0,0 +1,45 @@
<?php
namespace App\CMS\Interactions\Admin\Content;
use App\CMS\Contracts\Interaction;
use App\CMS\Contracts\Repositories\Module;
use App\Models\Content\Content;
use Illuminate\Support\Facades\App;
class DeletePage implements Interaction
{
/**
* Perform given action
*
* @param $data array input data
* @param $parameters array parameters
*
* @return void
*/
public function handle($data, $parameters)
{
/**
* @var $page Content
* @var $module Module
*/
$module = App::make(Module::class);
list ($page) = $parameters;
// delete page
$page->delete();
// let module to delete some extra content created during page's life
$module->callOnString($page->moduleHandler, 'delete', [$page]);
}
/**
* @param $data array input data
*
* @return array
*/
public function getValidationRules($data = [])
{
return [];
}
}
@@ -0,0 +1,43 @@
<?php
namespace App\CMS\Interactions\Admin\Content;
use App\CMS\Contracts\Interaction;
use App\CMS\Enumerations\ContentStatuses;
use App\Models\Content\Content;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
class SetHomepage implements Interaction
{
/**
* Set homepage of given page
*
* @param array $data
* @param array $parameters
*
* @return void
*/
public function handle($data, $parameters)
{
list ($page) = $parameters;
// disable any other existing homepage
Content::where('isHomepage', true)
->where('language', LaravelLocalization::getCurrentLocale())
->update(['isHomepage' => false]);
// set new homepage
$page->isHomepage = true;
$page->save();
}
/**
* Check if status is valid
*
* @param array $parameters
*
* @return array
*/
public function getValidationRules($parameters = [])
{
return [];
}
}
@@ -1,19 +1,25 @@
<?php
namespace App\Http\Controllers\Admin\Contents;
use App\CMS\Enumerations\ContentStatuses;
use App\Events\DeleteContent;
use App\Events\MakeDraftFromContent;
use App\Events\PublishContent;
use App\Events\SetHomepage;
use App\CMS\Contracts\Repositories\Content as ContentContract;
use App\Cms\Handlers\Interactions\Interaction;
use App\CMS\Interactions\Admin\Content\ChangeStatus;
use App\CMS\Interactions\Admin\Content\DeletePage;
use App\CMS\Interactions\Admin\Content\SetHomepage;
use App\Http\Controllers\Admin\Controller;
use App\Models\Content\Content;
use Illuminate\Support\Facades\Event;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ContentActions extends Controller
{
/**
* @var ContentContract
*/
private $content;
public function __construct(ContentContract $content)
{
$this->content = $content;
}
/**
* Method for changing page status
* This method is called in static page overview from quick actions. It checks if status is valid, if page exists
@@ -26,36 +32,9 @@ class ContentActions extends Controller
*/
public function changeStatus($id, $status)
{
// get possible statuses
$statuses = ContentStatuses::getWithTranslations();
// check if given status is valid
if (!isset($statuses[$status])) {
return redirect()
->route('content.overview')
->with('error', trans('admin/contents.invalidStatus'));
}
// try to find this page
$page = Content::findOrFail($id);
// try to change the page
list ($revision, $changedColumns) = $page->updateWithHistory(['status' => 'status'], ['status' => $status]);
// fire events
if ($status != Content::STATUS_DRAFT) {
Event::fire(new PublishContent($page, $revision));
} else {
Event::fire(new MakeDraftFromContent($page, $revision));
}
// was something changed?
if ($changedColumns == 0) {
return redirect()
->route('content.edit', ['id' => $page->id])
->with('error', trans('admin/contents.nothingToEdit'));
}
$page = $this->content->getOne($id);
Interaction::handle(ChangeStatus::class, ['status' => $status], $page);
// redirect back to overview with info for user
return redirect()
@@ -72,36 +51,15 @@ class ContentActions extends Controller
*/
public function delete($id)
{
try {
// try to find this page
$page = Content::findOrFail($id);
} catch (NotFoundHttpException $e) {
// page not found
return redirect()
->route('content.overview')
->with('error', trans('admin/contents.invalidPage'));
}
$page = $this->content->getOne($id);
// change url to something unique
$oldURL = $page->url;
$page->url .= uniqid('_deleted_');
$page->save();
// delete page
$page->delete();
// let module to delete some extra content created during page's life
call_user_func([$page->moduleHandler, 'delete'], $page);
// trigger delete event
Event::fire(new DeleteContent($page));
Interaction::handle(DeletePage::class, [], $page);
// redirect back to overview with info for user
return redirect()
->route('content.overview')
->with('success', trans('admin/contents.pageDeleted', [
'name' => $page->title,
'url' => $oldURL
'name' => $page->title
]));
}
@@ -114,36 +72,9 @@ class ContentActions extends Controller
*/
public function setHomepage($id)
{
try {
// try to find this page
/**
* @var $page Content
*/
$page = Content::findOrFail($id);
} catch (NotFoundHttpException $e) {
// page not found
return redirect()
->back()
->with('error', trans('admin/contents.invalidPage'));
}
$page = $this->content->getOne($id);
// disable any other existing homepage
Content::where('isHomepage', 1)
->where('language', LaravelLocalization::getCurrentLocale())
->update(['isHomepage' => 0]);
// set new homepage
list ($revision, $changedColumns) = $page->updateWithHistory(['isHomepage' => 'isHomepage'], ['isHomepage' => 1]);
// was something changed?
if ($changedColumns == 0) {
return redirect()
->back()
->with('error', trans('admin/contents.nothingToEdit'));
}
// trigger delete event
Event::fire(new SetHomepage($page, $revision));
Interaction::handle(SetHomepage::class, [], $page);
// redirect back to overview with info for user
return redirect()