FEATURE: improve logging

This commit is contained in:
Michal Kváček
2016-10-25 20:13:34 +02:00
parent 8cb06bf641
commit f940930c66
+86 -23
View File
@@ -1,13 +1,29 @@
<?php
namespace App\CMS\Observers;
use App\CMS\Contracts\Repositories\Content as ContentContract;
use App\Events\CreateNewContent;
use App\Events\DeleteContent;
use App\Events\EditContent;
use App\Events\MakeDraftFromContent;
use App\Events\PublishContent;
use App\Events\SetHomepage;
use App\Models\Content\Content as ContentModel;
use App\Models\Content\ContentHistory;
use Illuminate\Support\Facades\Event;
/**
* Class Content
*
* Model observer for logging changes on content page.
*
* @package App\CMS\Observers
*/
class Content
{
/**
* @var string
*/
protected $changeset;
public function __construct()
@@ -16,6 +32,41 @@ class Content
}
/**
* Log changes on edit
*
* @param ContentModel $content
*/
public function updated(ContentModel $content)
{
$fields = $this->getDirtyFields($content->getDirty(), $content->getOriginal());
$normalFieldsEdited = 0;
foreach ($fields as $field => $change) {
$history = [
'user_id' => \Auth::user()->id,
'content_id' => $content->id,
'edit_batch' => $this->changeset,
'column' => $field,
'old_value' => $change['old'],
'new_value' => $change['new']
];
// save history
ContentHistory::create($history);
// fire event
$this->fireEvent($field, $change, $content, $normalFieldsEdited);
}
if ($normalFieldsEdited > 0) {
Event::fire(new EditContent($content, $this->changeset));
}
}
/**
* Fire event when new instance of model is stored into database
*
* @param ContentModel $content
*/
public function created(ContentModel $content)
@@ -23,6 +74,14 @@ class Content
Event::fire(new CreateNewContent($content));
}
/**
* @param ContentModel $content
*/
public function deleted(ContentModel $content)
{
Event::fire(new DeleteContent($content));
}
/**
* Get all dirty changed from model.
*
@@ -40,8 +99,9 @@ class Content
$changes = $this->getNewAndOldValues(array_keys($changed), $changed, $original);
if (!isset($changed['moduleSettings']))
if (!isset($changed['moduleSettings'])) {
return $changes;
}
$moduleSettings = (array)json_decode($changed['moduleSettings']);
$originalSettings = (array)json_decode($original['moduleSettings']);
@@ -54,7 +114,7 @@ class Content
}
/**
*
* Get original and changed values in one array
*
* @param $fields
* @param $new
@@ -62,7 +122,8 @@ class Content
*
* @return array
*/
protected function getNewAndOldValues($fields, $new, $original) {
protected function getNewAndOldValues($fields, $new, $original)
{
$changes = [];
foreach ($fields as $field) {
$changes[$field] = [
@@ -74,29 +135,31 @@ class Content
return $changes;
}
public function updated(ContentModel $content)
{
$fields = $this->getDirtyFields($content->getDirty(), $content->getOriginal());
foreach ($fields as $field => $change) {
$history = [
'user_id' => \Auth::user()->id,
'content_id' => $content->id,
'edit_batch' => $this->changeset,
'column' => $field,
'old_value' => $change['old'],
'new_value' => $change['new']
];
// save history
ContentHistory::create($history);
}
}
/**
* Fire events for publishing/making draft and setting homepage
*
* @param $field
* @param $change
* @param ContentModel $content
* @param $normalFieldsEdited
*/
public function deleted(ContentModel $content)
protected function fireEvent($field, $change, ContentModel $content, &$normalFieldsEdited)
{
switch ($field) {
case 'status':
if ($change['new'] !== ContentContract::STATUS_DRAFT) {
Event::fire(new PublishContent($content, $this->changeset));
} else {
Event::fire(new MakeDraftFromContent($content, $this->changeset));
}
break;
case 'isHomepage':
Event::fire(new SetHomepage($content, $this->changeset));
break;
default:
$normalFieldsEdited++;
}
}
}