Merge pull request #16697 from eileenmcnaughton/deprecate
[civicrm-core.git] / CRM / Contact / Form / Task / EmailCommon.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
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 {
24 const MAX_EMAILS_KILL_SWITCH = 50;
25
26 public $_contactDetails = array();
27 public $_allContactDetails = array();
28 public $_toContactEmails = array();
29
30 /**
31 * @deprecated Generate an array of Domain email addresses.
32 * @return array $domainEmails;
33 */
34 public static function domainEmails() {
35 CRM_Core_Error::deprecatedFunctionWarning('CRM_Core_BAO_Email::domainEmails()');
36 return CRM_Core_BAO_Email::domainEmails();
37 }
38
39 /**
40 * Pre Process Form Addresses to be used in Quickform
41 * @param CRM_Core_Form $form
42 * @param bool $bounce determine if we want to throw a status bounce.
43 */
44 public static function preProcessFromAddress(&$form, $bounce = TRUE) {
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
54 $form->_emails = array();
55
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
58 // If we don't have any contact IDs, use the logged in contact ID
59 $form->_contactIds = $form->_contactIds ?: [CRM_Core_Session::getLoggedInContactID()];
60
61 $fromEmailValues = CRM_Core_BAO_Email::getFromEmail();
62
63 $form->_noEmails = FALSE;
64 if (empty($fromEmailValues)) {
65 $form->_noEmails = TRUE;
66 }
67 $form->assign('noEmails', $form->_noEmails);
68
69 if ($bounce) {
70 if ($form->_noEmails) {
71 CRM_Core_Error::statusBounce(ts('Your user record does not have a valid email address and no from addresses have been configured.'));
72 }
73 }
74
75 $form->_emails = $fromEmailValues;
76 $defaults = array();
77 $form->_fromEmails = $fromEmailValues;
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 }
81 if (is_numeric(key($form->_fromEmails))) {
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 }
91 }
92 $form->setDefaults($defaults);
93 }
94
95 /**
96 * Build the form object.
97 *
98 * @param CRM_Core_Form $form
99 */
100 public static function buildQuickForm(&$form) {
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) {
106 $form->_contactIds = explode(',', $cid);
107 }
108 if (count($form->_contactIds) > 1) {
109 $form->_single = FALSE;
110 }
111 CRM_Contact_Form_Task_EmailCommon::bounceIfSimpleMailLimitExceeded(count($form->_contactIds));
112
113 $emailAttributes = array(
114 'class' => 'huge',
115 );
116 $to = $form->add('text', 'to', ts('To'), $emailAttributes, TRUE);
117 $cc = $form->add('text', 'cc_id', ts('CC'), $emailAttributes);
118 $bcc = $form->add('text', 'bcc_id', ts('BCC'), $emailAttributes);
119
120 $setDefaults = TRUE;
121 if (property_exists($form, '_context') && $form->_context == 'standalone') {
122 $setDefaults = FALSE;
123 }
124
125 $elements = array('to', 'cc', 'bcc');
126 $form->_allContactIds = $form->_toContactIds = $form->_contactIds;
127 foreach ($elements as $element) {
128 if ($$element->getValue()) {
129 $allEmails = explode(',', $$element->getValue());
130 if ($element == 'to') {
131 $form->_toContactIds = $form->_contactIds = array();
132 }
133
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;
142
143 case 'cc':
144 $form->_ccContactIds[] = $contactId;
145 break;
146
147 case 'bcc':
148 $form->_bccContactIds[] = $contactId;
149 break;
150 }
151
152 $form->_allContactIds[] = $contactId;
153 }
154 }
155
156 $setDefaults = TRUE;
157 }
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');
163 $form->_allContactIds = $form->_contactIds = $contact;
164 }
165
166 // check if we need to setdefaults and check for valid contact emails / communication preferences
167 if (is_array($form->_allContactIds) && $setDefaults) {
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
178 // get the details for all selected contacts ( to, cc and bcc contacts )
179 list($form->_contactDetails) = CRM_Utils_Token::getTokenDetails($form->_allContactIds,
180 $returnProperties,
181 FALSE,
182 FALSE
183 );
184
185 // make a copy of all contact details
186 $form->_allContactDetails = $form->_contactDetails;
187
188 // perform all validations on unique contact Ids
189 foreach (array_unique($form->_allContactIds) as $key => $contactId) {
190 $value = $form->_contactDetails[$contactId];
191 if ($value['do_not_email'] || empty($value['email']) || !empty($value['is_deceased']) || $value['on_hold']) {
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 {
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];
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 }
208 $toArray[] = array(
209 'text' => '"' . $value['sort_name'] . '" <' . $email . '>',
210 'id' => "$contactId::{$email}",
211 );
212 }
213 elseif (in_array($contactId, $form->_ccContactIds)) {
214 $ccArray[] = array(
215 'text' => '"' . $value['sort_name'] . '" <' . $email . '>',
216 'id' => "$contactId::{$email}",
217 );
218 }
219 elseif (in_array($contactId, $form->_bccContactIds)) {
220 $bccArray[] = array(
221 'text' => '"' . $value['sort_name'] . '" <' . $email . '>',
222 'id' => "$contactId::{$email}",
223 );
224 }
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));
234 $form->assign('ccContact', json_encode($ccArray));
235 $form->assign('bccContact', json_encode($bccArray));
236
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
243 $form->add('select', 'from_email_address', ts('From'), $form->_fromEmails, TRUE);
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
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
303 $form->add('datepicker', 'followup_date', ts('in'));
304
305 foreach ($fields as $field => $values) {
306 if (!empty($fields[$field])) {
307 $attribute = $values['attributes'] ?? NULL;
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
322 //Added for CRM-15984: Add campaign field
323 CRM_Campaign_BAO_Campaign::addCampaign($form);
324
325 $form->addFormRule(array('CRM_Contact_Form_Task_EmailCommon', 'formRule'), $form);
326 CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'templates/CRM/Contact/Form/Task/EmailCommon.js', 0, 'html-header');
327 }
328
329 /**
330 * Form rule.
331 *
332 * @param array $fields
333 * The input form values.
334 * @param array $dontCare
335 * @param array $self
336 * Additional values form 'this'.
337 *
338 * @return bool|array
339 * true if no errors, else array of errors
340 */
341 public static function formRule($fields, $dontCare, $self) {
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
352 if (!empty($fields['saveTemplate']) && empty($fields['saveTemplateName'])) {
353 $errors['saveTemplateName'] = ts("Enter name to save message template");
354 }
355
356 return empty($errors) ? TRUE : $errors;
357 }
358
359 /**
360 * Process the form after the input has been submitted and validated.
361 *
362 * @param CRM_Core_Form $form
363 */
364 public static function postProcess(&$form) {
365 self::bounceIfSimpleMailLimitExceeded(count($form->_contactIds));
366
367 // check and ensure that
368 $formValues = $form->controller->exportValues($form->getName());
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) {
381 self::saveMessageTemplate($formValues);
382
383 $from = $formValues['from_email_address'] ?? NULL;
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>
387 $from = CRM_Utils_Mail::formatFromAddress($from);
388 $subject = $formValues['subject'];
389
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');
392 $additionalDetails = NULL;
393 $ccValues = $bccValues = array();
394 foreach ($elements as $element) {
395 if (!empty($formValues[$element])) {
396 $allEmails = explode(',', $formValues[$element]);
397 foreach ($allEmails as $value) {
398 list($contactId, $email) = explode('::', $value);
399 $contactURL = CRM_Utils_System::url('civicrm/contact/view', "reset=1&force=1&cid={$contactId}", TRUE);
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;
405
406 case 'bcc_id':
407 $bccValues['email'][] = '"' . $form->_contactDetails[$contactId]['sort_name'] . '" <' . $email . '>';
408 $bccValues['details'][] = "<a href='{$contactURL}'>" . $form->_contactDetails[$contactId]['display_name'] . "</a>";
409 break;
410 }
411 }
412 }
413 }
414
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
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
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();
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)) {
451 $tempEmails[] = $emailKey;
452 $details = $form->_contactDetails[$contactId];
453 $details['email'] = $email;
454 unset($details['email_id']);
455 $formattedContactDetails[] = $details;
456 }
457 }
458
459 $contributionIds = array();
460 if ($form->getVar('_contributionIds')) {
461 $contributionIds = $form->getVar('_contributionIds');
462 }
463
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,
476 array_keys($form->_toContactDetails),
477 $additionalDetails,
478 $contributionIds,
479 CRM_Utils_Array::value('campaign_id', $formValues),
480 $form->getVar('_caseId')
481 );
482
483 $followupStatus = '';
484 if ($sent) {
485 $followupActivity = NULL;
486 if (!empty($formValues['followup_activity_type_id'])) {
487 $params['followup_activity_type_id'] = $formValues['followup_activity_type_id'];
488 $params['followup_activity_subject'] = $formValues['followup_activity_subject'];
489 $params['followup_date'] = $formValues['followup_date'];
490 $params['target_contact_id'] = $form->_contactIds;
491 $params['followup_assignee_contact_id'] = explode(',', $formValues['followup_assignee_contact_id']);
492 $followupActivity = CRM_Activity_BAO_Activity::createFollowupActivity($activityId, $params);
493 $followupStatus = ts('A followup activity has been scheduled.');
494
495 if (Civi::settings()->get('activity_assignee_notification')) {
496 if ($followupActivity) {
497 $mailToFollowupContacts = array();
498 $assignee = array($followupActivity->id);
499 $assigneeContacts = CRM_Activity_BAO_ActivityAssignment::getAssigneeNames($assignee, TRUE, FALSE);
500 foreach ($assigneeContacts as $values) {
501 $mailToFollowupContacts[$values['email']] = $values;
502 }
503
504 $sentFollowup = CRM_Activity_BAO_Activity::sendToAssignee($followupActivity, $mailToFollowupContacts);
505 if ($sentFollowup) {
506 $followupStatus .= '<br />' . ts("A copy of the follow-up activity has also been sent to follow-up assignee contacts(s).");
507 }
508 }
509 }
510 }
511
512 $count_success = count($form->_toContactDetails);
513 CRM_Core_Session::setStatus(ts('One message was sent successfully. ', array(
514 'plural' => '%count messages were sent successfully. ',
515 'count' => $count_success,
516 )) . $followupStatus, ts('Message Sent', array('plural' => 'Messages Sent', 'count' => $count_success)), 'success');
517 }
518
519 // Display the name and number of contacts for those email is not sent.
520 // php 5.4 throws out a notice since the values of these below arrays are arrays.
521 // the behavior is not documented in the php manual, but it does the right thing
522 // suppressing the notices to get things in good shape going forward
523 $emailsNotSent = @array_diff_assoc($form->_allContactDetails, $form->_contactDetails);
524
525 if ($emailsNotSent) {
526 $not_sent = array();
527 foreach ($emailsNotSent as $contactId => $values) {
528 $displayName = $values['display_name'];
529 $email = $values['email'];
530 $contactViewUrl = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid=$contactId");
531 $not_sent[] = "<a href='$contactViewUrl' title='$email'>$displayName</a>" . ($values['on_hold'] ? '(' . ts('on hold') . ')' : '');
532 }
533 $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>';
534 CRM_Core_Session::setStatus($status, ts('One Message Not Sent', array(
535 'count' => count($emailsNotSent),
536 'plural' => '%count Messages Not Sent',
537 )), 'info');
538 }
539
540 if (isset($form->_caseId)) {
541 // if case-id is found in the url, create case activity record
542 $cases = explode(',', $form->_caseId);
543 foreach ($cases as $key => $val) {
544 if (is_numeric($val)) {
545 $caseParams = array(
546 'activity_id' => $activityId,
547 'case_id' => $val,
548 );
549 CRM_Case_BAO_Case::processCaseActivity($caseParams);
550 }
551 }
552 }
553 }
554
555 /**
556 * Save the template if update selected.
557 *
558 * @param array $formValues
559 */
560 protected static function saveMessageTemplate($formValues) {
561 if (!empty($formValues['saveTemplate']) || !empty($formValues['updateTemplate'])) {
562 $messageTemplate = array(
563 'msg_text' => $formValues['text_message'],
564 'msg_html' => $formValues['html_message'],
565 'msg_subject' => $formValues['subject'],
566 'is_active' => TRUE,
567 );
568
569 if (!empty($formValues['saveTemplate'])) {
570 $messageTemplate['msg_title'] = $formValues['saveTemplateName'];
571 CRM_Core_BAO_MessageTemplate::add($messageTemplate);
572 }
573
574 if (!empty($formValues['template']) && !empty($formValues['updateTemplate'])) {
575 $messageTemplate['id'] = $formValues['template'];
576 unset($messageTemplate['msg_title']);
577 CRM_Core_BAO_MessageTemplate::add($messageTemplate);
578 }
579 }
580 }
581
582 /**
583 * Bounce if there are more emails than permitted.
584 *
585 * @param int $count
586 * The number of emails the user is attempting to send
587 */
588 public static function bounceIfSimpleMailLimitExceeded($count) {
589 $limit = Civi::settings()->get('simple_mail_limit');
590 if ($count > $limit) {
591 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.',
592 array(1 => $limit)
593 ));
594 }
595 }
596
597 }