Commit | Line | Data |
---|---|---|
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 | */ |
43 | function 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 |
55 | function _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 | */ |
77 | function _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 | 109 | function _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 = " | |
122 | SELECT count(*) | |
123 | FROM civicrm_mailing m | |
124 | INNER JOIN civicrm_contact c ON m.created_id = c.id | |
125 | INNER JOIN civicrm_mailing_job j ON j.mailing_id = m.id | |
126 | INNER JOIN civicrm_mailing_event_queue meq ON meq.job_id = j.id | |
127 | $fromClause | |
128 | WHERE j.is_test = 0 | |
129 | AND meq.contact_id = %1 | |
130 | $whereClause | |
131 | GROUP 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 | |
a6fe948b | 167 | $sql = " |
2ede60ec DL |
168 | SELECT $select |
169 | FROM civicrm_mailing m | |
170 | INNER JOIN civicrm_contact c ON m.created_id = c.id | |
171 | INNER JOIN civicrm_mailing_job j ON j.mailing_id = m.id | |
172 | INNER JOIN civicrm_mailing_event_queue meq ON meq.job_id = j.id | |
173 | $fromClause | |
174 | WHERE j.is_test = 0 | |
175 | AND meq.contact_id = %1 | |
176 | $whereClause | |
177 | GROUP BY m.id | |
100afa30 | 178 | {$orderBy} |
2ede60ec DL |
179 | "; |
180 | ||
a6fe948b KJ |
181 | if ($limit > 0) { |
182 | $sql .= " | |
2ede60ec DL |
183 | LIMIT %2, %3 |
184 | "; | |
a6fe948b | 185 | } |
2ede60ec | 186 | |
a6fe948b KJ |
187 | $qParams = array( |
188 | 1 => array($contactID, 'Integer'), | |
189 | 2 => array($offset, 'Integer'), | |
21dfd5f5 | 190 | 3 => array($limit, 'Integer'), |
a6fe948b KJ |
191 | ); |
192 | $dao = CRM_Core_DAO::executeQuery($sql, $qParams); | |
2ede60ec | 193 | |
a6fe948b KJ |
194 | $results = array(); |
195 | while ($dao->fetch()) { | |
196 | foreach ($fields as $n => $l) { | |
197 | $results[$dao->mailing_id][$l] = $dao->$l; | |
198 | } | |
2ede60ec DL |
199 | } |
200 | } | |
201 | ||
b14ce773 | 202 | return $results; |
2ede60ec DL |
203 | } |
204 | ||
aa1b1481 | 205 | /** |
35823763 EM |
206 | * Get delivered mailing contacts. |
207 | * | |
100fef9d | 208 | * @param int $contactID |
aa1b1481 EM |
209 | * @param $offset |
210 | * @param $limit | |
211 | * @param $sort | |
212 | * @param $getCount | |
213 | * | |
214 | * @return array | |
215 | */ | |
2ede60ec DL |
216 | function _civicrm_api3_mailing_contact_get_delivered( |
217 | $contactID, | |
218 | $offset, | |
100afa30 | 219 | $limit, |
a6fe948b KJ |
220 | $sort, |
221 | $getCount | |
2ede60ec DL |
222 | ) { |
223 | $selectFields = array('med.time_stamp' => 'start_date'); | |
224 | ||
225 | $fromClause = " | |
226 | INNER JOIN civicrm_mailing_event_delivered med ON med.event_queue_id = meq.id | |
227 | LEFT JOIN civicrm_mailing_event_bounce meb ON meb.event_queue_id = meq.id | |
228 | "; | |
229 | ||
230 | $whereClause = " | |
231 | AND meb.id IS NULL | |
232 | "; | |
233 | ||
234 | return _civicrm_api3_mailing_contact_query( | |
2ede60ec DL |
235 | $contactID, |
236 | $offset, | |
237 | $limit, | |
238 | $selectFields, | |
239 | $fromClause, | |
100afa30 | 240 | $whereClause, |
a6fe948b KJ |
241 | $sort, |
242 | $getCount | |
2ede60ec DL |
243 | ); |
244 | } | |
245 | ||
aa1b1481 | 246 | /** |
1747ab99 EM |
247 | * Get bounced mailing contact records. |
248 | * | |
100fef9d | 249 | * @param int $contactID |
aa1b1481 EM |
250 | * @param $offset |
251 | * @param $limit | |
252 | * @param $sort | |
253 | * @param $getCount | |
254 | * | |
255 | * @return array | |
256 | */ | |
2ede60ec DL |
257 | function _civicrm_api3_mailing_contact_get_bounced( |
258 | $contactID, | |
259 | $offset, | |
100afa30 | 260 | $limit, |
a6fe948b KJ |
261 | $sort, |
262 | $getCount | |
2ede60ec DL |
263 | ) { |
264 | $fromClause = " | |
265 | INNER JOIN civicrm_mailing_event_bounce meb ON meb.event_queue_id = meq.id | |
266 | "; | |
267 | ||
268 | return _civicrm_api3_mailing_contact_query( | |
2ede60ec DL |
269 | $contactID, |
270 | $offset, | |
271 | $limit, | |
272 | NULL, | |
273 | $fromClause, | |
100afa30 | 274 | NULL, |
a6fe948b KJ |
275 | $sort, |
276 | $getCount | |
2ede60ec DL |
277 | ); |
278 | } | |
a6fe948b KJ |
279 | |
280 | /** | |
d1b0d05e | 281 | * Get count of all the mailings that a contact was involved with. |
a6fe948b | 282 | * |
cf470720 | 283 | * @param array $params |
02ac46aa | 284 | * Input parameters per getfields |
a6fe948b | 285 | * |
a6c01b45 | 286 | * @return array |
72b3a70c | 287 | * API result |
a6fe948b KJ |
288 | */ |
289 | function civicrm_api3_mailing_contact_getcount($params) { | |
b14ce773 | 290 | return _civicrm_api3_mailing_contact_getresults($params, TRUE); |
a6fe948b | 291 | } |