2023-07-09 14:27:41 +02:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2023 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\Controller;
|
|
|
|
|
2023-11-24 19:28:30 +01:00
|
|
|
use App\Entity\Parts\Part;
|
2023-07-09 17:55:41 +02:00
|
|
|
use App\Form\InfoProviderSystem\PartSearchType;
|
|
|
|
use App\Services\InfoProviderSystem\PartInfoRetriever;
|
2023-07-09 14:27:41 +02:00
|
|
|
use App\Services\InfoProviderSystem\ProviderRegistry;
|
2023-11-27 23:17:20 +01:00
|
|
|
use Psr\Log\LoggerInterface;
|
2023-11-24 19:28:30 +01:00
|
|
|
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
|
2023-07-09 14:27:41 +02:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
2023-11-27 23:17:20 +01:00
|
|
|
use Symfony\Component\HttpClient\Exception\ClientException;
|
2023-07-09 17:55:41 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2023-07-09 14:27:41 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2024-03-03 20:37:33 +01:00
|
|
|
use Symfony\Component\Routing\Attribute\Route;
|
2023-07-09 14:27:41 +02:00
|
|
|
|
2023-11-27 23:17:20 +01:00
|
|
|
use function Symfony\Component\Translation\t;
|
|
|
|
|
2023-07-09 14:27:41 +02:00
|
|
|
#[Route('/tools/info_providers')]
|
|
|
|
class InfoProviderController extends AbstractController
|
|
|
|
{
|
|
|
|
|
2023-07-09 23:31:40 +02:00
|
|
|
public function __construct(private readonly ProviderRegistry $providerRegistry,
|
2023-07-16 23:46:20 +02:00
|
|
|
private readonly PartInfoRetriever $infoRetriever)
|
2023-07-09 23:31:40 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-07-09 14:27:41 +02:00
|
|
|
#[Route('/providers', name: 'info_providers_list')]
|
2023-07-09 23:31:40 +02:00
|
|
|
public function listProviders(): Response
|
2023-07-09 14:27:41 +02:00
|
|
|
{
|
2023-07-16 20:33:24 +02:00
|
|
|
$this->denyAccessUnlessGranted('@info_providers.create_parts');
|
|
|
|
|
2023-07-09 14:27:41 +02:00
|
|
|
return $this->render('info_providers/providers_list/providers_list.html.twig', [
|
2023-07-09 23:31:40 +02:00
|
|
|
'active_providers' => $this->providerRegistry->getActiveProviders(),
|
|
|
|
'disabled_providers' => $this->providerRegistry->getDisabledProviders(),
|
2023-07-09 14:27:41 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[Route('/search', name: 'info_providers_search')]
|
2023-11-24 19:28:30 +01:00
|
|
|
#[Route('/update/{target}', name: 'info_providers_update_part_search')]
|
2023-11-27 23:17:20 +01:00
|
|
|
public function search(Request $request, #[MapEntity(id: 'target')] ?Part $update_target, LoggerInterface $exceptionLogger): Response
|
2023-07-09 14:27:41 +02:00
|
|
|
{
|
2023-07-16 20:33:24 +02:00
|
|
|
$this->denyAccessUnlessGranted('@info_providers.create_parts');
|
|
|
|
|
2023-07-09 17:55:41 +02:00
|
|
|
$form = $this->createForm(PartSearchType::class);
|
|
|
|
$form->handleRequest($request);
|
2023-07-09 14:27:41 +02:00
|
|
|
|
2023-07-09 17:55:41 +02:00
|
|
|
$results = null;
|
|
|
|
|
2023-11-24 19:28:30 +01:00
|
|
|
//When we are updating a part, use its name as keyword, to make searching easier
|
|
|
|
//However we can only do this, if the form was not submitted yet
|
|
|
|
if ($update_target !== null && !$form->isSubmitted()) {
|
|
|
|
$form->get('keyword')->setData($update_target->getName());
|
|
|
|
}
|
|
|
|
|
2023-07-09 17:55:41 +02:00
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
|
|
$keyword = $form->get('keyword')->getData();
|
|
|
|
$providers = $form->get('providers')->getData();
|
|
|
|
|
2023-11-27 23:17:20 +01:00
|
|
|
try {
|
|
|
|
$results = $this->infoRetriever->searchByKeyword(keyword: $keyword, providers: $providers);
|
|
|
|
} catch (ClientException $e) {
|
|
|
|
$this->addFlash('error', t('info_providers.search.error.client_exception'));
|
|
|
|
$this->addFlash('error',$e->getMessage());
|
|
|
|
//Log the exception
|
|
|
|
$exceptionLogger->error('Error during info provider search: ' . $e->getMessage(), ['exception' => $e]);
|
|
|
|
}
|
2023-07-09 17:55:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->render('info_providers/search/part_search.html.twig', [
|
|
|
|
'form' => $form,
|
|
|
|
'results' => $results,
|
2023-11-24 19:28:30 +01:00
|
|
|
'update_target' => $update_target
|
2023-07-09 17:55:41 +02:00
|
|
|
]);
|
2023-07-09 14:27:41 +02:00
|
|
|
}
|
|
|
|
}
|