Merge pull request #23561 from eileenmcnaughton/import_catcher_
[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 45 $fnName = '_civicrm_api3_mailing_contact_get_' . strtolower($params['type']);
f75133ed
CW
46 if (!function_exists($fnName)) {
47 throw new API_Exception('Invalid mailing type: ' . $params['type']);
48 }
2ede60ec 49 return $fnName(
b14ce773 50 $params['contact_id'],
51 $options['offset'],
52 $options['limit'],
53 $options['sort'],
54 $count
55 );
56}
9d32e6f7 57
b14ce773 58/**
9d32e6f7 59 * Adjust Metadata for Get action.
b14ce773 60 *
cf470720 61 * @param array $params
b081365f 62 * Array of parameters determined by getfields.
b14ce773 63 */
64function _civicrm_api3_mailing_contact_get_spec(&$params) {
cf8f0fff 65 $params['contact_id'] = [
d142432b
EM
66 'api.required' => 1,
67 'title' => 'Contact ID',
68 'type' => CRM_Utils_Type::T_INT,
cf8f0fff 69 ];
d142432b 70
cf8f0fff 71 $params['type'] = [
b14ce773 72 'api.default' => 'Delivered',
7c31ae57
SL
73 // doesn't really explain the field - but not sure I understand it to explain it better
74 'title' => 'Type',
b14ce773 75 'type' => CRM_Utils_Type::T_STRING,
cf8f0fff 76 'options' => [
b14ce773 77 'Delivered' => 'Delivered',
78 'Bounced' => 'Bounced',
cf8f0fff
CW
79 ],
80 ];
2ede60ec
DL
81}
82
aa1b1481 83/**
9d32e6f7
EM
84 * Helper function for mailing contact queries.
85 *
100fef9d 86 * @param int $contactID
2884d956
BT
87 * @param int $offset
88 * @param int $limit
89 * @param array|null $selectFields
90 * @param string|null $fromClause
91 * @param string|null $whereClause
92 * @param string|null $sort
93 * @param bool $getCount
aa1b1481
EM
94 *
95 * @return array
96 */
2ede60ec 97function _civicrm_api3_mailing_contact_query(
2ede60ec
DL
98 $contactID,
99 $offset,
100 $limit,
101 $selectFields,
102 $fromClause,
100afa30 103 $whereClause,
a6fe948b
KJ
104 $sort,
105 $getCount
2ede60ec 106) {
2ede60ec 107
a6fe948b
KJ
108 if ($getCount) {
109 $sql = "
110SELECT count(*)
111FROM civicrm_mailing m
112INNER JOIN civicrm_contact c ON m.created_id = c.id
113INNER JOIN civicrm_mailing_job j ON j.mailing_id = m.id
114INNER JOIN civicrm_mailing_event_queue meq ON meq.job_id = j.id
115 $fromClause
116WHERE j.is_test = 0
117AND meq.contact_id = %1
118 $whereClause
119GROUP BY m.id
120";
121
cf8f0fff
CW
122 $qParams = [
123 1 => [$contactID, 'Integer'],
124 ];
a6fe948b
KJ
125 $dao = CRM_Core_DAO::executeQuery($sql, $qParams);
126
b14ce773 127 $results = $dao->N;
2ede60ec
DL
128 }
129 else {
cf8f0fff 130 $defaultFields = [
a6fe948b
KJ
131 'm.id' => 'mailing_id',
132 'm.subject' => 'subject',
133 'c.id' => 'creator_id',
134 'c.sort_name' => 'creator_name',
cf8f0fff 135 ];
2ede60ec 136
a6fe948b
KJ
137 if ($selectFields) {
138 $fields = array_merge($selectFields, $defaultFields);
139 }
140 else {
141 $fields = $defaultFields;
142 }
2ede60ec 143
cf8f0fff 144 $select = [];
a6fe948b
KJ
145 foreach ($fields as $n => $l) {
146 $select[] = "$n as $l";
147 }
148 $select = implode(', ', $select);
149
dff3bb07 150 $orderBy = 'ORDER BY MIN(j.start_date) DESC';
a6fe948b
KJ
151 if ($sort) {
152 $orderBy = "ORDER BY $sort";
153 }
100afa30 154
29ddb61b 155 $groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns(array_keys($fields), "m.id");
156
a6fe948b 157 $sql = "
2ede60ec
DL
158SELECT $select
159FROM civicrm_mailing m
160INNER JOIN civicrm_contact c ON m.created_id = c.id
161INNER JOIN civicrm_mailing_job j ON j.mailing_id = m.id
162INNER JOIN civicrm_mailing_event_queue meq ON meq.job_id = j.id
163 $fromClause
164WHERE j.is_test = 0
165AND meq.contact_id = %1
166 $whereClause
29ddb61b 167{$groupBy}
100afa30 168{$orderBy}
2ede60ec
DL
169";
170
a6fe948b
KJ
171 if ($limit > 0) {
172 $sql .= "
2ede60ec
DL
173LIMIT %2, %3
174";
a6fe948b 175 }
2ede60ec 176
cf8f0fff
CW
177 $qParams = [
178 1 => [$contactID, 'Integer'],
179 2 => [$offset, 'Integer'],
180 3 => [$limit, 'Integer'],
181 ];
a6fe948b 182 $dao = CRM_Core_DAO::executeQuery($sql, $qParams);
2ede60ec 183
cf8f0fff 184 $results = [];
a6fe948b
KJ
185 while ($dao->fetch()) {
186 foreach ($fields as $n => $l) {
187 $results[$dao->mailing_id][$l] = $dao->$l;
188 }
2ede60ec
DL
189 }
190 }
191
b14ce773 192 return $results;
2ede60ec
DL
193}
194
aa1b1481 195/**
35823763
EM
196 * Get delivered mailing contacts.
197 *
100fef9d 198 * @param int $contactID
2884d956
BT
199 * @param int $offset
200 * @param int $limit
201 * @param string|null $sort
202 * @param bool $getCount
aa1b1481
EM
203 *
204 * @return array
205 */
2ede60ec
DL
206function _civicrm_api3_mailing_contact_get_delivered(
207 $contactID,
208 $offset,
100afa30 209 $limit,
a6fe948b
KJ
210 $sort,
211 $getCount
2ede60ec 212) {
cf8f0fff 213 $selectFields = ['med.time_stamp' => 'start_date'];
2ede60ec
DL
214
215 $fromClause = "
216INNER JOIN civicrm_mailing_event_delivered med ON med.event_queue_id = meq.id
217LEFT JOIN civicrm_mailing_event_bounce meb ON meb.event_queue_id = meq.id
218";
219
220 $whereClause = "
221AND meb.id IS NULL
222";
223
224 return _civicrm_api3_mailing_contact_query(
2ede60ec
DL
225 $contactID,
226 $offset,
227 $limit,
228 $selectFields,
229 $fromClause,
100afa30 230 $whereClause,
a6fe948b
KJ
231 $sort,
232 $getCount
2ede60ec
DL
233 );
234}
235
aa1b1481 236/**
1747ab99
EM
237 * Get bounced mailing contact records.
238 *
100fef9d 239 * @param int $contactID
2884d956
BT
240 * @param int $offset
241 * @param int $limit
242 * @param string|null $sort
243 * @param bool $getCount
aa1b1481
EM
244 *
245 * @return array
246 */
2ede60ec
DL
247function _civicrm_api3_mailing_contact_get_bounced(
248 $contactID,
249 $offset,
100afa30 250 $limit,
a6fe948b
KJ
251 $sort,
252 $getCount
2ede60ec
DL
253) {
254 $fromClause = "
255INNER JOIN civicrm_mailing_event_bounce meb ON meb.event_queue_id = meq.id
256";
257
258 return _civicrm_api3_mailing_contact_query(
2ede60ec
DL
259 $contactID,
260 $offset,
261 $limit,
262 NULL,
263 $fromClause,
100afa30 264 NULL,
a6fe948b
KJ
265 $sort,
266 $getCount
2ede60ec
DL
267 );
268}
a6fe948b
KJ
269
270/**
d1b0d05e 271 * Get count of all the mailings that a contact was involved with.
a6fe948b 272 *
cf470720 273 * @param array $params
02ac46aa 274 * Input parameters per getfields
a6fe948b 275 *
a6c01b45 276 * @return array
72b3a70c 277 * API result
a6fe948b
KJ
278 */
279function civicrm_api3_mailing_contact_getcount($params) {
b14ce773 280 return _civicrm_api3_mailing_contact_getresults($params, TRUE);
a6fe948b 281}