worked on CRM-12357, implemented getcount api
authorKurund Jalmi <kurund@civicrm.org>
Tue, 30 Apr 2013 00:17:27 +0000 (17:17 -0700)
committerKurund Jalmi <kurund@civicrm.org>
Tue, 30 Apr 2013 00:17:27 +0000 (17:17 -0700)
CRM/Mailing/BAO/Mailing.php
api/v3/MailingContact.php

index ba54ceccb42fcd5711adbd26c7f662bad2f0785e..d55ff5c9e6b63e1e1bf1b8959fe8a90cc5c5f139 100644 (file)
@@ -2744,7 +2744,6 @@ AND        m.id = %1
     $params['rowCount'] = $params['rp'];
     $params['sort']     = CRM_Utils_Array::value('sortBy', $params);
     $params['caseId']   = NULL;
-    $context            = CRM_Utils_Array::value('context', $params);
 
     // get contact mailings
     $mailings = CRM_Mailing_BAO_Mailing::getContactMailings($params);
@@ -2803,12 +2802,10 @@ AND        m.id = %1
    * @access public
    */
   static public function getContactMailingsCount(&$params) {
-    //FIX ME: need to implement getcount api for MailingContact
     $params['version'] = 3;
-    unset($params['offset']);
-    unset($params['limit']);
-    $result = civicrm_api('MailingContact', 'get', $params);
-    return count($result['values']);
+
+    $result = civicrm_api('MailingContact', 'getcount', $params);
+    return $result['values']['count'];
   }
 }
 
index 889f1b946f3a7f531cf13b4aa21c6efdea3868b4..05c033f5e79d0f874c776b2a4f8a5ecb29b929cf 100644 (file)
@@ -47,7 +47,7 @@
  * @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) {
@@ -71,7 +71,6 @@ function civicrm_api3_mailing_contact_get($params) {
     $params['offset'] = 0;
   }
 
-
   if (!isset($params['limit'])) {
     $params['limit'] = 50;
   }
@@ -81,7 +80,8 @@ function civicrm_api3_mailing_contact_get($params) {
     $params['contact_id'],
     $params['offset'],
     $params['limit'],
-    CRM_Utils_Array::value('sort', $params)
+    CRM_Utils_Array::value('sort', $params),
+    CRM_Utils_Array::value('getcount', $params)
   );
 }
 
@@ -93,34 +93,63 @@ function _civicrm_api3_mailing_contact_query(
   $selectFields,
   $fromClause,
   $whereClause,
-  $sort
+  $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 = array('count' => $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;
+    }
 
-  $orderBy = 'ORDER BY j.start_date';
-  if ($sort) {
-    $orderBy = "ORDER BY $sort";
-  }
+    $select = array();
+    foreach ($fields as $n => $l) {
+      $select[] = "$n as $l";
+    }
+    $select = implode(', ', $select);
+
+    $orderBy = 'ORDER BY j.start_date';
+    if ($sort) {
+      $orderBy = "ORDER BY $sort";
+    }
 
-  $sql = "
+    $sql = "
 SELECT     $select
 FROM       civicrm_mailing m
 INNER JOIN civicrm_contact c ON m.created_id = c.id
@@ -134,32 +163,34 @@ GROUP BY   m.id
 {$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
+    );
   }
 
-  $params = array(
-    'type'   => $type,
-    'contact_id' => $contactID,
-    'offset' => $offset,
-    'limit'  => $limit
-  );
   return civicrm_api3_create_success($results, $params);
 }
 
@@ -167,7 +198,8 @@ function _civicrm_api3_mailing_contact_get_delivered(
   $contactID,
   $offset,
   $limit,
-  $sort
+  $sort,
+  $getCount
 ) {
   $selectFields = array('med.time_stamp' => 'start_date');
 
@@ -188,7 +220,8 @@ AND        meb.id IS NULL
     $selectFields,
     $fromClause,
     $whereClause,
-    $sort
+    $sort,
+    $getCount
   );
 }
 
@@ -196,7 +229,8 @@ function _civicrm_api3_mailing_contact_get_bounced(
   $contactID,
   $offset,
   $limit,
-  $sort
+  $sort,
+  $getCount
 ) {
   $fromClause = "
 INNER JOIN civicrm_mailing_event_bounce meb ON meb.event_queue_id = meq.id
@@ -210,6 +244,31 @@ INNER JOIN civicrm_mailing_event_bounce meb ON meb.event_queue_id = meq.id
     NULL,
     $fromClause,
     NULL,
-    $sort
+    $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) {
+  if (empty($params['contact_id'])) {
+    return civicrm_api3_create_error('contact_id is a required field');
+  }
+
+  // set the count mode for the api
+  $params['getcount'] = 1;
+  return civicrm_api3_mailing_contact_get($params);
+}