From 0f4ad90cd8dffc2c4d8fa0899e741ae70bc90776 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Kv=C3=A1=C4=8Dek?= Date: Wed, 27 Jan 2016 20:13:57 +0100 Subject: [PATCH 1/3] FEATURE: add overview to all static pages --- .../Resources/views/admin/overview.blade.php | 54 ++++++++++--------- resources/assets/css/admin.css | 28 ++++++++++ 2 files changed, 57 insertions(+), 25 deletions(-) diff --git a/modules/StaticPages/Resources/views/admin/overview.blade.php b/modules/StaticPages/Resources/views/admin/overview.blade.php index 783c6e80..c38adb17 100644 --- a/modules/StaticPages/Resources/views/admin/overview.blade.php +++ b/modules/StaticPages/Resources/views/admin/overview.blade.php @@ -8,44 +8,48 @@
@forelse($pages as $page)
-
+
-
- + {{--
--}} + {{----}} - -
-
- -

{{$page->title}}

+

{{$page->title}}

{{$page->created_at}} {{$page->author_name}} - @if ($page->meta_description) -

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

+

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

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

+ {{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..3983f666 100644 --- a/resources/assets/css/admin.css +++ b/resources/assets/css/admin.css @@ -34,6 +34,34 @@ 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%; + background:red; +} .red { color: red; From 1b2d7db8f37ae2344869a89125b55825a46e6029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Kv=C3=A1=C4=8Dek?= Date: Thu, 28 Jan 2016 13:38:30 +0100 Subject: [PATCH 2/3] FEATURE: add quick actions to static page overview --- .../Http/Controllers/Admin/EditStaticPage.php | 84 +++++++++++++++++++ modules/StaticPages/Http/routes.php | 18 +++- .../StaticPages/Resources/lang/en/admin.php | 10 ++- .../Resources/views/admin/overview.blade.php | 35 +++++--- resources/assets/css/admin.css | 1 - 5 files changed, 133 insertions(+), 15 deletions(-) diff --git a/modules/StaticPages/Http/Controllers/Admin/EditStaticPage.php b/modules/StaticPages/Http/Controllers/Admin/EditStaticPage.php index e5d86fc0..42315879 100644 --- a/modules/StaticPages/Http/Controllers/Admin/EditStaticPage.php +++ b/modules/StaticPages/Http/Controllers/Admin/EditStaticPage.php @@ -1,10 +1,94 @@ 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/routes.php b/modules/StaticPages/Http/routes.php index 40bc73c1..cbcee5fa 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,21 @@ Route::group([ // post method for creating new static page Route::post('new', 'CreateNewStaticPage@postNew') ->name('staticpage.newPost'); + + // 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..63ab29b6 100644 --- a/modules/StaticPages/Resources/lang/en/admin.php +++ b/modules/StaticPages/Resources/lang/en/admin.php @@ -18,6 +18,12 @@ return [ 'viewPage' => 'View page on web', 'edit' => 'Edit page', '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.' + ]; \ 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 c38adb17..2ded01a8 100644 --- a/modules/StaticPages/Resources/views/admin/overview.blade.php +++ b/modules/StaticPages/Resources/views/admin/overview.blade.php @@ -3,6 +3,18 @@ @section('content')

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

+ + @if (session('success')) +
+ {{ session('success') }} +
+ @endif + + @if (session('error')) +
+ {{ session('error') }} +
+ @endif
@@ -19,15 +31,15 @@ {{--
--}}

{{$page->title}}

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

- @if (trim($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) @@ -36,18 +48,19 @@

- - + {{----}} + @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 - +

diff --git a/resources/assets/css/admin.css b/resources/assets/css/admin.css index 3983f666..8583030c 100644 --- a/resources/assets/css/admin.css +++ b/resources/assets/css/admin.css @@ -60,7 +60,6 @@ #static-page-overview .keywords { margin-bottom: 0; font-size: 80%; - background:red; } .red { From 73ff4e93311aada97582d3b4f67953b1c5410406 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Kv=C3=A1=C4=8Dek?= Date: Thu, 28 Jan 2016 15:51:41 +0100 Subject: [PATCH 3/3] FEATURE #1372: add feature for editing static pages. User can edit static pages. The edit itself also stores history of changed columns. --- app/Models/Content.php | 55 ++++++ app/Models/ContentHistory.php | 5 +- .../Http/Controllers/Admin/EditStaticPage.php | 70 +++++++- .../Http/Requests/EditStaticPageRequest.php | 29 ++++ modules/StaticPages/Http/routes.php | 4 + .../StaticPages/Resources/lang/en/admin.php | 6 +- .../Resources/views/admin/edit.blade.php | 15 ++ .../Resources/views/admin/editForm.blade.php | 162 ++++++++++++++++++ 8 files changed, 341 insertions(+), 5 deletions(-) create mode 100644 modules/StaticPages/Http/Requests/EditStaticPageRequest.php create mode 100644 modules/StaticPages/Resources/views/admin/edit.blade.php create mode 100644 modules/StaticPages/Resources/views/admin/editForm.blade.php 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') + +
+

@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