From 656e5c5b52e949791a90294e4be8cf43b7d39a9a Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Fri, 24 Feb 2017 11:17:35 -0700 Subject: [PATCH] CRM-20179 - Upgrade jstree on navigation admin screen --- CRM/Admin/Form/Navigation.php | 2 +- CRM/Admin/Page/AJAX.php | 8 - CRM/Admin/Page/Navigation.php | 4 +- CRM/Core/BAO/Navigation.php | 218 ++++++++---------------- CRM/Core/xml/Menu/Admin.xml | 6 - css/civicrmNavigation.css | 3 +- templates/CRM/Admin/Page/Navigation.tpl | 179 +++++++++---------- templates/CRM/Tag/Page/Tag.tpl | 4 - 8 files changed, 153 insertions(+), 271 deletions(-) diff --git a/CRM/Admin/Form/Navigation.php b/CRM/Admin/Form/Navigation.php index 37c0822536..1441b7d7a0 100644 --- a/CRM/Admin/Form/Navigation.php +++ b/CRM/Admin/Form/Navigation.php @@ -106,7 +106,7 @@ class CRM_Admin_Form_Navigation extends CRM_Admin_Form { * @return array */ public function setDefaultValues() { - $defaults = $this->_defaults; + $defaults = parent::setDefaultValues(); if (isset($this->_id)) { //Take parent id in object variable to calculate the menu //weight if menu parent id changed diff --git a/CRM/Admin/Page/AJAX.php b/CRM/Admin/Page/AJAX.php index ebf77802a8..35f5f2aabd 100644 --- a/CRM/Admin/Page/AJAX.php +++ b/CRM/Admin/Page/AJAX.php @@ -54,14 +54,6 @@ class CRM_Admin_Page_AJAX { CRM_Utils_System::civiExit(); } - /** - * Return menu tree as json data for editing. - */ - public static function getNavigationList() { - echo CRM_Core_BAO_Navigation::buildNavigation(TRUE, FALSE); - CRM_Utils_System::civiExit(); - } - /** * Process drag/move action for menu tree. */ diff --git a/CRM/Admin/Page/Navigation.php b/CRM/Admin/Page/Navigation.php index 22561bee50..d22d8be7cd 100644 --- a/CRM/Admin/Page/Navigation.php +++ b/CRM/Admin/Page/Navigation.php @@ -105,8 +105,8 @@ class CRM_Admin_Page_Navigation extends CRM_Core_Page_Basic { // Add jstree support CRM_Core_Resources::singleton() - ->addScriptFile('civicrm', 'packages/jquery/plugins/jstree/jquery.jstree.js', 0, 'html-header', FALSE) - ->addStyleFile('civicrm', 'packages/jquery/plugins/jstree/themes/default/style.css', 0, 'html-header'); + ->addScriptFile('civicrm', 'bower_components/jstree/dist/jstree.min.js', 0, 'html-header') + ->addStyleFile('civicrm', 'bower_components/jstree/dist/themes/default/style.min.css'); } } diff --git a/CRM/Core/BAO/Navigation.php b/CRM/Core/BAO/Navigation.php index 82cd56bbe2..a4ae608bbd 100644 --- a/CRM/Core/BAO/Navigation.php +++ b/CRM/Core/BAO/Navigation.php @@ -264,45 +264,25 @@ FROM civicrm_navigation WHERE domain_id = $domainID {$whereClause} ORDER BY pare /** * Build navigation tree. * - * @param array $navigationTree - * Nested array of menus. - * @param int $parentID - * Parent id. - * @param bool $navigationMenu - * True when called for building top navigation menu. - * * @return array * nested array of menus */ - public static function buildNavigationTree(&$navigationTree, $parentID, $navigationMenu = TRUE) { - $whereClause = " parent_id IS NULL"; - - if ($parentID) { - $whereClause = " parent_id = {$parentID}"; - } - + public static function buildNavigationTree() { $domainID = CRM_Core_Config::domainID(); + $navigationTree = array(); // get the list of menus $query = " SELECT id, label, url, permission, permission_operator, has_separator, parent_id, is_active, name FROM civicrm_navigation -WHERE {$whereClause} -AND domain_id = $domainID +WHERE domain_id = $domainID ORDER BY parent_id, weight"; $navigation = CRM_Core_DAO::executeQuery($query); - $config = CRM_Core_Config::singleton(); while ($navigation->fetch()) { - $label = $navigation->label; - if (!$navigationMenu) { - $label = addcslashes($label, '"'); - } - - // for each menu get their children $navigationTree[$navigation->id] = array( 'attributes' => array( - 'label' => $label, + 'label' => $navigation->label, 'name' => $navigation->name, 'url' => $navigation->url, 'permission' => $navigation->permission, @@ -313,75 +293,69 @@ ORDER BY parent_id, weight"; 'active' => $navigation->is_active, ), ); - self::buildNavigationTree($navigationTree[$navigation->id]['child'], $navigation->id, $navigationMenu); } - return $navigationTree; + return self::buildTree($navigationTree); } /** - * Build menu. + * Convert flat array to nested. + * + * @param array $elements + * @param int|null $parentId * - * @param bool $json - * By default output is html. - * @param bool $navigationMenu - * True when called for building top navigation menu. + * @return array + */ + private static function buildTree($elements, $parentId = NULL) { + $branch = array(); + + foreach ($elements as $id => $element) { + if ($element['attributes']['parentID'] == $parentId) { + $children = self::buildTree($elements, $id); + if ($children) { + $element['child'] = $children; + } + $branch[$id] = $element; + } + } + + return $branch; + } + + /** + * Build menu. * * @return string - * html or json string */ - public static function buildNavigation($json = FALSE, $navigationMenu = TRUE) { - $navigations = array(); - self::buildNavigationTree($navigations, $parent = NULL, $navigationMenu); - $navigationString = NULL; + public static function buildNavigation() { + $navigations = self::buildNavigationTree(); + $navigationString = ''; // run the Navigation through a hook so users can modify it CRM_Utils_Hook::navigationMenu($navigations); self::fixNavigationMenu($navigations); - $i18n = CRM_Core_I18n::singleton(); - //skip children menu item if user don't have access to parent menu item $skipMenuItems = array(); foreach ($navigations as $key => $value) { - if ($json) { - if ($navigationString) { - $navigationString .= '},'; - } - $data = $value['attributes']['label']; - $class = ''; - if (!$value['attributes']['active']) { - $class = ', "attr": { "class" : "disabled"} '; - } - $l10nName = $i18n->crm_translate($data, array('context' => 'menu')); - $navigationString .= ' { "attr": { "id" : "node_' . $key . '"}, "data": { "title":"' . $l10nName . '"' . $class . '}'; - } - else { - // Home is a special case - if ($value['attributes']['name'] != 'Home') { - $name = self::getMenuName($value, $skipMenuItems); - if ($name) { - //separator before - if (isset($value['attributes']['separator']) && $value['attributes']['separator'] == 2) { - $navigationString .= ''; - } - $removeCharacters = array('/', '!', '&', '*', ' ', '(', ')', '.'); - $navigationString .= ''; } + $removeCharacters = array('/', '!', '&', '*', ' ', '(', ')', '.'); + $navigationString .= '