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