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