public function save() { // Fill client hidden sections $user = new User(); $person = new Person(); $user->load(EGS_USERNAME); if (!is_null($user->person_id)) { $person->load($user->person_id); } // Is this quick entry? if ($this->_data['TicketResponse']['type'] == 'quick') { $this->_data['TicketResponse']['type'] = 'site'; $config = new TicketConfigurationCollection(new TicketConfiguration()); $sh = new SearchHandler($config); $sh->AddConstraint(new Constraint('usercompanyid', '=', EGS_COMPANY_ID)); $config->load($sh); $config = $config->getContents(); if (count($config) !== 1) { // Make one. $newConfig['usercompanyid'] = EGS_COMPANY_ID; $priority = self::findDefault(new TicketPriorityCollection(new TicketPriority())); $newConfig['client_ticket_priority_default'] = $priority->id; $newConfig['internal_ticket_priority_default'] = $priority->id; $severity = self::findDefault(new TicketSeverityCollection(new TicketSeverity())); $newConfig['client_ticket_severity_default'] = $severity->id; $newConfig['internal_ticket_severity_default'] = $severity->id; $queue = self::findDefault(new TicketQueueCollection(new TicketQueue())); $newConfig['ticket_queue_default'] = $queue->id; $category = self::findDefault(new TicketCategoryCollection(new TicketCategory())); $newConfig['ticket_category_default'] = $category->id; $status = self::findDefault(new TicketStatusCollection(new TicketStatus())); $newConfig['client_ticket_status_default'] = $status->id; $newConfig['internal_ticket_status_default'] = $status->id; $config = TicketConfiguration::Factory($newConfig); } else { $config = $config[0]; } $this->_data['Ticket']['client_ticket_priority_id'] = $config->client_ticket_priority_default; $this->_data['Ticket']['ticket_queue_id'] = $config->ticket_queue_default; } $this->_data['Ticket']['originator_person_id'] = $user->username; $this->_data['Ticket']['originator_company_id'] = $user->lastcompanylogin; $this->_data['Ticket']['internal_ticket_severity_id'] = $this->_data['Ticket']['client_ticket_severity_id']; $this->_data['Ticket']['internal_ticket_priority_id'] = $this->_data['Ticket']['client_ticket_priority_id']; if (!isset($this->_data['Ticket']['id'])) { // Force 'new' status initialy $ts = new TicketStatusCollection(new TicketStatus()); $sh = new SearchHandler($ts); $sh->addConstraint(new Constraint('usercompanyid', '=', EGS_COMPANY_ID)); $sh->addConstraint(new Constraint('status_code', '=', 'NEW')); $ts->load($sh); $statuses = $ts->getContents(); $status = $statuses[0]; // Should only ever be one status, this should be regulated by earlier validation $this->_data['Ticket']['client_ticket_status_id'] = $status->id; $this->_data['Ticket']['internal_ticket_status_id'] = $status->id; } if (isset($this->_data['Ticket']['id'])) { $originalTicket = new Ticket(); $originalTicket->load($this->_data['Ticket']['id']); $changes = array(array('param' => 'client_ticket_status_id', 'friendly' => 'Status', 'object' => 'TicketStatus'), array('param' => 'client_ticket_priority_id', 'friendly' => 'Priority', 'object' => 'TicketPriority'), array('param' => 'client_ticket_severity_id', 'friendly' => 'Severity', 'object' => 'TicketSeverity'), array('param' => 'ticket_queue_id', 'friendly' => 'Queue', 'object' => 'TicketQueue')); $changeText = array(); foreach ($changes as $change) { if ($this->_data['Ticket'][$change['param']] != $originalTicket->{$change}['param']) { $was = new $change['object'](); $now = new $change['object'](); $was->load($originalTicket->{$change}['param']); $now->load($this->_data['Ticket'][$change['param']]); $changeText[] = $change['friendly'] . ': was ' . $was->name . ' now ' . $now->name . '.'; } } if (count($changeText) > 0) { $errors = array(); $ticketResponse = TicketResponse::Factory(array('ticket_id' => $this->_data['Ticket']['id'], 'internal' => 'false', 'body' => implode("\n", $changeText), 'type' => 'status', 'owner' => EGS_USERNAME), $errors, 'TicketResponse'); $ticketResponse->save(); $queue = new TicketQueue(); $queue->load($originalTicket->ticket_queue_id); // Send mail $headers = array('From' => $queue->email_address); $header_string = ""; foreach ($headers as $header => $value) { $header_string .= $header . ': ' . $value . "\r\n"; } $body = TicketingUtils::StatusPlate($originalTicket) . implode("\n", $changeText); $recipients = TicketingUtils::GetRecipients($originalTicket); foreach ($recipients as $recipient) { mail($recipient, 're: [' . $originalTicket->ticket_queue_id . '-' . $originalTicket->id . '] ' . $originalTicket->summary, $body, $header_string); } } } parent::save('Ticket'); $ticket_id = $this->_data['id']; $this->_data['Ticket']['id'] = $this->_data['id']; if (isset($this->_data['id'])) { $this->_data['TicketResponse']['ticket_id'] = $ticket_id; parent::save('TicketResponse'); } sendTo('Client', 'view', array('ticketing'), array('id' => $ticket_id)); }
public function save() { // Set some defaults $errors = array(); $flash = Flash::Instance(); if (isset($this->_data['Ticket']['id']) && $this->_data['Ticket']['id'] != '') { $originalTicket = new Ticket(); $originalTicket->load($this->_data['Ticket']['id']); // Check for Client Status/Priority/Severity/Queue change $changes = array(array('param' => 'client_ticket_status_id', 'friendly' => 'Status', 'object' => 'TicketStatus'), array('param' => 'client_ticket_priority_id', 'friendly' => 'Priority', 'object' => 'TicketPriority'), array('param' => 'client_ticket_severity_id', 'friendly' => 'Severity', 'object' => 'TicketSeverity'), array('param' => 'ticket_queue_id', 'friendly' => 'Queue', 'object' => 'TicketQueue')); $changeText = array(); foreach ($changes as $change) { if ($this->_data['Ticket'][$change['param']] != $originalTicket->{$change}['param']) { $was = new $change['object'](); $now = new $change['object'](); $was->load($originalTicket->{$change}['param']); $now->load($this->_data['Ticket'][$change['param']]); $changeText[] = $change['friendly'] . ': was ' . $was->name . ' now ' . $now->name . '.'; } } if (count($changeText) > 0) { $ticketResponse = TicketResponse::Factory(array('ticket_id' => $this->_data['Ticket']['id'], 'internal' => 'false', 'body' => implode("\n", $changeText), 'type' => 'status', 'owner' => EGS_USERNAME), $errors, 'TicketResponse'); $ticketResponse->save(); if (!empty($this->_data['TicketResponse']['body']) && !isset($this->_data['TicketResponse']['internal'])) { $changeText[] = $this->_data['TicketResponse']['body']; } // Send mail $headers = array('From' => TicketingUtils::getReplyAddress($originalTicket)); $header_string = ""; foreach ($headers as $header => $value) { $header_string .= $header . ': ' . $value . "\r\n"; } $body = TicketingUtils::StatusPlate($originalTicket) . implode("\n", $changeText); $recipients = TicketingUtils::GetRecipients($originalTicket); foreach ($recipients as $recipient) { mail($recipient, 're: [' . $originalTicket->ticket_queue_id . '-' . $originalTicket->id . '] ' . $originalTicket->summary, $body, $header_string); } } // Check for Internal Status/Priority/Severity/Assigned to/Originator Person/Company change $changes = array(array('param' => 'internal_ticket_status_id', 'friendly' => 'Internal Status', 'object' => 'TicketStatus'), array('param' => 'internal_ticket_priority_id', 'friendly' => 'Internal Priority', 'object' => 'TicketPriority'), array('param' => 'internal_ticket_severity_id', 'friendly' => 'Internal Severity', 'object' => 'TicketSeverity'), array('param' => 'assigned_to', 'friendly' => 'Assigned To', 'object' => 'User', 'fields' => array('username')), array('param' => 'originator_person_id', 'friendly' => 'Person', 'object' => 'Person'), array('param' => 'originator_company_id', 'friendly' => 'Company', 'object' => 'Company')); $changeText = array(); foreach ($changes as $change) { if ($this->_data['Ticket'][$change['param']] != $originalTicket->{$change}['param']) { $was = new $change['object'](); $now = new $change['object'](); $was->load($originalTicket->{$change}['param']); $now->load($this->_data['Ticket'][$change['param']]); if (!isset($change['fields'])) { $was_value = $was->name; } else { $t = ''; foreach ($change['fields'] as $field) { $t .= $was->{$field} . ' '; } $t = rtrim($t); $was_value = $t; } if (!isset($change['fields'])) { $now_value = $now->name; } else { $t = ''; foreach ($change['fields'] as $field) { $t .= $now->{$field} . ' '; } $t = rtrim($t); $now_value = $t; } $changeText[] = $change['friendly'] . ': was ' . $was_value . ' now ' . $now_value . '.'; } } if (count($changeText) > 0) { $ticketResponse = TicketResponse::Factory(array('ticket_id' => $this->_data['Ticket']['id'], 'internal' => 'true', 'body' => implode("\n", $changeText), 'type' => 'status', 'owner' => EGS_USERNAME), $errors, 'TicketResponse'); $ticketResponse->save(); } // Assignation changed? if ($this->_data['Ticket']['assigned_to'] != $originalTicket->assigned_to and $this->_data['Ticket']['assigned_to'] != '') { // Find email address for user $user = new User(); $user->loadBy('username', $this->_data['Ticket']['assigned_to']); if (!is_null($user->person_id)) { $person = new Person(); $person->load($user->person_id); $to = $person->email->contactmethod; } else { $to = ''; } if (empty($to)) { $to = $user->email; } if ($to != '') { $body = "The following ticket has been assigned to you:\n"; $body .= '[' . $this->_data['Ticket']['ticket_queue_id'] . '-' . $this->_data['Ticket']['id'] . '] ' . $this->_data['Ticket']['summary'] . "\n"; $header_string = 'From: ' . TicketingUtils::getReplyAddress($originalTicket); mail($to, 'You\'ve been assigned: [' . $this->_data['Ticket']['ticket_queue_id'] . '-' . $this->_data['Ticket']['id'] . '] ' . $this->_data['Ticket']['summary'], $body, $header_string); } } } if (isset($this->_data['TicketResponse']) && empty($this->_data['TicketResponse']['body'])) { unset($this->_data['TicketResponse']); } if (parent::save('Ticket', '', $errors)) { sendTo('Tickets', 'view', array('ticketing'), array('id' => $this->saved_model->id)); } $errors[] = 'Error saving ticket'; $flash->addErrors($errors); $this->refresh(); }