Merge pull request #583 from yashodha/CRM-12463
[civicrm-core.git] / api / v3 / MailingContact.php
CommitLineData
2ede60ec
DL
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 input parameters
43 * - key: contact_id, value: int - required
44 * - key: type, value: Delivered | Bounced - optional, defaults to Delivered
45 * - Future extensions will include: Opened, Clicked, Forwarded
46 *
47 * @return array API result
48 * @static void
49 * @access public
a6fe948b 50 * @example CRM/Mailing/BAO/Mailing.php
2ede60ec
DL
51 *
52 */
53function civicrm_api3_mailing_contact_get($params) {
54 if (empty($params['contact_id'])) {
55 return civicrm_api3_create_error('contact_id is a required field');
56 }
57
58 if (empty($params['type'])) {
59 $params['type'] = 'Delivered';
60 }
61
62 $validTypeValues = array('Delivered', 'Bounced');
63 if (!in_array($params['type'], $validTypeValues)) {
64 return civicrm_api3_create_error(
65 'type should be one of the following: ' .
66 implode(', ', $validTypeValues)
67 );
68 }
69
70 if (!isset($params['offset'])) {
71 $params['offset'] = 0;
72 }
73
2ede60ec
DL
74 if (!isset($params['limit'])) {
75 $params['limit'] = 50;
76 }
77
78 $fnName = '_civicrm_api3_mailing_contact_get_' . strtolower($params['type']);
79 return $fnName(
80 $params['contact_id'],
81 $params['offset'],
100afa30 82 $params['limit'],
a6fe948b
KJ
83 CRM_Utils_Array::value('sort', $params),
84 CRM_Utils_Array::value('getcount', $params)
2ede60ec
DL
85 );
86}
87
88function _civicrm_api3_mailing_contact_query(
89 $type,
90 $contactID,
91 $offset,
92 $limit,
93 $selectFields,
94 $fromClause,
100afa30 95 $whereClause,
a6fe948b
KJ
96 $sort,
97 $getCount
2ede60ec 98) {
2ede60ec 99
a6fe948b
KJ
100 if ($getCount) {
101 $sql = "
102SELECT count(*)
103FROM civicrm_mailing m
104INNER JOIN civicrm_contact c ON m.created_id = c.id
105INNER JOIN civicrm_mailing_job j ON j.mailing_id = m.id
106INNER JOIN civicrm_mailing_event_queue meq ON meq.job_id = j.id
107 $fromClause
108WHERE j.is_test = 0
109AND meq.contact_id = %1
110 $whereClause
111GROUP BY m.id
112";
113
114 $qParams = array(
115 1 => array($contactID, 'Integer')
116 );
117 $dao = CRM_Core_DAO::executeQuery($sql, $qParams);
118
119 $params = array(
120 'type' => $type,
121 'contact_id' => $contactID
122 );
123
124 $results = array('count' => $dao->N);
2ede60ec
DL
125 }
126 else {
a6fe948b
KJ
127 $defaultFields = array(
128 'm.id' => 'mailing_id',
129 'm.subject' => 'subject',
130 'c.id' => 'creator_id',
131 'c.sort_name' => 'creator_name',
132 );
2ede60ec 133
a6fe948b
KJ
134 if ($selectFields) {
135 $fields = array_merge($selectFields, $defaultFields);
136 }
137 else {
138 $fields = $defaultFields;
139 }
2ede60ec 140
a6fe948b
KJ
141 $select = array();
142 foreach ($fields as $n => $l) {
143 $select[] = "$n as $l";
144 }
145 $select = implode(', ', $select);
146
147 $orderBy = 'ORDER BY j.start_date';
148 if ($sort) {
149 $orderBy = "ORDER BY $sort";
150 }
100afa30 151
a6fe948b 152 $sql = "
2ede60ec
DL
153SELECT $select
154FROM civicrm_mailing m
155INNER JOIN civicrm_contact c ON m.created_id = c.id
156INNER JOIN civicrm_mailing_job j ON j.mailing_id = m.id
157INNER JOIN civicrm_mailing_event_queue meq ON meq.job_id = j.id
158 $fromClause
159WHERE j.is_test = 0
160AND meq.contact_id = %1
161 $whereClause
162GROUP BY m.id
100afa30 163{$orderBy}
2ede60ec
DL
164";
165
a6fe948b
KJ
166 if ($limit > 0) {
167 $sql .= "
2ede60ec
DL
168LIMIT %2, %3
169";
a6fe948b 170 }
2ede60ec 171
a6fe948b
KJ
172 $qParams = array(
173 1 => array($contactID, 'Integer'),
174 2 => array($offset, 'Integer'),
175 3 => array($limit, 'Integer')
176 );
177 $dao = CRM_Core_DAO::executeQuery($sql, $qParams);
2ede60ec 178
a6fe948b
KJ
179 $results = array();
180 while ($dao->fetch()) {
181 foreach ($fields as $n => $l) {
182 $results[$dao->mailing_id][$l] = $dao->$l;
183 }
2ede60ec 184 }
a6fe948b
KJ
185
186 $params = array(
187 'type' => $type,
188 'contact_id' => $contactID,
189 'offset' => $offset,
190 'limit' => $limit
191 );
2ede60ec
DL
192 }
193
2ede60ec
DL
194 return civicrm_api3_create_success($results, $params);
195}
196
197function _civicrm_api3_mailing_contact_get_delivered(
198 $contactID,
199 $offset,
100afa30 200 $limit,
a6fe948b
KJ
201 $sort,
202 $getCount
2ede60ec
DL
203) {
204 $selectFields = array('med.time_stamp' => 'start_date');
205
206 $fromClause = "
207INNER JOIN civicrm_mailing_event_delivered med ON med.event_queue_id = meq.id
208LEFT JOIN civicrm_mailing_event_bounce meb ON meb.event_queue_id = meq.id
209";
210
211 $whereClause = "
212AND meb.id IS NULL
213";
214
215 return _civicrm_api3_mailing_contact_query(
216 'Delivered',
217 $contactID,
218 $offset,
219 $limit,
220 $selectFields,
221 $fromClause,
100afa30 222 $whereClause,
a6fe948b
KJ
223 $sort,
224 $getCount
2ede60ec
DL
225 );
226}
227
228function _civicrm_api3_mailing_contact_get_bounced(
229 $contactID,
230 $offset,
100afa30 231 $limit,
a6fe948b
KJ
232 $sort,
233 $getCount
2ede60ec
DL
234) {
235 $fromClause = "
236INNER JOIN civicrm_mailing_event_bounce meb ON meb.event_queue_id = meq.id
237";
238
239 return _civicrm_api3_mailing_contact_query(
240 'Bounced',
241 $contactID,
242 $offset,
243 $limit,
244 NULL,
245 $fromClause,
100afa30 246 NULL,
a6fe948b
KJ
247 $sort,
248 $getCount
2ede60ec
DL
249 );
250}
a6fe948b
KJ
251
252/**
253 * Get count of all the mailings that a contact was involved with
254 *
255 * @param array $params input parameters
256 * - key: contact_id, value: int - required
257 * - key: type, value: Delivered | Bounced - optional, defaults to Delivered
258 * - Future extensions will include: Opened, Clicked, Forwarded
259 *
260 * @return array API result
261 * @static void
262 * @access public
263 * @example CRM/Mailing/BAO/Mailing.php
264 *
265 */
266function civicrm_api3_mailing_contact_getcount($params) {
267 if (empty($params['contact_id'])) {
268 return civicrm_api3_create_error('contact_id is a required field');
269 }
270
271 // set the count mode for the api
272 $params['getcount'] = 1;
273 return civicrm_api3_mailing_contact_get($params);
274}