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
remotes/origin/main
iglocska 2020-06-08 14:11:20 +02:00
parent 5145ccbf07
commit dccf9d5fd6
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
5 changed files with 230 additions and 280 deletions

View File

@ -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
]
]
]
]
];
}
}

View File

@ -1,24 +1,38 @@
<?php
$navdata = '';
foreach ($menu['collapse'] as $k => $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(
'<li class="nav-item active"><a class="nav-link %s" href="%s">%s</a>',
empty($menuElement['class']) ? '' : h($menuElement['class']),
empty($menuElement['url']) ? '' : h($menuElement['url']),
empty($menuElement['text']) ? '' : h($menuElement['text'])
empty($name) ? '' : h($name)
);
} else if ($menuElement['type'] === 'group') {
} else if (empty($menuElement['type']) || $menuElement['type'] === 'group') {
$navdataElements = '';
foreach ($menuElement['children'] as $child) {
if (!empty($child['type']) && $child['type'] === 'divider') {
$first = true;
foreach ($menuElement as $subCategory => $subCategoryData) {
if (!empty($subCategoryData['skipTopMenu'])) {
continue;
}
if (!$first) {
$navdataElements .= '<div class="dropdown-divider"></div>';
} else {
}
$first = false;
foreach ($subCategoryData['children'] as $child) {
if (!empty($child['skipTopMenu'])) {
continue;
}
$navdataElements .= sprintf(
'<a class="dropdown-item %s" href="%s">%s</a>',
empty($child['class']) ? '' : h($child['class']),
empty($child['url']) ? '' : h($child['url']),
empty($child['text']) ? '' : h($child['text'])
empty($child['label']) ? '' : h($child['label'])
);
}
}
@ -26,8 +40,8 @@ foreach ($menu['collapse'] as $k => $menuElement) {
'<li class="nav-item dropdown">%s%s</li>',
sprintf(
'<a class="nav-link dropdown-toggle" href="#" id="%s" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">%s</a>',
'dropdown-label-' . h($k),
h($menuElement['name'])
'dropdown-label-' . h($i),
h($name)
),
sprintf(
'<div class="dropdown-menu" aria-labelledby="navbarDropdown">%s</div>',
@ -42,9 +56,9 @@ $navdata = sprintf(
);
$homeButton = sprintf(
'<a class="navbar-brand %s" href="%s">%s</a>',
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(

View File

@ -0,0 +1,43 @@
<?php
$children = '';
if (isset($menu[$metaGroup])) {
foreach ($menu[$metaGroup] as $scope => $scopeData) {
$children .= sprintf(
'<li class="sidebar-header"><a href="%s" class="%s">%s</a></li>',
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(
'<li class="sidebar-element %s"><a href="%s" class="%s">%s</a></li>',
($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(
'<div class="side-menu-div" id="side-menu-div"><ul class="side-bar-ul" style="width:100%%;">%s</ul></div>',
$children
);

View File

@ -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]);

View File

@ -1,184 +1,2 @@
<?php
$menu = [
'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']
],
'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(
'<li class="sidebar-header"><a href="%s" class="%s">%s</a></li>',
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(
'<li class="sidebar-element %s"><a href="%s" class="%s">%s</a></li>',
($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(
'<div class="side-menu-div" id="side-menu-div"><ul class="side-bar-ul" style="width:100%%;">%s</ul></div>',
$children
);
echo $this->element('genericElements/side_menu_scaffold', ['menu' => $menu]);