3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
30 * APIv3 functions for registering/processing mailing events.
32 * @package CiviCRM_APIv3
36 * Handle a create event.
38 * @param array $params
42 * @throws \API_Exception
43 * @throws \Civi\API\Exception\UnauthorizedException
45 function civicrm_api3_mailing_create($params) {
46 if (isset($params['template_options']) && is_array($params['template_options'])) {
47 $params['template_options'] = ($params['template_options'] === []) ?
'{}' : json_encode($params['template_options']);
49 if (CRM_Mailing_Info
::workflowEnabled()) {
50 // Note: 'schedule mailings' and 'approve mailings' can update certain fields, but can't create.
52 if (empty($params['id'])) {
53 if (!CRM_Core_Permission
::check('access CiviMail') && !CRM_Core_Permission
::check('create mailings')) {
54 throw new \Civi\API\Exception\
UnauthorizedException("Cannot create new mailing. Required permission: 'access CiviMail' or 'create mailings'");
59 $fieldPerms = CRM_Mailing_BAO_Mailing
::getWorkflowFieldPerms();
60 foreach (array_keys($params) as $field) {
61 if (CRM_Core_Permission
::check($fieldPerms[$field])) {
62 $safeParams[$field] = $params[$field];
67 $safeParams = $params;
69 $timestampCheck = TRUE;
70 if (!empty($params['id']) && !empty($params['modified_date'])) {
71 $timestampCheck = _civicrm_api3_compare_timestamps($safeParams['modified_date'], $safeParams['id'], 'Mailing');
72 unset($safeParams['modified_date']);
74 if (!$timestampCheck) {
75 throw new API_Exception("Mailing has not been saved, Content maybe out of date, please refresh the page and try again");
78 // FlexMailer is a refactoring of CiviMail which provides new hooks/APIs/docs. If the sysadmin has opted to enable it, then use that instead of CiviMail.
79 $safeParams['_evil_bao_validator_'] = \CRM_Utils_Constant
::value('CIVICRM_FLEXMAILER_HACK_SENDABLE', 'CRM_Mailing_BAO_Mailing::checkSendable');
80 $result = _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__
), $safeParams, 'Mailing');
81 return _civicrm_api3_mailing_get_formatResult($result);
85 * Get tokens for one or more entity type
87 * Output will be formatted either as a flat list,
88 * or pass sequential=1 to retrieve as a hierarchy formatted for select2.
90 * @param array $params
91 * Should contain an array of entities to retrieve tokens for.
94 * @throws \API_Exception
96 function civicrm_api3_mailing_gettokens($params) {
98 foreach ((array) $params['entity'] as $ent) {
99 $func = lcfirst($ent) . 'Tokens';
100 if (!method_exists('CRM_Core_SelectValues', $func)) {
101 throw new API_Exception('Unknown token entity: ' . $ent);
103 $tokens = array_merge(CRM_Core_SelectValues
::$func(), $tokens);
105 if (!empty($params['sequential'])) {
106 $tokens = CRM_Utils_Token
::formatTokensForDisplay($tokens);
108 return civicrm_api3_create_success($tokens, $params, 'Mailing', 'gettokens');
112 * Adjust Metadata for Create action.
114 * The metadata is used for setting defaults, documentation & validation.
116 * @param array $params
117 * Array of parameters determined by getfields.
119 function _civicrm_api3_mailing_gettokens_spec(&$params) {
120 $params['entity'] = [
121 'api.default' => ['contact'],
127 // Fetch a list of token functions and format to look like entity names
128 foreach (get_class_methods('CRM_Core_SelectValues') as $func) {
129 if (strpos($func, 'Tokens')) {
130 $ent = ucfirst(str_replace('Tokens', '', $func));
131 $params['entity']['options'][$ent] = $ent;
137 * Adjust Metadata for Create action.
139 * The metadata is used for setting defaults, documentation & validation.
141 * @param array $params
142 * Array of parameters determined by getfields.
144 function _civicrm_api3_mailing_create_spec(&$params) {
145 $params['created_id']['api.default'] = 'user_contact_id';
147 $params['override_verp']['api.default'] = !CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
, 'track_civimail_replies');
148 $params['visibility']['api.default'] = 'Public Pages';
149 $params['dedupe_email']['api.default'] = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
, 'dedupe_email_default');
151 $params['forward_replies']['api.default'] = FALSE;
152 $params['auto_responder']['api.default'] = FALSE;
153 $params['open_tracking']['api.default'] = TRUE;
154 $params['url_tracking']['api.default'] = TRUE;
156 $params['header_id']['api.default'] = CRM_Mailing_PseudoConstant
::defaultComponent('Header', '');
157 $params['footer_id']['api.default'] = CRM_Mailing_PseudoConstant
::defaultComponent('Footer', '');
158 $params['optout_id']['api.default'] = CRM_Mailing_PseudoConstant
::defaultComponent('OptOut', '');
159 $params['reply_id']['api.default'] = CRM_Mailing_PseudoConstant
::defaultComponent('Reply', '');
160 $params['resubscribe_id']['api.default'] = CRM_Mailing_PseudoConstant
::defaultComponent('Resubscribe', '');
161 $params['unsubscribe_id']['api.default'] = CRM_Mailing_PseudoConstant
::defaultComponent('Unsubscribe', '');
162 $params['mailing_type']['api.default'] = 'standalone';
163 $defaultAddress = CRM_Core_BAO_Domain
::getNameAndEmail(TRUE, TRUE);
164 foreach ($defaultAddress as $value) {
165 if (preg_match('/"(.*)" <(.*)>/', $value, $match)) {
166 $params['from_email']['api.default'] = $match[2];
167 $params['from_name']['api.default'] = $match[1];
173 * Adjust metadata for clone spec action.
177 function _civicrm_api3_mailing_clone_spec(&$spec) {
178 $mailingFields = CRM_Mailing_DAO_Mailing
::fields();
179 $spec['id'] = $mailingFields['id'];
180 $spec['id']['api.required'] = 1;
186 * @param array $params
189 * @throws \CiviCRM_API3_Exception
191 function civicrm_api3_mailing_clone($params) {
201 'approval_status_id',
208 $get = civicrm_api3('Mailing', 'getsingle', ['id' => $params['id']]);
211 $newParams['debug'] = CRM_Utils_Array
::value('debug', $params);
212 $newParams['groups']['include'] = [];
213 $newParams['groups']['exclude'] = [];
214 $newParams['mailings']['include'] = [];
215 $newParams['mailings']['exclude'] = [];
216 foreach ($get as $field => $value) {
217 if (!in_array($field, $BLACKLIST)) {
218 $newParams[$field] = $value;
222 $dao = new CRM_Mailing_DAO_MailingGroup();
223 $dao->mailing_id
= $params['id'];
225 while ($dao->fetch()) {
226 // CRM-11431; account for multi-lingual
227 $entity = (substr($dao->entity_table
, 0, 15) == 'civicrm_mailing') ?
'mailings' : 'groups';
228 $newParams[$entity][strtolower($dao->group_type
)][] = $dao->entity_id
;
231 return civicrm_api3('Mailing', 'create', $newParams);
235 * Handle a delete event.
237 * @param array $params
242 function civicrm_api3_mailing_delete($params) {
243 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
247 * Handle a get event.
249 * @param array $params
253 function civicrm_api3_mailing_get($params) {
254 $result = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params, TRUE, 'Mailing');
255 return _civicrm_api3_mailing_get_formatResult($result);
261 * @param array $result
264 * @throws \CRM_Core_Exception
266 function _civicrm_api3_mailing_get_formatResult($result) {
267 if (isset($result['values']) && is_array($result['values'])) {
268 foreach ($result['values'] as $key => $caseType) {
269 if (isset($result['values'][$key]['template_options']) && is_string($result['values'][$key]['template_options'])) {
270 $result['values'][$key]['template_options'] = json_decode($result['values'][$key]['template_options'], TRUE);
278 * Adjust metadata for mailing submit api function.
282 function _civicrm_api3_mailing_submit_spec(&$spec) {
283 $mailingFields = CRM_Mailing_DAO_Mailing
::fields();
284 $spec['id'] = $mailingFields['id'];
285 $spec['scheduled_date'] = $mailingFields['scheduled_date'];
286 $spec['approval_date'] = $mailingFields['approval_date'];
287 $spec['approval_status_id'] = $mailingFields['approval_status_id'];
288 $spec['approval_note'] = $mailingFields['approval_note'];
289 // _skip_evil_bao_auto_recipients_: bool
295 * @param array $params
298 * @throws API_Exception
300 function civicrm_api3_mailing_submit($params) {
301 civicrm_api3_verify_mandatory($params, 'CRM_Mailing_DAO_Mailing', ['id']);
303 if (!isset($params['scheduled_date']) && !isset($updateParams['approval_date'])) {
304 throw new API_Exception("Missing parameter scheduled_date and/or approval_date");
306 if (!is_numeric(CRM_Core_Session
::getLoggedInContactID())) {
307 throw new API_Exception("Failed to determine current user");
311 $updateParams['id'] = $params['id'];
313 // Note: we'll pass along scheduling/approval fields, but they may get ignored
314 // if we don't have permission.
315 if (isset($params['scheduled_date'])) {
316 $updateParams['scheduled_date'] = $params['scheduled_date'];
317 $updateParams['scheduled_id'] = CRM_Core_Session
::getLoggedInContactID();
319 if (isset($params['approval_date'])) {
320 $updateParams['approval_date'] = $params['approval_date'];
321 $updateParams['approver_id'] = CRM_Core_Session
::getLoggedInContactID();
322 $updateParams['approval_status_id'] = CRM_Utils_Array
::value('approval_status_id', $updateParams, CRM_Core_OptionGroup
::getDefaultValue('mail_approval_status'));
324 if (isset($params['approval_note'])) {
325 $updateParams['approval_note'] = $params['approval_note'];
327 if (isset($params['_skip_evil_bao_auto_recipients_'])) {
328 $updateParams['_skip_evil_bao_auto_recipients_'] = $params['_skip_evil_bao_auto_recipients_'];
331 $updateParams['options']['reload'] = 1;
332 return civicrm_api3('Mailing', 'create', $updateParams);
336 * Process a bounce event by passing through to the BAOs.
338 * @param array $params
340 * @throws API_Exception
343 function civicrm_api3_mailing_event_bounce($params) {
344 $body = $params['body'];
345 unset($params['body']);
347 $params +
= CRM_Mailing_BAO_BouncePattern
::match($body);
349 if (CRM_Mailing_Event_BAO_Bounce
::create($params)) {
350 return civicrm_api3_create_success($params);
353 throw new API_Exception(ts('Queue event could not be found'), 'no_queue_event');
358 * Adjust Metadata for bounce_spec action.
360 * The metadata is used for setting defaults, documentation & validation.
362 * @param array $params
363 * Array of parameters determined by getfields.
365 function _civicrm_api3_mailing_event_bounce_spec(&$params) {
366 $params['job_id']['api.required'] = 1;
367 $params['job_id']['title'] = 'Job ID';
368 $params['event_queue_id']['api.required'] = 1;
369 $params['event_queue_id']['title'] = 'Event Queue ID';
370 $params['hash']['api.required'] = 1;
371 $params['hash']['title'] = 'Hash';
372 $params['body']['api.required'] = 1;
373 $params['body']['title'] = 'Body';
377 * Handle a confirm event.
381 * @param array $params
385 function civicrm_api3_mailing_event_confirm($params) {
386 return civicrm_api('MailingEventConfirm', 'create', $params);
390 * Declare deprecated functions.
392 * @deprecated api notice
394 * Array of deprecated actions
396 function _civicrm_api3_mailing_deprecation() {
397 return ['event_confirm' => 'Mailing api "event_confirm" action is deprecated. Use the mailing_event_confirm api instead.'];
401 * Handle a reply event.
403 * @param array $params
407 function civicrm_api3_mailing_event_reply($params) {
408 $job = $params['job_id'];
409 $queue = $params['event_queue_id'];
410 $hash = $params['hash'];
411 $replyto = $params['replyTo'];
412 $bodyTxt = CRM_Utils_Array
::value('bodyTxt', $params);
413 $bodyHTML = CRM_Utils_Array
::value('bodyHTML', $params);
414 $fullEmail = CRM_Utils_Array
::value('fullEmail', $params);
416 $mailing = CRM_Mailing_Event_BAO_Reply
::reply($job, $queue, $hash, $replyto);
418 if (empty($mailing)) {
419 return civicrm_api3_create_error('Queue event could not be found');
422 CRM_Mailing_Event_BAO_Reply
::send($queue, $mailing, $bodyTxt, $replyto, $bodyHTML, $fullEmail);
424 return civicrm_api3_create_success($params);
428 * Adjust Metadata for event_reply action.
430 * The metadata is used for setting defaults, documentation & validation.
432 * @param array $params
433 * Array of parameters determined by getfields.
435 function _civicrm_api3_mailing_event_reply_spec(&$params) {
436 $params['job_id']['api.required'] = 1;
437 $params['job_id']['title'] = 'Job ID';
438 $params['event_queue_id']['api.required'] = 1;
439 $params['event_queue_id']['title'] = 'Event Queue ID';
440 $params['hash']['api.required'] = 1;
441 $params['hash']['title'] = 'Hash';
442 $params['replyTo']['api.required'] = 0;
443 //doesn't really explain adequately
444 $params['replyTo']['title'] = 'Reply To';
448 * Handle a forward event.
450 * @param array $params
454 function civicrm_api3_mailing_event_forward($params) {
455 $job = $params['job_id'];
456 $queue = $params['event_queue_id'];
457 $hash = $params['hash'];
458 $email = $params['email'];
459 $fromEmail = CRM_Utils_Array
::value('fromEmail', $params);
460 $params = CRM_Utils_Array
::value('params', $params);
462 $forward = CRM_Mailing_Event_BAO_Forward
::forward($job, $queue, $hash, $email, $fromEmail, $params);
465 return civicrm_api3_create_success($params);
468 return civicrm_api3_create_error('Queue event could not be found');
472 * Adjust Metadata for event_forward action.
474 * The metadata is used for setting defaults, documentation & validation.
476 * @param array $params
477 * Array of parameters determined by getfields.
479 function _civicrm_api3_mailing_event_forward_spec(&$params) {
480 $params['job_id']['api.required'] = 1;
481 $params['job_id']['title'] = 'Job ID';
482 $params['event_queue_id']['api.required'] = 1;
483 $params['event_queue_id']['title'] = 'Event Queue ID';
484 $params['hash']['api.required'] = 1;
485 $params['hash']['title'] = 'Hash';
486 $params['email']['api.required'] = 1;
487 $params['email']['title'] = 'Forwarded to Email';
491 * Handle a click event.
493 * @param array $params
497 function civicrm_api3_mailing_event_click($params) {
498 civicrm_api3_verify_mandatory($params,
499 'CRM_Mailing_Event_DAO_TrackableURLOpen',
500 ['event_queue_id', 'url_id'],
504 $url_id = $params['url_id'];
505 $queue = $params['event_queue_id'];
507 $url = CRM_Mailing_Event_BAO_TrackableURLOpen
::track($queue, $url_id);
510 $values['url'] = $url;
511 $values['is_error'] = 0;
513 return civicrm_api3_create_success($values);
517 * Handle an open event.
519 * @param array $params
523 function civicrm_api3_mailing_event_open($params) {
525 civicrm_api3_verify_mandatory($params,
526 'CRM_Mailing_Event_DAO_Opened',
531 $queue = $params['event_queue_id'];
532 $success = CRM_Mailing_Event_BAO_Opened
::open($queue);
535 return civicrm_api3_create_error('mailing open event failed');
538 return civicrm_api3_create_success($params);
544 * @param array $params
545 * Array per getfields metadata.
548 * @throws \API_Exception
550 function civicrm_api3_mailing_preview($params) {
552 if (!empty($params['from_email'])) {
553 $fromEmail = $params['from_email'];
556 $mailing = new CRM_Mailing_BAO_Mailing();
557 $mailingID = CRM_Utils_Array
::value('id', $params);
559 $mailing->id
= $mailingID;
560 $mailing->find(TRUE);
563 $mailing->copyValues($params);
566 $session = CRM_Core_Session
::singleton();
568 CRM_Mailing_BAO_Mailing
::tokenReplace($mailing);
570 // get and format attachments
571 $attachments = CRM_Core_BAO_File
::getEntityFile('civicrm_mailing', $mailing->id
);
573 $returnProperties = $mailing->getReturnProperties();
574 $contactID = CRM_Utils_Array
::value('contact_id', $params);
576 // If we still don't have a userID in a session because we are annon then set contactID to be 0
577 $contactID = empty($session->get('userID')) ?
0 : $session->get('userID');
579 $mailingParams = ['contact_id' => $contactID];
581 // if $contactID is zero we are dealing with annon user so call separate function for annon users
583 $details = CRM_Utils_Token
::getAnonymousTokenDetails($mailingParams, $returnProperties, TRUE, TRUE, NULL, $mailing->getFlattenedTokens());
586 $details = CRM_Utils_Token
::getTokenDetails($mailingParams, $returnProperties, TRUE, TRUE, NULL, $mailing->getFlattenedTokens());
589 $mime = $mailing->compose(NULL, NULL, NULL, $contactID, $fromEmail, $fromEmail,
590 TRUE, $details[0][$contactID], $attachments
593 return civicrm_api3_create_success([
594 'id' => $params['id'],
595 'contact_id' => $contactID,
596 'subject' => $mime->headers()['Subject'],
597 'body_html' => $mime->getHTMLBody(),
598 'body_text' => $mime->getTXTBody(),
603 * Adjust metadata for send test function.
607 function _civicrm_api3_mailing_send_test_spec(&$spec) {
608 $spec['test_group']['title'] = 'Test Group ID';
609 $spec['test_email']['title'] = 'Test Email Address';
610 $spec['mailing_id']['api.required'] = TRUE;
611 $spec['mailing_id']['title'] = ts('Mailing Id');
617 * @param array $params
620 * @throws \API_Exception
621 * @throws \CiviCRM_API3_Exception
623 function civicrm_api3_mailing_send_test($params) {
624 if (!array_key_exists('test_group', $params) && !array_key_exists('test_email', $params)) {
625 throw new API_Exception("Mandatory key(s) missing from params array: test_group and/or test_email field are required");
627 civicrm_api3_verify_mandatory($params,
628 'CRM_Mailing_DAO_MailingJob',
633 $testEmailParams = _civicrm_api3_generic_replace_base_params($params);
634 if (isset($testEmailParams['id'])) {
635 unset($testEmailParams['id']);
638 $testEmailParams['is_test'] = 1;
639 $testEmailParams['status'] = 'Scheduled';
640 $testEmailParams['scheduled_date'] = CRM_Utils_Date
::processDate(date('Y-m-d'), date('H:i:s'));
641 $job = civicrm_api3('MailingJob', 'create', $testEmailParams);
642 $testEmailParams['job_id'] = $job['id'];
643 $testEmailParams['emails'] = array_key_exists('test_email', $testEmailParams) ?
explode(',', strtolower($testEmailParams['test_email'])) : NULL;
644 if (!empty($params['test_email'])) {
645 $query = CRM_Utils_SQL_Select
::from('civicrm_email e')
646 ->select(['e.id', 'e.contact_id', 'e.email'])
647 ->join('c', 'INNER JOIN civicrm_contact c ON e.contact_id = c.id')
648 ->where('e.email IN (@emails)', ['@emails' => $testEmailParams['emails']])
649 ->where('e.on_hold = 0')
650 ->where('c.is_opt_out = 0')
651 ->where('c.do_not_email = 0')
652 ->where('c.is_deceased = 0')
653 ->where('c.is_deleted = 0')
655 ->orderBy(['e.is_bulkmail DESC', 'e.is_primary DESC'])
657 $dao = CRM_Core_DAO
::executeQuery($query);
659 // fetch contact_id and email id for all existing emails
660 while ($dao->fetch()) {
661 $emailDetail[strtolower($dao->email
)] = [
662 'contact_id' => $dao->contact_id
,
663 'email_id' => $dao->id
,
666 foreach ($testEmailParams['emails'] as $key => $email) {
667 $email = trim($email);
668 $contactId = $emailId = NULL;
669 if (array_key_exists($email, $emailDetail)) {
670 $emailId = $emailDetail[$email]['email_id'];
671 $contactId = $emailDetail[$email]['contact_id'];
674 //create new contact.
675 $contact = civicrm_api3('Contact', 'create',
677 'contact_type' => 'Individual',
679 'api.Email.get' => ['return' => 'id'],
682 $contactId = $contact['id'];
683 $emailId = $contact['values'][$contactId]['api.Email.get']['id'];
685 civicrm_api3('MailingEventQueue', 'create',
687 'job_id' => $job['id'],
688 'email_id' => $emailId,
689 'contact_id' => $contactId,
697 while (!$isComplete) {
698 // Q: In CRM_Mailing_BAO_Mailing::processQueue(), the three runJobs*()
699 // functions are all called. Why does Mailing.send_test only call one?
700 // CRM_Mailing_BAO_MailingJob::runJobs_pre($mailerJobSize, NULL);
701 $isComplete = CRM_Mailing_BAO_MailingJob
::runJobs($testEmailParams);
702 // CRM_Mailing_BAO_MailingJob::runJobs_post(NULL);
705 //return delivered mail info
706 $mailDelivered = CRM_Mailing_Event_BAO_Delivered
::getRows($params['mailing_id'], $job['id'], TRUE, NULL, NULL, NULL, TRUE);
708 return civicrm_api3_create_success($mailDelivered);
712 * Adjust Metadata for send_mail action.
714 * The metadata is used for setting defaults, documentation & validation.
716 * @param array $params
717 * Array of parameters determined by getfields.
719 function _civicrm_api3_mailing_stats_spec(&$params) {
720 $params['date']['api.default'] = 'now';
721 $params['date']['title'] = 'Date';
722 $params['is_distinct']['api.default'] = FALSE;
723 $params['is_distinct']['title'] = 'Is Distinct';
727 * Function which needs to be explained.
729 * @param array $params
732 * @throws \API_Exception
734 function civicrm_api3_mailing_stats($params) {
735 civicrm_api3_verify_mandatory($params,
736 'CRM_Mailing_DAO_MailingJob',
741 if ($params['date'] == 'now') {
742 $params['date'] = date('YmdHis');
745 $params['date'] = CRM_Utils_Date
::processDate($params['date'] . ' ' . $params['date_time']);
748 $stats[$params['mailing_id']] = [];
749 if (empty($params['job_id'])) {
750 $params['job_id'] = NULL;
752 foreach (['Delivered', 'Bounces', 'Unsubscribers', 'Unique Clicks', 'Opened'] as $detail) {
755 $stats[$params['mailing_id']] +
= [
756 $detail => CRM_Mailing_Event_BAO_Delivered
::getTotalCount($params['mailing_id'], $params['job_id'], (bool) $params['is_distinct'], $params['date']),
761 $stats[$params['mailing_id']] +
= [
762 $detail => CRM_Mailing_Event_BAO_Bounce
::getTotalCount($params['mailing_id'], $params['job_id'], (bool) $params['is_distinct'], $params['date']),
766 case 'Unsubscribers':
767 $stats[$params['mailing_id']] +
= [
768 $detail => CRM_Mailing_Event_BAO_Unsubscribe
::getTotalCount($params['mailing_id'], $params['job_id'], (bool) $params['is_distinct'], NULL, $params['date']),
772 case 'Unique Clicks':
773 $stats[$params['mailing_id']] +
= [
774 $detail => CRM_Mailing_Event_BAO_TrackableURLOpen
::getTotalCount($params['mailing_id'], $params['job_id'], (bool) $params['is_distinct'], NULL, $params['date']),
779 $stats[$params['mailing_id']] +
= [
780 $detail => CRM_Mailing_Event_BAO_Opened
::getTotalCount($params['mailing_id'], $params['job_id'], (bool) $params['is_distinct'], $params['date']),
785 $stats[$params['mailing_id']]['delivered_rate'] = $stats[$params['mailing_id']]['opened_rate'] = $stats[$params['mailing_id']]['clickthrough_rate'] = '0.00%';
786 if (!empty(CRM_Mailing_Event_BAO_Queue
::getTotalCount($params['mailing_id'], $params['job_id']))) {
787 $stats[$params['mailing_id']]['delivered_rate'] = round((100.0 * $stats[$params['mailing_id']]['Delivered']) / CRM_Mailing_Event_BAO_Queue
::getTotalCount($params['mailing_id'], $params['job_id']), 2) . '%';
789 if (!empty($stats[$params['mailing_id']]['Delivered'])) {
790 $stats[$params['mailing_id']]['opened_rate'] = round($stats[$params['mailing_id']]['Opened'] / $stats[$params['mailing_id']]['Delivered'] * 100.0, 2) . '%';
791 $stats[$params['mailing_id']]['clickthrough_rate'] = round($stats[$params['mailing_id']]['Unique Clicks'] / $stats[$params['mailing_id']]['Delivered'] * 100.0, 2) . '%';
793 return civicrm_api3_create_success($stats);
797 * Fix the reset dates on the email record based on when a mail was last delivered.
799 * We only consider mailings that were completed and finished in the last 3 to 7 days
800 * Both the min and max days can be set via the params
802 * @param array $params
806 function civicrm_api3_mailing_update_email_resetdate($params) {
807 CRM_Mailing_Event_BAO_Delivered
::updateEmailResetDate(
808 CRM_Utils_Array
::value('minDays', $params, 3),
809 CRM_Utils_Array
::value('maxDays', $params, 3)
811 return civicrm_api3_create_success();