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 e5d86fc0..826eea47 100644 --- a/modules/StaticPages/Http/Controllers/Admin/EditStaticPage.php +++ b/modules/StaticPages/Http/Controllers/Admin/EditStaticPage.php @@ -1,10 +1,162 @@ 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')); + } } } \ No newline at end of file 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 40bc73c1..b0adb65b 100644 --- a/modules/StaticPages/Http/routes.php +++ b/modules/StaticPages/Http/routes.php @@ -8,7 +8,6 @@ Route::group([ Route::get('/{url}', 'StaticPagesController@index'); }); - // routes group for admin area Route::group([ 'middleware' => ['web', 'auth'], @@ -31,4 +30,25 @@ Route::group([ // post method for creating new static page 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) + Route::get('status/{id}/{status}', 'EditStaticPage@changeStatus') + ->name('staticpage.changeStatus'); + + + // delete page + Route::get('delete/{id}', 'EditStaticPage@deletePage') + ->name('staticpage.delete'); + + // display change history of given page + // Route::get('history/{id}', 'StaticPageHistory@getHistory')->name('staticpage.history'); + + + // Route::get('', '')->name('staticpage.'); }); \ No newline at end of file diff --git a/modules/StaticPages/Resources/lang/en/admin.php b/modules/StaticPages/Resources/lang/en/admin.php index 45a8ab68..f0bfb95b 100644 --- a/modules/StaticPages/Resources/lang/en/admin.php +++ b/modules/StaticPages/Resources/lang/en/admin.php @@ -16,8 +16,16 @@ 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', - 'makeDraft' => 'Disable page and make it draft' + 'makePublic' => 'Publish page', + 'makeProtected' => 'Publish page only for logged users', + 'makeDraft' => 'Disable page and make it draft', + 'invalidStatus' => 'Cannot change status to something undefined.', + 'statusChanged' => 'Status successfully changed.', + 'invalidPage' => 'Specified page does not exist.', + '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') + +
+

@lang('staticpages::admin.edit', ['name' => $page->title])

+ +
+ +
+ @include('staticpages::admin.editForm') +
+@stop \ No newline at end of file diff --git a/modules/StaticPages/Resources/views/admin/editForm.blade.php b/modules/StaticPages/Resources/views/admin/editForm.blade.php new file mode 100644 index 00000000..15842660 --- /dev/null +++ b/modules/StaticPages/Resources/views/admin/editForm.blade.php @@ -0,0 +1,162 @@ +@if (session('success')) +
+ {{ session('success') }} +
+@endif + +@if (session('error')) +
+ {{ session('error') }} +
+@endif + +
+ {!! csrf_field() !!} + + + +
+
+
+
+ +
+
+ +
+
+ +
+
+ + + @if (isset($errors) && $errors->has('status')) +
+ {{ $errors->first('status') }} +
+ @endif +
+ +
+ +
+
+ +
+
+
+
+
+
+ +
+ +
+ +
+
+
+ +
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+ +
+
+
+ +@section('head_stylesheets') + +@stop + +@section('javascripts') + + + +@stop \ No newline at end of file diff --git a/modules/StaticPages/Resources/views/admin/overview.blade.php b/modules/StaticPages/Resources/views/admin/overview.blade.php index 783c6e80..2ded01a8 100644 --- a/modules/StaticPages/Resources/views/admin/overview.blade.php +++ b/modules/StaticPages/Resources/views/admin/overview.blade.php @@ -3,49 +3,66 @@ @section('content')

@lang('staticpages::admin.overview')

+ + @if (session('success')) +
+ {{ session('success') }} +
+ @endif + + @if (session('error')) +
+ {{ session('error') }} +
+ @endif
@forelse($pages as $page)
-
+
-
- + {{--
--}} + {{----}} - -
-
- -

{{$page->title}}

- {{$page->created_at}} +

{{$page->title}}

+ {{date('d.m.Y', strtotime($page->created_at))}} {{$page->author_name}} - @if ($page->meta_description) -

{{str_limit($page->meta_description)}}

- @else -

{{str_limit(strip_tags($page->content))}}

- @endif +

+ @if (trim($page->meta_description)) + {{str_limit($page->meta_description)}} + @else + {{str_limit(strip_tags($page->content))}} + @endif +

@if ($page->meta_keywords)

{{$page->meta_keywords}}

@endif +

+ + {{----}} + + + @if($page->status == \App\Models\Content::STATUS_DRAFT) + + + @endif + + @if(in_array($page->status, [\App\Models\Content::STATUS_PROTECTED, \App\Models\Content::STATUS_PUBLIC])) + + @endif + + +

+
@empty diff --git a/resources/assets/css/admin.css b/resources/assets/css/admin.css index 50b8e40f..8583030c 100644 --- a/resources/assets/css/admin.css +++ b/resources/assets/css/admin.css @@ -34,6 +34,33 @@ list-style-type: none; } +#static-page-overview .page { + position: relative; + padding-bottom: 3em; +} + +#static-page-overview .page .controls { + position: absolute; + bottom: 0; +} + +#static-page-overview .page .controls .button { + margin-bottom: 10px; +} + +#static-page-overview .date, #static-page-overview .author { + font-size: 80%; + +} + +#static-page-overview .perex { + +} + +#static-page-overview .keywords { + margin-bottom: 0; + font-size: 80%; +} .red { color: red;