Example #1
0
 /**
  * Creates an issue with the given email information.
  *
  * @param   integer $prj_id The project ID
  * @param   integer $usr_id The user responsible for this action
  * @param   string $sender The original sender of this email
  * @param   string $summary The issue summary
  * @param   string $description The issue description
  * @param   integer $category The category ID
  * @param   integer $priority The priority ID
  * @param   array $assignment The list of users to assign this issue to
  * @param   string $date The date the email was originally sent.
  * @param   string $msg_id The message ID of the email we are creating this issue from.
  * @param   integer $severity
  * @param   string $customer_id
  * @param   string $contact_id
  * @param   string $contract_id
  * @return int
  */
 public static function createFromEmail($prj_id, $usr_id, $sender, $summary, $description, $category, $priority, $assignment, $date, $msg_id, $severity, $customer_id, $contact_id, $contract_id)
 {
     $exclude_list = array();
     $managers = array();
     $sender_email = Mail_Helper::getEmailAddress($sender);
     $sender_usr_id = User::getUserIDByEmail($sender_email, true);
     if (!empty($sender_usr_id)) {
         $reporter = $sender_usr_id;
         $exclude_list[] = $sender_usr_id;
     }
     $data = array('category' => $category, 'priority' => $priority, 'severity' => $severity, 'description' => $description, 'summary' => $summary, 'msg_id' => $msg_id, 'customer' => false, 'contact' => false, 'contract' => false, 'contact_person_lname' => '', 'contact_person_fname' => '', 'contact_email' => '', 'contact_phone' => '', 'contact_timezone' => '');
     if (CRM::hasCustomerIntegration($prj_id)) {
         $crm = CRM::getInstance($prj_id);
         try {
             if ($contact_id != false) {
                 $contact = $crm->getContact($contact_id);
             } else {
                 $contact = $crm->getContactByEmail($sender_email);
             }
             // overwrite the reporter with the customer contact
             $reporter = User::getUserIDByContactID($contact->getContactID());
             $data['contact'] = $contact->getContactID();
             $data['contact_person_lname'] = $contact['last_name'];
             $data['contact_person_fname'] = $contact['first_name'];
             $data['contact_email'] = $sender_email;
             $data['contact_phone'] = $contact['phone'];
             $data['contact_timezone'] = Date_Helper::getPreferredTimezone($reporter);
         } catch (ContactNotFoundException $e) {
         }
         try {
             if ($contract_id != false) {
                 $contract = $crm->getContract($contract_id);
                 $data['contract'] = $contract->getContractID();
             } elseif (isset($contact)) {
                 // Just use first contract / customer for now.
                 $contracts = $contact->getContracts(array('active' => true));
                 $contract = $contracts[0];
                 $data['contract'] = $contract->getContractID();
             }
         } catch (ContractNotFoundException $e) {
         }
         try {
             if ($customer_id != false) {
                 $customer = $crm->getCustomer($customer_id);
                 $data['customer'] = $customer->getCustomerID();
             } elseif (isset($contract)) {
                 $customer = $contract->getCustomer();
                 $data['customer'] = $customer->getCustomerID();
             }
         } catch (CustomerNotFoundException $e) {
         }
     } else {
     }
     if (empty($reporter)) {
         $reporter = APP_SYSTEM_USER_ID;
     }
     $data['reporter'] = $reporter;
     $issue_id = self::insertIssue($prj_id, $data);
     if ($issue_id == -1) {
         return -1;
     }
     $has_RR = false;
     // log the creation of the issue
     History::add($issue_id, $usr_id, 'issue_opened', 'Issue opened by {sender}', array('sender' => $sender));
     $emails = array();
     // if there are any technical account managers associated with this customer, add these users to the notification list
     if ($data['customer']) {
         $managers = CRM::getAccountManagers($prj_id, $data['customer']);
         foreach ($managers as $manager) {
             $emails[] = $manager['usr_email'];
         }
     }
     // add the reporter to the notification list
     $emails[] = $sender;
     $emails = array_unique($emails);
     $actions = Notification::getDefaultActions($issue_id, false, 'issue_from_email');
     foreach ($emails as $address) {
         Notification::subscribeEmail($reporter, $issue_id, $address, $actions);
     }
     // only assign the issue to an user if the associated customer has any technical account managers
     $users = array();
     $has_TAM = false;
     if (CRM::hasCustomerIntegration($prj_id) && count($managers) > 0) {
         foreach ($managers as $manager) {
             if ($manager['cam_type'] == 'intpart') {
                 continue;
             }
             $users[] = $manager['cam_usr_id'];
             self::addUserAssociation($usr_id, $issue_id, $manager['cam_usr_id'], false);
             History::add($issue_id, $usr_id, 'issue_auto_assigned', 'Issue auto-assigned to {assignee} (TAM)', array('assignee' => User::getFullName($manager['cam_usr_id'])));
         }
         $has_TAM = true;
     }
     // now add the user/issue association
     if (@count($assignment) > 0) {
         foreach ($assignment as $ass_usr_id) {
             Notification::subscribeUser($reporter, $issue_id, $ass_usr_id, $actions);
             self::addUserAssociation(APP_SYSTEM_USER_ID, $issue_id, $ass_usr_id);
             if ($ass_usr_id != $usr_id) {
                 $users[] = $ass_usr_id;
             }
         }
     } else {
         // only use the round-robin feature if this new issue was not
         // already assigned to a customer account manager
         if (count($managers) < 1) {
             $assignee = Round_Robin::getNextAssignee($prj_id);
             // assign the issue to the round robin person
             if (!empty($assignee)) {
                 self::addUserAssociation(APP_SYSTEM_USER_ID, $issue_id, $assignee, false);
                 History::add($issue_id, APP_SYSTEM_USER_ID, 'rr_issue_assigned', 'Issue auto-assigned to {assignee} (RR)', array('assignee' => User::getFullName($assignee)));
                 $users[] = $assignee;
                 $has_RR = true;
             }
         }
     }
     Workflow::handleNewIssue($prj_id, $issue_id, $has_TAM, $has_RR);
     // send special 'an issue was auto-created for you' notification back to the sender
     Notification::notifyAutoCreatedIssue($prj_id, $issue_id, $sender, $date, $summary);
     // also notify any users that want to receive emails anytime a new issue is created
     Notification::notifyNewIssue($prj_id, $issue_id, $exclude_list);
     return $issue_id;
 }