From 7d12de7f16b882980e99f3215f5658e1b3e3a218 Mon Sep 17 00:00:00 2001 From: Joseph Lacey Date: Sun, 26 Apr 2015 21:46:11 -0600 Subject: [PATCH] Initial DataTables refactoring --- CRM/Activity/BAO/Activity.php | 76 +++++---- CRM/Activity/Page/AJAX.php | 43 ++--- CRM/Contact/BAO/Relationship.php | 49 ++++-- CRM/Contact/Page/AJAX.php | 50 ++---- js/Common.js | 30 ++++ templates/CRM/Activity/Selector/Selector.tpl | 148 ++++-------------- .../Page/View/RelationshipSelector.tpl | 112 +++---------- templates/CRM/common/l10n.js.tpl | 26 +++ 8 files changed, 207 insertions(+), 327 deletions(-) diff --git a/CRM/Activity/BAO/Activity.php b/CRM/Activity/BAO/Activity.php index f42e030d44..16e0fd8a15 100644 --- a/CRM/Activity/BAO/Activity.php +++ b/CRM/Activity/BAO/Activity.php @@ -2496,80 +2496,85 @@ INNER JOIN civicrm_option_group grp ON ( grp.id = val.option_group_id AND grp.n $mask = CRM_Core_Action::mask($permissions); foreach ($activities as $activityId => $values) { - $contactActivities[$activityId]['activity_type'] = $values['activity_type']; - $contactActivities[$activityId]['subject'] = $values['subject']; + $activity = array(); + // add class to this row if overdue + $activity['DT_RowClass'] = 'crm-entity'; + if (CRM_Utils_Date::overdue(CRM_Utils_Array::value('activity_date_time', $values)) + && CRM_Utils_Array::value('status_id', $values) == 1 + ) { + $activity['DT_RowClass'] .= ' status-overdue'; + } + else { + $activity['DT_RowClass'] .= ' status-ontime'; + } + + $activity['DT_RowData'] = array(); + $activity['DT_RowData']['entity'] = 'activity'; + $activity['DT_RowData']['id'] = $activityId; + + $activity['activity_type'] = $values['activity_type']; + $activity['subject'] = $values['subject']; if ($params['contact_id'] == $values['source_contact_id']) { - $contactActivities[$activityId]['source_contact'] = $values['source_contact_name']; + $activity['source_contact_name'] = $values['source_contact_name']; } elseif ($values['source_contact_id']) { - $contactActivities[$activityId]['source_contact'] = CRM_Utils_System::href($values['source_contact_name'], + $activity['source_contact_name'] = CRM_Utils_System::href($values['source_contact_name'], 'civicrm/contact/view', "reset=1&cid={$values['source_contact_id']}"); } else { - $contactActivities[$activityId]['source_contact'] = 'n/a'; + $activity['source_contact_name'] = 'n/a'; } if (isset($values['mailingId']) && !empty($values['mailingId'])) { - $contactActivities[$activityId]['target_contact'] = CRM_Utils_System::href($values['recipients'], + $activity['target_contact'] = CRM_Utils_System::href($values['recipients'], 'civicrm/mailing/report/event', "mid={$values['source_record_id']}&reset=1&event=queue&cid={$params['contact_id']}&context=activitySelector"); } elseif (!empty($values['recipients'])) { - $contactActivities[$activityId]['target_contact'] = $values['recipients']; + $activity['target_contact_name'] = $values['recipients']; } elseif (isset($values['target_contact_counter']) && $values['target_contact_counter']) { - $contactActivities[$activityId]['target_contact'] = ''; + $activity['target_contact_name'] = ''; foreach ($values['target_contact_name'] as $tcID => $tcName) { - $contactActivities[$activityId]['target_contact'] .= CRM_Utils_System::href($tcName, + $activity['target_contact_name'] .= CRM_Utils_System::href($tcName, 'civicrm/contact/view', "reset=1&cid={$tcID}"); } if ($extraCount = $values['target_contact_counter'] - 1) { - $contactActivities[$activityId]['target_contact'] .= ";
" . "(" . ts('%1 more', array(1 => $extraCount)) . ")"; + $activity['target_contact_name'] .= ";
" . "(" . ts('%1 more', array(1 => $extraCount)) . ")"; } } elseif (!$values['target_contact_name']) { - $contactActivities[$activityId]['target_contact'] = 'n/a'; + $activity['target_contact_name'] = 'n/a'; } if (empty($values['assignee_contact_name'])) { - $contactActivities[$activityId]['assignee_contact'] = 'n/a'; + $activity['assignee_contact_name'] = 'n/a'; } elseif (!empty($values['assignee_contact_name'])) { $count = 0; - $contactActivities[$activityId]['assignee_contact'] = ''; + $activity['assignee_contact_name'] = ''; foreach ($values['assignee_contact_name'] as $acID => $acName) { if ($acID && $count < 5) { - $contactActivities[$activityId]['assignee_contact'] .= CRM_Utils_System::href($acName, 'civicrm/contact/view', "reset=1&cid={$acID}"); + $activity['assignee_contact_name'] .= CRM_Utils_System::href($acName, 'civicrm/contact/view', "reset=1&cid={$acID}"); $count++; if ($count) { - $contactActivities[$activityId]['assignee_contact'] .= "; "; + $activity['assignee_contact_name'] .= "; "; } if ($count == 4) { - $contactActivities[$activityId]['assignee_contact'] .= "(" . ts('more') . ")"; + $activity['assignee_contact_name'] .= "(" . ts('more') . ")"; break; } } } } - $contactActivities[$activityId]['activity_date'] = CRM_Utils_Date::customFormat($values['activity_date_time']); - $contactActivities[$activityId]['status'] = $activityStatus[$values['status_id']]; - - // add class to this row if overdue - $contactActivities[$activityId]['class'] = ''; - if (CRM_Utils_Date::overdue(CRM_Utils_Array::value('activity_date_time', $values)) - && CRM_Utils_Array::value('status_id', $values) == 1 - ) { - $contactActivities[$activityId]['class'] = 'status-overdue'; - } - else { - $contactActivities[$activityId]['class'] = 'status-ontime'; - } + $activity['activity_date_time'] = CRM_Utils_Date::customFormat($values['activity_date_time']); + $activity['status_id'] = $activityStatus[$values['status_id']]; // build links - $contactActivities[$activityId]['links'] = ''; + $activity['links'] = ''; $accessMailingReport = FALSE; if (!empty($values['mailingId'])) { $accessMailingReport = TRUE; @@ -2584,7 +2589,7 @@ INNER JOIN civicrm_option_group grp ON ( grp.id = val.option_group_id AND grp.n $actionMask = array_sum(array_keys($actionLinks)) & $mask; - $contactActivities[$activityId]['links'] = CRM_Core_Action::formLink($actionLinks, + $activity['links'] = CRM_Core_Action::formLink($actionLinks, $actionMask, array( 'id' => $values['activity_id'], @@ -2602,10 +2607,17 @@ INNER JOIN civicrm_option_group grp ON ( grp.id = val.option_group_id AND grp.n if ($values['is_recurring_activity']) { $contactActivities[$activityId]['is_recurring_activity'] = CRM_Core_BAO_RecurringEntity::getPositionAndCount($values['activity_id'], 'civicrm_activity'); } + + array_push($contactActivities, $activity); } } - return $contactActivities; + $activitiesDT = array(); + $activitiesDT['data'] = $contactActivities; + $activitiesDT['recordsTotal'] = $params['total']; + $activitiesDT['recordsFiltered'] = $params['total']; + + return $activitiesDT; } /** diff --git a/CRM/Activity/Page/AJAX.php b/CRM/Activity/Page/AJAX.php index 9c79eae36c..9f6c7ee94f 100644 --- a/CRM/Activity/Page/AJAX.php +++ b/CRM/Activity/Page/AJAX.php @@ -455,26 +455,20 @@ class CRM_Activity_Page_AJAX { } public static function getContactActivity() { - $contactID = CRM_Utils_Type::escape($_POST['contact_id'], 'Integer'); + $contactID = CRM_Utils_Type::escape($_GET['cid'], 'Integer'); $context = CRM_Utils_Type::escape(CRM_Utils_Array::value('context', $_GET), 'String'); - $sortMapper = array( - 0 => 'activity_type', - 1 => 'subject', - 2 => 'source_contact_name', - 3 => '', - 4 => '', - 5 => 'activity_date_time', - 6 => 'status_id', - ); + $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; } @@ -530,22 +524,7 @@ class CRM_Activity_Page_AJAX { ); } - $iFilteredTotal = $iTotal = $params['total']; - $selectorElements = array( - 'activity_type', - 'subject', - 'source_contact', - 'target_contact', - 'assignee_contact', - 'activity_date', - 'status', - 'links', - 'class', - ); - - header('Content-Type: application/json'); - echo CRM_Utils_JSON::encodeDataTableSelector($activities, $sEcho, $iTotal, $iFilteredTotal, $selectorElements); - CRM_Utils_System::civiExit(); + CRM_Utils_JSON::output($activities); } } diff --git a/CRM/Contact/BAO/Relationship.php b/CRM/Contact/BAO/Relationship.php index 83316da013..9d3d710ba0 100644 --- a/CRM/Contact/BAO/Relationship.php +++ b/CRM/Contact/BAO/Relationship.php @@ -1924,7 +1924,7 @@ AND cc.sort_name LIKE '%$name%'"; $relationshipStatus, 0, 1, 0, NULL, NULL, $permissionedContacts); } else { - // FIX ME: we cannot directly determine total permissioned relationship, hence re-fire query + // FIXME: we cannot directly determine total permissioned relationship, hence re-fire query $permissionedRelationships = CRM_Contact_BAO_Relationship::getRelationship($params['contact_id'], $relationshipStatus, 0, 0, 0, @@ -1935,17 +1935,28 @@ AND cc.sort_name LIKE '%$name%'"; // format params foreach ($relationships as $relationshipId => $values) { + $relationship = array(); + + $relationship['DT_RowClass'] = 'crm-entity'; + if ($values['is_active'] == 0) { + $relationship['DT_RowClass'] .= ' disabled'; + } + + $relationship['DT_RowData'] = array(); + $relationship['DT_RowData']['entity'] = 'relationship'; + $relationship['DT_RowData']['id'] = $values['id']; + //Add image icon for related contacts: CRM-14919 $icon = CRM_Contact_BAO_Contact_Utils::getImage($values['contact_type'], FALSE, $values['cid'] ); - $contactRelationships[$relationshipId]['name'] = $icon . ' ' . CRM_Utils_System::href( + $relationship['sort_name'] = $icon . ' ' . CRM_Utils_System::href( $values['name'], 'civicrm/contact/view', "reset=1&cid={$values['cid']}"); - $contactRelationships[$relationshipId]['relation'] = CRM_Utils_System::href( + $relationship['relation'] = CRM_Utils_System::href( $values['relation'], 'civicrm/contact/view/rel', "action=view&reset=1&cid={$values['cid']}&id={$values['id']}&rtype={$values['rtype']}"); @@ -1954,32 +1965,38 @@ AND cc.sort_name LIKE '%$name%'"; if (($params['contact_id'] == $values['contact_id_a'] AND $values['is_permission_a_b'] == 1) OR ($params['contact_id'] == $values['contact_id_b'] AND $values['is_permission_b_a'] == 1) ) { - $contactRelationships[$relationshipId]['name'] .= ' *'; + $relationship['sort_name'] .= ' *'; } if (($values['cid'] == $values['contact_id_a'] AND $values['is_permission_a_b'] == 1) OR ($values['cid'] == $values['contact_id_b'] AND $values['is_permission_b_a'] == 1) ) { - $contactRelationships[$relationshipId]['relation'] .= ' *'; + $relationship['relation'] .= ' *'; } } if (!empty($values['description'])) { - $contactRelationships[$relationshipId]['relation'] .= "

{$values['description']}

"; + $relationship['relation'] .= "

{$values['description']}

"; } - $contactRelationships[$relationshipId]['start_date'] = CRM_Utils_Date::customFormat($values['start_date']); - $contactRelationships[$relationshipId]['end_date'] = CRM_Utils_Date::customFormat($values['end_date']); - $contactRelationships[$relationshipId]['city'] = $values['city']; - $contactRelationships[$relationshipId]['state'] = $values['state']; - $contactRelationships[$relationshipId]['email'] = $values['email']; - $contactRelationships[$relationshipId]['phone'] = $values['phone']; - $contactRelationships[$relationshipId]['links'] = $values['action']; - $contactRelationships[$relationshipId]['id'] = $values['id']; - $contactRelationships[$relationshipId]['is_active'] = $values['is_active']; + $relationship['start_date'] = CRM_Utils_Date::customFormat($values['start_date']); + $relationship['end_date'] = CRM_Utils_Date::customFormat($values['end_date']); + $relationship['city'] = $values['city']; + $relationship['state'] = $values['state']; + $relationship['email'] = $values['email']; + $relationship['phone'] = $values['phone']; + $relationship['links'] = $values['action']; + + array_push($contactRelationships, $relationship); } } - return $contactRelationships; + + $relationshipsDT = array(); + $relationshipsDT['data'] = $contactRelationships; + $relationshipsDT['recordsTotal'] = $params['total']; + $relationshipsDT['recordsFiltered'] = $params['total']; + + return $relationshipsDT; } } diff --git a/CRM/Contact/Page/AJAX.php b/CRM/Contact/Page/AJAX.php index 35f485a60a..558e8b043f 100644 --- a/CRM/Contact/Page/AJAX.php +++ b/CRM/Contact/Page/AJAX.php @@ -804,34 +804,23 @@ LIMIT {$offset}, {$rowCount} public static function getContactRelationships() { $contactID = CRM_Utils_Type::escape($_GET['cid'], 'Integer'); $context = CRM_Utils_Type::escape($_GET['context'], 'String'); - $relationship_type_id = CRM_Utils_Type::escape(CRM_Utils_Array::value('relationship_type_id', $_GET), 'Integer', - FALSE); + $relationship_type_id = CRM_Utils_Type::escape(CRM_Utils_Array::value('relationship_type_id', $_GET), 'Integer', FALSE); if (!CRM_Contact_BAO_Contact_Permission::allow($contactID)) { return CRM_Utils_System::permissionDenied(); } - $sortMapper = array( - 0 => 'relation', - 1 => 'sort_name', - 2 => 'start_date', - 3 => 'end_date', - 4 => 'city', - 5 => 'state', - 6 => 'email', - 7 => 'phone', - 8 => 'links', - 9 => '', - 10 => '', - ); + $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; } @@ -848,24 +837,7 @@ LIMIT {$offset}, {$rowCount} // get the contact relationships $relationships = CRM_Contact_BAO_Relationship::getContactRelationshipSelector($params); - $iFilteredTotal = $iTotal = $params['total']; - $selectorElements = array( - 'relation', - 'name', - 'start_date', - 'end_date', - 'city', - 'state', - 'email', - 'phone', - 'links', - 'id', - 'is_active', - ); - - header('Content-Type: application/json'); - echo CRM_Utils_JSON::encodeDataTableSelector($relationships, $sEcho, $iTotal, $iFilteredTotal, $selectorElements); - CRM_Utils_System::civiExit(); + CRM_Utils_JSON::output($relationships); } } diff --git a/js/Common.js b/js/Common.js index a488acc068..0ebd6daa2c 100644 --- a/js/Common.js +++ b/js/Common.js @@ -662,6 +662,34 @@ CRM.strings = CRM.strings || {}; }); }; + $.fn.crmAjaxTable = function() { + return $(this).each(function() { + //Declare the defaults for DataTables + var defaults = { + "processing": true, + "serverSide": true, + "dom": '<"crm-datatable-pager-top"lfp>rt<"crm-datatable-pager-bottom"ip>', + "pageLength": 25, + "drawCallback": function(settings) { + //Add data attributes to cells + $('thead th', settings.nTable).each( function( index ) { + $.each(this.attributes, function() { + if(this.name.match("^cell-")) { + $('tbody td:eq(' + index + ')').attr( this.name.substring(5), this.value ); + } + }); + }); + //Reload table after draw + $(settings.nTable).trigger('crmLoad'); + } + }; + //Include any table specific data + var settings = $.extend(true, defaults, $(this).data('table')); + //Make the DataTables call + $(this).DataTable(settings); + }); + }; + CRM.utils.formatSelect2Result = function (row) { var markup = '
'; if (row.image !== undefined) { @@ -824,6 +852,8 @@ CRM.strings = CRM.strings || {}; } }) .find('input.select-row:checked').parents('tr').addClass('crm-row-selected'); + $('table.crm-sortable', e.target).DataTable(); + $('table.crm-ajax-table', e.target).crmAjaxTable(); if ($("input:radio[name=radio_ts]").size() == 1) { $("input:radio[name=radio_ts]").prop("checked", true); } diff --git a/templates/CRM/Activity/Selector/Selector.tpl b/templates/CRM/Activity/Selector/Selector.tpl index 3c1cbf4880..8061466bf6 100644 --- a/templates/CRM/Activity/Selector/Selector.tpl +++ b/templates/CRM/Activity/Selector/Selector.tpl @@ -30,7 +30,7 @@ {ts}Filter by Activity Type{/ts}
-
+
{$form.activity_type_filter_id.label} {$form.activity_type_filter_id.html|crmAddClass:big}
@@ -40,126 +40,40 @@
- +
- - - - - - - - - + + + + + + + +
{ts}Type{/ts}{ts}Subject{/ts}{ts}Added By{/ts}{ts}With{/ts}{ts}Assigned{/ts}{ts}Date{/ts}{ts}Status{/ts}  {ts}Type{/ts}{ts}Subject{/ts}{ts}Added By{/ts}{ts}With{/ts}{ts}Assigned{/ts}{ts}Date{/ts}{ts}Status{/ts} 
- -{include file="CRM/Case/Form/ActivityToCase.tpl" contactID=$contactId} -{literal} - -{/literal} + $(function($) { + $('.activity-search-options :input').change(function(){ + CRM.$('.contact-activity-selector-activity').DataTable().draw(); + }); + }); + })(CRM.$); + + {/literal} + +{include file="CRM/Case/Form/ActivityToCase.tpl" contactID=$contactId} diff --git a/templates/CRM/Contact/Page/View/RelationshipSelector.tpl b/templates/CRM/Contact/Page/View/RelationshipSelector.tpl index dd17c8dc9e..787a5f43d8 100644 --- a/templates/CRM/Contact/Page/View/RelationshipSelector.tpl +++ b/templates/CRM/Contact/Page/View/RelationshipSelector.tpl @@ -26,100 +26,30 @@ {* relationship selector *}
- +
- - - - - - - - - + + + + + + + + +
{ts}Relationship{/ts} {ts}Start{/ts}{ts}End{/ts}{ts}City{/ts}{ts}State/Prov{/ts}{ts}Email{/ts}{ts}Phone{/ts}{ts}Relationship{/ts} {ts}Start{/ts}{ts}End{/ts}{ts}City{/ts}{ts}State/Prov{/ts}{ts}Email{/ts}{ts}Phone{/ts}
-
- -{literal} - -{/literal} + {literal} + + {/literal} + diff --git a/templates/CRM/common/l10n.js.tpl b/templates/CRM/common/l10n.js.tpl index c239bc79e5..544ceb234b 100644 --- a/templates/CRM/common/l10n.js.tpl +++ b/templates/CRM/common/l10n.js.tpl @@ -52,6 +52,32 @@ // Localize jQuery UI $.ui.dialog.prototype.options.closeText = "{ts escape='js'}Close{/ts}"; + // Localize jQuery DataTables + // Note the first two defaults set here aren't localization related, + // but need to be set globally for all DataTables. + $.extend( $.fn.dataTable.defaults, {ldelim} + "searching": false, + "jQueryUI": true, + "language": {ldelim} + "emptyTable": "{ts escape='js'}None found.{/ts}", + "info": "{ts escape='js' '1=_START_ 2=_END_ 3=_TOTAL_}Showing %1 to %2 of %3 entries{/ts}", + "infoEmpty": "{ts escape='js'}Showing 0 to 0 of 0 entries{/ts}", + "infoFiltered": "{ts escape='js' 1=_MAX_}(filtered from %1 total entries){/ts}", + "infoPostFix": "", + "thousands": {$config->monetaryThousandSeparator|json_encode}, + "lengthMenu": "{ts escape='js' 1=_MENU_}Show %1 entries{/ts}", + "loadingRecords": " ", + "processing": " ", + "zeroRecords": "{ts escape='js'}None found.{/ts}", + "paginate": {ldelim} + "first": "{ts escape='js'}First{/ts}", + "last": "{ts escape='js'}Last{/ts}", + "next": "{ts escape='js'}Next{/ts}", + "previous": "{ts escape='js'}Previous{/ts}" + {rdelim} + {rdelim} + {rdelim}); + // Localize strings for jQuery.validate var messages = {ldelim} required: "{ts escape='js'}This field is required.{/ts}", -- 2.25.1