Add pager to custom data tab
authorjitendrapurohit <jitendra.purohit@webaccessglobal.com>
Fri, 15 Jan 2016 13:15:25 +0000 (18:45 +0530)
committerjitendrapurohit <jitendra.purohit@webaccessglobal.com>
Fri, 15 Jan 2016 13:15:25 +0000 (18:45 +0530)
CRM/Core/xml/Menu/Custom.xml
CRM/Custom/Page/AJAX.php
CRM/Profile/Page/MultipleRecordFieldsListing.php
templates/CRM/Profile/Page/MultipleRecordFieldsListing.tpl

index 04a38ae614c7e6dff2b054c7267b31c50959364f..f1b40aa51e5ca5eb961d946496b3c1cc6e9cfae9 100644 (file)
@@ -15,4 +15,9 @@
      <page_callback>CRM_Custom_Page_AJAX::fixOrdering</page_callback>
      <access_arguments>access CiviCRM</access_arguments>
   </item>
+  <item>
+     <path>civicrm/ajax/multirecordfieldlist</path>
+     <page_callback>CRM_Custom_Page_AJAX::getMultiRecordFieldList</page_callback>
+     <access_arguments>access CiviCRM</access_arguments>
+  </item>
 </menu>
index 4885b1f17d3bf9b97d759b6d1325920e2db9af95..1bc0991def04d956f99a0d907ed1a4d01b6349df 100644 (file)
@@ -105,4 +105,54 @@ class CRM_Custom_Page_AJAX {
     CRM_Utils_JSON::output(TRUE);
   }
 
+  /**
+   * Get list of Multi Record Fields.
+   *
+   */
+  public static function getMultiRecordFieldList() {
+    $params = $_REQUEST;
+
+    $sEcho = CRM_Utils_Type::escape($_GET['sEcho'], 'Integer');
+    $offset = isset($params['iDisplayStart']) ? CRM_Utils_Type::escape($params['iDisplayStart'], 'Integer') : 0;
+    $rowCount = isset($params['iDisplayLength']) ? CRM_Utils_Type::escape($params['iDisplayLength'], 'Integer') : 25;
+
+    $params['page'] = ($offset / $rowCount) + 1;
+    $params['rp'] = $rowCount;
+    $contactType = CRM_Contact_BAO_Contact::getContactType($params['cid']);
+
+    $obj = new CRM_Profile_Page_MultipleRecordFieldsListing();
+    $obj->_pageViewType = 'customDataView';
+    $obj->_contactId = $params['cid'];
+    $obj->_customGroupId = $params['cgid'];
+    $obj->_contactType = $contactType;
+    $obj->_offset = ($params['page'] - 1) * $params['rp'];
+    $obj->_rowCount = $params['rp'];
+
+    list($headers, $multiRecordFields) = $obj->browse();
+
+    // sort the fields by the columns
+    $sortMapper = array_values($headers);
+    $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';
+    if ($sort && $sortOrder) {
+      $sortOrder = constant('SORT_' . strtoupper($sortOrder));
+      $sortCol = array();
+      $dataType = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $sort, 'data_type');
+      foreach ($multiRecordFields as $key=> $row) {
+        $sortCol[$key] = $row[$sort];
+        if ($dataType == 'Date') {
+          $sortCol[$key] = CRM_Utils_Date::unixTime($row[$sort]);
+        }
+      }
+      array_multisort($sortCol, $sortOrder, $multiRecordFields);
+    }
+
+    $iFilteredTotal = $iTotal = $obj->_total;
+    $selectorElements = array_merge($headers, array('action', 'class'));
+
+    CRM_Utils_System::setHttpHeader('Content-Type', 'application/json');
+    echo CRM_Utils_JSON::encodeDataTableSelector($multiRecordFields, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
+    CRM_Utils_System::civiExit();
+  }
+
 }
