From 1a66485d48478f1b228d83920adef0ae107368ab Mon Sep 17 00:00:00 2001 From: Mike Koch Date: Tue, 23 Jan 2018 22:14:25 -0500 Subject: [PATCH] Working on adding audit trail to events --- .../Calendar/CalendarHandler.php | 35 +++++++++++++++++-- .../Tickets/AuditTrailEntityType.php | 1 + .../Calendar/CalendarController.php | 3 +- api/DataAccess/Calendar/CalendarGateway.php | 32 +++++++++++++++++ language/en/text.php | 4 +++ 5 files changed, 71 insertions(+), 4 deletions(-) diff --git a/api/BusinessLogic/Calendar/CalendarHandler.php b/api/BusinessLogic/Calendar/CalendarHandler.php index ad5a1775..2c4967d1 100644 --- a/api/BusinessLogic/Calendar/CalendarHandler.php +++ b/api/BusinessLogic/Calendar/CalendarHandler.php @@ -3,14 +3,20 @@ namespace BusinessLogic\Calendar; +use BusinessLogic\DateTimeHelpers; use BusinessLogic\Security\UserContext; +use BusinessLogic\Tickets\AuditTrailEntityType; +use DataAccess\AuditTrail\AuditTrailGateway; use DataAccess\Calendar\CalendarGateway; class CalendarHandler extends \BaseClass { private $calendarGateway; + private $auditTrailGateway; - public function __construct(CalendarGateway $calendarGateway) { + public function __construct(CalendarGateway $calendarGateway, + AuditTrailGateway $auditTrailGateway) { $this->calendarGateway = $calendarGateway; + $this->auditTrailGateway = $auditTrailGateway; } public function getEventsForStaff($searchEventsFilter, $heskSettings) { @@ -37,10 +43,25 @@ class CalendarHandler extends \BaseClass { throw new \Exception("Expected exactly 1 event, found: " . count($events)); } - return $events[0]; + $event = $events[0]; + + $this->auditTrailGateway->insertAuditTrailRecord($event->id, + AuditTrailEntityType::CALENDAR_EVENT, + 'audit_event_updated', + DateTimeHelpers::heskDate($heskSettings), + array(0 => $userContext->name . ' (' . $userContext->username . ')'), $heskSettings); + + return $event; } + /** + * @param $calendarEvent CalendarEvent + * @param $userContext UserContext + * @param $heskSettings array + * @return AbstractEvent + * @throws \Exception + */ public function createEvent($calendarEvent, $userContext, $heskSettings) { $this->calendarGateway->createEvent($calendarEvent, $userContext, $heskSettings); @@ -54,7 +75,15 @@ class CalendarHandler extends \BaseClass { throw new \Exception("Expected exactly 1 event, found: " . count($events)); } - return $events[0]; + $event = $events[0]; + + $this->auditTrailGateway->insertAuditTrailRecord($event->id, + AuditTrailEntityType::CALENDAR_EVENT, + 'audit_event_created', + DateTimeHelpers::heskDate($heskSettings), + array(0 => $userContext->name . ' (' . $userContext->username . ')'), $heskSettings); + + return $event; } public function deleteEvent($id, $userContext, $heskSettings) { diff --git a/api/BusinessLogic/Tickets/AuditTrailEntityType.php b/api/BusinessLogic/Tickets/AuditTrailEntityType.php index feea16f6..dafcad42 100644 --- a/api/BusinessLogic/Tickets/AuditTrailEntityType.php +++ b/api/BusinessLogic/Tickets/AuditTrailEntityType.php @@ -5,4 +5,5 @@ namespace BusinessLogic\Tickets; class AuditTrailEntityType extends \BaseClass { const TICKET = 'TICKET'; + const CALENDAR_EVENT = 'CALENDAR_EVENT'; } \ No newline at end of file diff --git a/api/Controllers/Calendar/CalendarController.php b/api/Controllers/Calendar/CalendarController.php index 4b7ab9cd..0440f54a 100644 --- a/api/Controllers/Calendar/CalendarController.php +++ b/api/Controllers/Calendar/CalendarController.php @@ -69,6 +69,7 @@ class CalendarController extends \BaseClass { function post() { /* @var $userContext UserContext */ + /* @var $hesk_settings array */ global $applicationContext, $hesk_settings, $userContext; $json = JsonRetriever::getJsonData(); @@ -78,7 +79,7 @@ class CalendarController extends \BaseClass { /* @var $calendarHandler CalendarHandler */ $calendarHandler = $applicationContext->get(CalendarHandler::clazz()); - return output($calendarHandler->createEvent($event, $userContext, $hesk_settings)); + return output($calendarHandler->createEvent($event, $userContext, $hesk_settings), 201); } function put($id) { diff --git a/api/DataAccess/Calendar/CalendarGateway.php b/api/DataAccess/Calendar/CalendarGateway.php index 1693e23d..705651d0 100644 --- a/api/DataAccess/Calendar/CalendarGateway.php +++ b/api/DataAccess/Calendar/CalendarGateway.php @@ -10,6 +10,8 @@ use BusinessLogic\Calendar\SearchEventsFilter; use BusinessLogic\Calendar\TicketEvent; use BusinessLogic\Helpers; use BusinessLogic\Security\UserContext; +use BusinessLogic\Tickets\AuditTrail; +use BusinessLogic\Tickets\AuditTrailEntityType; use Core\Constants\Priority; use DataAccess\CommonDao; use DataAccess\Logging\LoggingGateway; @@ -73,6 +75,36 @@ class CalendarGateway extends CommonDao { $event->reminderValue = $row['reminder_value'] === null ? null : floatval($row['reminder_value']); $event->reminderUnits = $row['reminder_unit'] === null ? null : ReminderUnit::getByValue($row['reminder_unit']); + $auditTrailSql = "SELECT `at`.`id` AS `id`, `at`.`entity_id`, `at`.`language_key`, `at`.`date`, + `values`.`replacement_index`, `values`.`replacement_values` + FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "audit_trail` AS `at` + INNER JOIN `" . hesk_dbEscape($heskSettings['db_pfix']) . "audit_trail_to_replacement_values` AS `values` + ON `at`.`id` = `values`.`audit_trail_id` + WHERE `entity_id` = " . intval($event->id) . " + AND `entity_type` = '" . AuditTrailEntityType::CALENDAR_EVENT . "'"; + $auditTrailRs = hesk_dbFetchAssoc($auditTrailSql); + $auditTrailEntry = null; + while ($row = hesk_dbFetchAssoc($rs)) { + if ($auditTrailEntry == null || intval($auditTrailEntry['id']) !== intval($row['id'])) { + if ($auditTrailEntry !== null) { + //$audit_records[] = $auditTrailEntry; + } + + $auditTrailEntry = new AuditTrail(); + $auditTrailEntry->id = $row['id']; + $auditTrailEntry->entityId = $row['entity_id']; + $auditTrailEntry->entityType = AuditTrailEntityType::CALENDAR_EVENT; + $auditTrailEntry->languageKey = $row['language_key']; + $auditTrailEntry->date = $row['date']; + $auditTrailEntry->replacementValues = array(); + } + $auditTrailEntry->replacementValues[intval($row['replacement_index'])] = $row['replacement_value']; + } + + if ($auditTrailEntry !== null) { + //$event->auditTrail[] = $audiTrailEntry; + } + $events[] = $event; } diff --git a/language/en/text.php b/language/en/text.php index e55cd0e4..6f5e9838 100644 --- a/language/en/text.php +++ b/language/en/text.php @@ -2214,5 +2214,9 @@ $hesklang['audit_due_date_changed'] = '%s changed due date to %s'; $hesklang['audit_linked_ticket'] = '%s linked ticket %s to this ticket'; $hesklang['audit_unlinked_ticket'] = '%s unlinked ticket %s'; +// Added or modified in Mods for HESK 3.3.0 +$hesklang['audit_event_created'] = '%s created event'; +$hesklang['audit_event_updated'] = '%s updated event'; + // DO NOT CHANGE BELOW if (!defined('IN_SCRIPT')) die('PHP syntax OK!');