Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | |
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
731a0992 | 5 | | CiviCRM version 4.5 | |
6a488035 | 6 | +--------------------------------------------------------------------+ |
731a0992 | 7 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
8 | +--------------------------------------------------------------------+ |
9 | | This file is a part of CiviCRM. | | |
10 | | | | |
11 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
12 | | under the terms of the GNU Affero General Public License | | |
13 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
14 | | | | |
15 | | CiviCRM is distributed in the hope that it will be useful, but | | |
16 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
18 | | See the GNU Affero General Public License for more details. | | |
19 | | | | |
20 | | You should have received a copy of the GNU Affero General Public | | |
21 | | License and the CiviCRM Licensing Exception along | | |
22 | | with this program; if not, contact CiviCRM LLC | | |
23 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
24 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
25 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
26 | +--------------------------------------------------------------------+ | |
27 | */ | |
28 | ||
29 | /** | |
30 | * | |
31 | * APIv3 functions for registering/processing mailing events. | |
32 | * | |
33 | * @package CiviCRM_APIv3 | |
34 | * @subpackage API_Mailing | |
731a0992 | 35 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
36 | * $Id$ |
37 | * | |
38 | */ | |
39 | ||
40 | /** | |
41 | * Files required for this package | |
42 | */ | |
43 | ||
44 | /** | |
45 | * Handle a create event. | |
46 | * | |
47 | * @param array $params | |
77b97be7 EM |
48 | * @param array $ids |
49 | * | |
6a488035 TO |
50 | * @return array API Success Array |
51 | */ | |
52 | function civicrm_api3_mailing_create($params, $ids = array()) { | |
53 | return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params); | |
54 | } | |
55 | ||
11e09c59 | 56 | /** |
6a488035 | 57 | * Adjust Metadata for Create action |
11e09c59 TO |
58 | * |
59 | * The metadata is used for setting defaults, documentation & validation | |
60 | * @param array $params array or parameters determined by getfields | |
61 | */ | |
6a488035 TO |
62 | function _civicrm_api3_mailing_create_spec(&$params) { |
63 | $params['name']['api.required'] = 1; | |
64 | $params['subject']['api.required'] = 1; | |
65 | // should be able to default to 'user_contact_id' & have it work but it didn't work in test so | |
66 | // making required for simplicity | |
67 | $params['created_id']['api.required'] = 1; | |
68 | $params['api.mailing_job.create']['api.default'] = 1; | |
69 | } | |
70 | ||
0838af8e | 71 | |
72 | ||
73 | ||
74 | function civicrm_api3_mailing_stats($params) { | |
75 | civicrm_api3_verify_mandatory($params, | |
76 | 'CRM_Mailing_DAO_MailingJob', | |
77 | array('mailing_id'), | |
78 | FALSE | |
79 | ); | |
80 | $stats[$params['mailing_id']] = array(); | |
ef643544 | 81 | if (empty($params['job_id'])) { |
82 | $params['job_id'] = NULL; | |
83 | } | |
0838af8e | 84 | foreach (array('Delivered', 'Bounces', 'Unsubscribers', 'Unique Clicks', 'Opened') as $detail) { |
85 | switch ($detail) { | |
86 | case 'Delivered': | |
ef643544 | 87 | $stats[$params['mailing_id']] += array( |
88 | $detail => CRM_Mailing_Event_BAO_Delivered::getTotalCount($params['mailing_id'], $params['job_id']) | |
89 | ); | |
0838af8e | 90 | break; |
91 | case 'Bounces': | |
ef643544 | 92 | $stats[$params['mailing_id']] += array( |
93 | $detail => CRM_Mailing_Event_BAO_Bounce::getTotalCount($params['mailing_id'], $params['job_id']) | |
94 | ); | |
0838af8e | 95 | break; |
96 | case 'Unsubscribers': | |
ef643544 | 97 | $stats[$params['mailing_id']] += array( |
98 | $detail => CRM_Mailing_Event_BAO_Unsubscribe::getTotalCount($params['mailing_id'], $params['job_id']) | |
99 | ); | |
0838af8e | 100 | break; |
101 | case 'Unique Clicks': | |
ef643544 | 102 | $stats[$params['mailing_id']] += array( |
103 | $detail => CRM_Mailing_Event_BAO_TrackableURLOpen::getTotalCount($params['mailing_id'], $params['job_id']) | |
104 | ); | |
0838af8e | 105 | break; |
106 | case 'Opened': | |
ef643544 | 107 | $stats[$params['mailing_id']] += array( |
108 | $detail => CRM_Mailing_Event_BAO_Opened::getTotalCount($params['mailing_id'], $params['job_id']) | |
109 | ); | |
0838af8e | 110 | break; |
111 | } | |
112 | } | |
113 | return civicrm_api3_create_success($stats); | |
114 | } | |
115 | ||
116 | ||
ef643544 | 117 | |
118 | function civicrm_api3_mailing_a_b_recipients_update($params) { | |
119 | civicrm_api3_verify_mandatory($params, | |
120 | 'CRM_Mailing_DAO_MailingAB', | |
121 | array('id'), | |
122 | FALSE | |
123 | ); | |
124 | ||
125 | ||
126 | $mailingAB = civicrm_api3('MailingAB', 'get', $params); | |
127 | $mailingAB = $mailingAB['values'][$params['id']]; | |
128 | ||
129 | //update mailingC with include/exclude group id(s) provided | |
130 | civicrm_api3('Mailing', 'create', array('id' => $mailingAB['mailing_id_c'], 'groups' => $params['groups'])); | |
131 | //update recipients for mailing_id_c | |
132 | CRM_Mailing_BAO_Mailing::getRecipients($mailingAB['mailing_id_c'], $mailingAB['mailing_id_c'], NULL, NULL, TRUE); | |
133 | //calulate total number of random recipients for mail C from group_percentage selected | |
134 | $totalCount = civicrm_api3('MailingRecipients', 'getcount', array('mailing_id' => $mailingAB['mailing_id_c'])); | |
135 | $totalSelected = round(($totalCount * $mailingAB['group_percentage'])/100); | |
136 | foreach (array('mailing_id_a', 'mailing_id_b') as $columnName) { | |
137 | CRM_Mailing_BAO_Recipients::updateRandomRecipients($mailingAB['mailing_id_c'], $mailingAB[$columnName], $totalSelected); | |
138 | } | |
139 | return civicrm_api3_create_success(); | |
140 | } | |
141 | ||
142 | ||
143 | ||
e37b4b04 | 144 | /** |
eb1823ac | 145 | * Handle a delete event. |
e37b4b04 | 146 | * |
147 | * @param array $params | |
77b97be7 EM |
148 | * @param array $ids |
149 | * | |
e37b4b04 | 150 | * @return array API Success Array |
151 | */ | |
ef643544 | 152 | |
153 | function civicrm_api3_mailing_preview($params) { | |
154 | civicrm_api3_verify_mandatory($params, | |
155 | 'CRM_Mailing_DAO_Mailing', | |
156 | array('id'), | |
157 | FALSE | |
158 | ); | |
159 | $fromEmail = NULL; | |
160 | if (!empty($params['from_email'])) { | |
161 | $fromEmail = $params['from_email']; | |
162 | } | |
163 | $session = CRM_Core_Session::singleton(); | |
164 | $mailing = new CRM_Mailing_BAO_Mailing(); | |
165 | $mailing->id = $params['id']; | |
166 | $mailing->find(TRUE); | |
167 | CRM_Mailing_BAO_Mailing::tokenReplace($mailing); | |
168 | // get and format attachments | |
169 | $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_mailing', $mailing->id); | |
170 | $returnProperties = $mailing->getReturnProperties(); | |
171 | $contactID = CRM_Utils_Array::value('contact_id', $params); | |
172 | if (!$contactID) { | |
173 | $contactID = $session->get('userID'); | |
174 | } | |
175 | $mailingParams = array('contact_id' => $contactID); | |
176 | $details = CRM_Utils_Token::getTokenDetails($mailingParams, $returnProperties, TRUE, TRUE, NULL, $mailing->getFlattenedToken()); | |
177 | $mime = &$mailing->compose(NULL, NULL, NULL, $session->get('userID'), $fromEmail, $fromEmail, | |
178 | TRUE, $details[0][$contactID], $attachments | |
179 | ); | |
180 | return civicrm_api3_create_success(array('html' => $mime->getHTMLBody(), 'text' => $mime->getTXTBody())); | |
181 | } | |
182 | ||
183 | ||
e37b4b04 | 184 | function civicrm_api3_mailing_delete($params, $ids = array()) { |
185 | return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params); | |
186 | } | |
187 | ||
188 | ||
6a488035 TO |
189 | /** |
190 | * Handle a get event. | |
191 | * | |
192 | * @param array $params | |
193 | * @return array | |
194 | */ | |
e37b4b04 | 195 | function civicrm_api3_mailing_get($params) { |
6a488035 TO |
196 | return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
197 | } | |
198 | ||
199 | /** | |
200 | * Process a bounce event by passing through to the BAOs. | |
201 | * | |
202 | * @param array $params | |
203 | * | |
77b97be7 | 204 | * @throws API_Exception |
6a488035 TO |
205 | * @return array |
206 | */ | |
207 | function civicrm_api3_mailing_event_bounce($params) { | |
208 | ||
209 | $body = $params['body']; | |
210 | unset($params['body']); | |
211 | ||
212 | $params += CRM_Mailing_BAO_BouncePattern::match($body); | |
213 | ||
214 | if (CRM_Mailing_Event_BAO_Bounce::create($params)) { | |
215 | return civicrm_api3_create_success($params); | |
216 | } | |
217 | else { | |
218 | throw new API_Exception(ts('Queue event could not be found'),'no_queue_event | |
219 | '); | |
220 | } | |
221 | } | |
222 | ||
11e09c59 | 223 | /** |
6a488035 | 224 | * Adjust Metadata for bounce_spec action |
11e09c59 TO |
225 | * |
226 | * The metadata is used for setting defaults, documentation & validation | |
227 | * @param array $params array or parameters determined by getfields | |
228 | */ | |
6a488035 TO |
229 | function _civicrm_api3_mailing_event_bounce_spec(&$params) { |
230 | $params['job_id']['api.required'] = 1; | |
231 | $params['event_queue_id']['api.required'] = 1; | |
232 | $params['hash']['api.required'] = 1; | |
233 | $params['body']['api.required'] = 1; | |
234 | } | |
235 | ||
236 | /** | |
237 | * Handle a confirm event | |
238 | * @deprecated | |
239 | * | |
240 | * @param array $params | |
241 | * | |
242 | * @return array | |
243 | */ | |
244 | function civicrm_api3_mailing_event_confirm($params) { | |
245 | return civicrm_api('mailing_event_confirm', 'create', $params); | |
246 | } | |
247 | ||
248 | /** | |
249 | * Handle a reply event | |
250 | * | |
251 | * @param array $params | |
252 | * | |
253 | * @return array | |
254 | */ | |
255 | function civicrm_api3_mailing_event_reply($params) { | |
256 | $job = $params['job_id']; | |
257 | $queue = $params['event_queue_id']; | |
258 | $hash = $params['hash']; | |
259 | $replyto = $params['replyTo']; | |
260 | $bodyTxt = CRM_Utils_Array::value('bodyTxt', $params); | |
261 | $bodyHTML = CRM_Utils_Array::value('bodyHTML', $params); | |
262 | $fullEmail = CRM_Utils_Array::value('fullEmail', $params); | |
263 | ||
264 | $mailing = CRM_Mailing_Event_BAO_Reply::reply($job, $queue, $hash, $replyto); | |
265 | ||
266 | if (empty($mailing)) { | |
267 | return civicrm_api3_create_error('Queue event could not be found'); | |
268 | } | |
269 | ||
270 | CRM_Mailing_Event_BAO_Reply::send($queue, $mailing, $bodyTxt, $replyto, $bodyHTML, $fullEmail); | |
271 | ||
272 | return civicrm_api3_create_success($params); | |
273 | } | |
274 | ||
11e09c59 | 275 | /** |
6a488035 | 276 | * Adjust Metadata for event_reply action |
11e09c59 TO |
277 | * |
278 | * The metadata is used for setting defaults, documentation & validation | |
279 | * @param array $params array or parameters determined by getfields | |
280 | */ | |
6a488035 TO |
281 | function _civicrm_api3_mailing_event_reply_spec(&$params) { |
282 | $params['job_id']['api.required'] = 1; | |
283 | $params['event_queue_id']['api.required'] = 1; | |
284 | $params['hash']['api.required'] = 1; | |
fb18363b | 285 | $params['replyTo']['api.required'] = 0; |
6a488035 TO |
286 | } |
287 | ||
288 | /** | |
289 | * Handle a forward event | |
290 | * | |
291 | * @param array $params | |
292 | * | |
293 | * @return array | |
294 | */ | |
295 | function civicrm_api3_mailing_event_forward($params) { | |
296 | $job = $params['job_id']; | |
297 | $queue = $params['event_queue_id']; | |
298 | $hash = $params['hash']; | |
299 | $email = $params['email']; | |
300 | $fromEmail = CRM_Utils_Array::value('fromEmail', $params); | |
301 | $params = CRM_Utils_Array::value('params', $params); | |
302 | ||
303 | $forward = CRM_Mailing_Event_BAO_Forward::forward($job, $queue, $hash, $email, $fromEmail, $params); | |
304 | ||
305 | if ($forward) { | |
306 | return civicrm_api3_create_success($params); | |
307 | } | |
308 | ||
309 | return civicrm_api3_create_error('Queue event could not be found'); | |
310 | } | |
311 | ||
11e09c59 | 312 | /** |
6a488035 | 313 | * Adjust Metadata for event_forward action |
11e09c59 TO |
314 | * |
315 | * The metadata is used for setting defaults, documentation & validation | |
316 | * @param array $params array or parameters determined by getfields | |
317 | */ | |
6a488035 TO |
318 | function _civicrm_api3_mailing_event_forward_spec(&$params) { |
319 | $params['job_id']['api.required'] = 1; | |
320 | $params['event_queue_id']['api.required'] = 1; | |
321 | $params['hash']['api.required'] = 1; | |
322 | $params['email']['api.required'] = 1; | |
323 | } | |
324 | ||
325 | /** | |
326 | * Handle a click event | |
327 | * | |
328 | * @param array $params | |
329 | * | |
330 | * @return array | |
331 | */ | |
332 | function civicrm_api3_mailing_event_click($params) { | |
333 | ||
334 | civicrm_api3_verify_mandatory($params, | |
335 | 'CRM_Mailing_Event_DAO_TrackableURLOpen', | |
336 | array('event_queue_id', 'url_id'), | |
337 | FALSE | |
338 | ); | |
339 | ||
340 | $url_id = $params['url_id']; | |
341 | $queue = $params['event_queue_id']; | |
342 | ||
343 | $url = CRM_Mailing_Event_BAO_TrackableURLOpen::track($queue, $url_id); | |
344 | ||
345 | $values = array(); | |
346 | $values['url'] = $url; | |
347 | $values['is_error'] = 0; | |
348 | ||
349 | return civicrm_api3_create_success($values); | |
350 | } | |
351 | ||
352 | /** | |
353 | * Handle an open event | |
354 | * | |
355 | * @param array $params | |
356 | * | |
357 | * @return array | |
358 | */ | |
359 | function civicrm_api3_mailing_event_open($params) { | |
360 | ||
361 | civicrm_api3_verify_mandatory($params, | |
362 | 'CRM_Mailing_Event_DAO_Opened', | |
363 | array('event_queue_id'), | |
364 | FALSE | |
365 | ); | |
366 | ||
367 | $queue = $params['event_queue_id']; | |
368 | $success = CRM_Mailing_Event_BAO_Opened::open($queue); | |
369 | ||
370 | if (!$success) { | |
371 | return civicrm_api3_create_error('mailing open event failed'); | |
372 | } | |
373 | ||
374 | return civicrm_api3_create_success($params); | |
375 | } | |
376 | ||
377 | /** | |
378 | * Fix the reset dates on the email record based on when a mail was last delivered | |
379 | * We only consider mailings that were completed and finished in the last 3 to 7 days | |
380 | * Both the min and max days can be set via the params | |
381 | */ | |
382 | function civicrm_api3_mailing_update_email_resetdate($params) { | |
383 | CRM_Mailing_Event_BAO_Delivered::updateEmailResetDate( | |
384 | CRM_Utils_Array::value('minDays', $params, 3), | |
385 | CRM_Utils_Array::value('maxDays', $params, 3) | |
386 | ); | |
387 | return civicrm_api3_create_success(); | |
388 | } | |
389 | ||
390 |