2019-03-05 23:52:45 +01:00
|
|
|
<?php
|
2020-02-22 18:14:36 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
2022-11-29 22:28:53 +01:00
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
2020-02-22 18:14:36 +01:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
2020-01-05 15:46:58 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-03-05 23:52:45 +01:00
|
|
|
namespace App\Controller;
|
|
|
|
|
2022-08-15 01:01:27 +02:00
|
|
|
use App\DataTables\Filters\PartFilter;
|
2022-09-09 23:33:49 +02:00
|
|
|
use App\DataTables\Filters\PartSearchFilter;
|
2019-03-05 23:52:45 +01:00
|
|
|
use App\DataTables\PartsDataTable;
|
2019-09-05 22:27:18 +02:00
|
|
|
use App\Entity\Parts\Category;
|
2019-09-08 13:59:35 +02:00
|
|
|
use App\Entity\Parts\Footprint;
|
|
|
|
use App\Entity\Parts\Manufacturer;
|
2019-09-08 17:30:58 +02:00
|
|
|
use App\Entity\Parts\Storelocation;
|
|
|
|
use App\Entity\Parts\Supplier;
|
2022-08-15 01:01:27 +02:00
|
|
|
use App\Form\Filters\PartFilterType;
|
2020-05-23 19:06:46 +02:00
|
|
|
use App\Services\Parts\PartsTableActionHandler;
|
2022-08-21 01:34:17 +02:00
|
|
|
use App\Services\Trees\NodesListBuilder;
|
2020-05-16 20:53:35 +02:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2019-03-05 23:52:45 +01:00
|
|
|
use Omines\DataTablesBundle\DataTableFactory;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
2022-08-21 03:14:22 +02:00
|
|
|
use Symfony\Component\Form\FormInterface;
|
2020-01-05 22:49:00 +01:00
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
2019-03-05 23:52:45 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2020-01-05 22:49:00 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2019-03-05 23:52:45 +01:00
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
|
|
|
|
|
|
|
class PartListsController extends AbstractController
|
|
|
|
{
|
2022-09-18 22:59:31 +02:00
|
|
|
private EntityManagerInterface $entityManager;
|
|
|
|
private NodesListBuilder $nodesListBuilder;
|
|
|
|
private DataTableFactory $dataTableFactory;
|
2020-05-16 20:53:35 +02:00
|
|
|
|
2022-09-08 23:49:57 +02:00
|
|
|
public function __construct(EntityManagerInterface $entityManager, NodesListBuilder $nodesListBuilder, DataTableFactory $dataTableFactory)
|
2020-05-16 20:53:35 +02:00
|
|
|
{
|
|
|
|
$this->entityManager = $entityManager;
|
2022-08-21 03:14:22 +02:00
|
|
|
$this->nodesListBuilder = $nodesListBuilder;
|
2022-09-08 23:49:57 +02:00
|
|
|
$this->dataTableFactory = $dataTableFactory;
|
2020-05-16 20:53:35 +02:00
|
|
|
}
|
|
|
|
|
2020-05-23 19:06:46 +02:00
|
|
|
/**
|
|
|
|
* @Route("/table/action", name="table_action", methods={"POST"})
|
|
|
|
*/
|
|
|
|
public function tableAction(Request $request, PartsTableActionHandler $actionHandler): Response
|
|
|
|
{
|
2022-10-09 22:01:44 +02:00
|
|
|
$this->denyAccessUnlessGranted('@parts.edit');
|
|
|
|
|
2020-05-23 19:06:46 +02:00
|
|
|
$redirect = $request->request->get('redirect_back');
|
|
|
|
$ids = $request->request->get('ids');
|
|
|
|
$action = $request->request->get('action');
|
2020-05-24 18:26:10 +02:00
|
|
|
$target = $request->request->get('target');
|
2020-05-23 19:06:46 +02:00
|
|
|
|
|
|
|
if (!$this->isCsrfTokenValid('table_action', $request->request->get('_token'))) {
|
|
|
|
$this->addFlash('error', 'csfr_invalid');
|
2020-08-21 21:36:22 +02:00
|
|
|
|
2020-05-23 19:06:46 +02:00
|
|
|
return $this->redirect($redirect);
|
|
|
|
}
|
|
|
|
|
2020-08-21 21:36:22 +02:00
|
|
|
if (null === $action || null === $ids) {
|
2020-05-23 19:06:46 +02:00
|
|
|
$this->addFlash('error', 'part.table.actions.no_params_given');
|
|
|
|
} else {
|
|
|
|
$parts = $actionHandler->idStringToArray($ids);
|
2022-12-28 23:32:46 +01:00
|
|
|
$redirectResponse = $actionHandler->handleAction($action, $parts, $target ? (int) $target : null, $redirect);
|
2020-05-23 19:06:46 +02:00
|
|
|
|
|
|
|
//Save changes
|
|
|
|
$this->entityManager->flush();
|
|
|
|
|
2020-05-24 20:20:44 +02:00
|
|
|
$this->addFlash('success', 'part.table.actions.success');
|
2020-05-23 19:06:46 +02:00
|
|
|
}
|
|
|
|
|
2022-12-28 23:32:46 +01:00
|
|
|
//If the action handler returned a response, we use it, otherwise we redirect back to the previous page.
|
2023-01-08 18:30:41 +01:00
|
|
|
if (isset($redirectResponse) && $redirectResponse instanceof Response) {
|
2022-12-28 23:32:46 +01:00
|
|
|
return $redirectResponse;
|
|
|
|
}
|
|
|
|
|
2020-05-23 19:06:46 +02:00
|
|
|
return $this->redirect($redirect);
|
|
|
|
}
|
|
|
|
|
2022-08-21 03:14:22 +02:00
|
|
|
/**
|
|
|
|
* Disable the given form interface after creation of the form by removing and reattaching the form.
|
|
|
|
* @param FormInterface $form
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function disableFormFieldAfterCreation(FormInterface $form, bool $disabled = true): void
|
|
|
|
{
|
|
|
|
$attrs = $form->getConfig()->getOptions();
|
|
|
|
$attrs['disabled'] = $disabled;
|
|
|
|
|
|
|
|
$parent = $form->getParent();
|
|
|
|
if ($parent === null) {
|
|
|
|
throw new \RuntimeException('This function can only be used on form fields that are children of another form!');
|
|
|
|
}
|
|
|
|
|
|
|
|
$parent->remove($form->getName());
|
|
|
|
$parent->add($form->getName(), get_class($form->getConfig()->getType()->getInnerType()), $attrs);
|
|
|
|
}
|
|
|
|
|
2019-03-05 23:52:45 +01:00
|
|
|
/**
|
2022-09-08 23:49:57 +02:00
|
|
|
* Common implementation for the part list pages.
|
2022-09-10 00:08:59 +02:00
|
|
|
* @param Request $request The request to parse
|
|
|
|
* @param string $template The template that should be rendered
|
|
|
|
* @param callable|null $filter_changer A function that is called with the filter object as parameter. This function can be used to customize the filter
|
|
|
|
* @param callable|null $form_changer A function that is called with the form object as parameter. This function can be used to customize the form
|
|
|
|
* @param array $additonal_template_vars Any additional template variables that should be passed to the template
|
|
|
|
* @param array $additional_table_vars Any additional variables that should be passed to the table creation
|
2022-09-08 23:49:57 +02:00
|
|
|
* @return Response
|
2019-03-05 23:52:45 +01:00
|
|
|
*/
|
2022-09-10 00:08:59 +02:00
|
|
|
protected function showListWithFilter(Request $request, string $template, ?callable $filter_changer = null, ?callable $form_changer = null, array $additonal_template_vars = [], array $additional_table_vars = []): Response
|
2019-03-05 23:52:45 +01:00
|
|
|
{
|
2022-10-09 22:01:44 +02:00
|
|
|
$this->denyAccessUnlessGranted('@parts.read');
|
|
|
|
|
2022-08-21 03:14:22 +02:00
|
|
|
$formRequest = clone $request;
|
|
|
|
$formRequest->setMethod('GET');
|
|
|
|
$filter = new PartFilter($this->nodesListBuilder);
|
2022-09-08 23:49:57 +02:00
|
|
|
if($filter_changer !== null){
|
|
|
|
$filter_changer($filter);
|
|
|
|
}
|
2022-08-21 03:14:22 +02:00
|
|
|
|
|
|
|
$filterForm = $this->createForm(PartFilterType::class, $filter, ['method' => 'GET']);
|
2022-09-08 23:49:57 +02:00
|
|
|
if($form_changer !== null) {
|
|
|
|
$form_changer($filterForm);
|
|
|
|
}
|
|
|
|
|
2022-08-21 03:14:22 +02:00
|
|
|
$filterForm->handleRequest($formRequest);
|
|
|
|
|
2022-09-10 00:08:59 +02:00
|
|
|
$table = $this->dataTableFactory->createFromType(PartsDataTable::class, array_merge(['filter' => $filter], $additional_table_vars))
|
2019-09-08 14:32:44 +02:00
|
|
|
->handleRequest($request);
|
2019-03-05 23:52:45 +01:00
|
|
|
|
|
|
|
if ($table->isCallback()) {
|
|
|
|
return $table->getResponse();
|
|
|
|
}
|
|
|
|
|
2022-09-08 23:49:57 +02:00
|
|
|
return $this->render($template, array_merge([
|
2019-09-08 13:37:11 +02:00
|
|
|
'datatable' => $table,
|
2022-08-21 03:14:22 +02:00
|
|
|
'filterForm' => $filterForm->createView(),
|
2022-09-08 23:49:57 +02:00
|
|
|
], $additonal_template_vars));
|
2019-03-05 23:52:45 +01:00
|
|
|
}
|
|
|
|
|
2019-09-08 13:59:35 +02:00
|
|
|
/**
|
2022-09-08 23:49:57 +02:00
|
|
|
* @Route("/category/{id}/parts", name="part_list_category")
|
2019-09-08 13:59:35 +02:00
|
|
|
*
|
2020-01-05 22:49:00 +01:00
|
|
|
* @return JsonResponse|Response
|
2019-09-08 13:59:35 +02:00
|
|
|
*/
|
2022-09-08 23:49:57 +02:00
|
|
|
public function showCategory(Category $category, Request $request)
|
2019-09-08 13:59:35 +02:00
|
|
|
{
|
2022-10-31 23:10:21 +01:00
|
|
|
$this->denyAccessUnlessGranted('@categories.read');
|
|
|
|
|
2022-09-08 23:49:57 +02:00
|
|
|
return $this->showListWithFilter($request,
|
2023-02-04 23:05:39 +01:00
|
|
|
'parts/lists/category_list.html.twig',
|
2022-09-08 23:49:57 +02:00
|
|
|
function (PartFilter $filter) use ($category) {
|
|
|
|
$filter->getCategory()->setOperator('INCLUDING_CHILDREN')->setValue($category);
|
|
|
|
}, function (FormInterface $filterForm) {
|
|
|
|
$this->disableFormFieldAfterCreation($filterForm->get('category')->get('value'));
|
|
|
|
}, [
|
|
|
|
'entity' => $category,
|
|
|
|
'repo' => $this->entityManager->getRepository(Category::class),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2019-09-08 13:59:35 +02:00
|
|
|
|
2022-09-08 23:49:57 +02:00
|
|
|
/**
|
|
|
|
* @Route("/footprint/{id}/parts", name="part_list_footprint")
|
|
|
|
*
|
|
|
|
* @return JsonResponse|Response
|
|
|
|
*/
|
|
|
|
public function showFootprint(Footprint $footprint, Request $request)
|
|
|
|
{
|
2022-10-31 23:10:21 +01:00
|
|
|
$this->denyAccessUnlessGranted('@footprints.read');
|
|
|
|
|
2022-09-08 23:49:57 +02:00
|
|
|
return $this->showListWithFilter($request,
|
2023-02-04 23:05:39 +01:00
|
|
|
'parts/lists/footprint_list.html.twig',
|
2022-09-08 23:49:57 +02:00
|
|
|
function (PartFilter $filter) use ($footprint) {
|
|
|
|
$filter->getFootprint()->setOperator('INCLUDING_CHILDREN')->setValue($footprint);
|
|
|
|
}, function (FormInterface $filterForm) {
|
|
|
|
$this->disableFormFieldAfterCreation($filterForm->get('footprint')->get('value'));
|
|
|
|
}, [
|
|
|
|
'entity' => $footprint,
|
|
|
|
'repo' => $this->entityManager->getRepository(Footprint::class),
|
|
|
|
]
|
|
|
|
);
|
2019-09-08 13:59:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Route("/manufacturer/{id}/parts", name="part_list_manufacturer")
|
|
|
|
*
|
2020-01-05 22:49:00 +01:00
|
|
|
* @return JsonResponse|Response
|
2019-09-08 13:59:35 +02:00
|
|
|
*/
|
2022-09-08 23:49:57 +02:00
|
|
|
public function showManufacturer(Manufacturer $manufacturer, Request $request)
|
2019-09-08 13:59:35 +02:00
|
|
|
{
|
2022-10-31 23:10:21 +01:00
|
|
|
$this->denyAccessUnlessGranted('@manufacturers.read');
|
|
|
|
|
2022-09-08 23:49:57 +02:00
|
|
|
return $this->showListWithFilter($request,
|
2023-02-04 23:05:39 +01:00
|
|
|
'parts/lists/manufacturer_list.html.twig',
|
2022-09-08 23:49:57 +02:00
|
|
|
function (PartFilter $filter) use ($manufacturer) {
|
|
|
|
$filter->getManufacturer()->setOperator('INCLUDING_CHILDREN')->setValue($manufacturer);
|
|
|
|
}, function (FormInterface $filterForm) {
|
|
|
|
$this->disableFormFieldAfterCreation($filterForm->get('manufacturer')->get('value'));
|
|
|
|
}, [
|
|
|
|
'entity' => $manufacturer,
|
|
|
|
'repo' => $this->entityManager->getRepository(Manufacturer::class),
|
|
|
|
]
|
|
|
|
);
|
2019-09-08 13:59:35 +02:00
|
|
|
}
|
|
|
|
|
2019-09-08 17:30:58 +02:00
|
|
|
/**
|
|
|
|
* @Route("/store_location/{id}/parts", name="part_list_store_location")
|
|
|
|
*
|
2020-01-05 22:49:00 +01:00
|
|
|
* @return JsonResponse|Response
|
2019-09-08 17:30:58 +02:00
|
|
|
*/
|
2022-09-08 23:49:57 +02:00
|
|
|
public function showStorelocation(Storelocation $storelocation, Request $request)
|
2019-09-08 17:30:58 +02:00
|
|
|
{
|
2022-10-31 23:10:21 +01:00
|
|
|
$this->denyAccessUnlessGranted('@storelocations.read');
|
|
|
|
|
2022-09-08 23:49:57 +02:00
|
|
|
return $this->showListWithFilter($request,
|
2023-02-04 23:05:39 +01:00
|
|
|
'parts/lists/store_location_list.html.twig',
|
2022-09-08 23:49:57 +02:00
|
|
|
function (PartFilter $filter) use ($storelocation) {
|
|
|
|
$filter->getStorelocation()->setOperator('INCLUDING_CHILDREN')->setValue($storelocation);
|
|
|
|
}, function (FormInterface $filterForm) {
|
|
|
|
$this->disableFormFieldAfterCreation($filterForm->get('storelocation')->get('value'));
|
|
|
|
}, [
|
|
|
|
'entity' => $storelocation,
|
|
|
|
'repo' => $this->entityManager->getRepository(Storelocation::class),
|
|
|
|
]
|
|
|
|
);
|
2019-09-08 17:30:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Route("/supplier/{id}/parts", name="part_list_supplier")
|
|
|
|
*
|
2020-01-05 22:49:00 +01:00
|
|
|
* @return JsonResponse|Response
|
2019-09-08 17:30:58 +02:00
|
|
|
*/
|
2022-09-08 23:49:57 +02:00
|
|
|
public function showSupplier(Supplier $supplier, Request $request)
|
2019-09-08 17:30:58 +02:00
|
|
|
{
|
2022-10-31 23:10:21 +01:00
|
|
|
$this->denyAccessUnlessGranted('@suppliers.read');
|
|
|
|
|
2022-09-08 23:49:57 +02:00
|
|
|
return $this->showListWithFilter($request,
|
2023-02-04 23:05:39 +01:00
|
|
|
'parts/lists/supplier_list.html.twig',
|
2022-09-08 23:49:57 +02:00
|
|
|
function (PartFilter $filter) use ($supplier) {
|
|
|
|
$filter->getSupplier()->setOperator('INCLUDING_CHILDREN')->setValue($supplier);
|
|
|
|
}, function (FormInterface $filterForm) {
|
|
|
|
$this->disableFormFieldAfterCreation($filterForm->get('supplier')->get('value'));
|
|
|
|
}, [
|
|
|
|
'entity' => $supplier,
|
|
|
|
'repo' => $this->entityManager->getRepository(Supplier::class),
|
|
|
|
]
|
|
|
|
);
|
2019-09-08 17:30:58 +02:00
|
|
|
}
|
|
|
|
|
2019-09-05 22:27:18 +02:00
|
|
|
/**
|
2022-08-03 22:45:02 +02:00
|
|
|
* @Route("/parts/by_tag/{tag}", name="part_list_tags", requirements={"tag": ".*"})
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2020-01-05 22:49:00 +01:00
|
|
|
* @return JsonResponse|Response
|
2019-09-05 22:27:18 +02:00
|
|
|
*/
|
|
|
|
public function showTag(string $tag, Request $request, DataTableFactory $dataTable)
|
|
|
|
{
|
2022-08-03 22:45:02 +02:00
|
|
|
$tag = trim($tag);
|
2019-09-05 22:27:18 +02:00
|
|
|
|
2022-09-08 23:49:57 +02:00
|
|
|
return $this->showListWithFilter($request,
|
2023-02-04 23:05:39 +01:00
|
|
|
'parts/lists/tags_list.html.twig',
|
2022-09-08 23:49:57 +02:00
|
|
|
function (PartFilter $filter) use ($tag) {
|
|
|
|
$filter->getTags()->setOperator('ANY')->setValue($tag);
|
|
|
|
}, function (FormInterface $filterForm) {
|
|
|
|
$this->disableFormFieldAfterCreation($filterForm->get('tags')->get('value'));
|
|
|
|
}, [
|
|
|
|
'tag' => $tag,
|
|
|
|
]
|
|
|
|
);
|
2019-09-05 22:27:18 +02:00
|
|
|
}
|
|
|
|
|
2022-09-09 23:33:49 +02:00
|
|
|
private function searchRequestToFilter(Request $request): PartSearchFilter
|
|
|
|
{
|
|
|
|
$filter = new PartSearchFilter($request->query->get('keyword', ''));
|
|
|
|
|
|
|
|
$filter->setName($request->query->getBoolean('name', true));
|
|
|
|
$filter->setCategory($request->query->getBoolean('category', true));
|
|
|
|
$filter->setDescription($request->query->getBoolean('description', true));
|
|
|
|
$filter->setTags($request->query->getBoolean('tags', true));
|
|
|
|
$filter->setStorelocation($request->query->getBoolean('storelocation', true));
|
|
|
|
$filter->setComment($request->query->getBoolean('comment', true));
|
|
|
|
$filter->setOrdernr($request->query->getBoolean('ordernr', true));
|
|
|
|
$filter->setSupplier($request->query->getBoolean('supplier', false));
|
|
|
|
$filter->setManufacturer($request->query->getBoolean('manufacturer', false));
|
|
|
|
$filter->setFootprint($request->query->getBoolean('footprint', false));
|
|
|
|
|
|
|
|
$filter->setRegex($request->query->getBoolean('regex', false));
|
|
|
|
|
|
|
|
return $filter;
|
|
|
|
}
|
|
|
|
|
2019-09-05 22:27:18 +02:00
|
|
|
/**
|
2020-01-04 22:37:30 +01:00
|
|
|
* @Route("/parts/search", name="parts_search")
|
2020-03-15 13:56:31 +01:00
|
|
|
*
|
2020-02-02 14:05:36 +01:00
|
|
|
* @return JsonResponse|Response
|
2019-09-05 22:27:18 +02:00
|
|
|
*/
|
2020-01-04 22:37:30 +01:00
|
|
|
public function showSearch(Request $request, DataTableFactory $dataTable)
|
2019-09-05 22:27:18 +02:00
|
|
|
{
|
2022-09-09 23:33:49 +02:00
|
|
|
$searchFilter = $this->searchRequestToFilter($request);
|
2022-08-15 01:01:27 +02:00
|
|
|
|
2022-09-10 00:08:59 +02:00
|
|
|
return $this->showListWithFilter($request,
|
2023-02-04 23:05:39 +01:00
|
|
|
'parts/lists/search_list.html.twig',
|
2022-09-10 00:08:59 +02:00
|
|
|
null,
|
|
|
|
null,
|
|
|
|
[
|
|
|
|
'keyword' => $searchFilter->getKeyword(),
|
|
|
|
'searchFilter' => $searchFilter,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'search' => $searchFilter,
|
|
|
|
]
|
|
|
|
);
|
2019-09-05 22:27:18 +02:00
|
|
|
}
|
|
|
|
|
2019-03-06 00:02:33 +01:00
|
|
|
/**
|
2019-03-26 15:57:51 +01:00
|
|
|
* @Route("/parts", name="parts_show_all")
|
2019-03-06 00:02:33 +01:00
|
|
|
*
|
2020-01-05 22:49:00 +01:00
|
|
|
* @return JsonResponse|Response
|
2019-03-06 00:02:33 +01:00
|
|
|
*/
|
2022-08-21 03:14:22 +02:00
|
|
|
public function showAll(Request $request, DataTableFactory $dataTable)
|
2019-03-06 00:02:33 +01:00
|
|
|
{
|
2023-02-04 23:05:39 +01:00
|
|
|
return $this->showListWithFilter($request,'parts/lists/all_list.html.twig');
|
2019-03-06 00:02:33 +01:00
|
|
|
}
|
2019-03-20 23:16:07 +01:00
|
|
|
}
|