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