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