Merge pull request #5536 from totten/4.5-httpclient
[civicrm-core.git] / api / v3 / MailingContact.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 * This api exposes CiviCRM contact and mailing.
30 *
31 * @package CiviCRM_APIv3
32 */
33
34 /**
35 * Get all the mailings and details that a contact was involved with.
36 *
37 * @param array $params
38 * Input parameters - see _spec for details (returned by getfields)
39 *
40 * @return array
41 * API result
42 */
43 function civicrm_api3_mailing_contact_get($params) {
44 return civicrm_api3_create_success(_civicrm_api3_mailing_contact_getresults($params, FALSE));
45 }
46
47 /**
48 * This is a wrapper for the functions that return the results from the 'quasi-entity' mailing contact.
49 *
50 * @param array $params
51 * @param bool $count
52 *
53 * @throws Exception
54 */
55 function _civicrm_api3_mailing_contact_getresults($params, $count) {
56 if (empty($params['type'])) {
57 //ie. because the api is an anomaly & passing in id is not valid
58 throw new Exception('This api call does not accept api as a parameter');
59 }
60 $options = _civicrm_api3_get_options_from_params($params, TRUE, 'contribution', 'get');
61 $fnName = '_civicrm_api3_mailing_contact_get_' . strtolower($params['type']);
62 return $fnName(
63 $params['contact_id'],
64 $options['offset'],
65 $options['limit'],
66 $options['sort'],
67 $count
68 );
69 }
70
71 /**
72 * Adjust Metadata for Get action.
73 *
74 * @param array $params
75 * Array of parameters determined by getfields.
76 */
77 function _civicrm_api3_mailing_contact_get_spec(&$params) {
78 $params['contact_id'] = array(
79 'api.required' => 1,
80 'title' => 'Contact ID',
81 'type' => CRM_Utils_Type::T_INT,
82 );
83
84 $params['type'] = array(
85 'api.default' => 'Delivered',
86 'title' => 'Type', // doesn't really explain the field - but not sure I understand it to explain it better
87 'type' => CRM_Utils_Type::T_STRING,
88 'options' => array(
89 'Delivered' => 'Delivered',
90 'Bounced' => 'Bounced',
91 ),
92 );
93 }
94
95 /**
96 * Helper function for mailing contact queries.
97 *
98 * @param int $contactID
99 * @param $offset
100 * @param $limit
101 * @param $selectFields
102 * @param $fromClause
103 * @param $whereClause
104 * @param $sort
105 * @param $getCount
106 *
107 * @return array
108 */
109 function _civicrm_api3_mailing_contact_query(
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 $results = $dao->N;
140 }
141 else {
142 $defaultFields = array(
143 'm.id' => 'mailing_id',
144 'm.subject' => 'subject',
145 'c.id' => 'creator_id',
146 'c.sort_name' => 'creator_name',
147 );
148
149 if ($selectFields) {
150 $fields = array_merge($selectFields, $defaultFields);
151 }
152 else {
153 $fields = $defaultFields;
154 }
155
156 $select = array();
157 foreach ($fields as $n => $l) {
158 $select[] = "$n as $l";
159 }
160 $select = implode(', ', $select);
161
162 $orderBy = 'ORDER BY j.start_date DESC';
163 if ($sort) {
164 $orderBy = "ORDER BY $sort";
165 }
166
167 $sql = "
168 SELECT $select
169 FROM civicrm_mailing m
170 INNER JOIN civicrm_contact c ON m.created_id = c.id
171 INNER JOIN civicrm_mailing_job j ON j.mailing_id = m.id
172 INNER JOIN civicrm_mailing_event_queue meq ON meq.job_id = j.id
173 $fromClause
174 WHERE j.is_test = 0
175 AND meq.contact_id = %1
176 $whereClause
177 GROUP BY m.id
178 {$orderBy}
179 ";
180
181 if ($limit > 0) {
182 $sql .= "
183 LIMIT %2, %3
184 ";
185 }
186
187 $qParams = array(
188 1 => array($contactID, 'Integer'),
189 2 => array($offset, 'Integer'),
190 3 => array($limit, 'Integer'),
191 );
192 $dao = CRM_Core_DAO::executeQuery($sql, $qParams);
193
194 $results = array();
195 while ($dao->fetch()) {
196 foreach ($fields as $n => $l) {
197 $results[$dao->mailing_id][$l] = $dao->$l;
198 }
199 }
200 }
201
202 return $results;
203 }
204
205 /**
206 * Get delivered mailing contacts.
207 *
208 * @param int $contactID
209 * @param $offset
210 * @param $limit
211 * @param $sort
212 * @param $getCount
213 *
214 * @return array
215 */
216 function _civicrm_api3_mailing_contact_get_delivered(
217 $contactID,
218 $offset,
219 $limit,
220 $sort,
221 $getCount
222 ) {
223 $selectFields = array('med.time_stamp' => 'start_date');
224
225 $fromClause = "
226 INNER JOIN civicrm_mailing_event_delivered med ON med.event_queue_id = meq.id
227 LEFT JOIN civicrm_mailing_event_bounce meb ON meb.event_queue_id = meq.id
228 ";
229
230 $whereClause = "
231 AND meb.id IS NULL
232 ";
233
234 return _civicrm_api3_mailing_contact_query(
235 $contactID,
236 $offset,
237 $limit,
238 $selectFields,
239 $fromClause,
240 $whereClause,
241 $sort,
242 $getCount
243 );
244 }
245
246 /**
247 * Get bounced mailing contact records.
248 *
249 * @param int $contactID
250 * @param $offset
251 * @param $limit
252 * @param $sort
253 * @param $getCount
254 *
255 * @return array
256 */
257 function _civicrm_api3_mailing_contact_get_bounced(
258 $contactID,
259 $offset,
260 $limit,
261 $sort,
262 $getCount
263 ) {
264 $fromClause = "
265 INNER JOIN civicrm_mailing_event_bounce meb ON meb.event_queue_id = meq.id
266 ";
267
268 return _civicrm_api3_mailing_contact_query(
269 $contactID,
270 $offset,
271 $limit,
272 NULL,
273 $fromClause,
274 NULL,
275 $sort,
276 $getCount
277 );
278 }
279
280 /**
281 * Get count of all the mailings that a contact was involved with.
282 *
283 * @param array $params
284 * Input parameters per getfields
285 *
286 * @return array
287 * API result
288 */
289 function civicrm_api3_mailing_contact_getcount($params) {
290 return _civicrm_api3_mailing_contact_getresults($params, TRUE);
291 }