Allow to configure sidebar menu via the new settings system

This commit is contained in:
Jan Böhmer 2024-08-07 00:41:06 +02:00
parent 5e512f8935
commit 79da0518c2
15 changed files with 320 additions and 55 deletions

View file

@ -30,6 +30,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Contracts\Cache\TagAwareCacheInterface;
class SettingsController extends AbstractController
{
@ -37,7 +38,7 @@ class SettingsController extends AbstractController
{}
#[Route("/settings", name: "system_settings")]
public function systemSettings(Request $request): Response
public function systemSettings(Request $request, TagAwareCacheInterface $cache): Response
{
//Create a clone of the settings object
$settings = $this->settingsManager->createTemporaryCopy(AppSettings::class);
@ -56,8 +57,14 @@ class SettingsController extends AbstractController
if ($form->isSubmitted() && $form->isValid()) {
$this->settingsManager->mergeTemporaryCopy($settings);
$this->settingsManager->save($settings);
//It might be possible, that the tree settings have changed, so clear the cache
$cache->invalidateTags(['tree_treeview', 'sidebar_tree_update']);
}
//Render the form
return $this->render('settings/settings.html.twig', [
'form' => $form

View file

@ -0,0 +1,60 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2024 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\Form;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\EnumType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
class SelectTypeOrderExtension extends AbstractTypeExtension
{
public static function getExtendedTypes(): iterable
{
return [
ChoiceType::class,
EnumType::class
];
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefault('ordered', false);
$resolver->setDefault('by_reference', function (Options $options) {
//Disable by_reference if the field is ordered (otherwise the order will be lost)
return !$options['ordered'];
});
}
public function buildView(FormView $view, FormInterface $form, array $options)
{
//Pass the data in ordered form to the frontend controller, so it can make the items appear in the correct order.
if ($options['ordered']) {
$view->vars['attr']['data-ordered-value'] = json_encode($form->getViewData(), JSON_THROW_ON_ERROR);
}
}
}

View file

@ -37,6 +37,7 @@ use App\Repository\StructuralDBElementRepository;
use App\Services\Cache\ElementCacheTagGenerator;
use App\Services\Cache\UserCacheKeyGenerator;
use App\Services\EntityURLGenerator;
use App\Settings\BehaviorSettings\SidebarSettings;
use Doctrine\ORM\EntityManagerInterface;
use InvalidArgumentException;
use RecursiveIteratorIterator;
@ -52,6 +53,10 @@ use function count;
*/
class TreeViewGenerator
{
private readonly bool $rootNodeExpandedByDefault;
private readonly bool $rootNodeEnabled;
public function __construct(
protected EntityURLGenerator $urlGenerator,
protected EntityManagerInterface $em,
@ -60,10 +65,10 @@ class TreeViewGenerator
protected UserCacheKeyGenerator $keyGenerator,
protected TranslatorInterface $translator,
private readonly UrlGeneratorInterface $router,
protected bool $rootNodeExpandedByDefault,
protected bool $rootNodeEnabled,
private readonly SidebarSettings $sidebarSettings,
) {
$this->rootNodeEnabled = $this->sidebarSettings->rootNodeEnabled;
$this->rootNodeExpandedByDefault = $this->sidebarSettings->rootNodeExpanded;
}
/**

View file

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace App\Settings;
use App\Settings\BehaviorSettings\BehaviorSettings;
use App\Settings\InfoProviderSystem\InfoProviderSettings;
use App\Settings\MiscSettings\MiscSettings;
use App\Settings\SystemSettings\AttachmentsSettings;
@ -31,6 +32,7 @@ use Jbtronics\SettingsBundle\Settings\Settings;
use Jbtronics\SettingsBundle\Settings\SettingsTrait;
#[Settings]
#[SettingsIcon('folder-tree')]
class AppSettings
{
use SettingsTrait;
@ -39,6 +41,9 @@ class AppSettings
#[EmbeddedSettings()]
public ?SystemSettings $system = null;
#[EmbeddedSettings()]
public ?BehaviorSettings $behavior = null;
#[EmbeddedSettings()]
public ?InfoProviderSettings $infoProviders = null;

View file

@ -0,0 +1,37 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2024 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\Settings\BehaviorSettings;
use Jbtronics\SettingsBundle\Settings\EmbeddedSettings;
use Jbtronics\SettingsBundle\Settings\Settings;
use Jbtronics\SettingsBundle\Settings\SettingsTrait;
#[Settings]
class BehaviorSettings
{
use SettingsTrait;
#[EmbeddedSettings]
public ?SidebarSettings $sidebar = null;
}

View file

@ -0,0 +1,53 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2024 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\Settings\BehaviorSettings;
use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
enum SidebarItems: string implements TranslatableInterface
{
case TOOLS = "tools";
case CATEGORIES = "categories";
case LOCATIONS = "locations";
case FOOTPRINTS = "footprints";
case MANUFACTURERS = "manufacturers";
case SUPPLIERS = "suppliers";
case PROJECTS = "projects";
public function trans(TranslatorInterface $translator, ?string $locale = null): string
{
$key = match($this) {
self::TOOLS => 'tools.label',
self::CATEGORIES => 'category.labelp',
self::LOCATIONS => 'storelocation.labelp',
self::FOOTPRINTS => 'footprint.labelp',
self::MANUFACTURERS => 'manufacturer.labelp',
self::SUPPLIERS => 'supplier.labelp',
self::PROJECTS => 'project.labelp',
};
return $translator->trans($key, locale: $locale);
}
}

View file

@ -0,0 +1,69 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2024 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\Settings\BehaviorSettings;
use App\Settings\SettingsIcon;
use Jbtronics\SettingsBundle\ParameterTypes\ArrayType;
use Jbtronics\SettingsBundle\ParameterTypes\EnumType;
use Jbtronics\SettingsBundle\Settings\Settings;
use Jbtronics\SettingsBundle\Settings\SettingsParameter;
use Jbtronics\SettingsBundle\Settings\SettingsTrait;
use Symfony\Component\Translation\TranslatableMessage as TM;
use Symfony\Component\Validator\Constraints as Assert;
#[Settings(label: new TM("settings.behavior.sidebar"))]
#[SettingsIcon('fa-border-top-left')]
class SidebarSettings
{
use SettingsTrait;
/**
* @var SidebarItems[] The items to show in the sidebar.
*/
#[SettingsParameter(ArrayType::class,
label: new TM("settings.behavior.sidebar.items"),
description: new TM("settings.behavior.sidebar.items.help"),
options: ['type' => EnumType::class, 'options' => ['class' => SidebarItems::class]],
formType: \Symfony\Component\Form\Extension\Core\Type\EnumType::class,
formOptions: ['class' => SidebarItems::class, 'multiple' => true, 'ordered' => true]
)]
#[Assert\NotBlank()]
public array $items = [SidebarItems::CATEGORIES, SidebarItems::PROJECTS, SidebarItems::TOOLS];
/**
* @var bool Whether categories, etc. should be grouped under a root node or put directly into the sidebar trees.
*/
#[SettingsParameter(
label: new TM("settings.behavior.sidebar.rootNodeEnabled"),
description: new TM("settings.behavior.sidebar.rootNodeEnabled.help")
)]
public bool $rootNodeEnabled = true;
/**
* @var bool Whether the root node should be expanded by default, or not.
*/
#[SettingsParameter(label: new TM("settings.behavior.sidebar.rootNodeExpanded"))]
public bool $rootNodeExpanded = true;
}