changeset = uniqid(); } /** * @param ContentModel $content */ public function created(ContentModel $content) { Event::fire(new CreateNewContent($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 = (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); } /** * * * @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; } 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); } } /** * @param ContentModel $content */ public function deleted(ContentModel $content) { } }