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