Api docblock cleanup.
[civicrm-core.git] / api / v3 / Mailing.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
731a0992 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 * APIv3 functions for registering/processing mailing events.
31 *
32 * @package CiviCRM_APIv3
6a488035
TO
33 */
34
6a488035
TO
35/**
36 * Handle a create event.
37 *
38 * @param array $params
22242c87 39 *
1747ab99 40 * @return array
7c550ca0 41 * API Success Array
c23f45d3
EM
42 * @throws \API_Exception
43 * @throws \Civi\API\Exception\UnauthorizedException
6a488035 44 */
c23f45d3 45function civicrm_api3_mailing_create($params) {
6818346a
TO
46 if (CRM_Mailing_Info::workflowEnabled()) {
47 if (!CRM_Core_Permission::check('create mailings')) {
48 throw new \Civi\API\Exception\UnauthorizedException("This system uses advanced CiviMail workflows which require additional permissions");
49 }
50 if (!CRM_Core_Permission::check('schedule mailings')) {
51 unset($params['scheduled_date']);
52 unset($params['scheduled_id']);
53 }
54 if (!CRM_Core_Permission::check('approve mailings')) {
55 unset($params['approval_date']);
56 unset($params['approver_id']);
57 unset($params['approval_status_id']);
2c1e4dc1 58 unset($params['approval_note']);
6818346a
TO
59 }
60 }
7e6e8bd6 61 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params);
6a488035
TO
62}
63
c23f45d3 64/**
22242c87
EM
65 * Get mailing token.
66 *
c23f45d3 67 * @param array $params
22242c87 68 *
c23f45d3
EM
69 * @return array
70 * @throws \API_Exception
71 */
a95acf21 72function civicrm_api3_mailing_get_token($params) {
7e6e8bd6
TO
73 if (!array_key_exists("usage", $params)) {
74 throw new API_Exception('Mandatory keys missing from params array: entity');
75 }
a95acf21 76
7e6e8bd6
TO
77 $tokens = CRM_Core_SelectValues::contactTokens();
78 switch ($params['usage']) {
2241036a 79 case 'Mailing':
7e6e8bd6
TO
80 $tokens = array_merge(CRM_Core_SelectValues::mailingTokens(), $tokens);
81 break;
ea100cb5 82
2241036a 83 case 'ScheduleEventReminder':
7e6e8bd6
TO
84 $tokens = array_merge(CRM_Core_SelectValues::activityTokens(), $tokens);
85 $tokens = array_merge(CRM_Core_SelectValues::eventTokens(), $tokens);
86 $tokens = array_merge(CRM_Core_SelectValues::membershipTokens(), $tokens);
87 break;
ea100cb5 88
2241036a 89 case 'ManageEventScheduleReminder':
7e6e8bd6
TO
90 $tokens = array_merge(CRM_Core_SelectValues::eventTokens(), $tokens);
91 break;
92 }
a95acf21 93
7e6e8bd6
TO
94 return CRM_Utils_Token::formatTokensForDisplay($tokens);
95}
2b5e4108 96
11e09c59 97/**
0aa0303c
EM
98 * Adjust Metadata for Create action.
99 *
100 * The metadata is used for setting defaults, documentation & validation.
11e09c59 101 *
cf470720 102 * @param array $params
b081365f 103 * Array of parameters determined by getfields.
11e09c59 104 */
6a488035 105function _civicrm_api3_mailing_create_spec(&$params) {
6a488035 106 $params['created_id']['api.required'] = 1;
fddd185d 107 $params['created_id']['api.default'] = 'user_contact_id';
48280907
TO
108 $params['api.mailing_job.create']['api.default'] = 1;
109 $params['api.mailing_job.create']['title'] = 'Schedule Mailing?';
6a488035
TO
110}
111
e37b4b04 112/**
eb1823ac 113 * Handle a delete event.
e37b4b04 114 *
115 * @param array $params
77b97be7 116 *
a6c01b45 117 * @return array
72b3a70c 118 * API Success Array
e37b4b04 119 */
906e6120 120function civicrm_api3_mailing_delete($params) {
7e6e8bd6 121 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
e37b4b04 122}
123
6a488035
TO
124/**
125 * Handle a get event.
126 *
127 * @param array $params
d1b0d05e 128 *
6a488035
TO
129 * @return array
130 */
e37b4b04 131function civicrm_api3_mailing_get($params) {
7e6e8bd6 132 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
6a488035
TO
133}
134
c23f45d3 135/**
22242c87
EM
136 * Adjust metadata for mailing submit api function.
137 *
c23f45d3
EM
138 * @param array $spec
139 */
6818346a
TO
140function _civicrm_api3_mailing_submit_spec(&$spec) {
141 $mailingFields = CRM_Mailing_DAO_Mailing::fields();
142 $spec['id'] = $mailingFields['id'];
143 $spec['scheduled_date'] = $mailingFields['scheduled_date'];
144 $spec['approval_date'] = $mailingFields['approval_date'];
2c1e4dc1
TO
145 $spec['approval_status_id'] = $mailingFields['approval_status_id'];
146 $spec['approval_note'] = $mailingFields['approval_note'];
768c558c 147 // _skip_evil_bao_auto_recipients_: bool
6818346a
TO
148}
149
150/**
22242c87
EM
151 * Mailing submit.
152 *
6818346a 153 * @param array $params
22242c87 154 *
768c558c
TO
155 * @return array
156 * @throws API_Exception
6818346a
TO
157 */
158function civicrm_api3_mailing_submit($params) {
159 civicrm_api3_verify_mandatory($params, 'CRM_Mailing_DAO_Mailing', array('id'));
160
161 if (!isset($params['scheduled_date']) && !isset($updateParams['approval_date'])) {
162 throw new API_Exception("Missing parameter scheduled_date and/or approval_date");
163 }
164 if (!is_numeric(CRM_Core_Session::getLoggedInContactID())) {
165 throw new API_Exception("Failed to determine current user");
166 }
167
168 $updateParams = array();
169 $updateParams['id'] = $params['id'];
170
dc64d047
EM
171 // The BAO will auto-create the job - note: exact match to API default.
172 $updateParams['api.mailing_job.create'] = 0;
6818346a 173
dc64d047
EM
174 // Note: we'll pass along scheduling/approval fields, but they may get ignored
175 // if we don't have permission.
6818346a
TO
176 if (isset($params['scheduled_date'])) {
177 $updateParams['scheduled_date'] = $params['scheduled_date'];
178 $updateParams['scheduled_id'] = CRM_Core_Session::getLoggedInContactID();
179 }
768c558c 180 if (isset($params['approval_date'])) {
6818346a
TO
181 $updateParams['approval_date'] = $params['approval_date'];
182 $updateParams['approver_id'] = CRM_Core_Session::getLoggedInContactID();
183 $updateParams['approval_status_id'] = CRM_Utils_Array::value('approval_status_id', $updateParams, CRM_Core_OptionGroup::getDefaultValue('mail_approval_status'));
184 }
2c1e4dc1
TO
185 if (isset($params['approval_note'])) {
186 $updateParams['approval_note'] = $params['approval_note'];
187 }
768c558c
TO
188 if (isset($params['_skip_evil_bao_auto_recipients_'])) {
189 $updateParams['_skip_evil_bao_auto_recipients_'] = $params['_skip_evil_bao_auto_recipients_'];
190 }
6818346a
TO
191
192 $updateParams['options']['reload'] = 1;
193 return civicrm_api3('Mailing', 'create', $updateParams);
194}
195
6a488035
TO
196/**
197 * Process a bounce event by passing through to the BAOs.
198 *
199 * @param array $params
200 *
77b97be7 201 * @throws API_Exception
6a488035
TO
202 * @return array
203 */
204function civicrm_api3_mailing_event_bounce($params) {
7e6e8bd6
TO
205 $body = $params['body'];
206 unset($params['body']);
6a488035 207
7e6e8bd6 208 $params += CRM_Mailing_BAO_BouncePattern::match($body);
6a488035 209
7e6e8bd6
TO
210 if (CRM_Mailing_Event_BAO_Bounce::create($params)) {
211 return civicrm_api3_create_success($params);
212 }
213 else {
7c550ca0 214 throw new API_Exception(ts('Queue event could not be found'), 'no_queue_event
6a488035 215 ');
7e6e8bd6 216 }
6a488035
TO
217}
218
11e09c59 219/**
22242c87 220 * Adjust Metadata for bounce_spec action.
11e09c59 221 *
0aa0303c
EM
222 * The metadata is used for setting defaults, documentation & validation.
223 *
cf470720 224 * @param array $params
b081365f 225 * Array of parameters determined by getfields.
11e09c59 226 */
6a488035
TO
227function _civicrm_api3_mailing_event_bounce_spec(&$params) {
228 $params['job_id']['api.required'] = 1;
4c41ecb2 229 $params['job_id']['title'] = 'Job ID';
6a488035 230 $params['event_queue_id']['api.required'] = 1;
4c41ecb2 231 $params['event_queue_id']['title'] = 'Event Queue ID';
6a488035 232 $params['hash']['api.required'] = 1;
4c41ecb2 233 $params['hash']['title'] = 'Hash';
6a488035 234 $params['body']['api.required'] = 1;
4c41ecb2 235 $params['body']['title'] = 'Body';
6a488035
TO
236}
237
238/**
22242c87
EM
239 * Handle a confirm event.
240 *
6a488035
TO
241 * @deprecated
242 *
243 * @param array $params
244 *
245 * @return array
246 */
247function civicrm_api3_mailing_event_confirm($params) {
7e6e8bd6 248 return civicrm_api('mailing_event_confirm', 'create', $params);
6a488035
TO
249}
250
a14e9d08 251/**
d1b0d05e
EM
252 * Declare deprecated functions.
253 *
a14e9d08 254 * @deprecated api notice
a6c01b45 255 * @return array
16b10e64 256 * Array of deprecated actions
a14e9d08
CW
257 */
258function _civicrm_api3_mailing_deprecation() {
259 return array('event_confirm' => 'Mailing api "event_confirm" action is deprecated. Use the mailing_event_confirm api instead.');
260}
261
6a488035 262/**
22242c87 263 * Handle a reply event.
6a488035
TO
264 *
265 * @param array $params
266 *
267 * @return array
268 */
269function civicrm_api3_mailing_event_reply($params) {
7e6e8bd6
TO
270 $job = $params['job_id'];
271 $queue = $params['event_queue_id'];
272 $hash = $params['hash'];
273 $replyto = $params['replyTo'];
274 $bodyTxt = CRM_Utils_Array::value('bodyTxt', $params);
275 $bodyHTML = CRM_Utils_Array::value('bodyHTML', $params);
276 $fullEmail = CRM_Utils_Array::value('fullEmail', $params);
277
278 $mailing = CRM_Mailing_Event_BAO_Reply::reply($job, $queue, $hash, $replyto);
279
280 if (empty($mailing)) {
281 return civicrm_api3_create_error('Queue event could not be found');
282 }
6a488035 283
7e6e8bd6 284 CRM_Mailing_Event_BAO_Reply::send($queue, $mailing, $bodyTxt, $replyto, $bodyHTML, $fullEmail);
6a488035 285
7e6e8bd6 286 return civicrm_api3_create_success($params);
6a488035
TO
287}
288
11e09c59 289/**
22242c87 290 * Adjust Metadata for event_reply action.
11e09c59 291 *
0aa0303c
EM
292 * The metadata is used for setting defaults, documentation & validation.
293 *
cf470720 294 * @param array $params
b081365f 295 * Array of parameters determined by getfields.
11e09c59 296 */
6a488035
TO
297function _civicrm_api3_mailing_event_reply_spec(&$params) {
298 $params['job_id']['api.required'] = 1;
4c41ecb2 299 $params['job_id']['title'] = 'Job ID';
6a488035 300 $params['event_queue_id']['api.required'] = 1;
4c41ecb2 301 $params['event_queue_id']['title'] = 'Event Queue ID';
6a488035 302 $params['hash']['api.required'] = 1;
4c41ecb2 303 $params['hash']['title'] = 'Hash';
fb18363b 304 $params['replyTo']['api.required'] = 0;
4c41ecb2 305 $params['replyTo']['title'] = 'Reply To';//doesn't really explain adequately
6a488035
TO
306}
307
308/**
22242c87 309 * Handle a forward event.
6a488035
TO
310 *
311 * @param array $params
312 *
313 * @return array
314 */
315function civicrm_api3_mailing_event_forward($params) {
7e6e8bd6
TO
316 $job = $params['job_id'];
317 $queue = $params['event_queue_id'];
318 $hash = $params['hash'];
319 $email = $params['email'];
320 $fromEmail = CRM_Utils_Array::value('fromEmail', $params);
321 $params = CRM_Utils_Array::value('params', $params);
6a488035 322
7e6e8bd6 323 $forward = CRM_Mailing_Event_BAO_Forward::forward($job, $queue, $hash, $email, $fromEmail, $params);
6a488035 324
7e6e8bd6
TO
325 if ($forward) {
326 return civicrm_api3_create_success($params);
327 }
6a488035 328
7e6e8bd6 329 return civicrm_api3_create_error('Queue event could not be found');
6a488035
TO
330}
331
11e09c59 332/**
22242c87 333 * Adjust Metadata for event_forward action.
11e09c59 334 *
0aa0303c
EM
335 * The metadata is used for setting defaults, documentation & validation.
336 *
cf470720 337 * @param array $params
b081365f 338 * Array of parameters determined by getfields.
11e09c59 339 */
6a488035
TO
340function _civicrm_api3_mailing_event_forward_spec(&$params) {
341 $params['job_id']['api.required'] = 1;
48280907 342 $params['job_id']['title'] = 'Job ID';
6a488035 343 $params['event_queue_id']['api.required'] = 1;
4c41ecb2 344 $params['event_queue_id']['title'] = 'Event Queue ID';
6a488035 345 $params['hash']['api.required'] = 1;
4c41ecb2 346 $params['hash']['title'] = 'Hash';
6a488035 347 $params['email']['api.required'] = 1;
4c41ecb2 348 $params['email']['title'] = 'Forwarded to Email';
6a488035
TO
349}
350
351/**
22242c87 352 * Handle a click event.
6a488035
TO
353 *
354 * @param array $params
355 *
356 * @return array
357 */
358function civicrm_api3_mailing_event_click($params) {
7e6e8bd6
TO
359 civicrm_api3_verify_mandatory($params,
360 'CRM_Mailing_Event_DAO_TrackableURLOpen',
361 array('event_queue_id', 'url_id'),
362 FALSE
363 );
6a488035 364
7e6e8bd6
TO
365 $url_id = $params['url_id'];
366 $queue = $params['event_queue_id'];
6a488035 367
7e6e8bd6 368 $url = CRM_Mailing_Event_BAO_TrackableURLOpen::track($queue, $url_id);
6a488035 369
7e6e8bd6
TO
370 $values = array();
371 $values['url'] = $url;
372 $values['is_error'] = 0;
6a488035 373
7e6e8bd6 374 return civicrm_api3_create_success($values);
6a488035
TO
375}
376
377/**
22242c87 378 * Handle an open event.
6a488035
TO
379 *
380 * @param array $params
381 *
382 * @return array
383 */
384function civicrm_api3_mailing_event_open($params) {
385
7e6e8bd6
TO
386 civicrm_api3_verify_mandatory($params,
387 'CRM_Mailing_Event_DAO_Opened',
388 array('event_queue_id'),
389 FALSE
390 );
6a488035 391
7e6e8bd6
TO
392 $queue = $params['event_queue_id'];
393 $success = CRM_Mailing_Event_BAO_Opened::open($queue);
6a488035 394
7e6e8bd6
TO
395 if (!$success) {
396 return civicrm_api3_create_error('mailing open event failed');
397 }
6a488035 398
7e6e8bd6 399 return civicrm_api3_create_success($params);
6a488035
TO
400}
401
c23f45d3 402/**
22242c87
EM
403 * Preview mailing.
404 *
c23f45d3 405 * @param array $params
22242c87
EM
406 * Array per getfields metadata.
407 *
c23f45d3
EM
408 * @return array
409 * @throws \API_Exception
410 */
7811a84b 411function civicrm_api3_mailing_preview($params) {
412 civicrm_api3_verify_mandatory($params,
413 'CRM_Mailing_DAO_Mailing',
414 array('id'),
415 FALSE
6a488035 416 );
7811a84b 417
418 $fromEmail = NULL;
419 if (!empty($params['from_email'])) {
420 $fromEmail = $params['from_email'];
421 }
422
423 $session = CRM_Core_Session::singleton();
424 $mailing = new CRM_Mailing_BAO_Mailing();
425 $mailing->id = $params['id'];
426 $mailing->find(TRUE);
427
428 CRM_Mailing_BAO_Mailing::tokenReplace($mailing);
429
430 // get and format attachments
431 $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_mailing', $mailing->id);
432
433 $returnProperties = $mailing->getReturnProperties();
434 $contactID = CRM_Utils_Array::value('contact_id', $params);
435 if (!$contactID) {
436 $contactID = $session->get('userID');
437 }
438 $mailingParams = array('contact_id' => $contactID);
439
6bea4a47 440 $details = CRM_Utils_Token::getTokenDetails($mailingParams, $returnProperties, TRUE, TRUE, NULL, $mailing->getFlattenedTokens());
7811a84b 441
442 $mime = &$mailing->compose(NULL, NULL, NULL, $session->get('userID'), $fromEmail, $fromEmail,
443 TRUE, $details[0][$contactID], $attachments
444 );
445
bd6658bd
TO
446 return civicrm_api3_create_success(array(
447 'id' => $params['id'],
448 'contact_id' => $contactID,
449 'subject' => $mime->_headers['Subject'],
51ae62c4
TO
450 'body_html' => $mime->getHTMLBody(),
451 'body_text' => $mime->getTXTBody(),
bd6658bd 452 ));
6a488035
TO
453}
454
c23f45d3 455/**
22242c87
EM
456 * Adjust metadata for send test function.
457 *
c23f45d3
EM
458 * @param array $spec
459 */
7ada2376
TO
460function _civicrm_api3_mailing_send_test_spec(&$spec) {
461 $spec['test_group']['title'] = 'Test Group ID';
462 $spec['test_email']['title'] = 'Test Email Address';
463}
464
c23f45d3 465/**
22242c87
EM
466 * Send test mailing.
467 *
c23f45d3 468 * @param array $params
22242c87 469 *
c23f45d3
EM
470 * @return array
471 * @throws \API_Exception
472 * @throws \CiviCRM_API3_Exception
473 */
425723d9 474function civicrm_api3_mailing_send_test($params) {
475 if (!array_key_exists('test_group', $params) && !array_key_exists('test_email', $params)) {
481a74f4 476 throw new API_Exception("Mandatory key(s) missing from params array: test_group and/or test_email field are required");
425723d9 477 }
478 civicrm_api3_verify_mandatory($params,
479 'CRM_Mailing_DAO_MailingJob',
480 array('mailing_id'),
481 FALSE
482 );
7811a84b 483
425723d9 484 $testEmailParams = _civicrm_api3_generic_replace_base_params($params);
7811a84b 485 $testEmailParams['is_test'] = 1;
486 $job = civicrm_api3('MailingJob', 'create', $testEmailParams);
425723d9 487 $testEmailParams['job_id'] = $job['id'];
488 $testEmailParams['emails'] = explode(',', $testEmailParams['test_email']);
489 if (!empty($params['test_email'])) {
932c73e7
TO
490 $query = CRM_Utils_SQL_Select::from('civicrm_email e')
491 ->select(array('e.id', 'e.contact_id', 'e.email'))
492 ->join('c', 'INNER JOIN civicrm_contact c ON e.contact_id = c.id')
493 ->where('e.email IN (@emails)', array('@emails' => $testEmailParams['emails']))
494 ->where('e.on_hold = 0')
495 ->where('c.is_opt_out = 0')
496 ->where('c.do_not_email = 0')
497 ->where('c.is_deceased = 0')
498 ->groupBy('e.id')
499 ->orderBy(array('e.is_bulkmail DESC', 'e.is_primary DESC'))
500 ->toSQL();
425723d9 501 $dao = CRM_Core_DAO::executeQuery($query);
502 $emailDetail = array();
503 // fetch contact_id and email id for all existing emails
504 while ($dao->fetch()) {
505 $emailDetail[$dao->email] = array(
506 'contact_id' => $dao->contact_id,
507 'email_id' => $dao->id,
508 );
509 }
510 $dao->free();
511 foreach ($testEmailParams['emails'] as $key => $email) {
512 $email = trim($email);
513 $contactId = $emailId = NULL;
514 if (array_key_exists($email, $emailDetail)) {
515 $emailId = $emailDetail[$email]['email_id'];
516 $contactId = $emailDetail[$email]['contact_id'];
517 }
518 if (!$contactId) {
519 //create new contact.
520 $contact = civicrm_api3('Contact', 'create',
6ea503d4
TO
521 array(
522 'contact_type' => 'Individual',
425723d9 523 'email' => $email,
21dfd5f5 524 'api.Email.get' => array('return' => 'id'),
425723d9 525 )
526 );
527 $contactId = $contact['id'];
528 $emailId = $contact['values'][$contactId]['api.Email.get']['id'];
529 }
530 civicrm_api3('MailingEventQueue', 'create',
6ea503d4
TO
531 array(
532 'job_id' => $job['id'],
425723d9 533 'email_id' => $emailId,
21dfd5f5 534 'contact_id' => $contactId,
425723d9 535 )
536 );
537 }
538 }
7811a84b 539
425723d9 540 $isComplete = FALSE;
97b7d4a0
TO
541 $config = CRM_Core_Config::singleton();
542 $mailerJobSize = (property_exists($config, 'mailerJobSize')) ? $config->mailerJobSize : NULL;
425723d9 543 while (!$isComplete) {
97b7d4a0
TO
544 // Q: In CRM_Mailing_BAO_Mailing::processQueue(), the three runJobs*()
545 // functions are all called. Why does Mailing.send_test only call one?
546 // CRM_Mailing_BAO_MailingJob::runJobs_pre($mailerJobSize, NULL);
425723d9 547 $isComplete = CRM_Mailing_BAO_MailingJob::runJobs($testEmailParams);
97b7d4a0 548 // CRM_Mailing_BAO_MailingJob::runJobs_post(NULL);
425723d9 549 }
7811a84b 550
551 //return delivered mail info
552 $mailDelivered = CRM_Mailing_Event_BAO_Delivered::getRows($params['mailing_id'], $job['id'], TRUE, NULL, NULL, NULL, TRUE);
553
554 return civicrm_api3_create_success($mailDelivered);
425723d9 555}
556
7811a84b 557/**
22242c87 558 * Adjust Metadata for send_mail action.
7811a84b 559 *
0aa0303c
EM
560 * The metadata is used for setting defaults, documentation & validation.
561 *
cf470720 562 * @param array $params
b081365f 563 * Array of parameters determined by getfields.
7811a84b 564 */
565function _civicrm_api3_mailing_stats_spec(&$params) {
566 $params['date']['api.default'] = 'now';
bd6658bd 567 $params['date']['title'] = 'Date';
7811a84b 568}
6a488035 569
c23f45d3 570/**
22242c87
EM
571 * Function which needs to be explained.
572 *
c23f45d3 573 * @param array $params
22242c87 574 *
c23f45d3
EM
575 * @return array
576 * @throws \API_Exception
577 */
7811a84b 578function civicrm_api3_mailing_stats($params) {
579 civicrm_api3_verify_mandatory($params,
580 'CRM_Mailing_DAO_MailingJob',
581 array('mailing_id'),
582 FALSE
583 );
584
585 if ($params['date'] == 'now') {
586 $params['date'] = date('YmdHis');
587 }
588 else {
589 $params['date'] = CRM_Utils_Date::processDate($params['date'] . ' ' . $params['date_time']);
590 }
591
592 $stats[$params['mailing_id']] = array();
593 if (empty($params['job_id'])) {
594 $params['job_id'] = NULL;
595 }
596 foreach (array('Delivered', 'Bounces', 'Unsubscribers', 'Unique Clicks', 'Opened') as $detail) {
597 switch ($detail) {
598 case 'Delivered':
599 $stats[$params['mailing_id']] += array(
7c550ca0 600 $detail => CRM_Mailing_Event_BAO_Delivered::getTotalCount($params['mailing_id'], $params['job_id'], FALSE, $params['date']),
7811a84b 601 );
602 break;
ea100cb5 603
7811a84b 604 case 'Bounces':
605 $stats[$params['mailing_id']] += array(
7c550ca0 606 $detail => CRM_Mailing_Event_BAO_Bounce::getTotalCount($params['mailing_id'], $params['job_id'], FALSE, $params['date']),
7811a84b 607 );
608 break;
ea100cb5 609
7811a84b 610 case 'Unsubscribers':
611 $stats[$params['mailing_id']] += array(
7c550ca0 612 $detail => CRM_Mailing_Event_BAO_Unsubscribe::getTotalCount($params['mailing_id'], $params['job_id'], FALSE, NULL, $params['date']),
7811a84b 613 );
614 break;
ea100cb5 615
7811a84b 616 case 'Unique Clicks':
617 $stats[$params['mailing_id']] += array(
7c550ca0 618 $detail => CRM_Mailing_Event_BAO_TrackableURLOpen::getTotalCount($params['mailing_id'], $params['job_id'], FALSE, NULL, $params['date']),
7811a84b 619 );
620 break;
ea100cb5 621
7811a84b 622 case 'Opened':
623 $stats[$params['mailing_id']] += array(
7c550ca0 624 $detail => CRM_Mailing_Event_BAO_Opened::getTotalCount($params['mailing_id'], $params['job_id'], FALSE, $params['date']),
7811a84b 625 );
626 break;
627 }
628 }
629 return civicrm_api3_create_success($stats);
630}
631
6a488035 632/**
22242c87
EM
633 * Fix the reset dates on the email record based on when a mail was last delivered.
634 *
6a488035
TO
635 * We only consider mailings that were completed and finished in the last 3 to 7 days
636 * Both the min and max days can be set via the params
22242c87 637 *
d0997921 638 * @param array $params
22242c87 639 *
645ee340 640 * @return array
6a488035
TO
641 */
642function civicrm_api3_mailing_update_email_resetdate($params) {
7e6e8bd6
TO
643 CRM_Mailing_Event_BAO_Delivered::updateEmailResetDate(
644 CRM_Utils_Array::value('minDays', $params, 3),
645 CRM_Utils_Array::value('maxDays', $params, 3)
646 );
647 return civicrm_api3_create_success();
6a488035 648}