Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
35 | * This class provides the common functionality for sending email to | |
36 | * one or a group of contact ids. This class is reused by all the search | |
37 | * components in CiviCRM (since they all have send email as a task) | |
38 | */ | |
39 | class CRM_Contact_Form_Task_EmailCommon { | |
7da04cde | 40 | const MAX_EMAILS_KILL_SWITCH = 50; |
6a488035 TO |
41 | |
42 | public $_contactDetails = array(); | |
43 | public $_allContactDetails = array(); | |
44 | public $_toContactEmails = array(); | |
45 | ||
86538308 | 46 | /** |
f68954cb SL |
47 | * Generate an array of Domain email addresses. |
48 | * @return array $domainEmails; | |
49 | */ | |
50 | public static function domainEmails() { | |
51 | $domainEmails = array(); | |
52 | $domainFrom = CRM_Core_OptionGroup::values('from_email_address'); | |
53 | foreach (array_keys($domainFrom) as $k) { | |
54 | $domainEmail = $domainFrom[$k]; | |
55 | $domainEmails[$domainEmail] = htmlspecialchars($domainEmail); | |
56 | } | |
57 | return $domainEmails; | |
58 | } | |
59 | ||
60 | /** | |
61 | * Pre Process Form Addresses to be used in QUickfomr | |
20b22300 | 62 | * @param CRM_Core_Form $form |
f68954cb | 63 | * @param bool $bounce determine if we want to throw a status bounce. |
86538308 | 64 | */ |
f68954cb | 65 | public static function preProcessFromAddress(&$form, $bounce = TRUE) { |
6a488035 TO |
66 | $form->_single = FALSE; |
67 | $className = CRM_Utils_System::getClassName($form); | |
68 | if (property_exists($form, '_context') && | |
69 | $form->_context != 'search' && | |
70 | $className == 'CRM_Contact_Form_Task_Email' | |
71 | ) { | |
72 | $form->_single = TRUE; | |
73 | } | |
74 | ||
75 | $form->_emails = $emails = array(); | |
76 | ||
77 | $session = CRM_Core_Session::singleton(); | |
78 | $contactID = $session->get('userID'); | |
79 | ||
80 | $form->_contactIds = array($contactID); | |
81 | $contactEmails = CRM_Core_BAO_Email::allEmails($contactID); | |
82 | ||
83 | $form->_onHold = array(); | |
84 | ||
85 | $fromDisplayName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
86 | $contactID, 'display_name' | |
87 | ); | |
88 | ||
89 | foreach ($contactEmails as $emailId => $item) { | |
90 | $email = $item['email']; | |
3d41f850 DL |
91 | if (!$email && (count($emails) < 1)) { |
92 | // set it if no emails are present at all | |
6a488035 TO |
93 | $form->_noEmails = TRUE; |
94 | } | |
95 | else { | |
96 | if ($email) { | |
97 | if (in_array($email, $emails)) { | |
98 | // CRM-3624 | |
99 | continue; | |
100 | } | |
101 | ||
102 | $emails[$emailId] = '"' . $fromDisplayName . '" <' . $email . '> '; | |
103 | $form->_onHold[$emailId] = $item['on_hold']; | |
3d41f850 | 104 | $form->_noEmails = FALSE; |
6a488035 TO |
105 | } |
106 | } | |
f68954cb SL |
107 | if (!empty($email)) { |
108 | $form->_emails[$emailId] = $emails[$emailId]; | |
109 | $emails[$emailId] .= $item['locationType']; | |
6a488035 | 110 | |
f68954cb SL |
111 | if ($item['is_primary']) { |
112 | $emails[$emailId] .= ' ' . ts('(preferred)'); | |
113 | } | |
114 | $emails[$emailId] = htmlspecialchars($emails[$emailId]); | |
6a488035 | 115 | } |
6a488035 TO |
116 | } |
117 | ||
118 | $form->assign('noEmails', $form->_noEmails); | |
119 | ||
f68954cb SL |
120 | if ($bounce) { |
121 | if ($form->_noEmails) { | |
122 | CRM_Core_Error::statusBounce(ts('Your user record does not have a valid email address')); | |
123 | } | |
6a488035 TO |
124 | } |
125 | ||
126 | // now add domain from addresses | |
f68954cb SL |
127 | $domainEmails = self::domainEmails(); |
128 | foreach ($domainEmails as $domainEmail => $email) { | |
6a488035 TO |
129 | $form->_emails[$domainEmail] = $domainEmail; |
130 | } | |
6a488035 | 131 | $form->_fromEmails = CRM_Utils_Array::crmArrayMerge($emails, $domainEmails); |
e1239fe8 SL |
132 | $form->_fromEmails = array_filter($form->_fromEmails); |
133 | if (is_numeric(key($form->_fromEmails))) { | |
f68954cb SL |
134 | // Add signature |
135 | $defaultEmail = civicrm_api3('email', 'getsingle', array('id' => key($form->_fromEmails))); | |
136 | $defaults = array(); | |
137 | if (!empty($defaultEmail['signature_html'])) { | |
138 | $defaults['html_message'] = '<br/><br/>--' . $defaultEmail['signature_html']; | |
139 | } | |
140 | if (!empty($defaultEmail['signature_text'])) { | |
141 | $defaults['text_message'] = "\n\n--\n" . $defaultEmail['signature_text']; | |
142 | } | |
143 | $form->setDefaults($defaults); | |
20b22300 | 144 | } |
6a488035 TO |
145 | } |
146 | ||
147 | /** | |
fe482240 | 148 | * Build the form object. |
6a488035 | 149 | * |
c490a46a | 150 | * @param CRM_Core_Form $form |
6a488035 | 151 | */ |
00be9182 | 152 | public static function buildQuickForm(&$form) { |
6a488035 TO |
153 | $toArray = $ccArray = $bccArray = array(); |
154 | $suppressedEmails = 0; | |
155 | //here we are getting logged in user id as array but we need target contact id. CRM-5988 | |
156 | $cid = $form->get('cid'); | |
157 | if ($cid) { | |
b3dbca23 | 158 | $form->_contactIds = explode(',', $cid); |
9cb404b4 N |
159 | } |
160 | if (count($form->_contactIds) > 1) { | |
161 | $form->_single = FALSE; | |
6a488035 | 162 | } |
a03d5ab9 | 163 | |
cac1236c M |
164 | $emailAttributes = array( |
165 | 'class' => 'huge', | |
166 | ); | |
353ffa53 TO |
167 | $to = $form->add('text', 'to', ts('To'), $emailAttributes, TRUE); |
168 | $cc = $form->add('text', 'cc_id', ts('CC'), $emailAttributes); | |
cac1236c | 169 | $bcc = $form->add('text', 'bcc_id', ts('BCC'), $emailAttributes); |
6a488035 | 170 | |
3f77636a | 171 | $setDefaults = TRUE; |
172 | if (property_exists($form, '_context') && $form->_context == 'standalone') { | |
173 | $setDefaults = FALSE; | |
174 | } | |
175 | ||
176 | $elements = array('to', 'cc', 'bcc'); | |
16947fa6 | 177 | $form->_allContactIds = $form->_toContactIds = $form->_contactIds; |
6a488035 TO |
178 | foreach ($elements as $element) { |
179 | if ($$element->getValue()) { | |
3f77636a | 180 | $allEmails = explode(',', $$element->getValue()); |
181 | if ($element == 'to') { | |
16947fa6 | 182 | $form->_toContactIds = $form->_contactIds = array(); |
6a488035 | 183 | } |
6a488035 | 184 | |
3f77636a | 185 | foreach ($allEmails as $value) { |
186 | list($contactId, $email) = explode('::', $value); | |
187 | if ($contactId) { | |
188 | switch ($element) { | |
189 | case 'to': | |
190 | $form->_contactIds[] = $form->_toContactIds[] = $contactId; | |
191 | $form->_toContactEmails[] = $email; | |
192 | break; | |
ea100cb5 | 193 | |
3f77636a | 194 | case 'cc': |
195 | $form->_ccContactIds[] = $contactId; | |
196 | break; | |
ea100cb5 | 197 | |
3f77636a | 198 | case 'bcc': |
199 | $form->_bccContactIds[] = $contactId; | |
200 | break; | |
201 | } | |
202 | ||
203 | $form->_allContactIds[] = $contactId; | |
204 | } | |
6a488035 | 205 | } |
3f77636a | 206 | |
207 | $setDefaults = TRUE; | |
6a488035 | 208 | } |
6a488035 TO |
209 | } |
210 | ||
211 | //get the group of contacts as per selected by user in case of Find Activities | |
212 | if (!empty($form->_activityHolderIds)) { | |
213 | $contact = $form->get('contacts'); | |
16947fa6 | 214 | $form->_allContactIds = $form->_contactIds = $contact; |
6a488035 TO |
215 | } |
216 | ||
3f77636a | 217 | // check if we need to setdefaults and check for valid contact emails / communication preferences |
16947fa6 | 218 | if (is_array($form->_allContactIds) && $setDefaults) { |
6a488035 TO |
219 | $returnProperties = array( |
220 | 'sort_name' => 1, | |
221 | 'email' => 1, | |
222 | 'do_not_email' => 1, | |
223 | 'is_deceased' => 1, | |
224 | 'on_hold' => 1, | |
225 | 'display_name' => 1, | |
226 | 'preferred_mail_format' => 1, | |
227 | ); | |
228 | ||
3f77636a | 229 | // get the details for all selected contacts ( to, cc and bcc contacts ) |
230 | list($form->_contactDetails) = CRM_Utils_Token::getTokenDetails($form->_allContactIds, | |
6a488035 TO |
231 | $returnProperties, |
232 | FALSE, | |
233 | FALSE | |
234 | ); | |
235 | ||
236 | // make a copy of all contact details | |
237 | $form->_allContactDetails = $form->_contactDetails; | |
238 | ||
8034b1c0 | 239 | // perform all validations on unique contact Ids |
240 | foreach (array_unique($form->_allContactIds) as $key => $contactId) { | |
6a488035 | 241 | $value = $form->_contactDetails[$contactId]; |
8cc574cf | 242 | if ($value['do_not_email'] || empty($value['email']) || !empty($value['is_deceased']) || $value['on_hold']) { |
6a488035 TO |
243 | $suppressedEmails++; |
244 | ||
245 | // unset contact details for contacts that we won't be sending email. This is prevent extra computation | |
246 | // during token evaluation etc. | |
247 | unset($form->_contactDetails[$contactId]); | |
248 | } | |
249 | else { | |
3f77636a | 250 | $email = $value['email']; |
251 | ||
252 | // build array's which are used to setdefaults | |
253 | if (in_array($contactId, $form->_toContactIds)) { | |
254 | $form->_toContactDetails[$contactId] = $form->_contactDetails[$contactId]; | |
8e4aa90c CW |
255 | // If a particular address has been specified as the default, use that instead of contact's primary email |
256 | if (!empty($form->_toEmail) && $form->_toEmail['contact_id'] == $contactId) { | |
257 | $email = $form->_toEmail['email']; | |
258 | } | |
3f77636a | 259 | $toArray[] = array( |
a03d5ab9 | 260 | 'text' => '"' . $value['sort_name'] . '" <' . $email . '>', |
3f77636a | 261 | 'id' => "$contactId::{$email}", |
262 | ); | |
263 | } | |
264 | elseif (in_array($contactId, $form->_ccContactIds)) { | |
265 | $ccArray[] = array( | |
a03d5ab9 | 266 | 'text' => '"' . $value['sort_name'] . '" <' . $email . '>', |
3f77636a | 267 | 'id' => "$contactId::{$email}", |
268 | ); | |
6a488035 | 269 | } |
3f77636a | 270 | elseif (in_array($contactId, $form->_bccContactIds)) { |
271 | $bccArray[] = array( | |
a03d5ab9 | 272 | 'text' => '"' . $value['sort_name'] . '" <' . $email . '>', |
3f77636a | 273 | 'id' => "$contactId::{$email}", |
274 | ); | |
6a488035 | 275 | } |
6a488035 TO |
276 | } |
277 | } | |
278 | ||
279 | if (empty($toArray)) { | |
280 | CRM_Core_Error::statusBounce(ts('Selected contact(s) do not have a valid email address, or communication preferences specify DO NOT EMAIL, or they are deceased or Primary email address is On Hold.')); | |
281 | } | |
282 | } | |
283 | ||
284 | $form->assign('toContact', json_encode($toArray)); | |
3f77636a | 285 | $form->assign('ccContact', json_encode($ccArray)); |
286 | $form->assign('bccContact', json_encode($bccArray)); | |
287 | ||
6a488035 TO |
288 | $form->assign('suppressedEmails', $suppressedEmails); |
289 | ||
290 | $form->assign('totalSelectedContacts', count($form->_contactIds)); | |
291 | ||
292 | $form->add('text', 'subject', ts('Subject'), 'size=50 maxlength=254', TRUE); | |
293 | ||
f7305cbc | 294 | $form->add('select', 'fromEmailAddress', ts('From'), $form->_fromEmails, TRUE, array('class' => 'crm-select2 huge')); |
6a488035 TO |
295 | |
296 | CRM_Mailing_BAO_Mailing::commonCompose($form); | |
297 | ||
298 | // add attachments | |
299 | CRM_Core_BAO_File::buildAttachment($form, NULL); | |
300 | ||
301 | if ($form->_single) { | |
302 | // also fix the user context stack | |
303 | if ($form->_caseId) { | |
304 | $ccid = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseContact', $form->_caseId, | |
305 | 'contact_id', 'case_id' | |
306 | ); | |
307 | $url = CRM_Utils_System::url('civicrm/contact/view/case', | |
308 | "&reset=1&action=view&cid={$ccid}&id={$form->_caseId}" | |
309 | ); | |
310 | } | |
311 | elseif ($form->_context) { | |
312 | $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1'); | |
313 | } | |
314 | else { | |
315 | $url = CRM_Utils_System::url('civicrm/contact/view', | |
316 | "&show=1&action=browse&cid={$form->_contactIds[0]}&selectedChild=activity" | |
317 | ); | |
318 | } | |
319 | ||
320 | $session = CRM_Core_Session::singleton(); | |
321 | $session->replaceUserContext($url); | |
322 | $form->addDefaultButtons(ts('Send Email'), 'upload', 'cancel'); | |
323 | } | |
324 | else { | |
325 | $form->addDefaultButtons(ts('Send Email'), 'upload'); | |
326 | } | |
327 | ||
cfcf9132 | 328 | $fields = array( |
329 | 'followup_assignee_contact_id' => array( | |
330 | 'type' => 'entityRef', | |
331 | 'label' => ts('Assigned to'), | |
332 | 'attributes' => array( | |
333 | 'multiple' => TRUE, | |
334 | 'create' => TRUE, | |
335 | 'api' => array('params' => array('is_deceased' => 0)), | |
336 | ), | |
337 | ), | |
338 | 'followup_activity_type_id' => array( | |
339 | 'type' => 'select', | |
340 | 'label' => ts('Followup Activity'), | |
341 | 'attributes' => array('' => '- ' . ts('select activity') . ' -') + CRM_Core_PseudoConstant::ActivityType(FALSE), | |
342 | 'extra' => array('class' => 'crm-select2'), | |
343 | ), | |
344 | 'followup_activity_subject' => array( | |
345 | 'type' => 'text', | |
346 | 'label' => ts('Subject'), | |
347 | 'attributes' => CRM_Core_DAO::getAttribute('CRM_Activity_DAO_Activity', | |
348 | 'subject' | |
349 | ), | |
350 | ), | |
351 | ); | |
352 | ||
353 | //add followup date | |
354 | $form->addDateTime('followup_date', ts('in'), FALSE, array('formatType' => 'activityDateTime')); | |
355 | ||
356 | foreach ($fields as $field => $values) { | |
357 | if (!empty($fields[$field])) { | |
358 | $attribute = CRM_Utils_Array::value('attributes', $values); | |
359 | $required = !empty($values['required']); | |
360 | ||
361 | if ($values['type'] == 'select' && empty($attribute)) { | |
362 | $form->addSelect($field, array('entity' => 'activity'), $required); | |
363 | } | |
364 | elseif ($values['type'] == 'entityRef') { | |
365 | $form->addEntityRef($field, $values['label'], $attribute, $required); | |
366 | } | |
367 | else { | |
368 | $form->add($values['type'], $field, $values['label'], $attribute, $required, CRM_Utils_Array::value('extra', $values)); | |
369 | } | |
370 | } | |
371 | } | |
372 | ||
6a488035 | 373 | $form->addFormRule(array('CRM_Contact_Form_Task_EmailCommon', 'formRule'), $form); |
e573fefc | 374 | CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'templates/CRM/Contact/Form/Task/EmailCommon.js', 0, 'html-header'); |
6a488035 TO |
375 | } |
376 | ||
377 | /** | |
fe482240 | 378 | * Form rule. |
6a488035 | 379 | * |
77c5b619 TO |
380 | * @param array $fields |
381 | * The input form values. | |
6a488035 | 382 | * @param array $dontCare |
77c5b619 TO |
383 | * @param array $self |
384 | * Additional values form 'this'. | |
6a488035 | 385 | * |
72b3a70c CW |
386 | * @return bool|array |
387 | * true if no errors, else array of errors | |
6a488035 | 388 | */ |
00be9182 | 389 | public static function formRule($fields, $dontCare, $self) { |
6a488035 TO |
390 | $errors = array(); |
391 | $template = CRM_Core_Smarty::singleton(); | |
392 | ||
393 | if (isset($fields['html_message'])) { | |
394 | $htmlMessage = str_replace(array("\n", "\r"), ' ', $fields['html_message']); | |
395 | $htmlMessage = str_replace('"', '\"', $htmlMessage); | |
396 | $template->assign('htmlContent', $htmlMessage); | |
397 | } | |
398 | ||
399 | //Added for CRM-1393 | |
a7488080 | 400 | if (!empty($fields['saveTemplate']) && empty($fields['saveTemplateName'])) { |
6a488035 TO |
401 | $errors['saveTemplateName'] = ts("Enter name to save message template"); |
402 | } | |
403 | ||
404 | return empty($errors) ? TRUE : $errors; | |
405 | } | |
406 | ||
407 | /** | |
fe482240 | 408 | * Process the form after the input has been submitted and validated. |
6a488035 | 409 | * |
c490a46a | 410 | * @param CRM_Core_Form $form |
6a488035 | 411 | */ |
00be9182 | 412 | public static function postProcess(&$form) { |
6a488035 TO |
413 | if (count($form->_contactIds) > self::MAX_EMAILS_KILL_SWITCH) { |
414 | CRM_Core_Error::fatal(ts('Please do not use this task to send a lot of emails (greater than %1). We recommend using CiviMail instead.', | |
353ffa53 TO |
415 | array(1 => self::MAX_EMAILS_KILL_SWITCH) |
416 | )); | |
6a488035 TO |
417 | } |
418 | ||
419 | // check and ensure that | |
420 | $formValues = $form->controller->exportValues($form->getName()); | |
6a488035 | 421 | $fromEmail = $formValues['fromEmailAddress']; |
353ffa53 TO |
422 | $from = CRM_Utils_Array::value($fromEmail, $form->_emails); |
423 | $subject = $formValues['subject']; | |
6a488035 | 424 | |
3f77636a | 425 | // CRM-13378: Append CC and BCC information at the end of Activity Details and format cc and bcc fields |
426 | $elements = array('cc_id', 'bcc_id'); | |
c1d26519 | 427 | $additionalDetails = NULL; |
3f77636a | 428 | $ccValues = $bccValues = array(); |
c1d26519 | 429 | foreach ($elements as $element) { |
a7488080 | 430 | if (!empty($formValues[$element])) { |
3f77636a | 431 | $allEmails = explode(',', $formValues[$element]); |
432 | foreach ($allEmails as $value) { | |
433 | list($contactId, $email) = explode('::', $value); | |
4eeb9a5b | 434 | $contactURL = CRM_Utils_System::url('civicrm/contact/view', "reset=1&force=1&cid={$contactId}", TRUE); |
3f77636a | 435 | switch ($element) { |
436 | case 'cc_id': | |
437 | $ccValues['email'][] = '"' . $form->_contactDetails[$contactId]['sort_name'] . '" <' . $email . '>'; | |
438 | $ccValues['details'][] = "<a href='{$contactURL}'>" . $form->_contactDetails[$contactId]['display_name'] . "</a>"; | |
439 | break; | |
ea100cb5 | 440 | |
3f77636a | 441 | case 'bcc_id': |
d3e86119 | 442 | $bccValues['email'][] = '"' . $form->_contactDetails[$contactId]['sort_name'] . '" <' . $email . '>'; |
3f77636a | 443 | $bccValues['details'][] = "<a href='{$contactURL}'>" . $form->_contactDetails[$contactId]['display_name'] . "</a>"; |
444 | break; | |
445 | } | |
c1d26519 | 446 | } |
c1d26519 | 447 | } |
448 | } | |
449 | ||
3f77636a | 450 | $cc = $bcc = ''; |
451 | if (!empty($ccValues)) { | |
452 | $cc = implode(',', $ccValues['email']); | |
453 | $additionalDetails .= "\ncc : " . implode(", ", $ccValues['details']); | |
454 | } | |
455 | if (!empty($bccValues)) { | |
456 | $bcc = implode(',', $bccValues['email']); | |
457 | $additionalDetails .= "\nbcc : " . implode(", ", $bccValues['details']); | |
458 | } | |
459 | ||
6a488035 TO |
460 | // CRM-5916: prepend case id hash to CiviCase-originating emails’ subjects |
461 | if (isset($form->_caseId) && is_numeric($form->_caseId)) { | |
462 | $hash = substr(sha1(CIVICRM_SITE_KEY . $form->_caseId), 0, 7); | |
463 | $subject = "[case #$hash] $subject"; | |
464 | } | |
465 | ||
466 | // process message template | |
8cc574cf | 467 | if (!empty($formValues['saveTemplate']) || !empty($formValues['updateTemplate'])) { |
6a488035 TO |
468 | $messageTemplate = array( |
469 | 'msg_text' => $formValues['text_message'], | |
470 | 'msg_html' => $formValues['html_message'], | |
471 | 'msg_subject' => $formValues['subject'], | |
472 | 'is_active' => TRUE, | |
473 | ); | |
474 | ||
a7488080 | 475 | if (!empty($formValues['saveTemplate'])) { |
6a488035 | 476 | $messageTemplate['msg_title'] = $formValues['saveTemplateName']; |
c6327d7d | 477 | CRM_Core_BAO_MessageTemplate::add($messageTemplate); |
6a488035 TO |
478 | } |
479 | ||
8cc574cf | 480 | if (!empty($formValues['template']) && !empty($formValues['updateTemplate'])) { |
6a488035 TO |
481 | $messageTemplate['id'] = $formValues['template']; |
482 | unset($messageTemplate['msg_title']); | |
c6327d7d | 483 | CRM_Core_BAO_MessageTemplate::add($messageTemplate); |
6a488035 TO |
484 | } |
485 | } | |
486 | ||
487 | $attachments = array(); | |
488 | CRM_Core_BAO_File::formatAttachment($formValues, | |
489 | $attachments, | |
490 | NULL, NULL | |
491 | ); | |
492 | ||
493 | // format contact details array to handle multiple emails from same contact | |
494 | $formattedContactDetails = array(); | |
495 | $tempEmails = array(); | |
6a488035 TO |
496 | foreach ($form->_contactIds as $key => $contactId) { |
497 | // if we dont have details on this contactID, we should ignore | |
498 | // potentially this is due to the contact not wanting to receive email | |
499 | if (!isset($form->_contactDetails[$contactId])) { | |
500 | continue; | |
501 | } | |
502 | $email = $form->_toContactEmails[$key]; | |
503 | // prevent duplicate emails if same email address is selected CRM-4067 | |
504 | // we should allow same emails for different contacts | |
505 | $emailKey = "{$contactId}::{$email}"; | |
506 | if (!in_array($emailKey, $tempEmails)) { | |
353ffa53 TO |
507 | $tempEmails[] = $emailKey; |
508 | $details = $form->_contactDetails[$contactId]; | |
6a488035 TO |
509 | $details['email'] = $email; |
510 | unset($details['email_id']); | |
511 | $formattedContactDetails[] = $details; | |
512 | } | |
513 | } | |
514 | ||
515 | // send the mail | |
516 | list($sent, $activityId) = CRM_Activity_BAO_Activity::sendEmail( | |
517 | $formattedContactDetails, | |
518 | $subject, | |
519 | $formValues['text_message'], | |
520 | $formValues['html_message'], | |
521 | NULL, | |
522 | NULL, | |
523 | $from, | |
524 | $attachments, | |
525 | $cc, | |
526 | $bcc, | |
3f77636a | 527 | array_keys($form->_toContactDetails), |
c1d26519 | 528 | $additionalDetails |
6a488035 TO |
529 | ); |
530 | ||
cfcf9132 | 531 | $followupStatus = ''; |
6a488035 | 532 | if ($sent) { |
cfcf9132 | 533 | $followupActivity = NULL; |
57a6ee70 | 534 | if (!empty($formValues['followup_activity_type_id'])) { |
cfcf9132 | 535 | $params['followup_activity_type_id'] = $formValues['followup_activity_type_id']; |
536 | $params['followup_activity_subject'] = $formValues['followup_activity_subject']; | |
537 | $params['followup_date'] = $formValues['followup_date']; | |
538 | $params['followup_date_time'] = $formValues['followup_date_time']; | |
539 | $params['target_contact_id'] = $form->_contactIds; | |
57a6ee70 | 540 | $params['followup_assignee_contact_id'] = explode(',', $formValues['followup_assignee_contact_id']); |
cfcf9132 | 541 | $followupActivity = CRM_Activity_BAO_Activity::createFollowupActivity($activityId, $params); |
542 | $followupStatus = ts('A followup activity has been scheduled.'); | |
543 | ||
aaffa79f | 544 | if (Civi::settings()->get('activity_assignee_notification')) { |
cfcf9132 | 545 | if ($followupActivity) { |
546 | $mailToFollowupContacts = array(); | |
547 | $assignee = array($followupActivity->id); | |
548 | $assigneeContacts = CRM_Activity_BAO_ActivityAssignment::getAssigneeNames($assignee, TRUE, FALSE); | |
549 | foreach ($assigneeContacts as $values) { | |
550 | $mailToFollowupContacts[$values['email']] = $values; | |
551 | } | |
552 | ||
bc883279 | 553 | $sentFollowup = CRM_Activity_BAO_Activity::sendToAssignee($followupActivity, $mailToFollowupContacts); |
554 | if ($sentFollowup) { | |
5c89eeee | 555 | $followupStatus .= '<br />' . ts("A copy of the follow-up activity has also been sent to follow-up assignee contacts(s)."); |
bc883279 | 556 | } |
cfcf9132 | 557 | } |
558 | } | |
559 | } | |
560 | ||
3f77636a | 561 | $count_success = count($form->_toContactDetails); |
cfcf9132 | 562 | CRM_Core_Session::setStatus(ts('One message was sent successfully. ', array( |
563 | 'plural' => '%count messages were sent successfully. ', | |
389bcebf | 564 | 'count' => $count_success, |
57a6ee70 | 565 | )) . $followupStatus, ts('Message Sent', array('plural' => 'Messages Sent', 'count' => $count_success)), 'success'); |
6a488035 TO |
566 | } |
567 | ||
a5611c8e DL |
568 | // Display the name and number of contacts for those email is not sent. |
569 | // php 5.4 throws out a notice since the values of these below arrays are arrays. | |
570 | // the behavior is not documented in the php manual, but it does the right thing | |
571 | // suppressing the notices to get things in good shape going forward | |
572 | $emailsNotSent = @array_diff_assoc($form->_allContactDetails, $form->_contactDetails); | |
6a488035 TO |
573 | |
574 | if ($emailsNotSent) { | |
575 | $not_sent = array(); | |
576 | foreach ($emailsNotSent as $contactId => $values) { | |
353ffa53 TO |
577 | $displayName = $values['display_name']; |
578 | $email = $values['email']; | |
6a488035 TO |
579 | $contactViewUrl = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid=$contactId"); |
580 | $not_sent[] = "<a href='$contactViewUrl' title='$email'>$displayName</a>" . ($values['on_hold'] ? '(' . ts('on hold') . ')' : ''); | |
581 | } | |
582 | $status = '(' . ts('because no email address on file or communication preferences specify DO NOT EMAIL or Contact is deceased or Primary email address is On Hold') . ')<ul><li>' . implode('</li><li>', $not_sent) . '</li></ul>'; | |
353ffa53 TO |
583 | CRM_Core_Session::setStatus($status, ts('One Message Not Sent', array( |
584 | 'count' => count($emailsNotSent), | |
389bcebf | 585 | 'plural' => '%count Messages Not Sent', |
353ffa53 | 586 | )), 'info'); |
6a488035 TO |
587 | } |
588 | ||
9cb404b4 | 589 | if (isset($form->_caseId)) { |
6a488035 | 590 | // if case-id is found in the url, create case activity record |
9cb404b4 | 591 | $cases = explode(',', $form->_caseId); |
22e263ad | 592 | foreach ($cases as $key => $val) { |
9cb404b4 N |
593 | if (is_numeric($val)) { |
594 | $caseParams = array( | |
595 | 'activity_id' => $activityId, | |
596 | 'case_id' => $val, | |
597 | ); | |
598 | CRM_Case_BAO_Case::processCaseActivity($caseParams); | |
599 | } | |
600 | } | |
6a488035 TO |
601 | } |
602 | } | |
96025800 | 603 | |
6a488035 | 604 | } |