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