Merge pull request #3679 from yashodha/CRM-14951
[civicrm-core.git] / api / v3 / Mailing.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
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
35 * @copyright CiviCRM LLC (c) 2004-2014
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
48 * @param array $ids
49 *
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
56 /**
57 * Adjust Metadata for Create action
58 *
59 * The metadata is used for setting defaults, documentation & validation
60 * @param array $params array or parameters determined by getfields
61 */
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
71 /**
72 * Handle a delete event.
73 *
74 * @param array $params
75 * @param array $ids
76 *
77 * @return array API Success Array
78 */
79 function civicrm_api3_mailing_delete($params, $ids = array()) {
80 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
81 }
82
83
84 /**
85 * Handle a get event.
86 *
87 * @param array $params
88 * @return array
89 */
90 function civicrm_api3_mailing_get($params) {
91 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
92 }
93
94 /**
95 * Process a bounce event by passing through to the BAOs.
96 *
97 * @param array $params
98 *
99 * @throws API_Exception
100 * @return array
101 */
102 function civicrm_api3_mailing_event_bounce($params) {
103
104 $body = $params['body'];
105 unset($params['body']);
106
107 $params += CRM_Mailing_BAO_BouncePattern::match($body);
108
109 if (CRM_Mailing_Event_BAO_Bounce::create($params)) {
110 return civicrm_api3_create_success($params);
111 }
112 else {
113 throw new API_Exception(ts('Queue event could not be found'),'no_queue_event
114 ');
115 }
116 }
117
118 /**
119 * Adjust Metadata for bounce_spec action
120 *
121 * The metadata is used for setting defaults, documentation & validation
122 * @param array $params array or parameters determined by getfields
123 */
124 function _civicrm_api3_mailing_event_bounce_spec(&$params) {
125 $params['job_id']['api.required'] = 1;
126 $params['event_queue_id']['api.required'] = 1;
127 $params['hash']['api.required'] = 1;
128 $params['body']['api.required'] = 1;
129 }
130
131 /**
132 * Handle a confirm event
133 * @deprecated
134 *
135 * @param array $params
136 *
137 * @return array
138 */
139 function civicrm_api3_mailing_event_confirm($params) {
140 return civicrm_api('mailing_event_confirm', 'create', $params);
141 }
142
143 /**
144 * Handle a reply event
145 *
146 * @param array $params
147 *
148 * @return array
149 */
150 function civicrm_api3_mailing_event_reply($params) {
151 $job = $params['job_id'];
152 $queue = $params['event_queue_id'];
153 $hash = $params['hash'];
154 $replyto = $params['replyTo'];
155 $bodyTxt = CRM_Utils_Array::value('bodyTxt', $params);
156 $bodyHTML = CRM_Utils_Array::value('bodyHTML', $params);
157 $fullEmail = CRM_Utils_Array::value('fullEmail', $params);
158
159 $mailing = CRM_Mailing_Event_BAO_Reply::reply($job, $queue, $hash, $replyto);
160
161 if (empty($mailing)) {
162 return civicrm_api3_create_error('Queue event could not be found');
163 }
164
165 CRM_Mailing_Event_BAO_Reply::send($queue, $mailing, $bodyTxt, $replyto, $bodyHTML, $fullEmail);
166
167 return civicrm_api3_create_success($params);
168 }
169
170 /**
171 * Adjust Metadata for event_reply action
172 *
173 * The metadata is used for setting defaults, documentation & validation
174 * @param array $params array or parameters determined by getfields
175 */
176 function _civicrm_api3_mailing_event_reply_spec(&$params) {
177 $params['job_id']['api.required'] = 1;
178 $params['event_queue_id']['api.required'] = 1;
179 $params['hash']['api.required'] = 1;
180 $params['replyTo']['api.required'] = 0;
181 }
182
183 /**
184 * Handle a forward event
185 *
186 * @param array $params
187 *
188 * @return array
189 */
190 function civicrm_api3_mailing_event_forward($params) {
191 $job = $params['job_id'];
192 $queue = $params['event_queue_id'];
193 $hash = $params['hash'];
194 $email = $params['email'];
195 $fromEmail = CRM_Utils_Array::value('fromEmail', $params);
196 $params = CRM_Utils_Array::value('params', $params);
197
198 $forward = CRM_Mailing_Event_BAO_Forward::forward($job, $queue, $hash, $email, $fromEmail, $params);
199
200 if ($forward) {
201 return civicrm_api3_create_success($params);
202 }
203
204 return civicrm_api3_create_error('Queue event could not be found');
205 }
206
207 /**
208 * Adjust Metadata for event_forward action
209 *
210 * The metadata is used for setting defaults, documentation & validation
211 * @param array $params array or parameters determined by getfields
212 */
213 function _civicrm_api3_mailing_event_forward_spec(&$params) {
214 $params['job_id']['api.required'] = 1;
215 $params['event_queue_id']['api.required'] = 1;
216 $params['hash']['api.required'] = 1;
217 $params['email']['api.required'] = 1;
218 }
219
220 /**
221 * Handle a click event
222 *
223 * @param array $params
224 *
225 * @return array
226 */
227 function civicrm_api3_mailing_event_click($params) {
228
229 civicrm_api3_verify_mandatory($params,
230 'CRM_Mailing_Event_DAO_TrackableURLOpen',
231 array('event_queue_id', 'url_id'),
232 FALSE
233 );
234
235 $url_id = $params['url_id'];
236 $queue = $params['event_queue_id'];
237
238 $url = CRM_Mailing_Event_BAO_TrackableURLOpen::track($queue, $url_id);
239
240 $values = array();
241 $values['url'] = $url;
242 $values['is_error'] = 0;
243
244 return civicrm_api3_create_success($values);
245 }
246
247 /**
248 * Handle an open event
249 *
250 * @param array $params
251 *
252 * @return array
253 */
254 function civicrm_api3_mailing_event_open($params) {
255
256 civicrm_api3_verify_mandatory($params,
257 'CRM_Mailing_Event_DAO_Opened',
258 array('event_queue_id'),
259 FALSE
260 );
261
262 $queue = $params['event_queue_id'];
263 $success = CRM_Mailing_Event_BAO_Opened::open($queue);
264
265 if (!$success) {
266 return civicrm_api3_create_error('mailing open event failed');
267 }
268
269 return civicrm_api3_create_success($params);
270 }
271
272 /**
273 * Fix the reset dates on the email record based on when a mail was last delivered
274 * We only consider mailings that were completed and finished in the last 3 to 7 days
275 * Both the min and max days can be set via the params
276 */
277 function civicrm_api3_mailing_update_email_resetdate($params) {
278 CRM_Mailing_Event_BAO_Delivered::updateEmailResetDate(
279 CRM_Utils_Array::value('minDays', $params, 3),
280 CRM_Utils_Array::value('maxDays', $params, 3)
281 );
282 return civicrm_api3_create_success();
283 }
284
285