From f940930c66aca20bcef01464fb31437591205b74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Kv=C3=A1=C4=8Dek?= Date: Tue, 25 Oct 2016 20:13:34 +0200 Subject: [PATCH] FEATURE: improve logging --- app/CMS/Observers/Content.php | 111 ++++++++++++++++++++++++++-------- 1 file changed, 87 insertions(+), 24 deletions(-) diff --git a/app/CMS/Observers/Content.php b/app/CMS/Observers/Content.php index a44a88be..4f81bf98 100644 --- a/app/CMS/Observers/Content.php +++ b/app/CMS/Observers/Content.php @@ -1,13 +1,29 @@ 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) - { - } } \ No newline at end of file