REFACTORING: move part of content business logic into repository

This commit is contained in:
Michal Kváček
2016-10-18 15:36:26 +02:00
parent eabbf48ee0
commit 662700b4f2
19 changed files with 504 additions and 383 deletions
@@ -0,0 +1,162 @@
<?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\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
{
/**
* 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
* and updates status.
*
* @param $id
* @param $status
*
* @return \Illuminate\Http\RedirectResponse
*/
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 {
// 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'));
}
// try to change the page
list ($revision, $changedColumns) = $page->updateWithHistory(['status' => 'status'], ['status' => $status]);
// was something changed?
if ($changedColumns == 0) {
return redirect()
->route('content.edit', ['id' => $page->id])
->with('error', trans('admin/contents.nothingToEdit'));
}
// fire events
if ($status != Content::STATUS_DRAFT) {
Event::fire(new PublishContent($page, $revision));
} else {
Event::fire(new MakeDraftFromContent($page, $revision));
}
// redirect back to overview with info for user
return redirect()
->route('content.overview')
->with('success', trans('admin/contents.statusChanged'));
}
/**
* Delete page
*
* @param $id
*
* @return \Illuminate\Http\RedirectResponse
*/
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'));
}
// 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->module_handler, 'delete'], $page);
// trigger delete event
Event::fire(new DeleteContent($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
]));
}
/**
* Sets page as homepage
*
* @param $id
*
* @return \Illuminate\Http\RedirectResponse
*/
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'));
}
// disable any other existing homepage
Content::where('is_homepage', 1)
->where('language', LaravelLocalization::getCurrentLocale())
->update(['is_homepage' => 0]);
// set new homepage
list ($revision, $changedColumns) = $page->updateWithHistory(['is_homepage' => 'is_homepage'], ['is_homepage' => 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));
// redirect back to overview with info for user
return redirect()
->back()
->with('success', trans('admin/contents.homepageSetup', [
'name' => $page->title,
'url' => $page->url
]));
}
}
@@ -1,53 +1,45 @@
<?php
namespace App\Http\Controllers\Admin\Contents;
use App\CMS\Contracts\Repositories\Content as IContent;
use App\CMS\Contracts\Repositories\Module as IModule;
use App\CMS\Enumerations\ContentStatuses;
use App\Http\Controllers\Admin\Controller;
use App\Models\Content\Content;
use App\Models\Module;
use Illuminate\Support\Facades\Input;
class ContentOverview extends Controller
{
/**
* @var Content
* @var IContent
*/
private $content;
/**
* @var IModule
*/
private $module;
public function __construct(Content $content)
public function __construct(IContent $content, IModule $module)
{
parent::__construct();
$this->content = $content;
$this->module = $module;
}
/**
* Overview for all content pages.
*
* @param bool|false $moduleId
* @param null $status
* @param null|int $moduleId
* @param null $status
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function getOverview($moduleId = false, $status = null)
public function index($moduleId = null, $status = null)
{
$moduleFilterHandler = false;
$perPage = Input::get('perPage') ? Input::get('perPage') : 20;
$pageHeading = 'admin/contents.overview';
// does user want to filter results using module?
if (is_numeric($moduleId)) {
$module = Module::findOrFail($moduleId);
$moduleFilterHandler = $module->handler;
if (is_callable([$module->handler, 'getOverviewTitle'])) {
$pageHeading = call_user_func([$module->handler, 'getOverviewTitle']);
}
}
// get module settings
$module = $this->module->getOne($moduleId);
$pageHeading = $this->module->getOverviewTitle($module);
// get pages for overview
$pages = $this->content->getOverview($moduleFilterHandler, $status, true, true)
->paginate($perPage);
$handler = $module !== null ? $module->handler : null;
$pages = $this->content->getOverview($handler, [$status]);
// get homepage - for checking if homepage is set and available
$homepage = $this->content->getHomepage();
@@ -1,21 +1,19 @@
<?php
namespace App\Http\Controllers\Admin\Contents;
use App\CMS\Contracts\Repositories\Content as IContent;
use App\CMS\Contracts\Repositories\Module as IModuleRepository;
use App\CMS\Enumerations\ContentStatuses;
use App\Cms\Handlers\Interactions\Interaction;
use App\Cms\Interactions\Admin\Content\NewContent;
use App\Events\CreateNewContent as CreateNewContentEvent;
use App\Http\Controllers\Admin\Controller;
use App\Http\Requests\CreateNewContentRequest;
use App\Http\Requests\Request;
use App\Models\Content\Content;
use App\Models\FileManager\File;
use App\Models\Module;
use App\Models\Templates\Template;
use Illuminate\Support\Facades\Event;
use Modules\Categories\Entities\Category;
use Pingpong\Modules\Facades\Module as ModuleInstance;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Class CreateNewContent
@@ -31,23 +29,29 @@ class CreateNewContent extends Controller
*/
private $file;
/**
* @var Content
* @var IContent
*/
private $content;
/**
* @var IModuleRepository
*/
private $module;
/**
* CreateNewContent constructor.
*
* Sets File and Content models into class variables
*
* @param File $file
* @param Content $content
* @param File $file
* @param Content $content
* @param IModuleRepository $module
*/
public function __construct(File $file, Content $content)
public function __construct(File $file, IContent $content, IModuleRepository $module)
{
parent::__construct();
$this->file = $file;
$this->content = $content;
$this->module = $module;
}
/**
@@ -57,7 +61,7 @@ class CreateNewContent extends Controller
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function getNew($moduleId)
public function create($moduleId)
{
$module = Module::findOrFail($moduleId);
$statuses = ContentStatuses::getWithTranslations();
@@ -82,7 +86,7 @@ class CreateNewContent extends Controller
}
// create default content object
$this->content->status = Content::STATUS_DRAFT;
$this->content->status = IContent::STATUS_DRAFT;
// render view
return view('admin.content.new', [
@@ -110,21 +114,12 @@ class CreateNewContent extends Controller
*
* @return \Illuminate\Http\RedirectResponse
*/
public function postNew(Request $request, $moduleId)
public function store(Request $request, $moduleId)
{
try {
$module = Module::findOrFail($moduleId);
} catch (NotFoundHttpException $e) {
return redirect()
->route('content.overview')
->withInput()
->with('error', trans('admin/contents.invalidModule'));
}
$request->handler = $module->handler;
$module = Module::findOrFail($moduleId);
// create new page
Interaction::handle($request, new NewContent());
Interaction::handle($request, new NewContent(), $module->handler);
return redirect()
->route('content.overview')
@@ -6,10 +6,6 @@ use App\Cms\Handlers\Interactions\Interaction;
use App\CMS\Helpers\CurrentTemplate;
use App\Cms\Interactions\Admin\Content\EditContent as EditContentInteraction;
use App\Events\AttemptToEditInvalidContent;
use App\Events\DeleteContent;
use App\Events\MakeDraftFromContent;
use App\Events\PublishContent;
use App\Events\SetHomepage;
use App\Http\Controllers\Admin\Controller;
use App\Http\Requests\EditContentRequest;
use App\Http\Requests\Request;
@@ -19,7 +15,6 @@ use App\Models\FileManager\File;
use App\Models\Module;
use App\Models\Templates\Template;
use Illuminate\Support\Facades\Event;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Categories\Entities\Category;
use Pingpong\Modules\Facades\Module as ModuleInstance;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
@@ -56,7 +51,7 @@ class EditContent extends Controller
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function getEdit($id)
public function edit($id)
{
// select all content statuses
$statuses = ContentStatuses::getWithTranslations();
@@ -70,7 +65,7 @@ class EditContent extends Controller
->firstOrFail();
$pageHeading = trans('admin/contents.edit', ['name' => e($page->title)]);
if (is_callable([$module->handler, 'getCreateNewTitle'])) {
if (is_callable([$module->handler, 'getEditTitle'])) {
$pageHeading = call_user_func([$module->handler, 'getEditTitle'], $page);
}
@@ -120,7 +115,7 @@ class EditContent extends Controller
*
* @return \Illuminate\Http\RedirectResponse
*/
public function postEdit(Request $request, $id)
public function update(Request $request, $id)
{
try {
/**
@@ -154,152 +149,6 @@ class EditContent extends Controller
->with('success', trans('admin/contents.successfullyEdited'));
}
/**
* 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
* and updates status.
*
* @param $id
* @param $status
*
* @return \Illuminate\Http\RedirectResponse
*/
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 {
// 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'));
}
// try to change the page
list ($revision, $changedColumns) = $page->updateWithHistory(['status' => 'status'], ['status' => $status]);
// was something changed?
if ($changedColumns == 0) {
return redirect()
->route('content.edit', ['id' => $page->id])
->with('error', trans('admin/contents.nothingToEdit'));
}
// fire events
if ($status != Content::STATUS_DRAFT) {
Event::fire(new PublishContent($page, $revision));
} else {
Event::fire(new MakeDraftFromContent($page, $revision));
}
// redirect back to overview with info for user
return redirect()
->route('content.overview')
->with('success', trans('admin/contents.statusChanged'));
}
/**
* Delete page
*
* @param $id
*
* @return \Illuminate\Http\RedirectResponse
*/
public function deletePage($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'));
}
// 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->module_handler, 'delete'], $page);
// trigger delete event
Event::fire(new DeleteContent($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
]));
}
/**
* Sets page as homepage
*
* @param $id
*
* @return \Illuminate\Http\RedirectResponse
*/
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'));
}
// disable any other existing homepage
Content::where('is_homepage', 1)
->where('language', LaravelLocalization::getCurrentLocale())
->update(['is_homepage' => 0]);
// set new homepage
list ($revision, $changedColumns) = $page->updateWithHistory(['is_homepage' => 'is_homepage'], ['is_homepage' => 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));
// redirect back to overview with info for user
return redirect()
->back()
->with('success', trans('admin/contents.homepageSetup', [
'name' => $page->title,
'url' => $page->url
]));
}
/**
* Convert object of all request data into array of only wanted fields
*