Ejemplo n.º 1
0
 /**
  * Method used to send email notifications for a given issue.
  *
  * @param integer $issue_id The issue ID
  * @param string $type The notification type
  * @param int $entry_id The entries id that was changed
  * @param bool $internal_only Whether the notification should only be sent to internal users or not
  * @param array $extra_recipients
  * @return bool
  */
 public static function notify($issue_id, $type, $entry_id = null, $internal_only = false, $extra_recipients = null)
 {
     $prj_id = Issue::getProjectID($issue_id);
     $extra = array();
     if ($extra_recipients) {
         foreach ($extra_recipients as $user) {
             $extra[] = array('sub_usr_id' => $user, 'sub_email' => '');
         }
     }
     $emails = array();
     $users = self::getUsersByIssue($issue_id, $type);
     if ($extra_recipients && count($extra) > 0) {
         $users = array_merge($users, $extra);
     }
     $user_emails = Project::getUserEmailAssocList(Issue::getProjectID($issue_id), 'active', User::ROLE_CUSTOMER);
     $user_emails = Misc::lowercase($user_emails);
     foreach ($users as $user) {
         if (empty($user['sub_usr_id'])) {
             if ($internal_only == false || in_array(strtolower($user['sub_email']), array_values($user_emails))) {
                 $email = $user['sub_email'];
             }
         } else {
             $prefs = Prefs::get($user['sub_usr_id']);
             if (Auth::getUserID() == $user['sub_usr_id'] && (empty($prefs['receive_copy_of_own_action'][$prj_id]) || $prefs['receive_copy_of_own_action'][$prj_id] == false)) {
                 continue;
             }
             // if we are only supposed to send email to internal users, check if the role is lower than standard user
             if ($internal_only == true && User::getRoleByUser($user['sub_usr_id'], Issue::getProjectID($issue_id)) < User::ROLE_USER) {
                 continue;
             }
             if ($type == 'notes' && User::isPartner($user['sub_usr_id']) && !Partner::canUserAccessIssueSection($user['sub_usr_id'], 'notes')) {
                 continue;
             }
             $email = User::getFromHeader($user['sub_usr_id']);
         }
         // now add it to the list of emails
         if (!empty($email) && !in_array($email, $emails)) {
             $emails[] = $email;
         }
     }
     // prevent the primary customer contact from receiving two emails about the issue being closed
     if ($type == 'closed') {
         if (CRM::hasCustomerIntegration($prj_id)) {
             $crm = CRM::getInstance($prj_id);
             $stmt = 'SELECT
                         iss_customer_contact_id
                      FROM
                         {{%issue}}
                      WHERE
                         iss_id=?';
             $customer_contact_id = DB_Helper::getInstance()->getOne($stmt, array($issue_id));
             if (!empty($customer_contact_id)) {
                 try {
                     $contact = $crm->getContact($customer_contact_id);
                     $contact_email = $contact->getEmail();
                 } catch (CRMException $e) {
                     $contact_email = '';
                 }
                 foreach ($emails as $i => $email) {
                     $email = Mail_Helper::getEmailAddress($email);
                     if ($email == $contact_email) {
                         unset($emails[$i]);
                         $emails = array_values($emails);
                         break;
                     }
                 }
             }
         }
     }
     if (!$emails) {
         return null;
     }
     $headers = false;
     switch ($type) {
         case 'closed':
             $data = Issue::getDetails($issue_id);
             $data['closer_name'] = User::getFullName(History::getIssueCloser($issue_id));
             $subject = ev_gettext('Closed');
             if ($entry_id) {
                 $data['reason'] = Support::getEmail($entry_id);
             }
             break;
         case 'updated':
             // this should not be used anymore
             return false;
         case 'notes':
             $data = self::getNote($issue_id, $entry_id);
             $headers = array('Message-ID' => $data['note']['not_message_id']);
             if (@$data['note']['reference_msg_id'] != false) {
                 $headers['In-Reply-To'] = $data['note']['reference_msg_id'];
             } else {
                 $headers['In-Reply-To'] = Issue::getRootMessageID($issue_id);
             }
             $headers['References'] = Mail_Helper::fold(implode(' ', Mail_Helper::getReferences($issue_id, @$data['note']['reference_msg_id'], 'note')));
             $subject = 'Note';
             break;
         case 'emails':
             // this should not be used anymore
             return false;
         case 'files':
             $data = self::getAttachment($issue_id, $entry_id);
             $subject = 'File Attached';
             break;
     }
     // FIXME: $data and $subject might be used uninitialized
     self::notifySubscribers($issue_id, $emails, $type, $data, $subject, $internal_only, $entry_id, $headers);
 }