Allow to associate settings forms with info providers

This commit is contained in:
Jan Böhmer 2025-08-24 23:32:58 +02:00
parent eaaf44b391
commit ee33d743e6
7 changed files with 100 additions and 13 deletions

12
composer.lock generated
View file

@ -5090,16 +5090,16 @@
}, },
{ {
"name": "jbtronics/settings-bundle", "name": "jbtronics/settings-bundle",
"version": "v3.0.0", "version": "v3.0.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/jbtronics/settings-bundle.git", "url": "https://github.com/jbtronics/settings-bundle.git",
"reference": "34b9629af73c7ad8989d8284470e79f3f8d79712" "reference": "9103bd7f78f0b223d1c7167feb824004fc2a9f07"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/jbtronics/settings-bundle/zipball/34b9629af73c7ad8989d8284470e79f3f8d79712", "url": "https://api.github.com/repos/jbtronics/settings-bundle/zipball/9103bd7f78f0b223d1c7167feb824004fc2a9f07",
"reference": "34b9629af73c7ad8989d8284470e79f3f8d79712", "reference": "9103bd7f78f0b223d1c7167feb824004fc2a9f07",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -5160,7 +5160,7 @@
], ],
"support": { "support": {
"issues": "https://github.com/jbtronics/settings-bundle/issues", "issues": "https://github.com/jbtronics/settings-bundle/issues",
"source": "https://github.com/jbtronics/settings-bundle/tree/v3.0.0" "source": "https://github.com/jbtronics/settings-bundle/tree/v3.0.1"
}, },
"funding": [ "funding": [
{ {
@ -5172,7 +5172,7 @@
"type": "github" "type": "github"
} }
], ],
"time": "2025-08-24T17:17:43+00:00" "time": "2025-08-24T21:20:15+00:00"
}, },
{ {
"name": "jfcherng/php-color-output", "name": "jfcherng/php-color-output",

View file

@ -29,10 +29,14 @@ use App\Form\InfoProviderSystem\PartSearchType;
use App\Services\InfoProviderSystem\ExistingPartFinder; use App\Services\InfoProviderSystem\ExistingPartFinder;
use App\Services\InfoProviderSystem\PartInfoRetriever; use App\Services\InfoProviderSystem\PartInfoRetriever;
use App\Services\InfoProviderSystem\ProviderRegistry; use App\Services\InfoProviderSystem\ProviderRegistry;
use App\Settings\AppSettings;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Jbtronics\SettingsBundle\Form\SettingsFormFactoryInterface;
use Jbtronics\SettingsBundle\Manager\SettingsManagerInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Bridge\Doctrine\Attribute\MapEntity; use Symfony\Bridge\Doctrine\Attribute\MapEntity;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpClient\Exception\ClientException; use Symfony\Component\HttpClient\Exception\ClientException;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@ -46,7 +50,9 @@ class InfoProviderController extends AbstractController
public function __construct(private readonly ProviderRegistry $providerRegistry, public function __construct(private readonly ProviderRegistry $providerRegistry,
private readonly PartInfoRetriever $infoRetriever, private readonly PartInfoRetriever $infoRetriever,
private readonly ExistingPartFinder $existingPartFinder private readonly ExistingPartFinder $existingPartFinder,
private readonly SettingsManagerInterface $settingsManager,
private readonly SettingsFormFactoryInterface $settingsFormFactory
) )
{ {
@ -63,6 +69,48 @@ class InfoProviderController extends AbstractController
]); ]);
} }
#[Route('/provider/{provider}/settings', name: 'info_providers_provider_settings')]
public function providerSettings(string $provider, Request $request): Response
{
$this->denyAccessUnlessGranted('@config.change_system_settings');
$this->denyAccessUnlessGranted('@info_providers.create_parts');
$providerInstance = $this->providerRegistry->getProviderByKey($provider);
$settingsClass = $providerInstance->getProviderInfo()['settings_class'] ?? throw new \LogicException('Provider ' . $provider . ' does not have a settings class defined');
//Create a clone of the settings object
$settings = $this->settingsManager->createTemporaryCopy($settingsClass);
//Create a form builder for the settings object
$builder = $this->settingsFormFactory->createSettingsFormBuilder($settings);
//Add a submit button to the form
$builder->add('submit', SubmitType::class, ['label' => 'save']);
//Create the form
$form = $builder->getForm();
$form->handleRequest($request);
//If the form was submitted and is valid, save the settings
if ($form->isSubmitted() && $form->isValid()) {
$this->settingsManager->mergeTemporaryCopy($settings);
$this->settingsManager->save($settings);
$this->addFlash('success', t('settings.flash.saved'));
}
if ($form->isSubmitted() && !$form->isValid()) {
$this->addFlash('error', t('settings.flash.invalid'));
}
//Render the form
return $this->render('info_providers/settings/provider_settings.html.twig', [
'form' => $form,
'info_provider_key' => $provider,
'info_provider_info' => $providerInstance->getProviderInfo(),
]);
}
#[Route('/search', name: 'info_providers_search')] #[Route('/search', name: 'info_providers_search')]
#[Route('/update/{target}', name: 'info_providers_update_part_search')] #[Route('/update/{target}', name: 'info_providers_update_part_search')]
public function search(Request $request, #[MapEntity(id: 'target')] ?Part $update_target, LoggerInterface $exceptionLogger): Response public function search(Request $request, #[MapEntity(id: 'target')] ?Part $update_target, LoggerInterface $exceptionLogger): Response

View file

@ -39,8 +39,9 @@ interface InfoProviderInterface
* - url?: The url of the provider (e.g. "https://www.digikey.com") * - url?: The url of the provider (e.g. "https://www.digikey.com")
* - disabled_help?: A help text which is shown when the provider is disabled, explaining how to enable it * - disabled_help?: A help text which is shown when the provider is disabled, explaining how to enable it
* - oauth_app_name?: The name of the OAuth app which is used for authentication (e.g. "ip_digikey_oauth"). If this is set a connect button will be shown * - oauth_app_name?: The name of the OAuth app which is used for authentication (e.g. "ip_digikey_oauth"). If this is set a connect button will be shown
* - settings_class?: The class name of the settings class which contains the settings for this provider (e.g. "App\Settings\InfoProviderSettings\DigikeySettings"). If this is set a link to the settings will be shown
* *
* @phpstan-return array{ name: string, description?: string, logo?: string, url?: string, disabled_help?: string, oauth_app_name?: string } * @phpstan-return array{ name: string, description?: string, logo?: string, url?: string, disabled_help?: string, oauth_app_name?: string, settings_class?: class-string }
*/ */
public function getProviderInfo(): array; public function getProviderInfo(): array;

View file

@ -51,7 +51,8 @@ class ReicheltProvider implements InfoProviderInterface
'name' => 'Reichelt', 'name' => 'Reichelt',
'description' => 'Webscraping from reichelt.com to get part information', 'description' => 'Webscraping from reichelt.com to get part information',
'url' => 'https://www.reichelt.com/', 'url' => 'https://www.reichelt.com/',
'disabled_help' => 'Enable provider in provider settings.' 'disabled_help' => 'Enable provider in provider settings.',
'settings_class' => ReicheltSettings::class,
]; ];
} }

