Merge pull request #3679 from yashodha/CRM-14951
[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['type'] = array(
86 'api.default' => 'Delivered',
87 'type' => CRM_Utils_Type::T_STRING,
88 'options' => array(
89 'Delivered' => 'Delivered',
90 'Bounced' => 'Bounced',
91 )
92 );
93 }
94
95 /**
96 * @param $type
97 * @param $contactID
98 * @param $offset
99 * @param $limit
100 * @param $selectFields
101 * @param $fromClause
102 * @param $whereClause
103 * @param $sort
104 * @param $getCount
105 *
106 * @return array
107 */
108 function _civicrm_api3_mailing_contact_query(
109 $type,
110 $contactID,
111 $offset,
112 $limit,
113 $selectFields,
114 $fromClause,
115 $whereClause,
116 $sort,
117 $getCount
118 ) {
119
120 if ($getCount) {
121 $sql = "
122 SELECT count(*)
123 FROM civicrm_mailing m
124 INNER JOIN civicrm_contact c ON m.created_id = c.id
125 INNER JOIN civicrm_mailing_job j ON j.mailing_id = m.id
126 INNER JOIN civicrm_mailing_event_queue meq ON meq.job_id = j.id
127 $fromClause
128 WHERE j.is_test = 0
129 AND meq.contact_id = %1
130 $whereClause
131 GROUP BY m.id
132 ";
133
134 $qParams = array(
135 1 => array($contactID, 'Integer')
136 );
137 $dao = CRM_Core_DAO::executeQuery($sql, $qParams);
138
139 $params = array(
140 'type' => $type,
141 'contact_id' => $contactID
142 );
143
144 $results = $dao->N;
145 }
146 else {
147 $defaultFields = array(
148 'm.id' => 'mailing_id',
149 'm.subject' => 'subject',
150 'c.id' => 'creator_id',
151 'c.sort_name' => 'creator_name',
152 );
153
154 if ($selectFields) {
155 $fields = array_merge($selectFields, $defaultFields);
156 }
157 else {
158 $fields = $defaultFields;
159 }
160
161 $select = array();
162 foreach ($fields as $n => $l) {
163 $select[] = "$n as $l";
164 }
165 $select = implode(', ', $select);
166
167 $orderBy = 'ORDER BY j.start_date DESC';
168 if ($sort) {
169 $orderBy = "ORDER BY $sort";
170 }
171
172 $sql = "
173 SELECT $select
174 FROM civicrm_mailing m
175 INNER JOIN civicrm_contact c ON m.created_id = c.id
176 INNER JOIN civicrm_mailing_job j ON j.mailing_id = m.id
177 INNER JOIN civicrm_mailing_event_queue meq ON meq.job_id = j.id
178 $fromClause
179 WHERE j.is_test = 0
180 AND meq.contact_id = %1
181 $whereClause
182 GROUP BY m.id
183 {$orderBy}
184 ";
185
186 if ($limit > 0) {
187 $sql .= "
188 LIMIT %2, %3
189 ";
190 }
191
192 $qParams = array(
193 1 => array($contactID, 'Integer'),
194 2 => array($offset, 'Integer'),
195 3 => array($limit, 'Integer')
196 );
197 $dao = CRM_Core_DAO::executeQuery($sql, $qParams);
198
199 $results = array();
200 while ($dao->fetch()) {
201 foreach ($fields as $n => $l) {
202 $results[$dao->mailing_id][$l] = $dao->$l;
203 }
204 }
205 }
206
207 return $results;
208 }
209
210 /**
211 * @param $contactID
212 * @param $offset
213 * @param $limit
214 * @param $sort
215 * @param $getCount
216 *
217 * @return array
218 */
219 function _civicrm_api3_mailing_contact_get_delivered(
220 $contactID,
221 $offset,
222 $limit,
223 $sort,
224 $getCount
225 ) {
226 $selectFields = array('med.time_stamp' => 'start_date');
227
228 $fromClause = "
229 INNER JOIN civicrm_mailing_event_delivered med ON med.event_queue_id = meq.id
230 LEFT JOIN civicrm_mailing_event_bounce meb ON meb.event_queue_id = meq.id
231 ";
232
233 $whereClause = "
234 AND meb.id IS NULL
235 ";
236
237 return _civicrm_api3_mailing_contact_query(
238 'Delivered',
239 $contactID,
240 $offset,
241 $limit,
242 $selectFields,
243 $fromClause,
244 $whereClause,
245 $sort,
246 $getCount
247 );
248 }
249
250 /**
251 * @param $contactID
252 * @param $offset
253 * @param $limit
254 * @param $sort
255 * @param $getCount
256 *
257 * @return array
258 */
259 function _civicrm_api3_mailing_contact_get_bounced(
260 $contactID,
261 $offset,
262 $limit,
263 $sort,
264 $getCount
265 ) {
266 $fromClause = "
267 INNER JOIN civicrm_mailing_event_bounce meb ON meb.event_queue_id = meq.id
268 ";
269
270 return _civicrm_api3_mailing_contact_query(
271 'Bounced',
272 $contactID,
273 $offset,
274 $limit,
275 NULL,
276 $fromClause,
277 NULL,
278 $sort,
279 $getCount
280 );
281 }
282
283 /**
284 * Get count of all the mailings that a contact was involved with
285 *
286 * @param array $params input parameters
287 * - key: contact_id, value: int - required
288 * - key: type, value: Delivered | Bounced - optional, defaults to Delivered
289 * - Future extensions will include: Opened, Clicked, Forwarded
290 *
291 * @return array API result
292 * @static void
293 * @access public
294 * @example CRM/Mailing/BAO/Mailing.php
295 *
296 */
297 function civicrm_api3_mailing_contact_getcount($params) {
298 return _civicrm_api3_mailing_contact_getresults($params, TRUE);
299 }