3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 * This api exposes CiviCRM contact and mailing.
31 * @package CiviCRM_APIv3
35 * Get all the mailings and details that a contact was involved with.
37 * @param array $params
38 * Input parameters - see _spec for details (returned by getfields)
43 function civicrm_api3_mailing_contact_get($params) {
44 return civicrm_api3_create_success(_civicrm_api3_mailing_contact_getresults($params, FALSE));
48 * This is a wrapper for the functions that return the results from the 'quasi-entity' mailing contact.
50 * @param array $params
55 function _civicrm_api3_mailing_contact_getresults($params, $count) {
56 if (empty($params['type'])) {
57 //ie. because the api is an anomaly & passing in id is not valid
58 throw new Exception('This api call does not accept api as a parameter');
60 $options = _civicrm_api3_get_options_from_params($params, TRUE, 'contribution', 'get');
61 $fnName = '_civicrm_api3_mailing_contact_get_' . strtolower($params['type']);
63 $params['contact_id'],
72 * Adjust Metadata for Get action.
74 * @param array $params
75 * Array of parameters determined by getfields.
77 function _civicrm_api3_mailing_contact_get_spec(&$params) {
78 $params['contact_id'] = [
80 'title' => 'Contact ID',
81 'type' => CRM_Utils_Type
::T_INT
,
85 'api.default' => 'Delivered',
86 // doesn't really explain the field - but not sure I understand it to explain it better
88 'type' => CRM_Utils_Type
::T_STRING
,
90 'Delivered' => 'Delivered',
91 'Bounced' => 'Bounced',
97 * Helper function for mailing contact queries.
99 * @param int $contactID
102 * @param $selectFields
104 * @param $whereClause
110 function _civicrm_api3_mailing_contact_query(
124 FROM civicrm_mailing m
125 INNER JOIN civicrm_contact c ON m.created_id = c.id
126 INNER JOIN civicrm_mailing_job j ON j.mailing_id = m.id
127 INNER JOIN civicrm_mailing_event_queue meq ON meq.job_id = j.id
130 AND meq.contact_id = %1
136 1 => [$contactID, 'Integer'],
138 $dao = CRM_Core_DAO
::executeQuery($sql, $qParams);
144 'm.id' => 'mailing_id',
145 'm.subject' => 'subject',
146 'c.id' => 'creator_id',
147 'c.sort_name' => 'creator_name',
151 $fields = array_merge($selectFields, $defaultFields);
154 $fields = $defaultFields;
158 foreach ($fields as $n => $l) {
159 $select[] = "$n as $l";
161 $select = implode(', ', $select);
163 $orderBy = 'ORDER BY MIN(j.start_date) DESC';
165 $orderBy = "ORDER BY $sort";
168 $groupBy = CRM_Contact_BAO_Query
::getGroupByFromSelectColumns(array_keys($fields), "m.id");
172 FROM civicrm_mailing m
173 INNER JOIN civicrm_contact c ON m.created_id = c.id
174 INNER JOIN civicrm_mailing_job j ON j.mailing_id = m.id
175 INNER JOIN civicrm_mailing_event_queue meq ON meq.job_id = j.id
178 AND meq.contact_id = %1
191 1 => [$contactID, 'Integer'],
192 2 => [$offset, 'Integer'],
193 3 => [$limit, 'Integer'],
195 $dao = CRM_Core_DAO
::executeQuery($sql, $qParams);
198 while ($dao->fetch()) {
199 foreach ($fields as $n => $l) {
200 $results[$dao->mailing_id
][$l] = $dao->$l;
209 * Get delivered mailing contacts.
211 * @param int $contactID
219 function _civicrm_api3_mailing_contact_get_delivered(
226 $selectFields = ['med.time_stamp' => 'start_date'];
229 INNER JOIN civicrm_mailing_event_delivered med ON med.event_queue_id = meq.id
230 LEFT JOIN civicrm_mailing_event_bounce meb ON meb.event_queue_id = meq.id
237 return _civicrm_api3_mailing_contact_query(
250 * Get bounced mailing contact records.
252 * @param int $contactID
260 function _civicrm_api3_mailing_contact_get_bounced(
268 INNER JOIN civicrm_mailing_event_bounce meb ON meb.event_queue_id = meq.id
271 return _civicrm_api3_mailing_contact_query(
284 * Get count of all the mailings that a contact was involved with.
286 * @param array $params
287 * Input parameters per getfields
292 function civicrm_api3_mailing_contact_getcount($params) {
293 return _civicrm_api3_mailing_contact_getresults($params, TRUE);