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