changeset = uniqid(); } /** * 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) { if ($field == 'viewed') continue; $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) { Event::fire(new CreateNewContent($content)); } /** * @param ContentModel $content */ public function deleted(ContentModel $content) { Event::fire(new DeleteContent($content)); } /** * Get all dirty changed from model. * * If user did not change moduleSettings column, return result from ContentModel::getDirty(). Otherwise append all * changed columns from module settings. * * @param $changed * @param $original * * @return array */ protected function getDirtyFields($changed, $original) { unset($changed['updated_at']); $changes = $this->getNewAndOldValues(array_keys($changed), $changed, $original); if (!isset($changed['moduleSettings'])) { return $changes; } $moduleSettings = (array)json_decode($changed['moduleSettings']); $originalSettings = isset($original['moduleSettings']) ? (array)json_decode($original['moduleSettings']) : []; unset($changed['moduleSettings']); $settingChanged = array_keys(array_diff_assoc($moduleSettings, $originalSettings)); $changesInSettings = $this->getNewAndOldValues($settingChanged, $moduleSettings, $originalSettings); return array_merge($changes, $changesInSettings); } /** * Get original and changed values in one array * * @param $fields * @param $new * @param $original * * @return array */ 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 ]; } return $changes; } /** * 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) { 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++; } } }