| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 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-2014 |
| 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 |
| 43 | * Input parameters. |
| 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 |
| 51 | * @example CRM/Mailing/BAO/Mailing.php |
| 52 | * |
| 53 | */ |
| 54 | function civicrm_api3_mailing_contact_get($params) { |
| 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 |
| 61 | * @param bool $count |
| 62 | * @throws Exception |
| 63 | */ |
| 64 | function _civicrm_api3_mailing_contact_getresults($params, $count) { |
| 65 | if (empty($params['type'])) { |
| 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'); |
| 68 | } |
| 69 | $options = _civicrm_api3_get_options_from_params($params, TRUE,'contribution','get'); |
| 70 | $fnName = '_civicrm_api3_mailing_contact_get_' . strtolower($params['type']); |
| 71 | return $fnName( |
| 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 | * |
| 82 | * @param array $params |
| 83 | * Array or parameters determined by getfields. |
| 84 | */ |
| 85 | function _civicrm_api3_mailing_contact_get_spec(&$params) { |
| 86 | $params['contact_id']['api.required'] = 1; |
| 87 | $params['contact_id']['title'] = 'Contact ID'; |
| 88 | $params['type'] = array( |
| 89 | 'api.default' => 'Delivered', |
| 90 | 'title' => 'Type',// doesn't really explain the field - but not sure I understand it to explain it better |
| 91 | 'type' => CRM_Utils_Type::T_STRING, |
| 92 | 'options' => array( |
| 93 | 'Delivered' => 'Delivered', |
| 94 | 'Bounced' => 'Bounced', |
| 95 | ), |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @param $type |
| 101 | * @param int $contactID |
| 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 | */ |
| 112 | function _civicrm_api3_mailing_contact_query( |
| 113 | $type, |
| 114 | $contactID, |
| 115 | $offset, |
| 116 | $limit, |
| 117 | $selectFields, |
| 118 | $fromClause, |
| 119 | $whereClause, |
| 120 | $sort, |
| 121 | $getCount |
| 122 | ) { |
| 123 | |
| 124 | if ($getCount) { |
| 125 | $sql = " |
| 126 | SELECT count(*) |
| 127 | FROM civicrm_mailing m |
| 128 | INNER JOIN civicrm_contact c ON m.created_id = c.id |
| 129 | INNER JOIN civicrm_mailing_job j ON j.mailing_id = m.id |
| 130 | INNER JOIN civicrm_mailing_event_queue meq ON meq.job_id = j.id |
| 131 | $fromClause |
| 132 | WHERE j.is_test = 0 |
| 133 | AND meq.contact_id = %1 |
| 134 | $whereClause |
| 135 | GROUP 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 | |
| 148 | $results = $dao->N; |
| 149 | } |
| 150 | else { |
| 151 | $defaultFields = array( |
| 152 | 'm.id' => 'mailing_id', |
| 153 | 'm.subject' => 'subject', |
| 154 | 'c.id' => 'creator_id', |
| 155 | 'c.sort_name' => 'creator_name', |
| 156 | ); |
| 157 | |
| 158 | if ($selectFields) { |
| 159 | $fields = array_merge($selectFields, $defaultFields); |
| 160 | } |
| 161 | else { |
| 162 | $fields = $defaultFields; |
| 163 | } |
| 164 | |
| 165 | $select = array(); |
| 166 | foreach ($fields as $n => $l) { |
| 167 | $select[] = "$n as $l"; |
| 168 | } |
| 169 | $select = implode(', ', $select); |
| 170 | |
| 171 | $orderBy = 'ORDER BY j.start_date DESC'; |
| 172 | if ($sort) { |
| 173 | $orderBy = "ORDER BY $sort"; |
| 174 | } |
| 175 | |
| 176 | $sql = " |
| 177 | SELECT $select |
| 178 | FROM civicrm_mailing m |
| 179 | INNER JOIN civicrm_contact c ON m.created_id = c.id |
| 180 | INNER JOIN civicrm_mailing_job j ON j.mailing_id = m.id |
| 181 | INNER JOIN civicrm_mailing_event_queue meq ON meq.job_id = j.id |
| 182 | $fromClause |
| 183 | WHERE j.is_test = 0 |
| 184 | AND meq.contact_id = %1 |
| 185 | $whereClause |
| 186 | GROUP BY m.id |
| 187 | {$orderBy} |
| 188 | "; |
| 189 | |
| 190 | if ($limit > 0) { |
| 191 | $sql .= " |
| 192 | LIMIT %2, %3 |
| 193 | "; |
| 194 | } |
| 195 | |
| 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); |
| 202 | |
| 203 | $results = array(); |
| 204 | while ($dao->fetch()) { |
| 205 | foreach ($fields as $n => $l) { |
| 206 | $results[$dao->mailing_id][$l] = $dao->$l; |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | return $results; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * @param int $contactID |
| 216 | * @param $offset |
| 217 | * @param $limit |
| 218 | * @param $sort |
| 219 | * @param $getCount |
| 220 | * |
| 221 | * @return array |
| 222 | */ |
| 223 | function _civicrm_api3_mailing_contact_get_delivered( |
| 224 | $contactID, |
| 225 | $offset, |
| 226 | $limit, |
| 227 | $sort, |
| 228 | $getCount |
| 229 | ) { |
| 230 | $selectFields = array('med.time_stamp' => 'start_date'); |
| 231 | |
| 232 | $fromClause = " |
| 233 | INNER JOIN civicrm_mailing_event_delivered med ON med.event_queue_id = meq.id |
| 234 | LEFT JOIN civicrm_mailing_event_bounce meb ON meb.event_queue_id = meq.id |
| 235 | "; |
| 236 | |
| 237 | $whereClause = " |
| 238 | AND 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, |
| 248 | $whereClause, |
| 249 | $sort, |
| 250 | $getCount |
| 251 | ); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * @param int $contactID |
| 256 | * @param $offset |
| 257 | * @param $limit |
| 258 | * @param $sort |
| 259 | * @param $getCount |
| 260 | * |
| 261 | * @return array |
| 262 | */ |
| 263 | function _civicrm_api3_mailing_contact_get_bounced( |
| 264 | $contactID, |
| 265 | $offset, |
| 266 | $limit, |
| 267 | $sort, |
| 268 | $getCount |
| 269 | ) { |
| 270 | $fromClause = " |
| 271 | INNER 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, |
| 281 | NULL, |
| 282 | $sort, |
| 283 | $getCount |
| 284 | ); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Get count of all the mailings that a contact was involved with |
| 289 | * |
| 290 | * @param array $params |
| 291 | * Input parameters. |
| 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 | */ |
| 302 | function civicrm_api3_mailing_contact_getcount($params) { |
| 303 | return _civicrm_api3_mailing_contact_getresults($params, TRUE); |
| 304 | } |