index 20615f10b40a73f6df1e645dec85a802c2052571..9ee81ca02c5b0ae5b6f35a7f1118d0f629290e95 100644 (file)
@@ -45,13 +45,13 @@ class CRM_Profile_Page_MultipleRecordFieldsListing extends CRM_Core_Page_Basic {
 
   protected $_profileId = NULL;
 
-  protected $_contactId = NULL;
+  public $_contactId = NULL;
 
-  protected $_customGroupTitle = NULL;
+  public $_customGroupTitle = NULL;
 
-  protected $_pageViewType = NULL;
+  public $_pageViewType = NULL;
 
-  protected $_contactType = NULL;
+  public $_contactType = NULL;
 
   /**
    * Get BAO Name.
@@ -299,6 +299,11 @@ class CRM_Profile_Page_MultipleRecordFieldsListing extends CRM_Core_Page_Basic {
           unset($links[CRM_Core_Action::COPY]);
         }
         $newCgCount = (!$reached) ? count($result) + 1 : NULL;
+        if (isset($this->_offset) && isset($this->_rowCount)) {
+          $this->_total = count($result);
+          $result = array_slice($result, $this->_offset, $this->_rowCount, TRUE);
+          $cgcount = $this->_offset + 1;
+        }
         foreach ($result as $recId => &$value) {
           foreach ($value as $fieldId => &$val) {
             if (is_numeric($fieldId)) {
@@ -418,16 +423,20 @@ class CRM_Profile_Page_MultipleRecordFieldsListing extends CRM_Core_Page_Basic {
     $headers = array();
     if (!empty($fieldIDs)) {
       foreach ($fieldIDs as $fieldID) {
-        $headers[$fieldID] = ($this->_pageViewType == 'profileDataView') ? $customGroupInfo[$fieldID]['fieldLabel'] : $fieldLabels[$fieldID]['label'];
+        $headers[$fieldID] = ($this->_pageViewType == 'profileDataView') ? $customGroupInfo[$fieldID]['fieldLabel'] : !isset($this->_offset) ? $fieldLabels[$fieldID]['label'] : $fieldID;
       }
     }
     $this->assign('dateFields', $dateFields);
     $this->assign('dateFieldsVals', $dateFieldsVals);
     $this->assign('cgcount', $cgcount);
+    $this->assign('contactId', $this->_contactId);
+    $this->assign('contactType', $this->_contactType);
     $this->assign('customGroupTitle', $this->_customGroupTitle);
     $this->assign('headers', $headers);
     $this->assign('records', $result);
-    $this->assign('attributes', $attributes);
+    $this->assign('attributes', json_encode($attributes));
+
+    return array($headers, $result);
   }
 
   /**
index 72bcff5596ab579d9e785e63f7dae0c4797e1c35..e7fa21028a76a6dd0aecb3d9a36c31fa530b5d90 100644 (file)
@@ -35,7 +35,7 @@
     <div id="custom-{$customGroupId}-table-wrapper" {if $pageViewType eq 'customDataView'}class="crm-entity" data-entity="contact" data-id="{$contactId}"{/if}>
       <div>
         {strip}
-          <table id="records" class="display">
+          <table id="records" class={if $pageViewType eq 'customDataView'}"crm-multifield-selector"{else}'display'{/if}>
             <thead>
             <tr>
               {foreach from=$headers key=recId item=head}
               {/foreach}
             </tr>
             </thead>
-            {foreach from=$records key=recId item=rows}
-              <tr class="{cycle values="odd-row,even-row"}">
-                {foreach from=$headers key=hrecId item=head}
-                  <td {crmAttributes a=$attributes.$hrecId.$recId}>{$rows.$hrecId}</td>
-                {/foreach}
-                <td>{$rows.action}</td>
-                {foreach from=$dateFieldsVals key=fid item=rec}
-                    <td class='crm-field-{$fid}_date hiddenElement'>{$rec.$recId}</td>
-                {/foreach}
-              </tr>
-            {/foreach}
+            {if $pageViewType eq 'customDataView'}
+              {literal}
+              <script type="text/javascript">
+              CRM.$(function($) {
+                var sourceUrl = {/literal}'{crmURL p="civicrm/ajax/multirecordfieldlist" h=0 q="snippet=4&cid=$contactId&cgid=$customGroupId"}'{literal};
+
+                $('table.crm-multifield-selector').dataTable({
+                  "bProcessing": true,
+                  "aaSorting": [],
+                  "asStripClasses" : [ "odd-row", "even-row" ],
+                  "sPaginationType": "full_numbers",
+                  "sDom"       : '<"crm-datatable-pager-top"lfp>rt<"crm-datatable-pager-bottom"ip>',
+                  "bServerSide": true,
+                  "sAjaxSource": sourceUrl,
+                });
+
+                $(".crm-multifield-selector").on('click','.delete-custom-row', function (e) {
+                  var $el = $(this);
+                  CRM.confirm({
+                    message: '{/literal}{ts escape='js'}Are you sure you want to delete this record?{/ts}{literal}'
+                  }).on('crmConfirm:yes', function() {
+                    var postUrl = {/literal}"{crmURL p='civicrm/ajax/customvalue' h=0 }"{literal};
+                    var request = $.post(postUrl, $el.data('delete_params'));
+                    CRM.status({/literal}"{ts escape='js'}Record Deleted{/ts}"{literal}, request);
+                    request.done(function() {
+                      CRM.refreshParent($el);
+                    });
+                  })
+                  e.preventDefault();
+                });
+              });
+              </script>
+              {/literal}
+            {else}
+              {foreach from=$records key=recId item=rows}
+                <tr class="{cycle values="odd-row,even-row"}">
+                  {foreach from=$headers key=hrecId item=head}
+                    <td {crmAttributes a=$attributes.$hrecId.$recId}>{$rows.$hrecId}</td>
+                  {/foreach}
+                  <td>{$rows.action}</td>
+                  {foreach from=$dateFieldsVals key=fid item=rec}
+                      <td class='crm-field-{$fid}_date hiddenElement'>{$rec.$recId}</td>
+                  {/foreach}
+                </tr>
+              {/foreach}
+            {/if}
           </table>
         {/strip}
       </div>