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