View file

@ -13,7 +13,6 @@
{% else %} {% else %}
{{ provider.providerInfo.name | trans }} {{ provider.providerInfo.name | trans }}
{% endif %} {% endif %}
</h5> </h5>
<div> <div>
{% if provider.providerInfo.description is defined and provider.providerInfo.description is not null %} {% if provider.providerInfo.description is defined and provider.providerInfo.description is not null %}
@ -23,6 +22,11 @@
</div> </div>
<div class="col-6"> <div class="col-6">
{% if provider.providerInfo.settings_class is defined %}
<a href="{{ path('info_providers_provider_settings', {'provider': provider.providerKey}) }}" class="btn btn-primary btn-sm"
title="{% trans %}info_providers.settings.title{% endtrans %}"
><i class="fa-solid fa-cog"></i></a>
{% endif %}
{% for capability in provider.capabilities %} {% for capability in provider.capabilities %}
{# @var capability \App\Services\InfoProviderSystem\Providers\ProviderCapabilities #} {# @var capability \App\Services\InfoProviderSystem\Providers\ProviderCapabilities #}
<span class="badge text-bg-secondary"> <span class="badge text-bg-secondary">

View file

@ -0,0 +1,27 @@
{% extends "main_card.html.twig" %}
{% macro genId(widget) %}{{ widget.vars.full_name }}{% endmacro %}
{% form_theme form "form/settings_form.html.twig" %}
{% block title %}{% trans %}info_providers.settings.title{% endtrans %}: {{ info_provider_info.name }}{% endblock %}
{% block card_title %}<i class="fa-solid fa-gear fa-fw"></i> {% trans %}info_providers.settings.title{% endtrans %}: <b>{{ info_provider_info.name }}</b>{% endblock %}
{% block card_content %}
<div class="offset-sm-3">
<h3>
{% if info_provider_info.url %}
<a href="{{ info_provider_info.url }}" class="link-external" target="_blank" rel="nofollow">{{ info_provider_info.name }}</a>
{% else %}
{{ info_provider_info.name }}
{% endif %}
</h3>
{% if info_provider_info.description %}
<p class="text-muted">{{ info_provider_info.description }}</p>
{% endif %}
</div>
{{ form_start(form) }}
{{ form_help(form) }}
{{ form_end(form) }}
{% endblock %}

View file

@ -13045,5 +13045,11 @@ Please note, that you can not impersonate a disabled user. If you try you will g
<target>Settings are invalid. Please check your input!</target> <target>Settings are invalid. Please check your input!</target>
</segment> </segment>
</unit> </unit>
<unit id="yRXWSRN" name="info_providers.settings.title">
<segment>
<source>info_providers.settings.title</source>
<target>Info provider settings</target>
</segment>
</unit>
</file> </file>
</xliff> </xliff>