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