FEATURE: improve logging
This commit is contained in:
@@ -1,13 +1,29 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace App\CMS\Observers;
|
namespace App\CMS\Observers;
|
||||||
|
|
||||||
|
use App\CMS\Contracts\Repositories\Content as ContentContract;
|
||||||
use App\Events\CreateNewContent;
|
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\Content as ContentModel;
|
||||||
use App\Models\Content\ContentHistory;
|
use App\Models\Content\ContentHistory;
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Content
|
||||||
|
*
|
||||||
|
* Model observer for logging changes on content page.
|
||||||
|
*
|
||||||
|
* @package App\CMS\Observers
|
||||||
|
*/
|
||||||
class Content
|
class Content
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
protected $changeset;
|
protected $changeset;
|
||||||
|
|
||||||
public function __construct()
|
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
|
* @param ContentModel $content
|
||||||
*/
|
*/
|
||||||
public function created(ContentModel $content)
|
public function created(ContentModel $content)
|
||||||
@@ -23,6 +74,14 @@ class Content
|
|||||||
Event::fire(new CreateNewContent($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.
|
* Get all dirty changed from model.
|
||||||
*
|
*
|
||||||
@@ -40,8 +99,9 @@ class Content
|
|||||||
|
|
||||||
$changes = $this->getNewAndOldValues(array_keys($changed), $changed, $original);
|
$changes = $this->getNewAndOldValues(array_keys($changed), $changed, $original);
|
||||||
|
|
||||||
if (!isset($changed['moduleSettings']))
|
if (!isset($changed['moduleSettings'])) {
|
||||||
return $changes;
|
return $changes;
|
||||||
|
}
|
||||||
|
|
||||||
$moduleSettings = (array)json_decode($changed['moduleSettings']);
|
$moduleSettings = (array)json_decode($changed['moduleSettings']);
|
||||||
$originalSettings = (array)json_decode($original['moduleSettings']);
|
$originalSettings = (array)json_decode($original['moduleSettings']);
|
||||||
@@ -54,7 +114,7 @@ class Content
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Get original and changed values in one array
|
||||||
*
|
*
|
||||||
* @param $fields
|
* @param $fields
|
||||||
* @param $new
|
* @param $new
|
||||||
@@ -62,7 +122,8 @@ class Content
|
|||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function getNewAndOldValues($fields, $new, $original) {
|
protected function getNewAndOldValues($fields, $new, $original)
|
||||||
|
{
|
||||||
$changes = [];
|
$changes = [];
|
||||||
foreach ($fields as $field) {
|
foreach ($fields as $field) {
|
||||||
$changes[$field] = [
|
$changes[$field] = [
|
||||||
@@ -74,29 +135,31 @@ class Content
|
|||||||
return $changes;
|
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 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++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user