3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * This api exposes CiviCRM contact and mailing.
15 * @package CiviCRM_APIv3
19 * Get all the mailings and details that a contact was involved with.
21 * @param array $params
22 * Input parameters - see _spec for details (returned by getfields)
27 function civicrm_api3_mailing_contact_get($params) {
28 return civicrm_api3_create_success(_civicrm_api3_mailing_contact_getresults($params, FALSE));
32 * This is a wrapper for the functions that return the results from the 'quasi-entity' mailing contact.
34 * @param array $params
39 function _civicrm_api3_mailing_contact_getresults($params, $count) {
40 if (empty($params['type'])) {
41 //ie. because the api is an anomaly & passing in id is not valid
42 throw new Exception('This api call does not accept api as a parameter');
44 $options = _civicrm_api3_get_options_from_params($params, TRUE, 'contribution', 'get');
45 $fnName = '_civicrm_api3_mailing_contact_get_' . strtolower($params['type']);
47 $params['contact_id'],
56 * Adjust Metadata for Get action.
58 * @param array $params
59 * Array of parameters determined by getfields.
61 function _civicrm_api3_mailing_contact_get_spec(&$params) {
62 $params['contact_id'] = [
64 'title' => 'Contact ID',
65 'type' => CRM_Utils_Type
::T_INT
,
69 'api.default' => 'Delivered',
70 // doesn't really explain the field - but not sure I understand it to explain it better
72 'type' => CRM_Utils_Type
::T_STRING
,
74 'Delivered' => 'Delivered',
75 'Bounced' => 'Bounced',
81 * Helper function for mailing contact queries.
83 * @param int $contactID
86 * @param $selectFields
94 function _civicrm_api3_mailing_contact_query(
108 FROM civicrm_mailing m
109 INNER JOIN civicrm_contact c ON m.created_id = c.id
110 INNER JOIN civicrm_mailing_job j ON j.mailing_id = m.id
111 INNER JOIN civicrm_mailing_event_queue meq ON meq.job_id = j.id
114 AND meq.contact_id = %1
120 1 => [$contactID, 'Integer'],
122 $dao = CRM_Core_DAO
::executeQuery($sql, $qParams);
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 MIN(j.start_date) DESC';
149 $orderBy = "ORDER BY $sort";
152 $groupBy = CRM_Contact_BAO_Query
::getGroupByFromSelectColumns(array_keys($fields), "m.id");
156 FROM civicrm_mailing m
157 INNER JOIN civicrm_contact c ON m.created_id = c.id
158 INNER JOIN civicrm_mailing_job j ON j.mailing_id = m.id
159 INNER JOIN civicrm_mailing_event_queue meq ON meq.job_id = j.id
162 AND meq.contact_id = %1
175 1 => [$contactID, 'Integer'],
176 2 => [$offset, 'Integer'],
177 3 => [$limit, 'Integer'],
179 $dao = CRM_Core_DAO
::executeQuery($sql, $qParams);
182 while ($dao->fetch()) {
183 foreach ($fields as $n => $l) {
184 $results[$dao->mailing_id
][$l] = $dao->$l;
193 * Get delivered mailing contacts.
195 * @param int $contactID
203 function _civicrm_api3_mailing_contact_get_delivered(
210 $selectFields = ['med.time_stamp' => 'start_date'];
213 INNER JOIN civicrm_mailing_event_delivered med ON med.event_queue_id = meq.id
214 LEFT JOIN civicrm_mailing_event_bounce meb ON meb.event_queue_id = meq.id
221 return _civicrm_api3_mailing_contact_query(
234 * Get bounced mailing contact records.
236 * @param int $contactID
244 function _civicrm_api3_mailing_contact_get_bounced(
252 INNER JOIN civicrm_mailing_event_bounce meb ON meb.event_queue_id = meq.id
255 return _civicrm_api3_mailing_contact_query(
268 * Get count of all the mailings that a contact was involved with.
270 * @param array $params
271 * Input parameters per getfields
276 function civicrm_api3_mailing_contact_getcount($params) {
277 return _civicrm_api3_mailing_contact_getresults($params, TRUE);