FEATURE #1372: add feature for editing static pages.
User can edit static pages. The edit itself also stores history of changed columns.
This commit is contained in:
@@ -3,13 +3,81 @@ 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)
|
||||
{
|
||||
return view('staticpages::index');
|
||||
// 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'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
namespace Modules\StaticPages\Http\Requests;
|
||||
|
||||
use App\Enumerations\ContentStatuses;
|
||||
use App\Http\Requests\Request;
|
||||
|
||||
class EditStaticPageRequest extends Request
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
// get all available statuses
|
||||
$statuses = ContentStatuses::getSimple();
|
||||
$statuses = implode(',', $statuses);
|
||||
|
||||
return [
|
||||
'title' => 'required',
|
||||
'url' => 'required|unique:contents,url,'.$this->id,
|
||||
'published_from' => 'date',
|
||||
'published_to' => 'date',
|
||||
'title_photo' => 'exists:files,id',
|
||||
'status' => 'required|in:'.$statuses
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,10 @@ Route::group([
|
||||
Route::post('new', 'CreateNewStaticPage@postNew')
|
||||
->name('staticpage.newPost');
|
||||
|
||||
// post method for creating new static page
|
||||
Route::post('edit/{id}', 'EditStaticPage@postEdit')
|
||||
->name('staticpage.editPost');
|
||||
|
||||
// routes for quick actions
|
||||
|
||||
// change status of page (protected, public or draft)
|
||||
|
||||
Reference in New Issue
Block a user