mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-12-06 11:09:29 +00:00
Allow to select default info providers for search
This fixes issue #556
This commit is contained in:
parent
1f669a9c53
commit
0d1ae030be
5 changed files with 125 additions and 9 deletions
|
|
@ -30,6 +30,7 @@ use App\Services\InfoProviderSystem\ExistingPartFinder;
|
|||
use App\Services\InfoProviderSystem\PartInfoRetriever;
|
||||
use App\Services\InfoProviderSystem\ProviderRegistry;
|
||||
use App\Settings\AppSettings;
|
||||
use App\Settings\InfoProviderSystem\InfoProviderGeneralSettings;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Jbtronics\SettingsBundle\Form\SettingsFormFactoryInterface;
|
||||
use Jbtronics\SettingsBundle\Manager\SettingsManagerInterface;
|
||||
|
|
@ -113,7 +114,7 @@ class InfoProviderController extends AbstractController
|
|||
|
||||
#[Route('/search', name: 'info_providers_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, InfoProviderGeneralSettings $infoProviderSettings): Response
|
||||
{
|
||||
$this->denyAccessUnlessGranted('@info_providers.create_parts');
|
||||
|
||||
|
|
@ -144,6 +145,23 @@ class InfoProviderController extends AbstractController
|
|||
}
|
||||
}
|
||||
|
||||
//If the providers form is still empty, use our default value from the settings
|
||||
if (count($form->get('providers')->getData() ?? []) === 0) {
|
||||
$default_providers = $infoProviderSettings->defaultSearchProviders;
|
||||
$provider_objects = [];
|
||||
foreach ($default_providers as $provider_key) {
|
||||
try {
|
||||
$tmp = $this->providerRegistry->getProviderByKey($provider_key);
|
||||
if ($tmp->isActive()) {
|
||||
$provider_objects[] = $tmp;
|
||||
}
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
//If the provider is not found, just ignore it
|
||||
}
|
||||
}
|
||||
$form->get('providers')->setData($provider_objects);
|
||||
}
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$keyword = $form->get('keyword')->getData();
|
||||
$providers = $form->get('providers')->getData();
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ use App\Services\InfoProviderSystem\Providers\InfoProviderInterface;
|
|||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\ChoiceList\ChoiceList;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\OptionsResolver\Options;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class ProviderSelectType extends AbstractType
|
||||
|
|
@ -44,13 +45,43 @@ class ProviderSelectType extends AbstractType
|
|||
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'choices' => $this->providerRegistry->getActiveProviders(),
|
||||
'choice_label' => ChoiceList::label($this, static fn (?InfoProviderInterface $choice) => $choice?->getProviderInfo()['name']),
|
||||
'choice_value' => ChoiceList::value($this, static fn(?InfoProviderInterface $choice) => $choice?->getProviderKey()),
|
||||
$providers = $this->providerRegistry->getActiveProviders();
|
||||
|
||||
'multiple' => true,
|
||||
]);
|
||||
$resolver->setDefault('input', 'object');
|
||||
$resolver->setAllowedTypes('input', 'string');
|
||||
//Either the form returns the provider objects or their keys
|
||||
$resolver->setAllowedValues('input', ['object', 'string']);
|
||||
$resolver->setDefault('multiple', true);
|
||||
|
||||
$resolver->setDefault('choices', function (Options $options) use ($providers) {
|
||||
if ('object' === $options['input']) {
|
||||
return $this->providerRegistry->getActiveProviders();
|
||||
}
|
||||
|
||||
$tmp = [];
|
||||
foreach ($providers as $provider) {
|
||||
$name = $provider->getProviderInfo()['name'];
|
||||
$tmp[$name] = $provider->getProviderKey();
|
||||
}
|
||||
|
||||
return $tmp;
|
||||
});
|
||||
|
||||
//The choice_label and choice_value only needs to be set if we want the objects
|
||||
$resolver->setDefault('choice_label', function (Options $options){
|
||||
if ('object' === $options['input']) {
|
||||
return ChoiceList::label($this, static fn (?InfoProviderInterface $choice) => $choice?->getProviderInfo()['name']);
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
$resolver->setDefault('choice_value', function (Options $options) {
|
||||
if ('object' === $options['input']) {
|
||||
return ChoiceList::value($this, static fn(?InfoProviderInterface $choice) => $choice?->getProviderKey());
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2025 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\InfoProviderSystem;
|
||||
|
||||
use App\Form\InfoProviderSystem\ProviderSelectType;
|
||||
use App\Settings\SettingsIcon;
|
||||
use Jbtronics\SettingsBundle\ParameterTypes\ArrayType;
|
||||
use Jbtronics\SettingsBundle\ParameterTypes\StringType;
|
||||
use Jbtronics\SettingsBundle\Settings\Settings;
|
||||
use Jbtronics\SettingsBundle\Settings\SettingsParameter;
|
||||
use Symfony\Component\Translation\TranslatableMessage as TM;
|
||||
|
||||
#[Settings(label: new TM("settings.ips.general"))]
|
||||
#[SettingsIcon("fa-magnifying-glass")]
|
||||
class InfoProviderGeneralSettings
|
||||
{
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
#[SettingsParameter(type: ArrayType::class, label: new TM("settings.ips.default_providers"),
|
||||
description: new TM("settings.ips.default_providers.help"), options: ['type' => StringType::class],
|
||||
formType: ProviderSelectType::class, formOptions: ['input' => 'string'])]
|
||||
public array $defaultSearchProviders = [];
|
||||
}
|
||||
|
|
@ -25,6 +25,7 @@ namespace App\Settings\InfoProviderSystem;
|
|||
|
||||
use Jbtronics\SettingsBundle\Settings\EmbeddedSettings;
|
||||
use Jbtronics\SettingsBundle\Settings\Settings;
|
||||
use Jbtronics\SettingsBundle\Settings\SettingsParameter;
|
||||
use Jbtronics\SettingsBundle\Settings\SettingsTrait;
|
||||
|
||||
#[Settings()]
|
||||
|
|
@ -32,6 +33,9 @@ class InfoProviderSettings
|
|||
{
|
||||
use SettingsTrait;
|
||||
|
||||
#[EmbeddedSettings]
|
||||
public ?InfoProviderGeneralSettings $general = null;
|
||||
|
||||
#[EmbeddedSettings]
|
||||
public ?DigikeySettings $digikey = null;
|
||||
|
||||
|
|
|
|||
|
|
@ -13471,5 +13471,23 @@ Please note, that you can not impersonate a disabled user. If you try you will g
|
|||
<target>Extract parameters from part notes</target>
|
||||
</segment>
|
||||
</unit>
|
||||
<unit id="nCH2MW6" name="settings.ips.default_providers">
|
||||
<segment>
|
||||
<source>settings.ips.default_providers</source>
|
||||
<target>Default search providers</target>
|
||||
</segment>
|
||||
</unit>
|
||||
<unit id="TLNoCLT" name="settings.ips.general">
|
||||
<segment>
|
||||
<source>settings.ips.general</source>
|
||||
<target>General settings</target>
|
||||
</segment>
|
||||
</unit>
|
||||
<unit id="IDs2sXK" name="settings.ips.default_providers.help">
|
||||
<segment>
|
||||
<source>settings.ips.default_providers.help</source>
|
||||
<target>These providers will be preselected for searches in part providers.</target>
|
||||
</segment>
|
||||
</unit>
|
||||
</file>
|
||||
</xliff>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue