diff --git a/app/Models/Content.php b/app/Models/Content.php index ce6e4b4f..c4224f31 100644 --- a/app/Models/Content.php +++ b/app/Models/Content.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Http\Requests\Request; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\DB; @@ -55,6 +56,60 @@ class Content extends Model return $query; } + /** + * Updates current model with information about changed columns + * + * @param array $fields + * @param Request $request + * + * @return int number of changed columns + */ + public function updateWithHistory(array $fields, Request $request) + { + $editBatch = uniqid(); + $changed = 0; + + foreach ($fields as $fieldDB => $fieldRequest) { + // remove any whitespaces around real content + $request->$fieldRequest = trim($request->$fieldRequest); + + if ($this->$fieldDB != $request->$fieldRequest) { + + // setup history object + $history = [ + 'user_id' => \Auth::user()->id, + 'content_id' => $this->id, + 'edit_batch' => $editBatch, + 'column' => $fieldRequest, + 'old_value' => $this->$fieldDB, + 'new_value' => $request->$fieldRequest + ]; + + // save history + ContentHistory::create($history); + + // save change into database + $this->$fieldDB = $request->$fieldRequest; + + $changed++; + } + } + + $this->save(); + + return $changed; + } + + /** + * Mark relationship with its historical records + * + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function history() + { + return $this->hasMany('App\Models\ContentHistory'); + } + /** * Mark relationship with users table * diff --git a/app/Models/ContentHistory.php b/app/Models/ContentHistory.php index e919aa4b..9c6e7ada 100644 --- a/app/Models/ContentHistory.php +++ b/app/Models/ContentHistory.php @@ -9,13 +9,14 @@ use Illuminate\Database\Eloquent\Model; */ class ContentHistory extends Model { + protected $table = 'content_history'; - public $timestamps = true; + // this table does not have created_at and updated_at fields + public $timestamps = false; protected $fillable = [ 'user_id', 'content_id', - 'changed_at', 'edit_batch', 'column', 'new_value', diff --git a/modules/StaticPages/Http/Controllers/Admin/EditStaticPage.php b/modules/StaticPages/Http/Controllers/Admin/EditStaticPage.php index 42315879..826eea47 100644 --- a/modules/StaticPages/Http/Controllers/Admin/EditStaticPage.php +++ b/modules/StaticPages/Http/Controllers/Admin/EditStaticPage.php @@ -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')); } /** diff --git a/modules/StaticPages/Http/Requests/EditStaticPageRequest.php b/modules/StaticPages/Http/Requests/EditStaticPageRequest.php new file mode 100644 index 00000000..bf5523ba --- /dev/null +++ b/modules/StaticPages/Http/Requests/EditStaticPageRequest.php @@ -0,0 +1,29 @@ + 'required', + 'url' => 'required|unique:contents,url,'.$this->id, + 'published_from' => 'date', + 'published_to' => 'date', + 'title_photo' => 'exists:files,id', + 'status' => 'required|in:'.$statuses + ]; + } +} diff --git a/modules/StaticPages/Http/routes.php b/modules/StaticPages/Http/routes.php index cbcee5fa..b0adb65b 100644 --- a/modules/StaticPages/Http/routes.php +++ b/modules/StaticPages/Http/routes.php @@ -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) diff --git a/modules/StaticPages/Resources/lang/en/admin.php b/modules/StaticPages/Resources/lang/en/admin.php index 63ab29b6..f0bfb95b 100644 --- a/modules/StaticPages/Resources/lang/en/admin.php +++ b/modules/StaticPages/Resources/lang/en/admin.php @@ -16,7 +16,7 @@ return [ 'successfullyCreated' => 'Page successfully created', 'noPagesAvailable' => 'No static pages available. Try to create one first.', 'viewPage' => 'View page on web', - 'edit' => 'Edit page', + 'edit' => 'Edit page :name', 'deletePage' => 'Delete page (ireversible!)', 'makePublic' => 'Publish page', 'makeProtected' => 'Publish page only for logged users', @@ -24,6 +24,8 @@ return [ 'invalidStatus' => 'Cannot change status to something undefined.', 'statusChanged' => 'Status successfully changed.', 'invalidPage' => 'Specified page does not exist.', - 'pageDeleted' => 'Page :name (:url) successfully deleted.' + 'pageDeleted' => 'Page :name (:url) successfully deleted.', + 'nothingToEdit' => 'You did not submit any changes. Nothing was edited.', + 'successfullyEdited' => 'Edit successfull' ]; \ No newline at end of file diff --git a/modules/StaticPages/Resources/views/admin/edit.blade.php b/modules/StaticPages/Resources/views/admin/edit.blade.php new file mode 100644 index 00000000..fad2438f --- /dev/null +++ b/modules/StaticPages/Resources/views/admin/edit.blade.php @@ -0,0 +1,15 @@ +@extends('admin.layout') + +@section('pageName', @trans('staticpages::admin.edit', ['name' => $page->title])) + +@section('content') + +