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