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