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