From 3b448ecab2b6a90bdb219836f85b43601f599ab1 Mon Sep 17 00:00:00 2001 From: Joseph Lacey Date: Tue, 15 Dec 2015 12:58:02 -0500 Subject: [PATCH] CRM-16353 - Migrating the Manage Groups list to DataTables API v2 ---------------------------------------- * CRM-16353: Upgrade datatables integration to use v2 api https://issues.civicrm.org/jira/browse/CRM-16353 --- CRM/Contact/BAO/Group.php | 82 ++++---- CRM/Group/Page/AJAX.php | 43 ++--- templates/CRM/Group/Form/Search.tpl | 279 ++++++++++------------------ 3 files changed, 161 insertions(+), 243 deletions(-) diff --git a/CRM/Contact/BAO/Group.php b/CRM/Contact/BAO/Group.php index dd44875a45..790a5759b3 100644 --- a/CRM/Contact/BAO/Group.php +++ b/CRM/Contact/BAO/Group.php @@ -732,41 +732,61 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group { // format params and add links $groupList = array(); - if (!empty($groups)) { - foreach ($groups as $id => $value) { - $groupList[$id]['group_id'] = $value['id']; - $groupList[$id]['count'] = $value['count']; - $groupList[$id]['group_name'] = $value['title']; - - // append parent names if in search mode - if (empty($params['parent_id']) && !empty($value['parents'])) { - $groupIds = explode(',', $value['parents']); - $title = array(); - foreach ($groupIds as $gId) { - $title[] = $allGroups[$gId]; + foreach ($groups as $id => $value) { + $group = array(); + $group['group_id'] = $value['id']; + $group['count'] = $value['count']; + $group['title'] = $value['title']; + + // append parent names if in search mode + if (empty($params['parent_id']) && !empty($value['parents'])) { + $group['parent_id'] = $value['parents']; + $groupIds = explode(',', $value['parents']); + $title = array(); + foreach ($groupIds as $gId) { + $title[] = $allGroups[$gId]; + } + $group['title'] .= '
' . ts('Child of') . ': ' . implode(', ', $title) . '
'; + $value['class'] = array_diff($value['class'], array('crm-row-parent')); + } + $group['DT_RowId'] = 'row_' . $value['id']; + if (!$params['parentsOnly']) { + foreach($value['class'] as $id => $class) { + if ($class = 'crm-group-parent') { + unset($value['class'][$id]); } - $groupList[$id]['group_name'] .= '
' . ts('Child of') . ': ' . implode(', ', $title) . '
'; - $value['class'] = array_diff($value['class'], array('crm-row-parent')); } - $value['class'][] = 'crm-entity'; - $groupList[$id]['class'] = $value['id'] . ',' . implode(' ', $value['class']); + } + $group['DT_RowClass'] = 'crm-entity ' . implode(' ', $value['class']); + $group['DT_RowAttr'] = array(); + $group['DT_RowAttr']['data-id'] = $value['id']; + $group['DT_RowAttr']['data-entity'] = 'group'; - $groupList[$id]['group_description'] = CRM_Utils_Array::value('description', $value); - if (!empty($value['group_type'])) { - $groupList[$id]['group_type'] = $value['group_type']; - } - else { - $groupList[$id]['group_type'] = ''; - } - $groupList[$id]['visibility'] = $value['visibility']; - $groupList[$id]['links'] = $value['action']; - $groupList[$id]['org_info'] = CRM_Utils_Array::value('org_info', $value); - $groupList[$id]['created_by'] = CRM_Utils_Array::value('created_by', $value); + $group['description'] = CRM_Utils_Array::value('description', $value); - $groupList[$id]['is_parent'] = $value['is_parent']; + if (!empty($value['group_type'])) { + $group['group_type'] = $value['group_type']; + } + else { + $group['group_type'] = ''; } - return $groupList; + + $group['visibility'] = $value['visibility']; + $group['links'] = $value['action']; + $group['org_info'] = CRM_Utils_Array::value('org_info', $value); + $group['created_by'] = CRM_Utils_Array::value('created_by', $value); + + $group['is_parent'] = $value['is_parent']; + + array_push($groupList, $group); } + + $groupsDT = array(); + $groupsDT['data'] = $groupList; + $groupsDT['recordsTotal'] = $params['total']; + $groupsDT['recordsFiltered'] = $params['total']; + + return $groupsDT; } /** @@ -864,10 +884,6 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group { 'count' => '0', ); CRM_Core_DAO::storeValues($object, $values[$object->id]); - // Wrap with crm-editable. Not an ideal solution. - if (in_array(CRM_Core_Permission::EDIT, $groupPermissions)) { - $values[$object->id]['title'] = '' . $values[$object->id]['title'] . ''; - } if ($object->saved_search_id) { $values[$object->id]['title'] .= ' (' . ts('Smart Group') . ')'; diff --git a/CRM/Group/Page/AJAX.php b/CRM/Group/Page/AJAX.php index e6c3e7c162..47ca4f49ad 100644 --- a/CRM/Group/Page/AJAX.php +++ b/CRM/Group/Page/AJAX.php @@ -42,7 +42,7 @@ class CRM_Group_Page_AJAX { * @return array */ public static function getGroupList() { - $params = $_REQUEST; + $params = $_GET; if (isset($params['parent_id'])) { // requesting child groups for a given parent @@ -53,20 +53,16 @@ class CRM_Group_Page_AJAX { CRM_Utils_JSON::output($groups); } else { - $sortMapper = array( - 0 => 'groups.title', - 1 => 'count', - 2 => 'createdBy.sort_name', - 3 => '', - 4 => 'groups.group_type', - 5 => 'groups.visibility', - ); - $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'; + $sortMapper = array(); + foreach ($_GET['columns'] as $key => $value) { + $sortMapper[$key] = $value['data']; + }; + + $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'; if ($sort && $sortOrder) { $params['sortBy'] = $sort . ' ' . $sortOrder; @@ -90,31 +86,18 @@ class CRM_Group_Page_AJAX { } } - $iFilteredTotal = $iTotal = $params['total']; - $selectorElements = array( - 'group_name', - 'count', - 'created_by', - 'group_description', - 'group_type', - 'visibility', - 'org_info', - 'links', - 'class', - ); - + /* if (empty($params['showOrgInfo'])) { unset($selectorElements[6]); } + */ //add setting so this can be tested by unit test //@todo - ideally the portion of this that retrieves the groups should be extracted into a function separate // from the one which deals with web inputs & outputs so we have a properly testable & re-usable function if (!empty($params['is_unit_test'])) { return array($groups, $iFilteredTotal); } - CRM_Utils_System::setHttpHeader('Content-Type', 'application/json'); - echo CRM_Utils_JSON::encodeDataTableSelector($groups, $sEcho, $iTotal, $iFilteredTotal, $selectorElements); - CRM_Utils_System::civiExit(); + CRM_Utils_JSON::output($groups); } } diff --git a/templates/CRM/Group/Form/Search.tpl b/templates/CRM/Group/Form/Search.tpl index 1eeaebdd51..9b67bb5496 100644 --- a/templates/CRM/Group/Form/Search.tpl +++ b/templates/CRM/Group/Form/Search.tpl @@ -61,28 +61,24 @@ {$form.group_status.html} - - {$form.buttons.html} -
{ts}Update Smart Group Counts{/ts} {help id="update_smart_groups"}
- +
- - - - - - + + + + + + {if $showOrgInfo} - + {/if} - - +
{ts}Name{/ts}{ts}Count{/ts}{ts}Created By{/ts}{ts}Description{/ts}{ts}Group Type{/ts}{ts}Visibility{/ts}{ts}Name{/ts}{ts}Count{/ts}{ts}Created By{/ts}{ts}Description{/ts}{ts}Group Type{/ts}{ts}Visibility{/ts}{ts}Organization{/ts}{ts}Organization{/ts}   
@@ -92,160 +88,85 @@ {literal} {/literal} -- 2.25.1