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