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