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