FEATURE #1507: modularize content management

This commit is contained in:
Michal Kváček
2016-03-11 15:43:11 +01:00
parent 3407707046
commit 9f5bd46ca3
23 changed files with 1018 additions and 19 deletions
@@ -0,0 +1,36 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Models\Content;
use App\Models\Module;
use Illuminate\Support\Facades\Input;
class ContentOverview extends Controller
{
/**
* Overview for all content pages.
*
* @param bool|false $moduleId
* @param null $status
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function getOverview($moduleId = false, $status = null)
{
$moduleFilterHandler = false;
$perPage = Input::get('perPage') ? Input::get('perPage') : 20;
// does user want to filter results using module?
if (is_numeric($moduleId)) {
$module = Module::findOrFail($moduleId);
$moduleFilterHandler = $module->handler;
}
// get pages for overview
$model = new Content();
$pages = $model->getOverview($moduleFilterHandler, $status, true)
->paginate($perPage);
return view('admin.content.overview', ['pages' => $pages]);
}
}
@@ -0,0 +1,93 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Enumerations\ActiveContentStatuses;
use App\Models\Content;
use App\Models\Module;
use App\Models\Template;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Str;
use Modules\StaticPages\Http\Requests\CreateNewStaticPageRequest;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class CreateNewContent extends Controller
{
/**
* Display form for new content page
*
* @param $moduleId
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function getNew($moduleId)
{
$module = Module::findOrFail($moduleId);
$statuses = ActiveContentStatuses::getWithTranslations();
$templates = Template::where('module_id', $moduleId)
->select(['template_name', 'name', 'description'])
->get();
// create default content object
$page = new Content();
$page->status = Content::STATUS_DRAFT;
// render view
return view('admin.content.new', [
'contentStatuses' => $statuses,
'templates' => $templates,
'moduleId' => $moduleId,
'page' => $page,
'moduleName' => trans($module->name),
'formSectionNames' => call_user_func([$module->handler, 'getExtendedFormSections']),
'formSections' => call_user_func([$module->handler, 'getExtendedFormInputsCreate']),
]);
}
/**
* Save new content page
*
* @param CreateNewStaticPageRequest $request
*
* @param $moduleId
*
* @return \Illuminate\Http\RedirectResponse
*/
public function postNew(CreateNewContentRequest $request, $moduleId)
{
$page = new Content();
try {
$module = Module::findOrFail($moduleId);
} catch (NotFoundHttpException $e) {
return redirect()
->route('content.overview')
->withInput()
->with('error', trans('admin/contents.invalidModule'));
}
// setup basic info
$page->user_id = \Auth::user()->id;
$page->title = $request->title;
$page->url = Str::slug($request->url);
$page->published_from = $request->publishedFrom;
$page->published_to = $request->publishedTo;
$page->meta_keywords = $request->meta_description;
$page->meta_description = $request->meta_description;
$page->content = $request->pageContent;
$page->status = $request->status;
$page->template = $request->template;
$page->module_handler = $module->handler;
// save module-specific stuff
call_user_func_array([$module->handler, 'createNew'], [&$page, $request]);
$page->save();
Event::fire(new \Modules\StaticPages\Events\CreateNewStaticPage($page));
return redirect()
->route('content.overview')
->with('success', trans('admin/contents.successfullyCreated'));
}
}
+266
View File
@@ -0,0 +1,266 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Enumerations\ContentStatuses;
use App\Enumerations\ModulesHandlerToId;
use App\Models\Content;
use App\Models\ContentHistory;
use App\Models\Template;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Str;
use Modules\StaticPages\Events\DeleteStaticPage;
use Modules\StaticPages\Events\EditStaticPage as EditStaticPageEvent;
use Modules\StaticPages\Events\MakeDraftFromStaticPage;
use Modules\StaticPages\Events\PublishStaticPage;
use Modules\StaticPages\Http\Requests\EditStaticPageRequest;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class EditContent extends Controller
{
/**
* @var ContentHistory
*/
private $history;
public function __construct(ContentHistory $history)
{
$this->history = $history;
}
/**
* Display form for editing static page
*
* @param $id
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function getEdit($id)
{
// select all content statuses
$statuses = ContentStatuses::getWithTranslations();
// try to find requested page
$page = Content::where('module_handler', config('staticpages.handler'))
->findOrFail($id);
$allModules = ModulesHandlerToId::getWithTranslations();
$moduleId = $allModules[config('staticpages.handler')];
$templates = Template::where('module_id', $moduleId)
->select(['template_name', 'name', 'description'])
->get();
$history = $this->history->getStructuredHistoryOf($page);
// render view
return view('admin.content.edit', [
'contentStatuses' => $statuses,
'page' => $page,
'templates' => $templates,
'formSectionNames' => call_user_func([$page->module_handler, 'getExtendedFormSections']),
'formSections' => call_user_func([$page->module_handler, 'getExtendedFormInputsCreate']),
'historySize' => count($history['changesets'])
]);
}
/**
* Edit static page and save changes into history.
*
* @param EditStaticPageRequest $request
* @param $id
*
* @return \Illuminate\Http\RedirectResponse
*/
public function postEdit(EditStaticPageRequest $request, $id)
{
try {
$page = Content::findOrFail($id);
} catch (NotFoundHttpException $e) {
// page for editing not found
return redirect()
->route('content.overview')
->with('error', trans('admin/contents.invalidPage'));
}
$request->url = Str::slug($request->url);
// array of fields in database and fields from request
$fields = [
'title' => 'title',
'url' => 'url',
'template' => 'template',
'published_from' => 'publishedFrom',
'published_to' => 'publishedTo',
'meta_keywords' => 'metaKeywords',
'meta_description' => 'metaDescription',
'content' => 'pageContent',
'status' => 'status'
];
// save new values with history
list($revision, $changedColumns) = $page->updateWithHistory($fields, $request->all());
$changedColumns += call_user_func_array([$page->module_handler, 'edit'], [&$page, $request, $revision]);
// was something changed?
if ($changedColumns == 0) {
return redirect()
->route('content.edit', ['id' => $page->id])
->with('error', trans('admin/contents.nothingToEdit'));
}
// fire event with
Event::fire(new EditStaticPageEvent($page, $revision));
// something was changed, inform user
return redirect()
->route('content.edit', ['id' => $page->id])
->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();
unset($statuses[Content::STATUS_DELETED]);
// 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 PublishStaticPage($page, $revision));
} else {
Event::fire(new MakeDraftFromStaticPage($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'));
}
list ($revision, $changedColumns) = $page->updateWithHistory(['status' => 'status'], ['status' => Content::STATUS_DELETED]);
// was something changed?
if ($changedColumns == 0) {
return redirect()
->route('content.edit', ['id' => $page->id])
->with('error', trans('admin/contents.nothingToEdit'));
}
// trigger delete event
Event::fire(new DeleteStaticPage($page, $revision));
// redirect back to overview with info for user
return redirect()
->route('content.overview')
->with('success', trans('admin/contents.pageDeleted', [
'name' => $page->title,
'url' => $page->url
]));
}
/**
* 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);
$homepage = $page->getHomepage();
} catch (NotFoundHttpException $e) {
// page not found
return redirect()
->route('content.overview')
->with('error', trans('admin/contents.invalidPage'));
}
list ($revision, $changedColumns) = $page->updateWithHistory(['is_homepage' => 'is_homepage'], ['is_homepage' => 1]);
if ($homepage)
$homepage->updateWithHistory(['isHomepage' => 'is_homepage'], ['ishomepage' => 0]);
// was something changed?
if ($changedColumns == 0) {
return redirect()
->route('content.overview')
->with('error', trans('admin/contents.nothingToEdit'));
}
// trigger delete event
// Event::fire(new DeleteStaticPage($page, $revision));
// redirect back to overview with info for user
return redirect()
->route('content.overview')
->with('success', trans('admin/contents.homepageSetup', [
'name' => $page->title,
'url' => $page->url
]));
}
}