FEATURE: improve logging
This commit is contained in:
@@ -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,41 +122,44 @@ class Content
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getNewAndOldValues($fields, $new, $original) {
|
||||
protected function getNewAndOldValues($fields, $new, $original)
|
||||
{
|
||||
$changes = [];
|
||||
foreach ($fields as $field) {
|
||||
$changes[$field] = [
|
||||
'new' => isset($new[$field]) ? $new[$field] : null,
|
||||
'old' => isset($original[$field])? $original[$field] : null
|
||||
'old' => isset($original[$field]) ? $original[$field] : null
|
||||
];
|
||||
}
|
||||
|
||||
return $changes;
|
||||
}
|
||||
|
||||
public function updated(ContentModel $content)
|
||||
/**
|
||||
* Fire events for publishing/making draft and setting homepage
|
||||
*
|
||||
* @param $field
|
||||
* @param $change
|
||||
* @param ContentModel $content
|
||||
* @param $normalFieldsEdited
|
||||
*/
|
||||
protected function fireEvent($field, $change, ContentModel $content, &$normalFieldsEdited)
|
||||
{
|
||||
$fields = $this->getDirtyFields($content->getDirty(), $content->getOriginal());
|
||||
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;
|
||||
|
||||
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']
|
||||
];
|
||||
case 'isHomepage':
|
||||
Event::fire(new SetHomepage($content, $this->changeset));
|
||||
break;
|
||||
|
||||
// save history
|
||||
ContentHistory::create($history);
|
||||
default:
|
||||
$normalFieldsEdited++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ContentModel $content
|
||||
*/
|
||||
public function deleted(ContentModel $content)
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user