new: [sidebar:bookmarks] Added early version of user-defined bookmarks

Bookmark configs are saved in their respective user setting for each users
pull/72/head
Sami Mokaddem 2021-10-08 16:57:38 +02:00
parent 7fbf83bf49
commit 29ca08ce60
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
11 changed files with 192 additions and 11 deletions

View File

@ -457,6 +457,9 @@ class ACLComponent extends Component
{
$menu = $this->Navigation->getSideMenu();
foreach ($menu as $group => $subMenu) {
if ($group == '__bookmarks') {
continue;
}
foreach ($subMenu as $subMenuElementName => $subMenuElement) {
if (!empty($subMenuElement['url']) && !$this->checkAccessUrl($subMenuElement['url'], true) === true) {
unset($menu[$group][$subMenuElementName]);

View File

@ -50,9 +50,37 @@ class NavigationComponent extends Component
public function getSideMenu(): array
{
$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
{
$controller = $this->request->getParam('controller');

View File

@ -136,4 +136,20 @@ class UserSettingsController extends AppController
}
$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);
}
}

View File

@ -57,3 +57,25 @@ class UserSettingsTable extends AppTable
$savedData = $this->save($setting);
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;
}
}

View File

@ -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>

View File

@ -2,19 +2,32 @@
<div class="sidebar-scroll">
<div class="sidebar-content">
<ul class="sidebar-elements">
<?php foreach ($menu as $category => $categorized): ?>
<?= $this->element('layouts/sidebar/category', ['label' => $category]) ?>
<?php foreach ($categorized as $parentName => $parent): ?>
<?= $this->element('layouts/sidebar/entry', [
<?php foreach ($menu as $category => $categorized) : ?>
<?php if ($category == '__bookmarks') : ?>
<?= $this->element('layouts/sidebar/category', ['label' => __('Bookmarks'), 'class' => 'bookmark-categ']) ?>
<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,
])
?>
<?php endforeach; ?>
?>
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
</ul>
</div>
</div>
<span class="lock-sidebar align-self-center mt-auto w-100 d-none d-sm-block" onclick="$('.sidebar').toggleClass('expanded')">
<a type="button" class="btn btn-sm w-100"></a>
<span class="lock-sidebar align-self-center mt-auto w-100 d-none d-sm-block">
<a type="button" class="btn-lock-sidebar btn btn-sm w-100"></a>
</span>
</div>
</div>

View File

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

View File

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

View File

@ -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-divider"><hr /></span>
</li>

View File

@ -243,11 +243,30 @@ ul.sidebar-elements > li {
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:hover ul.sidebar-elements li > a.sidebar-link {
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 {
position: relative;
display: block;

View File

@ -177,3 +177,12 @@ $(document).ready(() => {
value: expanded ? 0 : 1
}, { 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)
})
})
})