Skip to content

Commit 0d1ae03

Browse files
committed
Allow to select default info providers for search
This fixes issue #556
1 parent 1f669a9 commit 0d1ae03

File tree

5 files changed

+125
-9
lines changed

5 files changed

+125
-9
lines changed

src/Controller/InfoProviderController.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use App\Services\InfoProviderSystem\PartInfoRetriever;
3131
use App\Services\InfoProviderSystem\ProviderRegistry;
3232
use App\Settings\AppSettings;
33+
use App\Settings\InfoProviderSystem\InfoProviderGeneralSettings;
3334
use Doctrine\ORM\EntityManagerInterface;
3435
use Jbtronics\SettingsBundle\Form\SettingsFormFactoryInterface;
3536
use Jbtronics\SettingsBundle\Manager\SettingsManagerInterface;
@@ -113,7 +114,7 @@ public function providerSettings(string $provider, Request $request): Response
113114

114115
#[Route('/search', name: 'info_providers_search')]
115116
#[Route('/update/{target}', name: 'info_providers_update_part_search')]
116-
public function search(Request $request, #[MapEntity(id: 'target')] ?Part $update_target, LoggerInterface $exceptionLogger): Response
117+
public function search(Request $request, #[MapEntity(id: 'target')] ?Part $update_target, LoggerInterface $exceptionLogger, InfoProviderGeneralSettings $infoProviderSettings): Response
117118
{
118119
$this->denyAccessUnlessGranted('@info_providers.create_parts');
119120

@@ -144,6 +145,23 @@ public function search(Request $request, #[MapEntity(id: 'target')] ?Part $updat
144145
}
145146
}
146147

148+
//If the providers form is still empty, use our default value from the settings
149+
if (count($form->get('providers')->getData() ?? []) === 0) {
150+
$default_providers = $infoProviderSettings->defaultSearchProviders;
151+
$provider_objects = [];
152+
foreach ($default_providers as $provider_key) {
153+
try {
154+
$tmp = $this->providerRegistry->getProviderByKey($provider_key);
155+
if ($tmp->isActive()) {
156+
$provider_objects[] = $tmp;
157+
}
158+
} catch (\InvalidArgumentException $e) {
159+
//If the provider is not found, just ignore it
160+
}
161+
}
162+
$form->get('providers')->setData($provider_objects);
163+
}
164+
147165
if ($form->isSubmitted() && $form->isValid()) {
148166
$keyword = $form->get('keyword')->getData();
149167
$providers = $form->get('providers')->getData();

src/Form/InfoProviderSystem/ProviderSelectType.php

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Symfony\Component\Form\AbstractType;
2929
use Symfony\Component\Form\ChoiceList\ChoiceList;
3030
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
31+
use Symfony\Component\OptionsResolver\Options;
3132
use Symfony\Component\OptionsResolver\OptionsResolver;
3233

3334
class ProviderSelectType extends AbstractType
@@ -44,13 +45,43 @@ public function getParent(): string
4445

4546
public function configureOptions(OptionsResolver $resolver): void
4647
{
47-
$resolver->setDefaults([
48-
'choices' => $this->providerRegistry->getActiveProviders(),
49-
'choice_label' => ChoiceList::label($this, static fn (?InfoProviderInterface $choice) => $choice?->getProviderInfo()['name']),
50-
'choice_value' => ChoiceList::value($this, static fn(?InfoProviderInterface $choice) => $choice?->getProviderKey()),
48+
$providers = $this->providerRegistry->getActiveProviders();
5149

52-
'multiple' => true,
53-
]);
50+
$resolver->setDefault('input', 'object');
51+
$resolver->setAllowedTypes('input', 'string');
52+
//Either the form returns the provider objects or their keys
53+
$resolver->setAllowedValues('input', ['object', 'string']);
54+
$resolver->setDefault('multiple', true);
55+
56+
$resolver->setDefault('choices', function (Options $options) use ($providers) {
57+
if ('object' === $options['input']) {
58+
return $this->providerRegistry->getActiveProviders();
59+
}
60+
61+
$tmp = [];
62+
foreach ($providers as $provider) {
63+
$name = $provider->getProviderInfo()['name'];
64+
$tmp[$name] = $provider->getProviderKey();
65+
}
66+
67+
return $tmp;
68+
});
69+
70+
//The choice_label and choice_value only needs to be set if we want the objects
71+
$resolver->setDefault('choice_label', function (Options $options){
72+
if ('object' === $options['input']) {
73+
return ChoiceList::label($this, static fn (?InfoProviderInterface $choice) => $choice?->getProviderInfo()['name']);
74+
}
75+
76+
return null;
77+
});
78+
$resolver->setDefault('choice_value', function (Options $options) {
79+
if ('object' === $options['input']) {
80+
return ChoiceList::value($this, static fn(?InfoProviderInterface $choice) => $choice?->getProviderKey());
81+
}
82+
83+
return null;
84+
});
5485
}
5586

56-
}
87+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/*
3+
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4+
*
5+
* Copyright (C) 2019 - 2025 Jan Böhmer (https://github.com/jbtronics)
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License as published
9+
* by the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
24+
namespace App\Settings\InfoProviderSystem;
25+
26+
use App\Form\InfoProviderSystem\ProviderSelectType;
27+
use App\Settings\SettingsIcon;
28+
use Jbtronics\SettingsBundle\ParameterTypes\ArrayType;
29+
use Jbtronics\SettingsBundle\ParameterTypes\StringType;
30+
use Jbtronics\SettingsBundle\Settings\Settings;
31+
use Jbtronics\SettingsBundle\Settings\SettingsParameter;
32+
use Symfony\Component\Translation\TranslatableMessage as TM;
33+
34+
#[Settings(label: new TM("settings.ips.general"))]
35+
#[SettingsIcon("fa-magnifying-glass")]
36+
class InfoProviderGeneralSettings
37+
{
38+
/**
39+
* @var string[]
40+
*/
41+
#[SettingsParameter(type: ArrayType::class, label: new TM("settings.ips.default_providers"),
42+
description: new TM("settings.ips.default_providers.help"), options: ['type' => StringType::class],
43+
formType: ProviderSelectType::class, formOptions: ['input' => 'string'])]
44+
public array $defaultSearchProviders = [];
45+
}

