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