From dccf9d5fd6c1ad13d5983a5c13ef41456551aa71 Mon Sep 17 00:00:00 2001 From: iglocska Date: Mon, 8 Jun 2020 14:11:20 +0200 Subject: [PATCH] new: [menu] centralised top and side menu - single source, top menu uses masked version of side menu - constructor in AppTables, making it available across the application --- app/src/Model/Table/AppTable.php | 157 +++++++++++++++ .../genericElements/header_scaffold.php | 40 ++-- .../genericElements/side_menu_scaffold.php | 43 ++++ app/templates/element/header.php | 86 +------- app/templates/element/side_menu.php | 184 +----------------- 5 files changed, 230 insertions(+), 280 deletions(-) create mode 100644 app/templates/element/genericElements/side_menu_scaffold.php diff --git a/app/src/Model/Table/AppTable.php b/app/src/Model/Table/AppTable.php index 09a5669..14921eb 100644 --- a/app/src/Model/Table/AppTable.php +++ b/app/src/Model/Table/AppTable.php @@ -10,4 +10,161 @@ class AppTable extends Table public function initialize(array $config): void { } + + public function getMenu() + { + return [ + 'ContactDB' => [ + 'Individuals' => [ + 'label' => __('Individuals'), + 'url' => '/individuals/index', + 'children' => [ + 'index' => [ + 'url' => '/individuals/index', + 'label' => __('List individuals') + ], + 'add' => [ + 'url' => '/individuals/add', + 'label' => __('Add individual') + ], + 'view' => [ + 'url' => '/individuals/view/{{id}}', + 'label' => __('View individual'), + 'actions' => ['delete', 'edit', 'view'], + 'skipTopMenu' => 1 + ], + 'edit' => [ + 'url' => '/individuals/edit/{{id}}', + 'label' => __('Edit individual'), + 'actions' => ['edit', 'delete', 'view'], + 'skipTopMenu' => 1 + ], + 'delete' => [ + 'url' => '/individuals/delete/{{id}}', + 'label' => __('Delete individual'), + 'actions' => ['delete', 'edit', 'view'], + 'skipTopMenu' => 1 + ] + ] + ], + 'Organisations' => [ + 'label' => __('Organisations'), + 'url' => '/organisations/index', + 'children' => [ + 'index' => [ + 'url' => '/organisations/index', + 'label' => __('List organisations') + ], + 'add' => [ + 'url' => '/organisations/add', + 'label' => __('Add organisation') + ], + 'view' => [ + 'url' => '/organisations/view/{{id}}', + 'label' => __('View organisation'), + 'actions' => ['delete', 'edit', 'view'], + 'skipTopMenu' => 1 + ], + 'edit' => [ + 'url' => '/organisations/edit/{{id}}', + 'label' => __('Edit organisation'), + 'actions' => ['edit', 'delete', 'view'], + 'skipTopMenu' => 1 + ], + 'delete' => [ + 'url' => '/organisations/delete/{{id}}', + 'label' => __('Delete organisation'), + 'actions' => ['delete', 'edit', 'view'], + 'skipTopMenu' => 1 + ] + ] + ] + ], + 'Administration' => [ + 'Roles' => [ + 'label' => __('Roles'), + 'url' => '/roles/index', + 'children' => [ + 'index' => [ + 'url' => '/roles/index', + 'label' => __('List roles') + ], + 'add' => [ + 'url' => '/roles/add', + 'label' => __('Add role') + ], + 'view' => [ + 'url' => '/roles/view/{{id}}', + 'label' => __('View role'), + 'actions' => ['delete', 'edit', 'view'], + 'skipTopMenu' => 1 + ], + 'edit' => [ + 'url' => '/roles/edit/{{id}}', + 'label' => __('Edit role'), + 'actions' => ['edit', 'delete', 'view'], + 'skipTopMenu' => 1 + ], + 'delete' => [ + 'url' => '/roles/delete/{{id}}', + 'label' => __('Delete role'), + 'actions' => ['delete', 'edit', 'view'], + 'skipTopMenu' => 1 + ] + ] + ], + 'Users' => [ + 'label' => __('Users'), + 'url' => '/users/index', + 'children' => [ + 'index' => [ + 'url' => '/users/index', + 'label' => __('List users') + ], + 'add' => [ + 'url' => '/users/add', + 'label' => __('Add user') + ], + 'view' => [ + 'url' => '/users/view/{{id}}', + 'label' => __('View user'), + 'actions' => ['delete', 'edit', 'view'], + 'skipTopMenu' => 1 + ], + 'edit' => [ + 'url' => '/users/edit/{{id}}', + 'label' => __('Edit user'), + 'actions' => ['edit', 'delete', 'view'], + 'skipTopMenu' => 1 + ], + 'delete' => [ + 'url' => '/users/delete/{{id}}', + 'label' => __('Delete user'), + 'actions' => ['delete', 'edit', 'view'], + 'skipTopMenu' => 1 + ] + ] + ] + ], + 'Cerebrate' => [ + 'Roles' => [ + 'label' => __('Roles'), + 'url' => '/roles/index', + 'children' => [ + 'index' => [ + 'url' => '/roles/index', + 'label' => __('List roles') + ], + 'view' => [ + 'url' => '/roles/view/{{id}}', + 'label' => __('View role'), + 'actions' => ['delete', 'edit', 'view'], + 'skipTopMenu' => 1 + ] + ] + ] + ] + + ]; + } } diff --git a/app/templates/element/genericElements/header_scaffold.php b/app/templates/element/genericElements/header_scaffold.php index e07cb58..c14ecd8 100644 --- a/app/templates/element/genericElements/header_scaffold.php +++ b/app/templates/element/genericElements/header_scaffold.php @@ -1,24 +1,38 @@ $menuElement) { - if ($menuElement['type'] === 'single') { +$i = 0; +foreach ($data['menu'] as $name => $menuElement) { + $i++; + if (!empty($menuElement['skipTopMenu'])) { + continue; + } + if (!empty($menuElement['type']) === 'single' && $menuElement['type'] === 'single') { $navdata .= sprintf( '', sprintf( '', - 'dropdown-label-' . h($k), - h($menuElement['name']) + 'dropdown-label-' . h($i), + h($name) ), sprintf( '', @@ -42,9 +56,9 @@ $navdata = sprintf( ); $homeButton = sprintf( '%s', - empty($menu['home']['class']) ? '' : h($menu['home']['class']), - empty($menu['home']['url']) ? '' : h($menu['home']['url']), - empty($menu['home']['text']) ? '' : h($menu['home']['text']) + empty($data['home']['class']) ? '' : h($data['home']['class']), + empty($data['home']['url']) ? '' : h($data['home']['url']), + empty($data['home']['text']) ? '' : h($data['home']['text']) ); echo sprintf( diff --git a/app/templates/element/genericElements/side_menu_scaffold.php b/app/templates/element/genericElements/side_menu_scaffold.php new file mode 100644 index 0000000..ac9c224 --- /dev/null +++ b/app/templates/element/genericElements/side_menu_scaffold.php @@ -0,0 +1,43 @@ + $scopeData) { + $children .= sprintf( + '', + empty($scopeData['url']) ? '#' : h($scopeData['url']), + empty($scopeData['class']) ? '' : h($scopeData['class']), + empty($scopeData['label']) ? h($scope) : $scopeData['label'] + ); + foreach ($scopeData['children'] as $action => $data) { + if ( + (!empty($data['requirements']) && !$data['requirements']) || + ( + !empty($data['actions']) && + !in_array($this->request->getParam('action'), $data['actions']) + ) || + !empty($data['actions']) && $scope !== $this->request->getParam('controller') + ) { + continue; + } + $matches = []; + preg_match_all('/\{\{.*?\}\}/', $data['url'], $matches); + if (!empty($matches[0])) { + $mainEntity = \Cake\Utility\Inflector::underscore(\Cake\Utility\Inflector::singularize($scope)); + foreach ($matches as $match) { + $data['url'] = str_replace($match[0], ${$mainEntity}[substr($match[0], 2, 2)], $data['url']); + } + } + $children .= sprintf( + '', + ($scope === $this->request->getParam('controller') && $action === $this->request->getParam('action')) ? 'active' : '', + empty($data['url']) ? '#' : h($data['url']), + empty($data['class']) ? '' : h($data['class']), + empty($data['label']) ? h($action) : $data['label'] + ); + } + } +} +echo sprintf( + '
', + $children +); diff --git a/app/templates/element/header.php b/app/templates/element/header.php index e526e1e..e891982 100644 --- a/app/templates/element/header.php +++ b/app/templates/element/header.php @@ -5,88 +5,6 @@ 'class' => 'navbar-brand', 'text' => 'Cerebrate' ), - 'collapse' => array( - array( - 'type' => 'group', - 'name' => 'ContactDB', - 'children' => array( - array( - 'text' => __('List Organisations'), - 'url' => '/organisations/index' - ), - array( - 'text' => __('Add Organisation'), - 'url' => '/organisations/add' - ), - array( - 'type' => 'divider' - ), - array( - 'text' => __('List Individuals'), - 'url' => '/individuals/index' - ), - array( - 'text' => __('Add Individual'), - 'url' => '/individuals/add' - ), - ) - ), - array( - 'type' => 'group', - 'name' => 'Trust Circles', - 'children' => array( - array( - 'text' => __('List Sharing Groups'), - 'url' => '/sharing_groups/index' - ), - array( - 'text' => __('Add Sharing Group'), - 'url' => '/sharing_groups/add' - ) - ) - ), - array( - 'type' => 'group', - 'name' => __('Toolbox'), - 'children' => array( - array( - 'text' => __('List All'), - 'url' => '/tools/index' - ), - array( - 'text' => __('Add Tool'), - 'url' => '/tools/add' - ) - ) - ), - array( - 'type' => 'group', - 'name' => __('Admnistration'), - 'children' => array( - array( - 'text' => __('List Sharing Groups'), - 'url' => '/sharing_groups/index' - ), - array( - 'text' => __('Add Sharing Group'), - 'url' => '/sharing_groups/add' - ) - ) - ), - array( - 'type' => 'group', - 'name' => __('My Profile'), - 'children' => array( - array( - 'text' => __('View My Profile'), - 'url' => '/users/view/me' - ), - array( - 'text' => __('Modify My Profile'), - 'url' => '/users/edit/me' - ) - ) - ) - ) + 'menu' => $menu ); - echo $this->element('genericElements/header_scaffold', ['menu' => $menu]); + echo $this->element('genericElements/header_scaffold', ['data' => $menu]); diff --git a/app/templates/element/side_menu.php b/app/templates/element/side_menu.php index e2506d5..fb74fd4 100644 --- a/app/templates/element/side_menu.php +++ b/app/templates/element/side_menu.php @@ -1,184 +1,2 @@ [ - 'Individuals' => [ - 'label' => __('Individuals'), - 'url' => '/individuals/index', - 'children' => [ - 'index' => [ - 'url' => '/individuals/index', - 'label' => __('List individuals') - ], - 'add' => [ - 'url' => '/individuals/add', - 'label' => __('Add individual') - ], - 'view' => [ - 'url' => '/individuals/view/{{id}}', - 'label' => __('View individual'), - 'actions' => ['delete', 'edit', 'view'] - ], - 'edit' => [ - 'url' => '/individuals/edit/{{id}}', - 'label' => __('Edit individual'), - 'actions' => ['edit', 'delete', 'view'] - ], - 'delete' => [ - 'url' => '/individuals/delete/{{id}}', - 'label' => __('Delete individual'), - 'actions' => ['delete', 'edit', 'view'] - ] - ] - ], - 'Organisations' => [ - 'label' => __('Organisations'), - 'url' => '/organisations/index', - 'children' => [ - 'index' => [ - 'url' => '/organisations/index', - 'label' => __('List organisations') - ], - 'add' => [ - 'url' => '/organisations/add', - 'label' => __('Add organisation') - ], - 'view' => [ - 'url' => '/organisations/view/{{id}}', - 'label' => __('View organisation'), - 'actions' => ['delete', 'edit', 'view'] - ], - 'edit' => [ - 'url' => '/organisations/edit/{{id}}', - 'label' => __('Edit organisation'), - 'actions' => ['edit', 'delete', 'view'] - ], - 'delete' => [ - 'url' => '/organisations/delete/{{id}}', - 'label' => __('Delete organisation'), - 'actions' => ['delete', 'edit', 'view'] - ] - ] - ] - ], - 'Administration' => [ - 'Roles' => [ - 'label' => __('Roles'), - 'url' => '/roles/index', - 'children' => [ - 'index' => [ - 'url' => '/roles/index', - 'label' => __('List roles') - ], - 'add' => [ - 'url' => '/roles/add', - 'label' => __('Add role') - ], - 'view' => [ - 'url' => '/roles/view/{{id}}', - 'label' => __('View role'), - 'actions' => ['delete', 'edit', 'view'] - ], - 'edit' => [ - 'url' => '/roles/edit/{{id}}', - 'label' => __('Edit role'), - 'actions' => ['edit', 'delete', 'view'] - ], - 'delete' => [ - 'url' => '/roles/delete/{{id}}', - 'label' => __('Delete role'), - 'actions' => ['delete', 'edit', 'view'] - ] - ] - ], - 'Users' => [ - 'label' => __('Users'), - 'url' => '/users/index', - 'children' => [ - 'index' => [ - 'url' => '/users/index', - 'label' => __('List users') - ], - 'add' => [ - 'url' => '/users/add', - 'label' => __('Add user') - ], - 'view' => [ - 'url' => '/users/view/{{id}}', - 'label' => __('View user'), - 'actions' => ['delete', 'edit', 'view'] - ], - 'edit' => [ - 'url' => '/users/edit/{{id}}', - 'label' => __('Edit user'), - 'actions' => ['edit', 'delete', 'view'] - ], - 'delete' => [ - 'url' => '/users/delete/{{id}}', - 'label' => __('Delete user'), - 'actions' => ['delete', 'edit', 'view'] - ] - ] - ] - ], - 'Cerebrate' => [ - 'Roles' => [ - 'label' => __('Roles'), - 'url' => '/roles/index', - 'children' => [ - 'index' => [ - 'url' => '/roles/index', - 'label' => __('List roles') - ], - 'view' => [ - 'url' => '/roles/view/{{id}}', - 'label' => __('View role'), - 'actions' => ['delete', 'edit', 'view'] - ] - ] - ] - ] - -]; -$children = ''; -if (isset($menu[$metaGroup])) { - foreach ($menu[$metaGroup] as $scope => $scopeData) { - $children .= sprintf( - '', - empty($scopeData['url']) ? '#' : h($scopeData['url']), - empty($scopeData['class']) ? '' : h($scopeData['class']), - empty($scopeData['label']) ? h($scope) : $scopeData['label'] - ); - foreach ($scopeData['children'] as $action => $data) { - if ( - (!empty($data['requirements']) && !$data['requirements']) || - ( - !empty($data['actions']) && - !in_array($this->request->getParam('action'), $data['actions']) - ) || - !empty($data['actions']) && $scope !== $this->request->getParam('controller') - ) { - continue; - } - $matches = []; - preg_match_all('/\{\{.*?\}\}/', $data['url'], $matches); - if (!empty($matches[0])) { - $mainEntity = \Cake\Utility\Inflector::underscore(\Cake\Utility\Inflector::singularize($scope)); - foreach ($matches as $match) { - $data['url'] = str_replace($match[0], ${$mainEntity}[substr($match[0], 2, 2)], $data['url']); - } - } - $children .= sprintf( - '', - ($scope === $this->request->getParam('controller') && $action === $this->request->getParam('action')) ? 'active' : '', - empty($data['url']) ? '#' : h($data['url']), - empty($data['class']) ? '' : h($data['class']), - empty($data['label']) ? h($action) : $data['label'] - ); - } - } -} -echo sprintf( - '
', - $children -); +echo $this->element('genericElements/side_menu_scaffold', ['menu' => $menu]);