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