Source of file LoggingExtension.php
Size: 2,702 Bytes - Last Modified: 2021-12-24T06:50:28+00:00
/var/www/docs.ssmods.com/process/src/src/LoggingExtension.php
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 | <?php namespace Gurucomkz\DataObjectLogger; use SilverStripe\ORM\DataExtension; use SilverStripe\ORM\DataObject; use SilverStripe\Security\Security; /** * @property-read DataObject $owner */ class LoggingExtension extends DataExtension { public function classValidToLog() { $excluded = LogUtils::config()->get('excluded_classes'); return !in_array($this->owner->ClassName, $excluded); } public function onAfterDelete() { parent::onAfterDelete(); if (!$this->classValidToLog()) { return; //don't log the log entries :) } $this->doLog(ActivityLogEntry::ACTION_DELETE); } public function onBeforeWrite() { parent::onBeforeWrite(); if (!$this->classValidToLog()) { return; //don't log the log entries :) } $this->owner->_logger_being_created = !$this->owner->isInDB(); } public function onAfterWrite() { parent::onAfterWrite(); if (!$this->classValidToLog()) { return; //don't log the log entries :) } if (!$this->owner->_logger_being_created && !count($this->owner->getChangedFields(null, DataObject::CHANGE_VALUE))) { return; } $this->doLog($this->owner->_logger_being_created ? ActivityLogEntry::ACTION_CREATE : ActivityLogEntry::ACTION_UPDATE); } public function onAfterArchive() { if (!$this->classValidToLog()) { return; //don't log the log entries :) } $this->doLog(ActivityLogEntry::ACTION_ARCHIVE); } public function onAfterPublish() { if (!$this->classValidToLog()) { return; //don't log the log entries :) } $this->doLog(ActivityLogEntry::ACTION_PUBLISH); } public function onAfterRevertToLive() { if (!$this->classValidToLog()) { return; //don't log the log entries :) } $this->doLog(ActivityLogEntry::ACTION_UNARCHIVE); } private function doLog($action, $details = '') { try { $entry = new ActivityLogEntry(); if (!$details) { $details = json_encode($this->owner->toMap(), JSON_PRETTY_PRINT|JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES); } $entry->Action = $action; $entry->Details = $details; $entry->ObjectID = $this->owner->ID; $entry->ObjectClass = $this->owner->ClassName; $user = Security::getCurrentUser(); $entry->MemberID = $user ? $user->ID : 0; $entry->write(); } catch (\Throwable $e) { } } } |