src/Settings/InfoProviderSystem/InfoProviderSettings.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@
2525

2626
use Jbtronics\SettingsBundle\Settings\EmbeddedSettings;
2727
use Jbtronics\SettingsBundle\Settings\Settings;
28+
use Jbtronics\SettingsBundle\Settings\SettingsParameter;
2829
use Jbtronics\SettingsBundle\Settings\SettingsTrait;
2930

3031
#[Settings()]
3132
class InfoProviderSettings
3233
{
3334
use SettingsTrait;
3435

36+
#[EmbeddedSettings]
37+
public ?InfoProviderGeneralSettings $general = null;
38+
3539
#[EmbeddedSettings]
3640
public ?DigikeySettings $digikey = null;
3741

@@ -58,4 +62,4 @@ class InfoProviderSettings
5862

5963
#[EmbeddedSettings]
6064
public ?PollinSettings $pollin = null;
61-
}
65+
}

translations/messages.en.xlf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13471,5 +13471,23 @@ Please note, that you can not impersonate a disabled user. If you try you will g
1347113471
<target>Extract parameters from part notes</target>
1347213472
</segment>
1347313473
</unit>
13474+
<unit id="nCH2MW6" name="settings.ips.default_providers">
13475+
<segment>
13476+
<source>settings.ips.default_providers</source>
13477+
<target>Default search providers</target>
13478+
</segment>
13479+
</unit>
13480+
<unit id="TLNoCLT" name="settings.ips.general">
13481+
<segment>
13482+
<source>settings.ips.general</source>
13483+
<target>General settings</target>
13484+
</segment>
13485+
</unit>
13486+
<unit id="IDs2sXK" name="settings.ips.default_providers.help">
13487+
<segment>
13488+
<source>settings.ips.default_providers.help</source>
13489+
<target>These providers will be preselected for searches in part providers.</target>
13490+
</segment>
13491+
</unit>
1347413492
</file>
1347513493
</xliff>

0 commit comments

Comments
 (0)