<?php
/*
+--------------------------------------------------------------------+
- | CiviCRM version 4.3 |
+ | CiviCRM version 4.5 |
+--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013 |
+ | Copyright CiviCRM LLC (c) 2004-2014 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
* @package CiviCRM_APIv3
* @subpackage API_MailingContact
*
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
* @version $Id$
*
*/
* @return array API result
* @static void
* @access public
- * @example CRM/Mailing/Page/Tab.php
+ * @example CRM/Mailing/BAO/Mailing.php
*
*/
function civicrm_api3_mailing_contact_get($params) {
- if (empty($params['contact_id'])) {
- return civicrm_api3_create_error('contact_id is a required field');
- }
-
- if (empty($params['type'])) {
- $params['type'] = 'Delivered';
- }
-
- $validTypeValues = array('Delivered', 'Bounced');
- if (!in_array($params['type'], $validTypeValues)) {
- return civicrm_api3_create_error(
- 'type should be one of the following: ' .
- implode(', ', $validTypeValues)
- );
- }
-
- if (!isset($params['offset'])) {
- $params['offset'] = 0;
- }
-
-
- if (!isset($params['limit'])) {
- $params['limit'] = 50;
+ return civicrm_api3_create_success(_civicrm_api3_mailing_contact_getresults($params, FALSE));
+}
+/**
+ * This is a wrapper for the functions that return the results from the 'quasi-entity'
+ * mailing contact
+ * @param array $params
+ * @param Boolean $count
+ * @throws Exception
+ */
+function _civicrm_api3_mailing_contact_getresults($params, $count){
+ if(empty($params['type'])){
+ //ie. because the api is an anomoly & passing in id is not valid
+ throw new Exception('This api call does not accept api as a parameter');
}
-
+ $options = _civicrm_api3_get_options_from_params($params, TRUE,'contribution','get');
$fnName = '_civicrm_api3_mailing_contact_get_' . strtolower($params['type']);
return $fnName(
- $params['contact_id'],
- $params['offset'],
- $params['limit']
+ $params['contact_id'],
+ $options['offset'],
+ $options['limit'],
+ $options['sort'],
+ $count
+ );
+}
+/**
+ * Adjust Metadata for Get action
+ *
+ * @param array $params array or parameters determined by getfields
+ */
+function _civicrm_api3_mailing_contact_get_spec(&$params) {
+ $params['contact_id']['api.required'] = 1;
+ $params['type'] = array(
+ 'api.default' => 'Delivered',
+ 'type' => CRM_Utils_Type::T_STRING,
+ 'options' => array(
+ 'Delivered' => 'Delivered',
+ 'Bounced' => 'Bounced',
+ )
);
}
$limit,
$selectFields,
$fromClause,
- $whereClause
+ $whereClause,
+ $sort,
+ $getCount
) {
- $defaultFields = array(
- 'm.id' => 'mailing_id',
- 'm.subject' => 'subject',
- 'c.id' => 'creator_id',
- 'c.sort_name' => 'creator_name',
- );
- if ($selectFields) {
- $fields = array_merge($selectFields, $defaultFields);
+ if ($getCount) {
+ $sql = "
+SELECT count(*)
+FROM civicrm_mailing m
+INNER JOIN civicrm_contact c ON m.created_id = c.id
+INNER JOIN civicrm_mailing_job j ON j.mailing_id = m.id
+INNER JOIN civicrm_mailing_event_queue meq ON meq.job_id = j.id
+ $fromClause
+WHERE j.is_test = 0
+AND meq.contact_id = %1
+ $whereClause
+GROUP BY m.id
+";
+
+ $qParams = array(
+ 1 => array($contactID, 'Integer')
+ );
+ $dao = CRM_Core_DAO::executeQuery($sql, $qParams);
+
+ $params = array(
+ 'type' => $type,
+ 'contact_id' => $contactID
+ );
+
+ $results = $dao->N;
}
else {
- $fields = $defaultFields;
- }
+ $defaultFields = array(
+ 'm.id' => 'mailing_id',
+ 'm.subject' => 'subject',
+ 'c.id' => 'creator_id',
+ 'c.sort_name' => 'creator_name',
+ );
- $select = array();
- foreach ($fields as $n => $l) {
- $select[] = "$n as $l";
- }
- $select = implode(', ', $select);
+ if ($selectFields) {
+ $fields = array_merge($selectFields, $defaultFields);
+ }
+ else {
+ $fields = $defaultFields;
+ }
+
+ $select = array();
+ foreach ($fields as $n => $l) {
+ $select[] = "$n as $l";
+ }
+ $select = implode(', ', $select);
- $sql = "
+ $orderBy = 'ORDER BY j.start_date DESC';
+ if ($sort) {
+ $orderBy = "ORDER BY $sort";
+ }
+
+ $sql = "
SELECT $select
FROM civicrm_mailing m
INNER JOIN civicrm_contact c ON m.created_id = c.id
AND meq.contact_id = %1
$whereClause
GROUP BY m.id
-ORDER BY j.start_date
+{$orderBy}
";
- if ($limit > 0) {
- $sql .= "
+ if ($limit > 0) {
+ $sql .= "
LIMIT %2, %3
";
- }
+ }
- $qParams = array(
- 1 => array($contactID, 'Integer'),
- 2 => array($offset, 'Integer'),
- 3 => array($limit, 'Integer')
- );
- $dao = CRM_Core_DAO::executeQuery($sql, $qParams);
+ $qParams = array(
+ 1 => array($contactID, 'Integer'),
+ 2 => array($offset, 'Integer'),
+ 3 => array($limit, 'Integer')
+ );
+ $dao = CRM_Core_DAO::executeQuery($sql, $qParams);
- $results = array();
- while ($dao->fetch()) {
- foreach ($fields as $n => $l) {
- $results[$dao->mailing_id][$l] = $dao->$l;
+ $results = array();
+ while ($dao->fetch()) {
+ foreach ($fields as $n => $l) {
+ $results[$dao->mailing_id][$l] = $dao->$l;
+ }
}
}
- $params = array(
- 'type' => $type,
- 'contact_id' => $contactID,
- 'offset' => $offset,
- 'limit' => $limit
- );
- return civicrm_api3_create_success($results, $params);
+ return $results;
}
function _civicrm_api3_mailing_contact_get_delivered(
$contactID,
$offset,
- $limit
+ $limit,
+ $sort,
+ $getCount
) {
$selectFields = array('med.time_stamp' => 'start_date');
$limit,
$selectFields,
$fromClause,
- $whereClause
+ $whereClause,
+ $sort,
+ $getCount
);
}
function _civicrm_api3_mailing_contact_get_bounced(
$contactID,
$offset,
- $limit
+ $limit,
+ $sort,
+ $getCount
) {
$fromClause = "
INNER JOIN civicrm_mailing_event_bounce meb ON meb.event_queue_id = meq.id
$limit,
NULL,
$fromClause,
- NULL
+ NULL,
+ $sort,
+ $getCount
);
}
+
+/**
+ * Get count of all the mailings that a contact was involved with
+ *
+ * @param array $params input parameters
+ * - key: contact_id, value: int - required
+ * - key: type, value: Delivered | Bounced - optional, defaults to Delivered
+ * - Future extensions will include: Opened, Clicked, Forwarded
+ *
+ * @return array API result
+ * @static void
+ * @access public
+ * @example CRM/Mailing/BAO/Mailing.php
+ *
+ */
+function civicrm_api3_mailing_contact_getcount($params) {
+ return _civicrm_api3_mailing_contact_getresults($params, TRUE);
+}