Files
core/modules/StaticPages/Http/Controllers/Admin/EditStaticPage.php
T
Michal Kváček 73ff4e9331 FEATURE #1372: add feature for editing static pages.
User can edit static pages. The edit itself also stores history of changed columns.
2016-01-28 15:51:41 +01:00

162 lines
5.1 KiB
PHP

<?php
namespace Modules\StaticPages\Http\Controllers\Admin;
use App\Enumerations\ContentStatuses;
use App\Models\Content;
use Modules\StaticPages\Http\Requests\EditStaticPageRequest;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class EditStaticPage extends Controller
{
/**
* 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);
// render view
return view('staticpages::admin.edit', [
'contentStatuses' => $statuses,
'page' => $page
]);
}
/**
* 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::where('module_handler', config('staticpages.handler'))
->findOrFail($id);
} catch (NotFoundHttpException $e) {
// page for editing not found
return redirect()
->route('staticpage.overview')
->with('error', trans('staticpages::admin.invalidPage'));
}
// array of fields in database and fields from request
$fields = [
'title' => 'title',
'url' => 'url',
'published_from' => 'publishedFrom',
'published_to' => 'publishedTo',
'meta_keywords' => 'metaKeywords',
'meta_description' => 'metaDescription',
'content' => 'pageContent',
'status' => 'status'
];
// save new values with history
$changedColumns = $page->updateWithHistory($fields, $request);
// was something changed?
if ($changedColumns == 0) {
return redirect()
->route('staticpage.edit', ['id' => $page->id])
->with('error', trans('staticpages::admin.nothingToEdit'));
}
// something was changed, inform user
return redirect()
->route('staticpage.edit', ['id' => $page->id])
->with('success', trans('staticpages::admin.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('staticpage.overview')
->with('error', trans('staticpages::admin.invalidStatus'));
}
try {
// try to find this page
$page = Content::where('module_handler', config('staticpages.handler'))
->findOrFail($id);
// page found, save status
$page->status = $status;
$page->save();
// redirect back to overview with info for user
return redirect()
->route('staticpage.overview')
->with('success', trans('staticpages::admin.statusChanged'));
} catch (NotFoundHttpException $e) {
// page not found
return redirect()
->route('staticpage.overview')
->with('error', trans('staticpages::admin.invalidPage'));
}
}
/**
* Delete page
*
* @param $id
*
* @return \Illuminate\Http\RedirectResponse
*/
public function deletePage($id)
{
try {
// try to find this page
$page = Content::where('module_handler', config('staticpages.handler'))
->findOrFail($id);
// page found, save status
$page->status = Content::STATUS_DELETED;
$page->save();
// redirect back to overview with info for user
return redirect()
->route('staticpage.overview')
->with('success', trans('staticpages::admin.pageDeleted', [
'name' => $page->title,
'url' => $page->url
]));
} catch (NotFoundHttpException $e) {
// page not found
return redirect()
->route('staticpage.overview')
->with('error', trans('staticpages::admin.invalidPage'));
}
}
}