Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
35 | * This class provides the common functionality for sending email to | |
36 | * one or a group of contact ids. This class is reused by all the search | |
37 | * components in CiviCRM (since they all have send email as a task) | |
38 | */ | |
39 | class CRM_Contact_Form_Task_EmailCommon { | |
7da04cde | 40 | const MAX_EMAILS_KILL_SWITCH = 50; |
6a488035 TO |
41 | |
42 | public $_contactDetails = array(); | |
43 | public $_allContactDetails = array(); | |
44 | public $_toContactEmails = array(); | |
45 | ||
86538308 | 46 | /** |
beac1417 | 47 | * @deprecated Generate an array of Domain email addresses. |
f68954cb SL |
48 | * @return array $domainEmails; |
49 | */ | |
50 | public static function domainEmails() { | |
496320c3 | 51 | CRM_Core_Error::deprecatedFunctionWarning('CRM_Core_BAO_Email::domainEmails()'); |
beac1417 | 52 | return CRM_Core_BAO_Email::domainEmails(); |
f68954cb SL |
53 | } |
54 | ||
55 | /** | |
beac1417 | 56 | * Pre Process Form Addresses to be used in Quickform |
20b22300 | 57 | * @param CRM_Core_Form $form |
f68954cb | 58 | * @param bool $bounce determine if we want to throw a status bounce. |
86538308 | 59 | */ |
f68954cb | 60 | public static function preProcessFromAddress(&$form, $bounce = TRUE) { |
6a488035 TO |
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 | ||
beac1417 | 70 | $form->_emails = array(); |
6a488035 | 71 | |
fb622d6c | 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 | |
97dce831 | 74 | // If we don't have any contact IDs, use the logged in contact ID |
fb622d6c | 75 | $form->_contactIds = $form->_contactIds ?: [CRM_Core_Session::getLoggedInContactID()]; |
6a488035 | 76 | |
beac1417 | 77 | $fromEmailValues = CRM_Core_BAO_Email::getFromEmail(); |
6a488035 | 78 | |
beac1417 MW |
79 | $form->_noEmails = FALSE; |
80 | if (empty($fromEmailValues)) { | |
81 | $form->_noEmails = TRUE; | |
6a488035 | 82 | } |
6a488035 TO |
83 | $form->assign('noEmails', $form->_noEmails); |
84 | ||
f68954cb SL |
85 | if ($bounce) { |
86 | if ($form->_noEmails) { | |
beac1417 | 87 | CRM_Core_Error::statusBounce(ts('Your user record does not have a valid email address and no from addresses have been configured.')); |
f68954cb | 88 | } |
6a488035 TO |
89 | } |
90 | ||
beac1417 MW |
91 | $form->_emails = $fromEmailValues; |
92 | $form->_fromEmails = $fromEmailValues; | |
e1239fe8 | 93 | if (is_numeric(key($form->_fromEmails))) { |
f68954cb SL |
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); | |
20b22300 | 104 | } |
6a488035 TO |
105 | } |
106 | ||
107 | /** | |
fe482240 | 108 | * Build the form object. |
6a488035 | 109 | * |
c490a46a | 110 | * @param CRM_Core_Form $form |
6a488035 | 111 | */ |
00be9182 | 112 | public static function buildQuickForm(&$form) { |
6a488035 TO |
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) { | |
b3dbca23 | 118 | $form->_contactIds = explode(',', $cid); |
9cb404b4 N |
119 | } |
120 | if (count($form->_contactIds) > 1) { | |
121 | $form->_single = FALSE; | |
6a488035 | 122 | } |
a03d5ab9 | 123 | |
cac1236c M |
124 | $emailAttributes = array( |
125 | 'class' => 'huge', | |
126 | ); | |
353ffa53 TO |
127 | $to = $form->add('text', 'to', ts('To'), $emailAttributes, TRUE); |
128 | $cc = $form->add('text', 'cc_id', ts('CC'), $emailAttributes); | |
cac1236c | 129 | $bcc = $form->add('text', 'bcc_id', ts('BCC'), $emailAttributes); |
6a488035 | 130 | |
3f77636a | 131 | $setDefaults = TRUE; |
132 | if (property_exists($form, '_context') && $form->_context == 'standalone') { | |
133 | $setDefaults = FALSE; | |
134 | } | |
135 | ||
136 | $elements = array('to', 'cc', 'bcc'); | |
16947fa6 | 137 | $form->_allContactIds = $form->_toContactIds = $form->_contactIds; |
6a488035 TO |
138 | foreach ($elements as $element) { |
139 | if ($$element->getValue()) { | |
3f77636a | 140 | $allEmails = explode(',', $$element->getValue()); |
141 | if ($element == 'to') { | |
16947fa6 | 142 | $form->_toContactIds = $form->_contactIds = array(); |
6a488035 | 143 | } |
6a488035 | 144 | |
3f77636a | 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; | |
ea100cb5 | 153 | |
3f77636a | 154 | case 'cc': |
155 | $form->_ccContactIds[] = $contactId; | |
156 | break; | |
ea100cb5 | 157 | |
3f77636a | 158 | case 'bcc': |
159 | $form->_bccContactIds[] = $contactId; | |
160 | break; | |
161 | } | |
162 | ||
163 | $form->_allContactIds[] = $contactId; | |
164 | } | |
6a488035 | 165 | } |
3f77636a | 166 | |
167 | $setDefaults = TRUE; | |
6a488035 | 168 | } |
6a488035 TO |
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'); | |
16947fa6 | 174 | $form->_allContactIds = $form->_contactIds = $contact; |
6a488035 TO |
175 | } |
176 | ||
3f77636a | 177 | // check if we need to setdefaults and check for valid contact emails / communication preferences |
16947fa6 | 178 | if (is_array($form->_allContactIds) && $setDefaults) { |
6a488035 TO |
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 | ||
3f77636a | 189 | // get the details for all selected contacts ( to, cc and bcc contacts ) |
190 | list($form->_contactDetails) = CRM_Utils_Token::getTokenDetails($form->_allContactIds, | |
6a488035 TO |
191 | $returnProperties, |
192 | FALSE, | |
193 | FALSE | |
194 | ); | |
195 | ||
196 | // make a copy of all contact details | |
197 | $form->_allContactDetails = $form->_contactDetails; | |
198 | ||
8034b1c0 | 199 | // perform all validations on unique contact Ids |
200 | foreach (array_unique($form->_allContactIds) as $key => $contactId) { | |
6a488035 | 201 | $value = $form->_contactDetails[$contactId]; |
8cc574cf | 202 | if ($value['do_not_email'] || empty($value['email']) || !empty($value['is_deceased']) || $value['on_hold']) { |
6a488035 TO |
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 { | |
3f77636a | 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]; | |
8e4aa90c CW |
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 | } | |
3f77636a | 219 | $toArray[] = array( |
a03d5ab9 | 220 | 'text' => '"' . $value['sort_name'] . '" <' . $email . '>', |
3f77636a | 221 | 'id' => "$contactId::{$email}", |
222 | ); | |
223 | } | |
224 | elseif (in_array($contactId, $form->_ccContactIds)) { | |
225 | $ccArray[] = array( | |
a03d5ab9 | 226 | 'text' => '"' . $value['sort_name'] . '" <' . $email . '>', |
3f77636a | 227 | 'id' => "$contactId::{$email}", |
228 | ); | |
6a488035 | 229 | } |
3f77636a | 230 | elseif (in_array($contactId, $form->_bccContactIds)) { |
231 | $bccArray[] = array( | |
a03d5ab9 | 232 | 'text' => '"' . $value['sort_name'] . '" <' . $email . '>', |
3f77636a | 233 | 'id' => "$contactId::{$email}", |
234 | ); | |
6a488035 | 235 | } |
6a488035 TO |
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)); | |
3f77636a | 245 | $form->assign('ccContact', json_encode($ccArray)); |
246 | $form->assign('bccContact', json_encode($bccArray)); | |
247 | ||
6a488035 TO |
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 | ||
beac1417 | 254 | $form->add('select', 'from_email_address', ts('From'), $form->_fromEmails, TRUE); |
6a488035 TO |
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 | ||
cfcf9132 | 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 | ||
824989b9 AS |
333 | //Added for CRM-15984: Add campaign field |
334 | CRM_Campaign_BAO_Campaign::addCampaign($form); | |
335 | ||
6a488035 | 336 | $form->addFormRule(array('CRM_Contact_Form_Task_EmailCommon', 'formRule'), $form); |
e573fefc | 337 | CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'templates/CRM/Contact/Form/Task/EmailCommon.js', 0, 'html-header'); |
6a488035 TO |
338 | } |
339 | ||
340 | /** | |
fe482240 | 341 | * Form rule. |
6a488035 | 342 | * |
77c5b619 TO |
343 | * @param array $fields |
344 | * The input form values. | |
6a488035 | 345 | * @param array $dontCare |
77c5b619 TO |
346 | * @param array $self |
347 | * Additional values form 'this'. | |
6a488035 | 348 | * |
72b3a70c CW |
349 | * @return bool|array |
350 | * true if no errors, else array of errors | |
6a488035 | 351 | */ |
00be9182 | 352 | public static function formRule($fields, $dontCare, $self) { |
6a488035 TO |
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 | |
a7488080 | 363 | if (!empty($fields['saveTemplate']) && empty($fields['saveTemplateName'])) { |
6a488035 TO |
364 | $errors['saveTemplateName'] = ts("Enter name to save message template"); |
365 | } | |
366 | ||
367 | return empty($errors) ? TRUE : $errors; | |
368 | } | |
369 | ||
370 | /** | |
fe482240 | 371 | * Process the form after the input has been submitted and validated. |
6a488035 | 372 | * |
c490a46a | 373 | * @param CRM_Core_Form $form |
6a488035 | 374 | */ |
00be9182 | 375 | public static function postProcess(&$form) { |
eb523ccd | 376 | self::bounceIfSimpleMailLimitExceeded(count($form->_contactIds)); |
6a488035 TO |
377 | |
378 | // check and ensure that | |
379 | $formValues = $form->controller->exportValues($form->getName()); | |
8093086b | 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) { | |
ed999bcd | 392 | self::saveMessageTemplate($formValues); |
393 | ||
beac1417 | 394 | $from = CRM_Utils_Array::value('from_email_address', $formValues); |
353ffa53 | 395 | $subject = $formValues['subject']; |
6a488035 | 396 | |
3f77636a | 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'); | |
c1d26519 | 399 | $additionalDetails = NULL; |
3f77636a | 400 | $ccValues = $bccValues = array(); |
c1d26519 | 401 | foreach ($elements as $element) { |
a7488080 | 402 | if (!empty($formValues[$element])) { |
3f77636a | 403 | $allEmails = explode(',', $formValues[$element]); |
404 | foreach ($allEmails as $value) { | |
405 | list($contactId, $email) = explode('::', $value); | |
4eeb9a5b | 406 | $contactURL = CRM_Utils_System::url('civicrm/contact/view', "reset=1&force=1&cid={$contactId}", TRUE); |
3f77636a | 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; | |
ea100cb5 | 412 | |
3f77636a | 413 | case 'bcc_id': |
d3e86119 | 414 | $bccValues['email'][] = '"' . $form->_contactDetails[$contactId]['sort_name'] . '" <' . $email . '>'; |
3f77636a | 415 | $bccValues['details'][] = "<a href='{$contactURL}'>" . $form->_contactDetails[$contactId]['display_name'] . "</a>"; |
416 | break; | |
417 | } | |
c1d26519 | 418 | } |
c1d26519 | 419 | } |
420 | } | |
421 | ||
3f77636a | 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 | ||
6a488035 TO |
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 | ||
6a488035 TO |
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(); | |
6a488035 TO |
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)) { | |
353ffa53 TO |
458 | $tempEmails[] = $emailKey; |
459 | $details = $form->_contactDetails[$contactId]; | |
6a488035 TO |
460 | $details['email'] = $email; |
461 | unset($details['email_id']); | |
462 | $formattedContactDetails[] = $details; | |
463 | } | |
464 | } | |
465 | ||
7e2ec997 E |
466 | $contributionIds = array(); |
467 | if ($form->getVar('_contributionIds')) { | |
468 | $contributionIds = $form->getVar('_contributionIds'); | |
469 | } | |
470 | ||
6a488035 TO |
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, | |
3f77636a | 483 | array_keys($form->_toContactDetails), |
7e2ec997 | 484 | $additionalDetails, |
824989b9 AS |
485 | $contributionIds, |
486 | CRM_Utils_Array::value('campaign_id', $formValues) | |
6a488035 TO |
487 | ); |
488 | ||
cfcf9132 | 489 | $followupStatus = ''; |
6a488035 | 490 | if ($sent) { |
cfcf9132 | 491 | $followupActivity = NULL; |
57a6ee70 | 492 | if (!empty($formValues['followup_activity_type_id'])) { |
cfcf9132 | 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; | |
57a6ee70 | 498 | $params['followup_assignee_contact_id'] = explode(',', $formValues['followup_assignee_contact_id']); |
cfcf9132 | 499 | $followupActivity = CRM_Activity_BAO_Activity::createFollowupActivity($activityId, $params); |
500 | $followupStatus = ts('A followup activity has been scheduled.'); | |
501 | ||
aaffa79f | 502 | if (Civi::settings()->get('activity_assignee_notification')) { |
cfcf9132 | 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 | ||
bc883279 | 511 | $sentFollowup = CRM_Activity_BAO_Activity::sendToAssignee($followupActivity, $mailToFollowupContacts); |
512 | if ($sentFollowup) { | |
5c89eeee | 513 | $followupStatus .= '<br />' . ts("A copy of the follow-up activity has also been sent to follow-up assignee contacts(s)."); |
bc883279 | 514 | } |
cfcf9132 | 515 | } |
516 | } | |
517 | } | |
518 | ||
3f77636a | 519 | $count_success = count($form->_toContactDetails); |
cfcf9132 | 520 | CRM_Core_Session::setStatus(ts('One message was sent successfully. ', array( |
521 | 'plural' => '%count messages were sent successfully. ', | |
389bcebf | 522 | 'count' => $count_success, |
57a6ee70 | 523 | )) . $followupStatus, ts('Message Sent', array('plural' => 'Messages Sent', 'count' => $count_success)), 'success'); |
6a488035 TO |
524 | } |
525 | ||
a5611c8e DL |
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); | |
6a488035 TO |
531 | |
532 | if ($emailsNotSent) { | |
533 | $not_sent = array(); | |
534 | foreach ($emailsNotSent as $contactId => $values) { | |
353ffa53 TO |
535 | $displayName = $values['display_name']; |
536 | $email = $values['email']; | |
6a488035 TO |
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>'; | |
353ffa53 TO |
541 | CRM_Core_Session::setStatus($status, ts('One Message Not Sent', array( |
542 | 'count' => count($emailsNotSent), | |
389bcebf | 543 | 'plural' => '%count Messages Not Sent', |
353ffa53 | 544 | )), 'info'); |
6a488035 TO |
545 | } |
546 | ||
9cb404b4 | 547 | if (isset($form->_caseId)) { |
6a488035 | 548 | // if case-id is found in the url, create case activity record |
9cb404b4 | 549 | $cases = explode(',', $form->_caseId); |
22e263ad | 550 | foreach ($cases as $key => $val) { |
9cb404b4 N |
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 | } | |
6a488035 TO |
559 | } |
560 | } | |
96025800 | 561 | |
ed999bcd | 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 | ||
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) { | |
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 | ||
6a488035 | 604 | } |