Merge remote-tracking branch 'upstream/4.4' into 4.4-4.5-2014-10-01-19-08-00
[civicrm-core.git] / api / v3 / MailingContact.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * File for the CiviCRM APIv3 contact and mailing functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_MailingContact
33 *
34 * @copyright CiviCRM LLC (c) 2004-2014
35 * @version $Id$
36 *
37 */
38
39 /**
40 * Get all the mailings and details that a contact was involved with
41 *
42 * @param array $params input parameters
43 * - key: contact_id, value: int - required
44 * - key: type, value: Delivered | Bounced - optional, defaults to Delivered
45 * - Future extensions will include: Opened, Clicked, Forwarded
46 *
47 * @return array API result
48 * @static void
49 * @access public
50 * @example CRM/Mailing/BAO/Mailing.php
51 *
52 */
53 function civicrm_api3_mailing_contact_get($params) {
54 return civicrm_api3_create_success(_civicrm_api3_mailing_contact_getresults($params, FALSE));
55 }
56 /**
57 * This is a wrapper for the functions that return the results from the 'quasi-entity'
58 * mailing contact
59 * @param array $params
60 * @param Boolean $count
61 * @throws Exception
62 */
63 function _civicrm_api3_mailing_contact_getresults($params, $count){
64 if(empty($params['type'])){
65 //ie. because the api is an anomoly & passing in id is not valid
66 throw new Exception('This api call does not accept api as a parameter');
67 }
68 $options = _civicrm_api3_get_options_from_params($params, TRUE,'contribution','get');
69 $fnName = '_civicrm_api3_mailing_contact_get_' . strtolower($params['type']);
70 return $fnName(
71 $params['contact_id'],
72 $options['offset'],
73 $options['limit'],
74 $options['sort'],
75 $count
76 );
77 }
78 /**
79 * Adjust Metadata for Get action
80 *
81 * @param array $params array or parameters determined by getfields
82 */
83 function _civicrm_api3_mailing_contact_get_spec(&$params) {
84 $params['contact_id']['api.required'] = 1;
85 $params['contact_id']['title'] = 'Contact ID';
86 $params['type'] = array(
87 'api.default' => 'Delivered',
88 'title' => 'Type',// doesn't really explain the field - but not sure I understand it to explain it better
89 'type' => CRM_Utils_Type::T_STRING,
90 'options' => array(
91 'Delivered' => 'Delivered',
92 'Bounced' => 'Bounced',
93 )
94 );
95 }
96
97 /**
98 * @param $type
99 * @param $contactID
100 * @param $offset
101 * @param $limit
102 * @param $selectFields
103 * @param $fromClause
104 * @param $whereClause
105 * @param $sort
106 * @param $getCount
107 *
108 * @return array
109 */
110 function _civicrm_api3_mailing_contact_query(
111 $type,
112 $contactID,
113 $offset,
114 $limit,
115 $selectFields,
116 $fromClause,
117 $whereClause,
118 $sort,
119 $getCount
120 ) {
121
122 if ($getCount) {
123 $sql = "
124 SELECT count(*)
125 FROM civicrm_mailing m
126 INNER JOIN civicrm_contact c ON m.created_id = c.id
127 INNER JOIN civicrm_mailing_job j ON j.mailing_id = m.id
128 INNER JOIN civicrm_mailing_event_queue meq ON meq.job_id = j.id
129 $fromClause
130 WHERE j.is_test = 0
131 AND meq.contact_id = %1
132 $whereClause
133 GROUP BY m.id
134 ";
135
136 $qParams = array(
137 1 => array($contactID, 'Integer')
138 );
139 $dao = CRM_Core_DAO::executeQuery($sql, $qParams);
140
141 $params = array(
142 'type' => $type,
143 'contact_id' => $contactID
144 );
145
146 $results = $dao->N;
147 }
148 else {
149 $defaultFields = array(
150 'm.id' => 'mailing_id',
151 'm.subject' => 'subject',
152 'c.id' => 'creator_id',
153 'c.sort_name' => 'creator_name',
154 );
155
156 if ($selectFields) {
157 $fields = array_merge($selectFields, $defaultFields);
158 }
159 else {
160 $fields = $defaultFields;
161 }
162
163 $select = array();
164 foreach ($fields as $n => $l) {
165 $select[] = "$n as $l";
166 }
167 $select = implode(', ', $select);
168
169 $orderBy = 'ORDER BY j.start_date DESC';
170 if ($sort) {
171 $orderBy = "ORDER BY $sort";
172 }
173
174 $sql = "
175 SELECT $select
176 FROM civicrm_mailing m
177 INNER JOIN civicrm_contact c ON m.created_id = c.id
178 INNER JOIN civicrm_mailing_job j ON j.mailing_id = m.id
179 INNER JOIN civicrm_mailing_event_queue meq ON meq.job_id = j.id
180 $fromClause
181 WHERE j.is_test = 0
182 AND meq.contact_id = %1
183 $whereClause
184 GROUP BY m.id
185 {$orderBy}
186 ";
187
188 if ($limit > 0) {
189 $sql .= "
190 LIMIT %2, %3
191 ";
192 }
193
194 $qParams = array(
195 1 => array($contactID, 'Integer'),
196 2 => array($offset, 'Integer'),
197 3 => array($limit, 'Integer')
198 );
199 $dao = CRM_Core_DAO::executeQuery($sql, $qParams);
200
201 $results = array();
202 while ($dao->fetch()) {
203 foreach ($fields as $n => $l) {
204 $results[$dao->mailing_id][$l] = $dao->$l;
205 }
206 }
207 }
208
209 return $results;
210 }
211
212 /**
213 * @param $contactID
214 * @param $offset
215 * @param $limit
216 * @param $sort
217 * @param $getCount
218 *
219 * @return array
220 */
221 function _civicrm_api3_mailing_contact_get_delivered(
222 $contactID,
223 $offset,
224 $limit,
225 $sort,
226 $getCount
227 ) {
228 $selectFields = array('med.time_stamp' => 'start_date');
229
230 $fromClause = "
231 INNER JOIN civicrm_mailing_event_delivered med ON med.event_queue_id = meq.id
232 LEFT JOIN civicrm_mailing_event_bounce meb ON meb.event_queue_id = meq.id
233 ";
234
235 $whereClause = "
236 AND meb.id IS NULL
237 ";
238
239 return _civicrm_api3_mailing_contact_query(
240 'Delivered',
241 $contactID,
242 $offset,
243 $limit,
244 $selectFields,
245 $fromClause,
246 $whereClause,
247 $sort,
248 $getCount
249 );
250 }
251
252 /**
253 * @param $contactID
254 * @param $offset
255 * @param $limit
256 * @param $sort
257 * @param $getCount
258 *
259 * @return array
260 */
261 function _civicrm_api3_mailing_contact_get_bounced(
262 $contactID,
263 $offset,
264 $limit,
265 $sort,
266 $getCount
267 ) {
268 $fromClause = "
269 INNER JOIN civicrm_mailing_event_bounce meb ON meb.event_queue_id = meq.id
270 ";
271
272 return _civicrm_api3_mailing_contact_query(
273 'Bounced',
274 $contactID,
275 $offset,
276 $limit,
277 NULL,
278 $fromClause,
279 NULL,
280 $sort,
281 $getCount
282 );
283 }
284
285 /**
286 * Get count of all the mailings that a contact was involved with
287 *
288 * @param array $params input parameters
289 * - key: contact_id, value: int - required
290 * - key: type, value: Delivered | Bounced - optional, defaults to Delivered
291 * - Future extensions will include: Opened, Clicked, Forwarded
292 *
293 * @return array API result
294 * @static void
295 * @access public
296 * @example CRM/Mailing/BAO/Mailing.php
297 *
298 */
299 function civicrm_api3_mailing_contact_getcount($params) {
300 return _civicrm_api3_mailing_contact_getresults($params, TRUE);
301 }