Skip to content

Commit

Permalink
Allow to define icons for settings
Browse files Browse the repository at this point in the history
  • Loading branch information
jbtronics committed Aug 3, 2024
1 parent 947cce7 commit 47830dc
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 3 deletions.
32 changes: 32 additions & 0 deletions src/Settings/SettingsIcon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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;

#[\Attribute(\Attribute::TARGET_CLASS)]
class SettingsIcon
{
public function __construct(public string $icon)
{
}
}
2 changes: 2 additions & 0 deletions src/Settings/SystemSettings/AttachmentsSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace App\Settings\SystemSettings;

use App\Settings\SettingsIcon;
use Jbtronics\SettingsBundle\Metadata\EnvVarMode;
use Jbtronics\SettingsBundle\Settings\Settings;
use Jbtronics\SettingsBundle\Settings\SettingsParameter;
Expand All @@ -31,6 +32,7 @@
use Symfony\Component\Validator\Constraints as Assert;

#[Settings(label: new TM("settings.system.attachments"))]
#[SettingsIcon("fa-paperclip")]
class AttachmentsSettings
{
use SettingsTrait;
Expand Down
2 changes: 2 additions & 0 deletions src/Settings/SystemSettings/CustomizationSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use App\Form\Type\RichTextEditorType;
use App\Form\Type\ThemeChoiceType;
use App\Settings\SettingsIcon;
use App\Validator\Constraints\ValidTheme;
use Jbtronics\SettingsBundle\Metadata\EnvVarMode;
use Jbtronics\SettingsBundle\Settings\Settings;
Expand All @@ -33,6 +34,7 @@
use Symfony\Component\Translation\TranslatableMessage as TM;

#[Settings(name: "customization", label: new TM("settings.system.customization"))]
#[SettingsIcon("fa-paint-roller")]
class CustomizationSettings
{
use SettingsTrait;
Expand Down
2 changes: 2 additions & 0 deletions src/Settings/SystemSettings/HistorySettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use App\Form\History\EnforceEventCommentTypesType;
use App\Services\LogSystem\EventCommentType;
use App\Settings\SettingsIcon;
use Jbtronics\SettingsBundle\Metadata\EnvVarMode;
use Jbtronics\SettingsBundle\ParameterTypes\ArrayType;
use Jbtronics\SettingsBundle\ParameterTypes\EnumType;
Expand All @@ -34,6 +35,7 @@
use Symfony\Component\Translation\TranslatableMessage as TM;

#[Settings(label: new TM("settings.system.history"))]
#[SettingsIcon("fa-binoculars")]
class HistorySettings
{
use SettingsTrait;
Expand Down
2 changes: 2 additions & 0 deletions src/Settings/SystemSettings/LocalizationSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace App\Settings\SystemSettings;

use App\Form\Type\LocaleSelectType;
use App\Settings\SettingsIcon;
use Jbtronics\SettingsBundle\Metadata\EnvVarMode;
use Jbtronics\SettingsBundle\Settings\Settings;
use Jbtronics\SettingsBundle\Settings\SettingsParameter;
Expand All @@ -34,6 +35,7 @@
use Symfony\Component\Validator\Constraints as Assert;

#[Settings(label: new TM("settings.system.localization"))]
#[SettingsIcon("fa-globe")]
class LocalizationSettings
{
use SettingsTrait;
Expand Down
3 changes: 2 additions & 1 deletion src/Settings/SystemSettings/PrivacySettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@

namespace App\Settings\SystemSettings;

use App\Settings\SettingsIcon;
use Jbtronics\SettingsBundle\Metadata\EnvVarMode;
use Jbtronics\SettingsBundle\Settings\Settings;
use Jbtronics\SettingsBundle\Settings\SettingsParameter;
use Jbtronics\SettingsBundle\Settings\SettingsTrait;
use Symfony\Component\Translation\TranslatableMessage as TM;

#[Settings(label: new TM("settings.system.privacy"))]
#[SettingsIcon("fa-location-pin-lock")]
class PrivacySettings
{
use SettingsTrait;
Expand All @@ -48,5 +50,4 @@ class PrivacySettings
description: new TM("settings.system.privacy.useGravatar.description"),
envVar: 'bool:USE_GRAVATAR', envVarMode: EnvVarMode::OVERWRITE)]
public bool $useGravatar = false;

}
25 changes: 25 additions & 0 deletions src/Twig/MiscExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
namespace App\Twig;

use App\Services\LogSystem\EventCommentType;
use Jbtronics\SettingsBundle\Proxy\SettingsProxyInterface;
use ReflectionClass;
use Twig\TwigFunction;
use App\Services\LogSystem\EventCommentNeededHelper;
use Twig\Extension\AbstractExtension;
Expand All @@ -37,6 +39,8 @@ public function getFunctions(): array
{
return [
new TwigFunction('event_comment_needed', $this->evenCommentNeeded(...)),

new TwigFunction('settings_icon', $this->settingsIcon(...)),
];
}

Expand All @@ -48,4 +52,25 @@ private function evenCommentNeeded(string|EventCommentType $operation_type): boo

return $this->eventCommentNeededHelper->isCommentNeeded($operation_type);
}

/**
* Returns the value of the icon attribute of the SettingsIcon attribute of the given class.
* If the class does not have a SettingsIcon attribute, then null is returned.
* @param string|object $objectOrClass
* @return string|null
* @throws \ReflectionException
*/
private function settingsIcon(string|object $objectOrClass): ?string
{
//If the given object is a proxy, then get the real object
if (is_a($objectOrClass, SettingsProxyInterface::class)) {
$objectOrClass = get_parent_class($objectOrClass);
}

$reflection = new ReflectionClass($objectOrClass);

$attribute = $reflection->getAttributes(\App\Settings\SettingsIcon::class)[0] ?? null;

return $attribute?->newInstance()->icon;
}
}
7 changes: 5 additions & 2 deletions templates/settings/settings.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,20 @@
{{ form_errors(tab_widget) }}

{% for section_widget in tab_widget %}
{% set settings_object = section_widget.vars.value %}

{% if section_widget.vars.compound ?? false %}
<fieldset>
<legend>
<legend class="offset-3">
<i class="fa-solid {{ settings_icon(settings_object)|default('fa-sliders') }} fa-fw"></i>
{{ (section_widget.vars.label ?? section_widget.vars.name|humanize)|trans }}
</legend>
{{ form_help(section_widget) }}
{{ form_errors(section_widget) }}
{{ form_widget(section_widget) }}
</fieldset>
{% if not loop.last %}
<hr class="m-0">
<hr class="mx-0 mb-2 mt-2">
{% endif %}
{% else %} {# If not a compound render as normal row #}
{{ form_row(section_widget) }}
Expand Down

0 comments on commit 47830dc

Please sign in to comment.