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