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