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