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