Skip to content
Permalink
Browse files

Remove Redundancy

We can audit most ticket and task actions within the logEvent function rather than having to write individual logs for each one.
  • Loading branch information...
aydreeihn committed Jul 17, 2019
1 parent ff90638 commit 40771eaee20c1dc5d383ed289cc74f42cba0c947
Showing with 44 additions and 86 deletions.
  1. +18 −31 include/class.task.php
  2. +26 −55 include/class.ticket.php
@@ -556,11 +556,6 @@ function setStatus($status, $comments='', &$errors=array()) {
$ecb = function ($t) use($thisstaff) {
$t->logEvent('reopened', false, null, 'closed');
if (PluginManager::auditPlugin()) {
$type = array('type' => 'reopened');
Signal::send('object.edited', $this, $type);
}
if ($t->ticket) {
$t->ticket->reopen();
$vars = array(
@@ -588,10 +583,7 @@ function setStatus($status, $comments='', &$errors=array()) {
$this->closed = SqlFunction::NOW();
$ecb = function($t) use($thisstaff) {
$t->logEvent('closed');
if (PluginManager::auditPlugin()) {
$type = array('type' => 'closed');
Signal::send('object.edited', $t, $type);
}
if ($t->ticket) {
$vars = array(
'title' => sprintf('Task %s Closed',
@@ -660,6 +652,22 @@ function __toString() {
/* util routines */
function logEvent($state, $data=null, $user=null, $annul=null) {
if (PluginManager::auditPlugin()) {
switch ($state) {
case 'transferred':
case 'edited':
$type = $data;
$type['type'] = $state;
break;
case 'assigned':
break;
default:
$type = array('type' => $state);
break;
}
if ($type)
Signal::send('object.created', $this, $type);
}
$this->getThread()->getEvents()->log($this, $state, $data, $user, $annul);
}
@@ -869,12 +877,7 @@ function transfer(TransferForm $form, &$errors, $alert=true) {
return false;
// Log transfer event
$this->logEvent('transferred');
if (PluginManager::auditPlugin()) {
$type = array('type' => 'transferred', 'dept' => $dept->getName());
Signal::send('object.edited', $this, $type);
}
$this->logEvent('transferred', array('dept' => $dept->getName()));
// Post internal note if any
$note = $form->getField('comments')->getClean();
@@ -1336,10 +1339,6 @@ function update($forms, $vars, &$errors) {
if ($changes)
$this->logEvent('edited', array('fields' => $changes));
if (PluginManager::auditPlugin()) {
$type = array('type' => 'edited', 'fields' => $changes);
Signal::send('object.edited', $this, $type);
}
Signal::send('model.updated', $this);
return $this->save();
}
@@ -1388,14 +1387,8 @@ static function create($vars=false) {
$thread = TaskThread::create($task);
$thread->addDescription($vars);
$task->logEvent('created', null, $thisstaff);
if (PluginManager::auditPlugin()) {
$type = array('type' => 'created');
Signal::send('object.created', $task, $type);
}
// Get role for the dept
$role = $thisstaff->getRole($task->getDept());
// Assignment
@@ -1428,12 +1421,6 @@ function delete($comments='') {
$thread->delete();
$this->logEvent('deleted');
if (PluginManager::auditPlugin()) {
$type = array('type' => 'deleted');
Signal::send('object.deleted', $this, $type);
}
Draft::deleteForNamespace('task.%.' . $this->getId());
foreach (DynamicFormEntry::forObject($this->getId(), ObjectModel::OBJECT_TYPE_TASK) as $form)
@@ -1255,10 +1255,6 @@ function updateCollaborators($vars, &$errors) {
$collabs[] = (string) $c;
}
$this->logEvent('collab', array('del' => $collabs));
if (PluginManager::auditPlugin()) {
$type = array('type' => 'collab', 'del' => $collabs);
Signal::send('object.deleted', $this, $type);
}
}
//statuses
@@ -1511,11 +1507,6 @@ function setStatus($status, $comments='', &$errors=array(), $set_closing_agent=t
$this->closed = $this->lastupdate = $this->reopened = SqlFunction::NOW();
$ecb = function ($t) {
$t->logEvent('reopened', false, null, 'closed');
if (PluginManager::auditPlugin()) {
$type = array('type' => 'reopened');
Signal::send('object.edited', $t, $type);
}
};
}
@@ -2629,12 +2620,7 @@ function transfer(TransferForm $form, &$errors, $alert=true) {
$this->selectSLAId($slaId);
// Log transfer event
$this->logEvent('transferred');
if (PluginManager::auditPlugin()) {
$type = array('type' => 'transferred', 'dept' => $dept->getName());
Signal::send('object.edited', $this, $type);
}
$this->logEvent('transferred', array('dept' => $dept->getName()));
// Post internal note if any
$note = null;
@@ -2751,6 +2737,7 @@ function assignToStaff($staff, $note, $alert=true, $user=null) {
$this->logEvent('assigned', $data, $user);
if (PluginManager::auditPlugin()) {
$thisstaff = $staff;
$type = array('type' => 'assigned', 'claim' => true);
Signal::send('object.edited', $this, $type);
}
@@ -2866,23 +2853,12 @@ function unassign() {
}
function release($info=array(), &$errors) {
$type = array('type' => 'released');
if ($info['sid'] && $info['tid']) {
if (PluginManager::auditPlugin())
Signal::send('object.edited', $this, $type);
if ($info['sid'] && $info['tid'])
return $this->unassign();
}
elseif ($info['sid'] && $this->setStaffId(0)) {
if (PluginManager::auditPlugin())
Signal::send('object.edited', $this, $type);
elseif ($info['sid'] && $this->setStaffId(0))
return true;
}
elseif ($info['tid'] && $this->setTeamId(0)) {
if (PluginManager::auditPlugin())
Signal::send('object.edited', $this, $type);
elseif ($info['tid'] && $this->setTeamId(0))
return true;
}
return false;
}
@@ -3000,12 +2976,7 @@ function changeOwner($user) {
if ($c)
$c->delete();
$this->logEvent('edited', array('owner' => $user->getId()));
if (PluginManager::auditPlugin()) {
$type = array('type' => 'edited', 'fields' => array('Ticket Owner' => $user->getName()->name));
Signal::send('object.edited', $this, $type);
}
$this->logEvent('edited', array('owner' => $user->getId(), 'fields' => array('Ticket Owner' => $user->getName()->name)));
return true;
}
@@ -3413,6 +3384,26 @@ function logActivity($title, $note) {
// History log -- used for statistics generation (pretty reports)
function logEvent($state, $data=null, $user=null, $annul=null) {
if (PluginManager::auditPlugin()) {
switch ($state) {
case 'collab':
case 'transferred':
$type = $data;
$type['type'] = $state;
break;
case 'edited':
$type = array('type' => $state, 'fields' => $data['fields'] ? $data['fields'] : $data);
break;
case 'assigned':
case 'referred':
break;
default:
$type = array('type' => $state);
break;
}
if ($type)
Signal::send('object.created', $this, $type);
}
if ($this->getThread())
$this->getThread()->getEvents()->log($this, $state, $data, $user, $annul);
}
@@ -3558,11 +3549,6 @@ function delete($comments='') {
$this->logEvent('deleted');
if (PluginManager::auditPlugin()) {
$type = array('type' => 'deleted');
Signal::send('object.deleted', $this, $type);
}
foreach (DynamicFormEntry::forTicket($this->getId()) as $form)
$form->delete();
@@ -3715,11 +3701,6 @@ function update($vars, &$errors) {
if ($changes) {
$this->logEvent('edited', $changes);
if (PluginManager::auditPlugin()) {
$type = array('type' => 'edited', 'fields' => $changes['fields'] ?: $changes);
Signal::send('object.edited', $this, $type);
}
}
@@ -3810,11 +3791,6 @@ function updateField($form, &$errors) {
// Record the changes
$this->logEvent('edited', $changes);
if (PluginManager::auditPlugin()) {
$type = array('type' => 'edited', 'fields' => $changes['fields'] ?: $changes);
Signal::send('object.edited', $this, $type);
}
// Log comments (if any)
if (($comments = $form->getField('comments')->getClean())) {
$title = sprintf(__('%s updated'), __($field->getLabel()));
@@ -4322,11 +4298,6 @@ static function create($vars, &$errors, $origin, $autorespond=true,
// Start tracking ticket lifecycle events (created should come first!)
$ticket->logEvent('created', null, $thisstaff ?: $user);
if (PluginManager::auditPlugin()) {
$type = array('type' => 'created');
Signal::send('object.created', $ticket, $type);
}
// Add collaborators (if any)
if (isset($vars['ccs']) && count($vars['ccs']))
$ticket->addCollaborators($vars['ccs'], array(), $errors);

0 comments on commit 40771ea

Please sign in to comment.
You can’t perform that action at this time.