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