CRM-16353 - Mailing DataTables refactoring
authorJoseph Lacey <joseph@palantetech.coop>
Thu, 30 Apr 2015 02:43:10 +0000 (20:43 -0600)
committerJoseph Lacey <joseph@palantetech.coop>
Thu, 30 Apr 2015 02:43:10 +0000 (20:43 -0600)
----------------------------------------
* CRM-16353: Upgrade datatables integration to use v2 api
  https://issues.civicrm.org/jira/browse/CRM-16353

CRM/Mailing/BAO/Mailing.php
CRM/Mailing/Page/AJAX.php
templates/CRM/Mailing/Page/Tab.tpl

index a0c7b9176e9491ea58e982dd2c6ea039a4ad129b..d7e259d66ca0fa09f91f542a7cf488537f883bf2 100644 (file)
@@ -3046,18 +3046,17 @@ AND        m.id = %1
     // format params and add links
     $contactMailings = array();
     foreach ($mailings as $mailingId => $values) {
-      $contactMailings[$mailingId]['subject'] = $values['subject'];
-      $contactMailings[$mailingId]['start_date'] = CRM_Utils_Date::customFormat($values['start_date']);
-      $contactMailings[$mailingId]['recipients'] = CRM_Utils_System::href(ts('(recipients)'), 'civicrm/mailing/report/event',
-        "mid={$values['mailing_id']}&reset=1&cid={$params['contact_id']}&event=queue&context=mailing");
-
-      $contactMailings[$mailingId]['mailing_creator'] = CRM_Utils_System::href(
+      $mailing = array();
+      $mailing['subject'] = $values['subject'];
+      $mailing['creator_name'] = CRM_Utils_System::href(
         $values['creator_name'],
         'civicrm/contact/view',
         "reset=1&cid={$values['creator_id']}");
-
+      $mailing['recipients'] = CRM_Utils_System::href(ts('(recipients)'), 'civicrm/mailing/report/event',
+        "mid={$values['mailing_id']}&reset=1&cid={$params['contact_id']}&event=queue&context=mailing");
+      $mailing['start_date'] = CRM_Utils_Date::customFormat($values['start_date']);
       //CRM-12814
-      $contactMailings[$mailingId]['openstats'] = "Opens: " .
+      $mailing['openstats'] = "Opens: " .
         CRM_Utils_Array::value($values['mailing_id'], $openCounts, 0) .
         "<br />Clicks: " .
         CRM_Utils_Array::value($values['mailing_id'], $clickCounts, 0);
@@ -3083,7 +3082,7 @@ AND        m.id = %1
         $mailingKey = $hash;
       }
 
-      $contactMailings[$mailingId]['links'] = CRM_Core_Action::formLink(
+      $mailing['links'] = CRM_Core_Action::formLink(
         $actionLinks,
         NULL,
         array(
@@ -3097,9 +3096,16 @@ AND        m.id = %1
         'Mailing',
         $values['mailing_id']
       );
+
+      array_push($contactMailings, $mailing);
     }
 
-    return $contactMailings;
+    $contactMailingsDT = array();
+    $contactMailingsDT['data'] = $contactMailings;
+    $contactMailingsDT['recordsTotal'] = $params['total'];
+    $contactMailingsDT['recordsFiltered'] = $params['total'];
+
+    return $contactMailingsDT;
   }
 
   /**
index e49519e2e17881fa713a22e706818c82310b2091..af2c5d274ad01499e7f8262ba04d6cc17e7d0b39 100644 (file)
@@ -65,22 +65,17 @@ class CRM_Mailing_Page_AJAX {
   public static function getContactMailings() {
     $contactID = CRM_Utils_Type::escape($_GET['contact_id'], 'Integer');
 
-    $sortMapper = array(
-      0 => 'subject',
-      1 => 'creator_name',
-      2 => '',
-      3 => 'start_date',
-      4 => '',
-      5 => 'links',
-    );
+    $sortMapper = array();
+    foreach ($_GET['columns'] as $key => $value) {
+      $sortMapper[$key] = $value['data'];
+    };
 
-    $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
-    $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
-    $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
-    $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : NULL;
-    $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
+    $offset = isset($_GET['start']) ? CRM_Utils_Type::escape($_GET['start'], 'Integer') : 0;
+    $rowCount = isset($_GET['length']) ? CRM_Utils_Type::escape($_GET['length'], 'Integer') : 25;
+    $sort = isset($_GET['order'][0]['column']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_GET['order'][0]['column'], 'Integer'), $sortMapper) : NULL;
+    $sortOrder = isset($_GET['order'][0]['dir']) ? CRM_Utils_Type::escape($_GET['order'][0]['dir'], 'String') : 'asc';
 
-    $params = $_POST;
+    $params = $_GET;
     if ($sort && $sortOrder) {
       $params['sortBy'] = $sort . ' ' . $sortOrder;
     }
@@ -94,19 +89,7 @@ class CRM_Mailing_Page_AJAX {
     // get the contact mailings
     $mailings = CRM_Mailing_BAO_Mailing::getContactMailingSelector($params);
 
-    $iFilteredTotal = $iTotal = $params['total'];
-    $selectorElements = array(
-      'subject',
-      'mailing_creator',
-      'recipients',
-      'start_date',
-      'openstats',
-      'links',
-    );
-
-    header('Content-Type: application/json');
-    echo CRM_Utils_JSON::encodeDataTableSelector($mailings, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
-    CRM_Utils_System::civiExit();
+    CRM_Utils_JSON::output($mailings);
   }
 
 }
index b6e987fa43fc144248a6165d6e295ecf27478056..3c817fdbf0c6ee49285f019bb3e87d7f24d995c7 100644 (file)
 *}
 
 <div class="crm-mailing-selector">
-  <table class="contact-mailing-selector">
+  <table class="contact-mailing-selector crm-ajax-table">
     <thead>
-    <tr>
-      <th class='crm-mailing-contact-subject'>{ts}Subject{/ts}</th>
-      <th class='crm-mailing-contact_created'>{ts}Added By{/ts}</th>
-      <th class='crm-contact-activity_contact nosort'>{ts}Recipients{/ts}</th>
-      <th class='crm-mailing-contact-date'>{ts}Date{/ts}</th>
-      <th class='crm-mailing_openstats'>{ts}Opens/ Clicks{/ts}</th>
-      <th class='crm-mailing-contact-links nosort'>&nbsp;</th>
-    </tr>
+      <tr>
+        <th data-data="subject" class="crm-mailing-contact-subject">{ts}Subject{/ts}</th>
+        <th data-data="creator_name" class="crm-mailing-contact_created">{ts}Added By{/ts}</th>
+        <th data-data="recipients" data-orderable="false" class="crm-contact-activity_contact">{ts}Recipients{/ts}</th>
+        <th data-data="start_date" class="crm-mailing-contact-date">{ts}Date{/ts}</th>
+        <th data-data="openstats" data-orderable="false" class="crm-mailing_openstats">{ts}Opens/ Clicks{/ts}</th>
+        <th data-data="links" data-orderable="false" class="crm-mailing-contact-links">&nbsp;</th>
+      </tr>
     </thead>
   </table>
-</div>
-{literal}
-<script type="text/javascript">
-  CRM.$(function($) {
-    var oTable;
-
-    buildMailingContact();
-
-    function buildMailingContact() {
-      var sourceUrl = {/literal}'{crmURL p="civicrm/ajax/contactmailing" h=0 q="contact_id=$contactId"}'{literal};
-
-      var ZeroRecordText = {/literal}'{ts escape="js"}None found.{/ts}'{literal};
-
-      oTable = $('.contact-mailing-selector').dataTable({
-        "bFilter": false,
-        "bAutoWidth": false,
-        "aaSorting": [],
-        "aoColumns": [
-          {sClass: 'crm-mailing-contact-subject'},
-          {sClass: 'crm-mailing-contact_created'},
-          {sClass: 'crm-contact-activity_contact', bSortable:false},
-          {sClass: 'crm-mailing-contact-date'},
-          {sClass: 'crm-mailing_openstats', bSortable:false},
-          {sClass: 'crm-mailing-contact-links', bSortable: false}
-        ],
-        "bProcessing": true,
-        "sPaginationType": "full_numbers",
-        "sDom": '<"crm-datatable-pager-top"lfp>rt<"crm-datatable-pager-bottom"ip>',
-        "bServerSide": true,
-        "bJQueryUI": true,
-        "sAjaxSource": sourceUrl,
-        "iDisplayLength": 25,
-        "oLanguage": {
-          "sZeroRecords": ZeroRecordText,
-          "sProcessing": {/literal}"{ts escape='js'}Processing...{/ts}"{literal},
-          "sLengthMenu": {/literal}"{ts escape='js'}Show _MENU_ entries{/ts}"{literal},
-          "sInfo": {/literal}"{ts escape='js'}Showing _START_ to _END_ of _TOTAL_ entries{/ts}"{literal},
-          "sInfoEmpty": {/literal}"{ts escape='js'}Showing 0 to 0 of 0 entries{/ts}"{literal},
-          "sInfoFiltered": {/literal}"{ts escape='js'}(filtered from _MAX_ total entries){/ts}"{literal},
-          "sSearch": {/literal}"{ts escape='js'}Search:{/ts}"{literal},
-          "oPaginate": {
-            "sFirst": {/literal}"{ts escape='js'}First{/ts}"{literal},
-            "sPrevious": {/literal}"{ts escape='js'}Previous{/ts}"{literal},
-            "sNext": {/literal}"{ts escape='js'}Next{/ts}"{literal},
-            "sLast": {/literal}"{ts escape='js'}Last{/ts}"{literal}
+  {literal}
+    <script type="text/javascript">
+      (function($) {
+        CRM.$('table.contact-mailing-selector').data({
+          "ajax": {
+            "url": {/literal}'{crmURL p="civicrm/ajax/contactmailing" h=0 q="contact_id=$contactId"}'{literal}
           }
-        }
-      });
-    }
-  });
-</script>
-{/literal}
+        });
+      })(CRM.$);
+    </script>
+  {/literal}
+</div>