3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * File for the CiviCRM APIv3 contact and mailing functions
31 * @package CiviCRM_APIv3
32 * @subpackage API_MailingContact
34 * @copyright CiviCRM LLC (c) 2004-2013
40 * Get all the mailings and details that a contact was involved with
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
47 * @return array API result
50 * @example CRM/Mailing/BAO/Mailing.php
53 function civicrm_api3_mailing_contact_get($params) {
54 if (empty($params['contact_id'])) {
55 return civicrm_api3_create_error('contact_id is a required field');
58 if (empty($params['type'])) {
59 $params['type'] = 'Delivered';
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)
70 if (!isset($params['offset'])) {
71 $params['offset'] = 0;
74 if (!isset($params['limit'])) {
75 $params['limit'] = 50;
78 $fnName = '_civicrm_api3_mailing_contact_get_' . strtolower($params['type']);
80 $params['contact_id'],
83 CRM_Utils_Array
::value('sort', $params),
84 CRM_Utils_Array
::value('getcount', $params)
88 function _civicrm_api3_mailing_contact_query(
103 FROM civicrm_mailing m
104 INNER JOIN civicrm_contact c ON m.created_id = c.id
105 INNER JOIN civicrm_mailing_job j ON j.mailing_id = m.id
106 INNER JOIN civicrm_mailing_event_queue meq ON meq.job_id = j.id
109 AND meq.contact_id = %1
115 1 => array($contactID, 'Integer')
117 $dao = CRM_Core_DAO
::executeQuery($sql, $qParams);
121 'contact_id' => $contactID
124 $results = array('count' => $dao->N
);
127 $defaultFields = array(
128 'm.id' => 'mailing_id',
129 'm.subject' => 'subject',
130 'c.id' => 'creator_id',
131 'c.sort_name' => 'creator_name',
135 $fields = array_merge($selectFields, $defaultFields);
138 $fields = $defaultFields;
142 foreach ($fields as $n => $l) {
143 $select[] = "$n as $l";
145 $select = implode(', ', $select);
147 $orderBy = 'ORDER BY j.start_date';
149 $orderBy = "ORDER BY $sort";
154 FROM civicrm_mailing m
155 INNER JOIN civicrm_contact c ON m.created_id = c.id
156 INNER JOIN civicrm_mailing_job j ON j.mailing_id = m.id
157 INNER JOIN civicrm_mailing_event_queue meq ON meq.job_id = j.id
160 AND meq.contact_id = %1
173 1 => array($contactID, 'Integer'),
174 2 => array($offset, 'Integer'),
175 3 => array($limit, 'Integer')
177 $dao = CRM_Core_DAO
::executeQuery($sql, $qParams);
180 while ($dao->fetch()) {
181 foreach ($fields as $n => $l) {
182 $results[$dao->mailing_id
][$l] = $dao->$l;
188 'contact_id' => $contactID,
194 return civicrm_api3_create_success($results, $params);
197 function _civicrm_api3_mailing_contact_get_delivered(
204 $selectFields = array('med.time_stamp' => 'start_date');
207 INNER JOIN civicrm_mailing_event_delivered med ON med.event_queue_id = meq.id
208 LEFT JOIN civicrm_mailing_event_bounce meb ON meb.event_queue_id = meq.id
215 return _civicrm_api3_mailing_contact_query(
228 function _civicrm_api3_mailing_contact_get_bounced(
236 INNER JOIN civicrm_mailing_event_bounce meb ON meb.event_queue_id = meq.id
239 return _civicrm_api3_mailing_contact_query(
253 * Get count of all the mailings that a contact was involved with
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
260 * @return array API result
263 * @example CRM/Mailing/BAO/Mailing.php
266 function civicrm_api3_mailing_contact_getcount($params) {
267 if (empty($params['contact_id'])) {
268 return civicrm_api3_create_error('contact_id is a required field');
271 // set the count mode for the api
272 $params['getcount'] = 1;
273 return civicrm_api3_mailing_contact_get($params);