Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2014-12-16-01-08-03
[civicrm-core.git] / api / v3 / Mailing.php
CommitLineData
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 */
52function 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
62function _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;
1fdb479f 69 $params['api.mailing_job.create']['title'] = 'Schedule Mailing?';
6a488035
TO
70}
71
e37b4b04 72/**
eb1823ac 73 * Handle a delete event.
e37b4b04 74 *
75 * @param array $params
77b97be7
EM
76 * @param array $ids
77 *
e37b4b04 78 * @return array API Success Array
79 */
80function civicrm_api3_mailing_delete($params, $ids = array()) {
81 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
82}
83
84
6a488035
TO
85/**
86 * Handle a get event.
87 *
88 * @param array $params
89 * @return array
90 */
e37b4b04 91function civicrm_api3_mailing_get($params) {
6a488035
TO
92 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
93}
94
95/**
96 * Process a bounce event by passing through to the BAOs.
97 *
98 * @param array $params
99 *
77b97be7 100 * @throws API_Exception
6a488035
TO
101 * @return array
102 */
103function civicrm_api3_mailing_event_bounce($params) {
104
105 $body = $params['body'];
106 unset($params['body']);
107
108 $params += CRM_Mailing_BAO_BouncePattern::match($body);
109
110 if (CRM_Mailing_Event_BAO_Bounce::create($params)) {
111 return civicrm_api3_create_success($params);
112 }
113 else {
114 throw new API_Exception(ts('Queue event could not be found'),'no_queue_event
115 ');
116 }
117}
118
11e09c59 119/**
6a488035 120 * Adjust Metadata for bounce_spec action
11e09c59
TO
121 *
122 * The metadata is used for setting defaults, documentation & validation
123 * @param array $params array or parameters determined by getfields
124 */
6a488035
TO
125function _civicrm_api3_mailing_event_bounce_spec(&$params) {
126 $params['job_id']['api.required'] = 1;
4c41ecb2 127 $params['job_id']['title'] = 'Job ID';
6a488035 128 $params['event_queue_id']['api.required'] = 1;
4c41ecb2 129 $params['event_queue_id']['title'] = 'Event Queue ID';
6a488035 130 $params['hash']['api.required'] = 1;
4c41ecb2 131 $params['hash']['title'] = 'Hash';
6a488035 132 $params['body']['api.required'] = 1;
4c41ecb2 133 $params['body']['title'] = 'Body';
6a488035
TO
134}
135
136/**
137 * Handle a confirm event
138 * @deprecated
139 *
140 * @param array $params
141 *
142 * @return array
143 */
144function civicrm_api3_mailing_event_confirm($params) {
145 return civicrm_api('mailing_event_confirm', 'create', $params);
146}
147
a14e9d08
CW
148/**
149 * @deprecated api notice
150 * @return array of deprecated actions
151 */
152function _civicrm_api3_mailing_deprecation() {
153 return array('event_confirm' => 'Mailing api "event_confirm" action is deprecated. Use the mailing_event_confirm api instead.');
154}
155
6a488035
TO
156/**
157 * Handle a reply event
158 *
159 * @param array $params
160 *
161 * @return array
162 */
163function civicrm_api3_mailing_event_reply($params) {
164 $job = $params['job_id'];
165 $queue = $params['event_queue_id'];
166 $hash = $params['hash'];
167 $replyto = $params['replyTo'];
168 $bodyTxt = CRM_Utils_Array::value('bodyTxt', $params);
169 $bodyHTML = CRM_Utils_Array::value('bodyHTML', $params);
170 $fullEmail = CRM_Utils_Array::value('fullEmail', $params);
171
172 $mailing = CRM_Mailing_Event_BAO_Reply::reply($job, $queue, $hash, $replyto);
173
174 if (empty($mailing)) {
175 return civicrm_api3_create_error('Queue event could not be found');
176 }
177
178 CRM_Mailing_Event_BAO_Reply::send($queue, $mailing, $bodyTxt, $replyto, $bodyHTML, $fullEmail);
179
180 return civicrm_api3_create_success($params);
181}
182
11e09c59 183/**
6a488035 184 * Adjust Metadata for event_reply action
11e09c59
TO
185 *
186 * The metadata is used for setting defaults, documentation & validation
187 * @param array $params array or parameters determined by getfields
188 */
6a488035
TO
189function _civicrm_api3_mailing_event_reply_spec(&$params) {
190 $params['job_id']['api.required'] = 1;
4c41ecb2 191 $params['job_id']['title'] = 'Job ID';
6a488035 192 $params['event_queue_id']['api.required'] = 1;
4c41ecb2 193 $params['event_queue_id']['title'] = 'Event Queue ID';
6a488035 194 $params['hash']['api.required'] = 1;
4c41ecb2 195 $params['hash']['title'] = 'Hash';
fb18363b 196 $params['replyTo']['api.required'] = 0;
4c41ecb2 197 $params['replyTo']['title'] = 'Reply To';//doesn't really explain adequately
6a488035
TO
198}
199
200/**
201 * Handle a forward event
202 *
203 * @param array $params
204 *
205 * @return array
206 */
207function civicrm_api3_mailing_event_forward($params) {
208 $job = $params['job_id'];
209 $queue = $params['event_queue_id'];
210 $hash = $params['hash'];
211 $email = $params['email'];
212 $fromEmail = CRM_Utils_Array::value('fromEmail', $params);
213 $params = CRM_Utils_Array::value('params', $params);
214
215 $forward = CRM_Mailing_Event_BAO_Forward::forward($job, $queue, $hash, $email, $fromEmail, $params);
216
217 if ($forward) {
218 return civicrm_api3_create_success($params);
219 }
220
221 return civicrm_api3_create_error('Queue event could not be found');
222}
223
11e09c59 224/**
6a488035 225 * Adjust Metadata for event_forward action
11e09c59
TO
226 *
227 * The metadata is used for setting defaults, documentation & validation
228 * @param array $params array or parameters determined by getfields
229 */
6a488035
TO
230function _civicrm_api3_mailing_event_forward_spec(&$params) {
231 $params['job_id']['api.required'] = 1;
4c41ecb2 232 $params['job_id']['title'] = 'Job ID';
6a488035 233 $params['event_queue_id']['api.required'] = 1;
4c41ecb2 234 $params['event_queue_id']['title'] = 'Event Queue ID';
6a488035 235 $params['hash']['api.required'] = 1;
4c41ecb2 236 $params['hash']['title'] = 'Hash';
6a488035 237 $params['email']['api.required'] = 1;
4c41ecb2 238 $params['email']['title'] = 'Forwarded to Email';
6a488035
TO
239}
240
241/**
242 * Handle a click event
243 *
244 * @param array $params
245 *
246 * @return array
247 */
248function civicrm_api3_mailing_event_click($params) {
249
250 civicrm_api3_verify_mandatory($params,
251 'CRM_Mailing_Event_DAO_TrackableURLOpen',
252 array('event_queue_id', 'url_id'),
253 FALSE
254 );
255
256 $url_id = $params['url_id'];
257 $queue = $params['event_queue_id'];
258
259 $url = CRM_Mailing_Event_BAO_TrackableURLOpen::track($queue, $url_id);
260
261 $values = array();
262 $values['url'] = $url;
263 $values['is_error'] = 0;
264
265 return civicrm_api3_create_success($values);
266}
267
268/**
269 * Handle an open event
270 *
271 * @param array $params
272 *
273 * @return array
274 */
275function civicrm_api3_mailing_event_open($params) {
276
277 civicrm_api3_verify_mandatory($params,
278 'CRM_Mailing_Event_DAO_Opened',
279 array('event_queue_id'),
280 FALSE
281 );
282
283 $queue = $params['event_queue_id'];
284 $success = CRM_Mailing_Event_BAO_Opened::open($queue);
285
286 if (!$success) {
287 return civicrm_api3_create_error('mailing open event failed');
288 }
289
290 return civicrm_api3_create_success($params);
291}
292
293/**
294 * Fix the reset dates on the email record based on when a mail was last delivered
295 * We only consider mailings that were completed and finished in the last 3 to 7 days
296 * Both the min and max days can be set via the params
297 */
298function civicrm_api3_mailing_update_email_resetdate($params) {
299 CRM_Mailing_Event_BAO_Delivered::updateEmailResetDate(
300 CRM_Utils_Array::value('minDays', $params, 3),
301 CRM_Utils_Array::value('maxDays', $params, 3)
302 );
303 return civicrm_api3_create_success();
304}
305
306