Skip to content

Commit ee33d74

Browse files
committed
Allow to associate settings forms with info providers
1 parent eaaf44b commit ee33d74

File tree

7 files changed

+100
-13
lines changed

7 files changed

+100
-13
lines changed

composer.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Controller/InfoProviderController.php

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@
2929
use App\Services\InfoProviderSystem\ExistingPartFinder;
3030
use App\Services\InfoProviderSystem\PartInfoRetriever;
3131
use App\Services\InfoProviderSystem\ProviderRegistry;
32+
use App\Settings\AppSettings;
3233
use Doctrine\ORM\EntityManagerInterface;
34+
use Jbtronics\SettingsBundle\Form\SettingsFormFactoryInterface;
35+
use Jbtronics\SettingsBundle\Manager\SettingsManagerInterface;
3336
use Psr\Log\LoggerInterface;
3437
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
3538
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
39+
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
3640
use Symfony\Component\HttpClient\Exception\ClientException;
3741
use Symfony\Component\HttpFoundation\Request;
3842
use Symfony\Component\HttpFoundation\Response;
@@ -46,7 +50,9 @@ class InfoProviderController extends AbstractController
4650

4751
public function __construct(private readonly ProviderRegistry $providerRegistry,
4852
private readonly PartInfoRetriever $infoRetriever,
49-
private readonly ExistingPartFinder $existingPartFinder
53+
private readonly ExistingPartFinder $existingPartFinder,
54+
private readonly SettingsManagerInterface $settingsManager,
55+
private readonly SettingsFormFactoryInterface $settingsFormFactory
5056
)
5157
{
5258

@@ -63,6 +69,48 @@ public function listProviders(): Response
6369
]);
6470
}
6571

72+
#[Route('/provider/{provider}/settings', name: 'info_providers_provider_settings')]
73+
public function providerSettings(string $provider, Request $request): Response
74+
{
75+
$this->denyAccessUnlessGranted('@config.change_system_settings');
76+
$this->denyAccessUnlessGranted('@info_providers.create_parts');
77+
78+
$providerInstance = $this->providerRegistry->getProviderByKey($provider);
79+
$settingsClass = $providerInstance->getProviderInfo()['settings_class'] ?? throw new \LogicException('Provider ' . $provider . ' does not have a settings class defined');
80+
81+
//Create a clone of the settings object
82+
$settings = $this->settingsManager->createTemporaryCopy($settingsClass);
83+
84+
//Create a form builder for the settings object
85+
$builder = $this->settingsFormFactory->createSettingsFormBuilder($settings);
86+
87+
//Add a submit button to the form
88+
$builder->add('submit', SubmitType::class, ['label' => 'save']);
89+
90+
//Create the form
91+
$form = $builder->getForm();
92+
$form->handleRequest($request);
93+
94+
//If the form was submitted and is valid, save the settings
95+
if ($form->isSubmitted() && $form->isValid()) {
96+
$this->settingsManager->mergeTemporaryCopy($settings);
97+
$this->settingsManager->save($settings);
98+
99+
$this->addFlash('success', t('settings.flash.saved'));
100+
}
101+
102+
if ($form->isSubmitted() && !$form->isValid()) {
103+
$this->addFlash('error', t('settings.flash.invalid'));
104+
}
105+
106+
//Render the form
107+
return $this->render('info_providers/settings/provider_settings.html.twig', [
108+
'form' => $form,
109+
'info_provider_key' => $provider,
110+
'info_provider_info' => $providerInstance->getProviderInfo(),
111+
]);
112+
}
113+
66114
#[Route('/search', name: 'info_providers_search')]
67115
#[Route('/update/{target}', name: 'info_providers_update_part_search')]
68116
public function search(Request $request, #[MapEntity(id: 'target')] ?Part $update_target, LoggerInterface $exceptionLogger): Response
@@ -128,4 +176,4 @@ public function search(Request $request, #[MapEntity(id: 'target')] ?Part $updat
128176
'update_target' => $update_target
129177
]);
130178
}
131-
}
179+
}

