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