new: [sidebar:bookmarks] Added early version of user-defined bookmarks
Bookmark configs are saved in their respective user setting for each userspull/72/head
parent
7fbf83bf49
commit
29ca08ce60
|
@ -457,6 +457,9 @@ class ACLComponent extends Component
|
||||||
{
|
{
|
||||||
$menu = $this->Navigation->getSideMenu();
|
$menu = $this->Navigation->getSideMenu();
|
||||||
foreach ($menu as $group => $subMenu) {
|
foreach ($menu as $group => $subMenu) {
|
||||||
|
if ($group == '__bookmarks') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
foreach ($subMenu as $subMenuElementName => $subMenuElement) {
|
foreach ($subMenu as $subMenuElementName => $subMenuElement) {
|
||||||
if (!empty($subMenuElement['url']) && !$this->checkAccessUrl($subMenuElement['url'], true) === true) {
|
if (!empty($subMenuElement['url']) && !$this->checkAccessUrl($subMenuElement['url'], true) === true) {
|
||||||
unset($menu[$group][$subMenuElementName]);
|
unset($menu[$group][$subMenuElementName]);
|
||||||
|
|
|
@ -50,9 +50,37 @@ class NavigationComponent extends Component
|
||||||
public function getSideMenu(): array
|
public function getSideMenu(): array
|
||||||
{
|
{
|
||||||
$sidemenu = new Sidemenu($this->iconToTableMapping, $this->request);
|
$sidemenu = new Sidemenu($this->iconToTableMapping, $this->request);
|
||||||
return $sidemenu->get();
|
$sidemenu = $sidemenu->get();
|
||||||
|
$sidemenu = $this->addUserBookmarks($sidemenu);
|
||||||
|
return $sidemenu;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function addUserBookmarks($sidemenu): array
|
||||||
|
{
|
||||||
|
$bookmarks = $this->getUserBookmarks();
|
||||||
|
$sidemenu = array_merge([
|
||||||
|
'__bookmarks' => $bookmarks
|
||||||
|
], $sidemenu);
|
||||||
|
return $sidemenu;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getUserBookmarks(): array
|
||||||
|
{
|
||||||
|
$userSettingTable = TableRegistry::getTableLocator()->get('UserSettings');
|
||||||
|
$setting = $userSettingTable->getSettingByName($this->request->getAttribute('identity'), 'ui.sidebar.bookmarks');
|
||||||
|
$bookmarks = is_null($setting) ? [] : json_decode($setting->value, true);
|
||||||
|
|
||||||
|
$links = array_map(function($bookmark) {
|
||||||
|
return [
|
||||||
|
'name' => $bookmark['name'],
|
||||||
|
'label' => $bookmark['label'],
|
||||||
|
'url' => $bookmark['url'],
|
||||||
|
];
|
||||||
|
}, $bookmarks);
|
||||||
|
return $links;
|
||||||
|
}
|
||||||
|
|
||||||
public function getBreadcrumb(): array
|
public function getBreadcrumb(): array
|
||||||
{
|
{
|
||||||
$controller = $this->request->getParam('controller');
|
$controller = $this->request->getParam('controller');
|
||||||
|
|
|
@ -136,4 +136,20 @@ class UserSettingsController extends AppController
|
||||||
}
|
}
|
||||||
$this->set('settingName', $settingsName);
|
$this->set('settingName', $settingsName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function saveBookmark()
|
||||||
|
{
|
||||||
|
if (!$this->request->is('get')) {
|
||||||
|
$result = $this->UserSettings->saveBookmark($this->ACL->getUser(), $this->request->getData());
|
||||||
|
$success = !empty($result);
|
||||||
|
$message = $success ? __('Bookmark saved') : __('Could not save bookmark');
|
||||||
|
$this->CRUD->setResponseForController('saveBookmark', $success, $message, $result);
|
||||||
|
$responsePayload = $this->CRUD->getResponsePayload();
|
||||||
|
if (!empty($responsePayload)) {
|
||||||
|
return $responsePayload;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->set('user_id', $this->ACL->getUser()->id);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -57,3 +57,25 @@ class UserSettingsTable extends AppTable
|
||||||
$savedData = $this->save($setting);
|
$savedData = $this->save($setting);
|
||||||
return $savedData;
|
return $savedData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function saveBookmark($user, $data)
|
||||||
|
{
|
||||||
|
$bookmarkSettingName = 'ui.sidebar.bookmarks';
|
||||||
|
$setting = $this->getSettingByName($user, $bookmarkSettingName);
|
||||||
|
$bookmarkData = [
|
||||||
|
'label' => $data['bookmark_label'],
|
||||||
|
'name' => $data['bookmark_name'],
|
||||||
|
'url' => $data['bookmark_url'],
|
||||||
|
];
|
||||||
|
if (is_null($setting)) { // setting not found, create it
|
||||||
|
$bookmarksData = json_encode([$bookmarkData]);
|
||||||
|
$result = $this->createSetting($user, $bookmarkSettingName, $bookmarksData);
|
||||||
|
} else {
|
||||||
|
$bookmarksData = json_decode($setting->value);
|
||||||
|
$bookmarksData[] = $bookmarkData;
|
||||||
|
$bookmarksData = json_encode($bookmarksData);
|
||||||
|
$result = $this->editSetting($user, $bookmarkSettingName, $bookmarksData);
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
<?php
|
||||||
|
echo $this->element('genericElements/Form/genericForm', [
|
||||||
|
'data' => [
|
||||||
|
'title' => __('Add a bookmark'),
|
||||||
|
'description' => __('Specify the name and the URL of the bookmark which will be tied to your user profile.'),
|
||||||
|
'model' => 'UserSettings',
|
||||||
|
'fields' => [
|
||||||
|
[
|
||||||
|
'field' => 'bookmark_name',
|
||||||
|
'label' => __('Name'),
|
||||||
|
'placeholder' => __('Home page'),
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'field' => 'bookmark_label',
|
||||||
|
'label' => __('Label'),
|
||||||
|
'placeholder' => __('Home'),
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'field' => 'bookmark_url',
|
||||||
|
'label' => __('URL'),
|
||||||
|
'placeholder' => '/instance/home',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'submit' => [
|
||||||
|
'action' => $this->request->getParam('action')
|
||||||
|
]
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
?>
|
||||||
|
</div>
|
|
@ -2,19 +2,32 @@
|
||||||
<div class="sidebar-scroll">
|
<div class="sidebar-scroll">
|
||||||
<div class="sidebar-content">
|
<div class="sidebar-content">
|
||||||
<ul class="sidebar-elements">
|
<ul class="sidebar-elements">
|
||||||
<?php foreach ($menu as $category => $categorized): ?>
|
<?php foreach ($menu as $category => $categorized) : ?>
|
||||||
<?= $this->element('layouts/sidebar/category', ['label' => $category]) ?>
|
<?php if ($category == '__bookmarks') : ?>
|
||||||
<?php foreach ($categorized as $parentName => $parent): ?>
|
<?= $this->element('layouts/sidebar/category', ['label' => __('Bookmarks'), 'class' => 'bookmark-categ']) ?>
|
||||||
<?= $this->element('layouts/sidebar/entry', [
|
<li class="bookmarks">
|
||||||
|
<?php foreach ($categorized as $parentName => $entry) : ?>
|
||||||
|
<?= $this->element('layouts/sidebar/bookmark-entry', [
|
||||||
|
'entry' => $entry,
|
||||||
|
])
|
||||||
|
?>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
<?= $this->element('layouts/sidebar/bookmark-add') ?>
|
||||||
|
</li>
|
||||||
|
<?php else : ?>
|
||||||
|
<?= $this->element('layouts/sidebar/category', ['label' => $category]) ?>
|
||||||
|
<?php foreach ($categorized as $parentName => $parent) : ?>
|
||||||
|
<?= $this->element('layouts/sidebar/entry', [
|
||||||
'parent' => $parent,
|
'parent' => $parent,
|
||||||
])
|
])
|
||||||
?>
|
?>
|
||||||
<?php endforeach; ?>
|
<?php endforeach; ?>
|
||||||
|
<?php endif; ?>
|
||||||
<?php endforeach; ?>
|
<?php endforeach; ?>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<span class="lock-sidebar align-self-center mt-auto w-100 d-none d-sm-block" onclick="$('.sidebar').toggleClass('expanded')">
|
<span class="lock-sidebar align-self-center mt-auto w-100 d-none d-sm-block">
|
||||||
<a type="button" class="btn btn-sm w-100"></a>
|
<a type="button" class="btn-lock-sidebar btn btn-sm w-100"></a>
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
|
@ -0,0 +1,11 @@
|
||||||
|
<?php
|
||||||
|
echo $this->Bootstrap->button([
|
||||||
|
'nodeType' => 'a',
|
||||||
|
'icon' => 'plus',
|
||||||
|
'title' => __('Add new bookmark'),
|
||||||
|
'variant' => 'primary',
|
||||||
|
'size' => 'sm',
|
||||||
|
'params' => [
|
||||||
|
'id' => 'btn-add-bookmark',
|
||||||
|
]
|
||||||
|
]);
|
|
@ -0,0 +1,30 @@
|
||||||
|
<?php
|
||||||
|
use Cake\Routing\Router;
|
||||||
|
|
||||||
|
$seed = 'sb-' . mt_rand();
|
||||||
|
$icon = $entry['icon'] ?? '';
|
||||||
|
$label = $entry['label'] ?? '';
|
||||||
|
$name = $entry['name'] ?? '';
|
||||||
|
$active = false;
|
||||||
|
|
||||||
|
$url = $entry['url'];
|
||||||
|
|
||||||
|
$currentURL = Router::url(null);
|
||||||
|
if ($url == $currentURL) {
|
||||||
|
$active = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
echo $this->Bootstrap->button([
|
||||||
|
'nodeType' => 'a',
|
||||||
|
'text' => h($label),
|
||||||
|
'title' => h($name),
|
||||||
|
'variant' => 'dark',
|
||||||
|
'outline' => !$active,
|
||||||
|
'size' => 'sm',
|
||||||
|
'icon' => h($icon),
|
||||||
|
'class' => ['mb-1'],
|
||||||
|
'params' => [
|
||||||
|
'href' => h($url),
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
?>
|
|
@ -1,4 +1,4 @@
|
||||||
<li class="category text-muted">
|
<li class="category text-muted <?= !empty($class) ? h($class) : '' ?>">
|
||||||
<span class="category-label"><?= h($label) ?></span>
|
<span class="category-label"><?= h($label) ?></span>
|
||||||
<span class="category-divider"><hr /></span>
|
<span class="category-divider"><hr /></span>
|
||||||
</li>
|
</li>
|
||||||
|
|
|
@ -243,11 +243,30 @@ ul.sidebar-elements > li {
|
||||||
list-style: none;
|
list-style: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ul.sidebar-elements li.bookmarks {
|
||||||
|
padding: 0 calc((var(--sidebar-width-collapsed) - 25px) / 2);
|
||||||
|
}
|
||||||
|
|
||||||
.sidebar.expanded ul.sidebar-elements li > a.sidebar-link,
|
.sidebar.expanded ul.sidebar-elements li > a.sidebar-link,
|
||||||
.sidebar:hover ul.sidebar-elements li > a.sidebar-link {
|
.sidebar:hover ul.sidebar-elements li > a.sidebar-link {
|
||||||
text-overflow: ellipsis;
|
text-overflow: ellipsis;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.sidebar ul.sidebar-elements li.bookmark-categ,
|
||||||
|
.sidebar ul.sidebar-elements li.bookmarks {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.sidebar.expanded ul.sidebar-elements li.bookmark-categ,
|
||||||
|
.sidebar:hover ul.sidebar-elements li.bookmark-categ {
|
||||||
|
display: flex;
|
||||||
|
}
|
||||||
|
|
||||||
|
.sidebar.expanded ul.sidebar-elements li.bookmarks,
|
||||||
|
.sidebar:hover ul.sidebar-elements li.bookmarks {
|
||||||
|
display: list-item;
|
||||||
|
}
|
||||||
|
|
||||||
ul.sidebar-elements li > a.sidebar-link {
|
ul.sidebar-elements li > a.sidebar-link {
|
||||||
position: relative;
|
position: relative;
|
||||||
display: block;
|
display: block;
|
||||||
|
|
|
@ -177,3 +177,12 @@ $(document).ready(() => {
|
||||||
value: expanded ? 0 : 1
|
value: expanded ? 0 : 1
|
||||||
}, { provideFeedback: false})
|
}, { provideFeedback: false})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
$('.sidebar #btn-add-bookmark').click(() => {
|
||||||
|
const url = '/user-settings/saveBookmark';
|
||||||
|
UI.submissionModal(url).then(([modalFactory, ajaxApi]) => {
|
||||||
|
const $input = modalFactory.$modal.find('input[name="bookmark_url"]')
|
||||||
|
$input.val(window.location.pathname)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
Loading…
Reference in New Issue