src/Services/InfoProviderSystem/Providers/InfoProviderInterface.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ interface InfoProviderInterface
3939
* - url?: The url of the provider (e.g. "https://www.digikey.com")
4040
* - disabled_help?: A help text which is shown when the provider is disabled, explaining how to enable it
4141
* - 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
42+
* - 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
4243
*
43-
* @phpstan-return array{ name: string, description?: string, logo?: string, url?: string, disabled_help?: string, oauth_app_name?: string }
44+
* @phpstan-return array{ name: string, description?: string, logo?: string, url?: string, disabled_help?: string, oauth_app_name?: string, settings_class?: class-string }
4445
*/
4546
public function getProviderInfo(): array;
4647

@@ -78,4 +79,4 @@ public function getDetails(string $id): PartDetailDTO;
7879
* @return ProviderCapabilities[]
7980
*/
8081
public function getCapabilities(): array;
81-
}
82+
}

src/Services/InfoProviderSystem/Providers/ReicheltProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public function getProviderInfo(): array
5151
'name' => 'Reichelt',
5252
'description' => 'Webscraping from reichelt.com to get part information',
5353
'url' => 'https://www.reichelt.com/',
54-
'disabled_help' => 'Enable provider in provider settings.'
54+
'disabled_help' => 'Enable provider in provider settings.',
55+
'settings_class' => ReicheltSettings::class,
5556
];
5657
}
5758

templates/info_providers/providers.macro.html.twig

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
{% else %}
1414
{{ provider.providerInfo.name | trans }}
1515
{% endif %}
16-
1716
</h5>
1817
<div>
1918
{% if provider.providerInfo.description is defined and provider.providerInfo.description is not null %}
@@ -23,6 +22,11 @@
2322

2423
</div>
2524
<div class="col-6">
25+
{% if provider.providerInfo.settings_class is defined %}
26+
<a href="{{ path('info_providers_provider_settings', {'provider': provider.providerKey}) }}" class="btn btn-primary btn-sm"
27+
title="{% trans %}info_providers.settings.title{% endtrans %}"
28+
><i class="fa-solid fa-cog"></i></a>
29+
{% endif %}
2630
{% for capability in provider.capabilities %}
2731
{# @var capability \App\Services\InfoProviderSystem\Providers\ProviderCapabilities #}
2832
<span class="badge text-bg-secondary">
@@ -52,4 +56,4 @@
5256
{% endfor %}
5357
</tbody>
5458
</table>
55-
{% endmacro %}
59+
{% endmacro %}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{% extends "main_card.html.twig" %}
2+
{% macro genId(widget) %}{{ widget.vars.full_name }}{% endmacro %}
3+
4+
{% form_theme form "form/settings_form.html.twig" %}
5+
6+
{% block title %}{% trans %}info_providers.settings.title{% endtrans %}: {{ info_provider_info.name }}{% endblock %}
7+
8+
{% 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 %}
9+
10+
{% block card_content %}
11+
<div class="offset-sm-3">
12+
<h3>
13+
{% if info_provider_info.url %}
14+
<a href="{{ info_provider_info.url }}" class="link-external" target="_blank" rel="nofollow">{{ info_provider_info.name }}</a>
15+
{% else %}
16+
{{ info_provider_info.name }}
17+
{% endif %}
18+
</h3>
19+
{% if info_provider_info.description %}
20+
<p class="text-muted">{{ info_provider_info.description }}</p>
21+
{% endif %}
22+
</div>
23+
24+
{{ form_start(form) }}
25+
{{ form_help(form) }}
26+
{{ form_end(form) }}
27+
{% endblock %}

translations/messages.en.xlf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13045,5 +13045,11 @@ Please note, that you can not impersonate a disabled user. If you try you will g
1304513045
<target>Settings are invalid. Please check your input!</target>
1304613046
</segment>
1304713047
</unit>
13048+
<unit id="yRXWSRN" name="info_providers.settings.title">
13049+
<segment>
13050+
<source>info_providers.settings.title</source>
13051+
<target>Info provider settings</target>
13052+
</segment>
13053+
</unit>
1304813054
</file>
1304913055
</xliff>

0 commit comments

Comments
 (0)