FEATURE #1374: add logging for static pages

This commit is contained in:
Michal Kváček
2016-01-29 01:57:12 +01:00
parent 593506c646
commit ee55715093
16 changed files with 424 additions and 35 deletions
+1
View File
@@ -14,6 +14,7 @@ class ActionLogType extends Model
protected $fillable = [
'name',
'string_id',
'value'
];
+14 -10
View File
@@ -2,7 +2,6 @@
namespace App\Models;
use App\Http\Requests\Request;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
@@ -59,21 +58,26 @@ class Content extends Model
/**
* Updates current model with information about changed columns
*
* @param array $fields
* @param Request $request
* @param array $fields
* @param array $request
*
* @return int number of changed columns
*/
public function updateWithHistory(array $fields, Request $request)
public function updateWithHistory(array $fields, array $request)
{
$editBatch = uniqid();
$changed = 0;
foreach ($fields as $fieldDB => $fieldRequest) {
// remove any whitespaces around real content
$request->$fieldRequest = trim($request->$fieldRequest);
// ignore non-existing fields
if (!isset($this->$fieldDB) || !isset($request[$fieldRequest])) {
continue;
}
if ($this->$fieldDB != $request->$fieldRequest) {
// remove any whitespaces around real content
$request[$fieldRequest] = trim($request[$fieldRequest]);
if ($this->$fieldDB != $request[$fieldRequest]) {
// setup history object
$history = [
@@ -82,14 +86,14 @@ class Content extends Model
'edit_batch' => $editBatch,
'column' => $fieldRequest,
'old_value' => $this->$fieldDB,
'new_value' => $request->$fieldRequest
'new_value' => $request[$fieldRequest]
];
// save history
ContentHistory::create($history);
// save change into database
$this->$fieldDB = $request->$fieldRequest;
$this->$fieldDB = $request[$fieldRequest];
$changed++;
}
@@ -97,7 +101,7 @@ class Content extends Model
$this->save();
return $changed;
return [$editBatch, $changed];
}
/**
@@ -0,0 +1,25 @@
<?php
namespace Modules\StaticPages\Events;
use App\Events\Event;
use App\Models\Content;
use Illuminate\Queue\SerializesModels;
class CreateNewStaticPage extends Event {
use SerializesModels;
/**
* @var Content
*/
public $page;
/**
* Create a new event instance.
*
* @param Content $page
*/
public function __construct(Content $page)
{
$this->page = $page;
}
}
@@ -0,0 +1,27 @@
<?php
namespace Modules\StaticPages\Events;
use App\Events\Event;
use App\Models\Content;
use Illuminate\Queue\SerializesModels;
class DeleteStaticPage extends Event {
use SerializesModels;
/**
* @var Content
*/
public $page;
public $revision;
/**
* Create a new event instance.
*
* @param Content $page
* @param $revision
*/
public function __construct(Content $page, $revision)
{
$this->page = $page;
$this->revision = $revision;
}
}
@@ -0,0 +1,28 @@
<?php
namespace Modules\StaticPages\Events;
use App\Events\Event;
use App\Models\Content;
use Illuminate\Queue\SerializesModels;
class EditStaticPage extends Event {
use SerializesModels;
/**
* @var Content
*/
public $page;
public $revision;
/**
* Create a new event instance.
*
* @param Content $page
* @param $revision
*/
public function __construct(Content $page, $revision)
{
$this->page = $page;
$this->revision = $revision;
}
}
@@ -0,0 +1,28 @@
<?php
namespace Modules\StaticPages\Events;
use App\Events\Event;
use App\Models\Content;
use Illuminate\Queue\SerializesModels;
class MakeDraftFromStaticPage extends Event {
use SerializesModels;
/**
* @var Content
*/
public $page;
public $revision;
/**
* Create a new event instance.
*
* @param Content $page
* @param $revision
*/
public function __construct(Content $page, $revision)
{
$this->page = $page;
$this->revision = $revision;
}
}
@@ -0,0 +1,29 @@
<?php
namespace Modules\StaticPages\Events;
use App\Events\Event;
use App\Models\Content;
use Illuminate\Queue\SerializesModels;
class PublishStaticPage extends Event {
use SerializesModels;
/**
* @var Content
*/
public $page;
public $revision;
/**
* Create a new event instance.
*
* @param Content $page
* @param $revision
*/
public function __construct(Content $page, $revision)
{
$this->page = $page;
$this->revision = $revision;
}
}
@@ -3,6 +3,7 @@ namespace Modules\StaticPages\Http\Controllers\Admin;
use App\Enumerations\ContentStatuses;
use App\Models\Content;
use Illuminate\Support\Facades\Event;
use Modules\StaticPages\Http\Requests\CreateNewStaticPageRequest;
class CreateNewStaticPage extends Controller
@@ -50,6 +51,8 @@ class CreateNewStaticPage extends Controller
$page->save();
Event::fire(new \Modules\StaticPages\Events\CreateNewStaticPage($page));
return redirect()
->route('staticpage.new')
->with('success', trans('staticpages::admin.successfullyCreated'));
@@ -3,6 +3,11 @@ namespace Modules\StaticPages\Http\Controllers\Admin;
use App\Enumerations\ContentStatuses;
use App\Models\Content;
use Illuminate\Support\Facades\Event;
use Modules\StaticPages\Events\DeleteStaticPage;
use Modules\StaticPages\Events\EditStaticPage as EditStaticPageEvent;
use Modules\StaticPages\Events\MakeDraftFromStaticPage;
use Modules\StaticPages\Events\PublishStaticPage;
use Modules\StaticPages\Http\Requests\EditStaticPageRequest;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
@@ -65,7 +70,7 @@ class EditStaticPage extends Controller
];
// save new values with history
$changedColumns = $page->updateWithHistory($fields, $request);
list($revision, $changedColumns) = $page->updateWithHistory($fields, $request->all());
// was something changed?
if ($changedColumns == 0) {
@@ -74,6 +79,9 @@ class EditStaticPage extends Controller
->with('error', trans('staticpages::admin.nothingToEdit'));
}
// fire event with
Event::fire(new EditStaticPageEvent($page, $revision));
// something was changed, inform user
return redirect()
->route('staticpage.edit', ['id' => $page->id])
@@ -108,22 +116,34 @@ class EditStaticPage extends Controller
// 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'));
}
// try to change the page
list ($revision, $changedColumns) = $page->updateWithHistory(['status' => 'status'], ['status' => $status]);
// was something changed?
if ($changedColumns == 0) {
return redirect()
->route('staticpage.edit', ['id' => $page->id])
->with('error', trans('staticpages::admin.nothingToEdit'));
}
// fire events
if ($status != Content::STATUS_DRAFT) {
Event::fire(new PublishStaticPage($page, $revision));
} else {
Event::fire(new MakeDraftFromStaticPage($page, $revision));
}
// redirect back to overview with info for user
return redirect()
->route('staticpage.overview')
->with('success', trans('staticpages::admin.statusChanged'));
}
/**
@@ -139,24 +159,31 @@ class EditStaticPage extends Controller
// 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'));
}
list ($revision, $changedColumns) = $page->updateWithHistory(['status' => 'status'], ['status' => Content::STATUS_DELETED]);
// was something changed?
if ($changedColumns == 0) {
return redirect()
->route('staticpage.edit', ['id' => $page->id])
->with('error', trans('staticpages::admin.nothingToEdit'));
}
// trigger delete event
Event::fire(new DeleteStaticPage($page, $revision));
// 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
]));
}
}
@@ -0,0 +1,27 @@
<?php
namespace Modules\StaticPages\Listeners;
use App\Listeners\Logger;
use Modules\StaticPages\Events\CreateNewStaticPage;
class LogCreateNewStaticPage extends Logger {
/**
* Getter for action name. Used only internally
*
* @return mixed
*/
protected function getName()
{
return 'staticpages::createNew';
}
public function handle(CreateNewStaticPage $event) {
$parameters = [
'id' => $event->page->id,
'url' => $event->page->url,
'title' => $event->page->title
];
$this->saveLog($parameters);
}
}
@@ -0,0 +1,30 @@
<?php
namespace Modules\StaticPages\Listeners;
use App\Listeners\Logger;
use Modules\StaticPages\Events\DeleteStaticPage;
class LogDeleteStaticPage extends Logger
{
/**
* Getter for action name. Used only internally
*
* @return mixed
*/
protected function getName()
{
return 'staticpages::delete';
}
public function handle(DeleteStaticPage $event)
{
$parameters = [
'id' => $event->page->id,
'url' => $event->page->url,
'title' => $event->page->title,
'revision' => $event->revision
];
$this->saveLog($parameters);
}
}
@@ -0,0 +1,30 @@
<?php
namespace Modules\StaticPages\Listeners;
use App\Listeners\Logger;
use Modules\StaticPages\Events\EditStaticPage;
class LogEditStaticPage extends Logger
{
/**
* Getter for action name. Used only internally
*
* @return mixed
*/
protected function getName()
{
return 'staticpages::edit';
}
public function handle(EditStaticPage $event)
{
$parameters = [
'id' => $event->page->id,
'url' => $event->page->url,
'title' => $event->page->title,
'revision' => $event->revision
];
$this->saveLog($parameters);
}
}
@@ -0,0 +1,30 @@
<?php
namespace Modules\StaticPages\Listeners;
use App\Listeners\Logger;
use Modules\StaticPages\Events\MakeDraftFromStaticPage;
class LogMakeDraftFromStaticPage extends Logger
{
/**
* Getter for action name. Used only internally
*
* @return mixed
*/
protected function getName()
{
return 'staticpages::makeDraft';
}
public function handle(MakeDraftFromStaticPage $event)
{
$parameters = [
'id' => $event->page->id,
'url' => $event->page->url,
'title' => $event->page->title,
'revision' => $event->revision
];
$this->saveLog($parameters);
}
}
@@ -0,0 +1,30 @@
<?php
namespace Modules\StaticPages\Listeners;
use App\Listeners\Logger;
use Modules\StaticPages\Events\PublishStaticPage;
class LogPublishStaticPage extends Logger
{
/**
* Getter for action name. Used only internally
*
* @return mixed
*/
protected function getName()
{
return 'staticpages::publish';
}
public function handle(PublishStaticPage $event)
{
$parameters = [
'id' => $event->page->id,
'url' => $event->page->url,
'title' => $event->page->title,
'revision' => $event->revision
];
$this->saveLog($parameters);
}
}
@@ -0,0 +1,40 @@
<?php
namespace Modules\StaticPages\Providers;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
'Modules\StaticPages\Events\CreateNewStaticPage' => [
'Modules\StaticPages\Listeners\LogCreateNewStaticPage'
],
'Modules\StaticPages\Events\EditStaticPage' => [
'Modules\StaticPages\Listeners\LogEditStaticPage'
],
'Modules\StaticPages\Events\PublishStaticPage' => [
'Modules\StaticPages\Listeners\LogPublishStaticPage'
],
'Modules\StaticPages\Events\DeleteStaticPage' => [
'Modules\StaticPages\Listeners\LogDeleteStaticPage'
],
];
/**
* Register any other events for your application.
*
* @param \Illuminate\Contracts\Events\Dispatcher $events
* @return void
*/
public function boot(DispatcherContract $events)
{
parent::boot($events);
}
}
@@ -1,6 +1,7 @@
<?php namespace Modules\Staticpages\Providers;
use App\Enumerations\Modules;
use App\Models\ActionLogType;
use App\Models\Module;
use Illuminate\Support\ServiceProvider;
@@ -40,7 +41,8 @@ class StaticPagesServiceProvider extends ServiceProvider
*/
public function register()
{
//
// register event provider for firing module-specific events
$this->app->register('Modules\StaticPages\Providers\EventServiceProvider');
}
/**
@@ -58,6 +60,34 @@ class StaticPagesServiceProvider extends ServiceProvider
'name' => 'staticpages::module.name',
'description' => 'staticpages::module.description'
]);
// insert action log types into database
ActionLogType::create([
'string_id' => 'staticpages::createNew',
'name' => 'staticpages::log.nameCreateNew',
'value' => 'staticpages::log.nameCreateNew'
]);
ActionLogType::create([
'string_id' => 'staticpages::edit',
'name' => 'staticpages::log.nameEdit',
'value' => 'staticpages::log.edit'
]);
ActionLogType::create([
'string_id' => 'staticpages::delete',
'name' => 'staticpages::log.nameDelete',
'value' => 'staticpages::log.delete'
]);
ActionLogType::create([
'string_id' => 'staticpages::makeDraft',
'name' => 'staticpages::log.nameMakeDraft',
'value' => 'staticpages::log.makeDraft'
]);
ActionLogType::create([
'string_id' => 'staticpages::publish',
'name' => 'staticpages::log.namePublish',
'value' => 'staticpages::log.publish'
]);
}
}