Merge pull request #4899 from colemanw/INFRA-132
[civicrm-core.git] / api / v3 / MailingContact.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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
43 * Input parameters.
44 * - key: contact_id, value: int - required
45 * - key: type, value: Delivered | Bounced - optional, defaults to Delivered
46 * - Future extensions will include: Opened, Clicked, Forwarded
47 *
48 * @return array
49 * API result
50 * @static void
51 * @access public
52 * @example CRM/Mailing/BAO/Mailing.php
53 *
54 */
55 function civicrm_api3_mailing_contact_get($params) {
56 return civicrm_api3_create_success(_civicrm_api3_mailing_contact_getresults($params, FALSE));
57 }
58 /**
59 * This is a wrapper for the functions that return the results from the 'quasi-entity'
60 * mailing contact
61 * @param array $params
62 * @param bool $count
63 * @throws Exception
64 */
65 function _civicrm_api3_mailing_contact_getresults($params, $count) {
66 if (empty($params['type'])) {
67 //ie. because the api is an anomoly & passing in id is not valid
68 throw new Exception('This api call does not accept api as a parameter');
69 }
70 $options = _civicrm_api3_get_options_from_params($params, TRUE,'contribution','get');
71 $fnName = '_civicrm_api3_mailing_contact_get_' . strtolower($params['type']);
72 return $fnName(
73 $params['contact_id'],
74 $options['offset'],
75 $options['limit'],
76 $options['sort'],
77 $count
78 );
79 }
80 /**
81 * Adjust Metadata for Get action
82 *
83 * @param array $params
84 * Array or parameters determined by getfields.
85 */
86 function _civicrm_api3_mailing_contact_get_spec(&$params) {
87 $params['contact_id']['api.required'] = 1;
88 $params['contact_id']['title'] = 'Contact ID';
89 $params['type'] = array(
90 'api.default' => 'Delivered',
91 'title' => 'Type',// doesn't really explain the field - but not sure I understand it to explain it better
92 'type' => CRM_Utils_Type::T_STRING,
93 'options' => array(
94 'Delivered' => 'Delivered',
95 'Bounced' => 'Bounced',
96 ),
97 );
98 }
99
100 /**
101 * @param $type
102 * @param int $contactID
103 * @param $offset
104 * @param $limit
105 * @param $selectFields
106 * @param $fromClause
107 * @param $whereClause
108 * @param $sort
109 * @param $getCount
110 *
111 * @return array
112 */
113 function _civicrm_api3_mailing_contact_query(
114 $type,
115 $contactID,
116 $offset,
117 $limit,
118 $selectFields,
119 $fromClause,
120 $whereClause,
121 $sort,
122 $getCount
123 ) {
124
125 if ($getCount) {
126 $sql = "
127 SELECT count(*)
128 FROM civicrm_mailing m
129 INNER JOIN civicrm_contact c ON m.created_id = c.id
130 INNER JOIN civicrm_mailing_job j ON j.mailing_id = m.id
131 INNER JOIN civicrm_mailing_event_queue meq ON meq.job_id = j.id
132 $fromClause
133 WHERE j.is_test = 0
134 AND meq.contact_id = %1
135 $whereClause
136 GROUP BY m.id
137 ";
138
139 $qParams = array(
140 1 => array($contactID, 'Integer'),
141 );
142 $dao = CRM_Core_DAO::executeQuery($sql, $qParams);
143
144 $params = array(
145 'type' => $type,
146 'contact_id' => $contactID,
147 );
148
149 $results = $dao->N;
150 }
151 else {
152 $defaultFields = array(
153 'm.id' => 'mailing_id',
154 'm.subject' => 'subject',
155 'c.id' => 'creator_id',
156 'c.sort_name' => 'creator_name',
157 );
158
159 if ($selectFields) {
160 $fields = array_merge($selectFields, $defaultFields);
161 }
162 else {
163 $fields = $defaultFields;
164 }
165
166 $select = array();
167 foreach ($fields as $n => $l) {
168 $select[] = "$n as $l";
169 }
170 $select = implode(', ', $select);
171
172 $orderBy = 'ORDER BY j.start_date DESC';
173 if ($sort) {
174 $orderBy = "ORDER BY $sort";
175 }
176
177 $sql = "
178 SELECT $select
179 FROM civicrm_mailing m
180 INNER JOIN civicrm_contact c ON m.created_id = c.id
181 INNER JOIN civicrm_mailing_job j ON j.mailing_id = m.id
182 INNER JOIN civicrm_mailing_event_queue meq ON meq.job_id = j.id
183 $fromClause
184 WHERE j.is_test = 0
185 AND meq.contact_id = %1
186 $whereClause
187 GROUP BY m.id
188 {$orderBy}
189 ";
190
191 if ($limit > 0) {
192 $sql .= "
193 LIMIT %2, %3
194 ";
195 }
196
197 $qParams = array(
198 1 => array($contactID, 'Integer'),
199 2 => array($offset, 'Integer'),
200 3 => array($limit, 'Integer'),
201 );
202 $dao = CRM_Core_DAO::executeQuery($sql, $qParams);
203
204 $results = array();
205 while ($dao->fetch()) {
206 foreach ($fields as $n => $l) {
207 $results[$dao->mailing_id][$l] = $dao->$l;
208 }
209 }
210 }
211
212 return $results;
213 }
214
215 /**
216 * @param int $contactID
217 * @param $offset
218 * @param $limit
219 * @param $sort
220 * @param $getCount
221 *
222 * @return array
223 */
224 function _civicrm_api3_mailing_contact_get_delivered(
225 $contactID,
226 $offset,
227 $limit,
228 $sort,
229 $getCount
230 ) {
231 $selectFields = array('med.time_stamp' => 'start_date');
232
233 $fromClause = "
234 INNER JOIN civicrm_mailing_event_delivered med ON med.event_queue_id = meq.id
235 LEFT JOIN civicrm_mailing_event_bounce meb ON meb.event_queue_id = meq.id
236 ";
237
238 $whereClause = "
239 AND meb.id IS NULL
240 ";
241
242 return _civicrm_api3_mailing_contact_query(
243 'Delivered',
244 $contactID,
245 $offset,
246 $limit,
247 $selectFields,
248 $fromClause,
249 $whereClause,
250 $sort,
251 $getCount
252 );
253 }
254
255 /**
256 * @param int $contactID
257 * @param $offset
258 * @param $limit
259 * @param $sort
260 * @param $getCount
261 *
262 * @return array
263 */
264 function _civicrm_api3_mailing_contact_get_bounced(
265 $contactID,
266 $offset,
267 $limit,
268 $sort,
269 $getCount
270 ) {
271 $fromClause = "
272 INNER JOIN civicrm_mailing_event_bounce meb ON meb.event_queue_id = meq.id
273 ";
274
275 return _civicrm_api3_mailing_contact_query(
276 'Bounced',
277 $contactID,
278 $offset,
279 $limit,
280 NULL,
281 $fromClause,
282 NULL,
283 $sort,
284 $getCount
285 );
286 }
287
288 /**
289 * Get count of all the mailings that a contact was involved with
290 *
291 * @param array $params
292 * Input parameters.
293 * - key: contact_id, value: int - required
294 * - key: type, value: Delivered | Bounced - optional, defaults to Delivered
295 * - Future extensions will include: Opened, Clicked, Forwarded
296 *
297 * @return array
298 * API result
299 * @static void
300 * @access public
301 * @example CRM/Mailing/BAO/Mailing.php
302 *
303 */
304 function civicrm_api3_mailing_contact_getcount($params) {
305 return _civicrm_api3_mailing_contact_getresults($params, TRUE);
306 }