diff --git a/src/Controller/PartListsController.php b/src/Controller/PartListsController.php index 116edaab..4974af46 100644 --- a/src/Controller/PartListsController.php +++ b/src/Controller/PartListsController.php @@ -55,6 +55,7 @@ use App\Services\Trees\NodesListBuilder; use Doctrine\ORM\EntityManagerInterface; use Omines\DataTablesBundle\DataTableFactory; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\Form\FormInterface; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -63,10 +64,12 @@ use Symfony\Component\Routing\Annotation\Route; class PartListsController extends AbstractController { private $entityManager; + private $nodesListBuilder; - public function __construct(EntityManagerInterface $entityManager) + public function __construct(EntityManagerInterface $entityManager, NodesListBuilder $nodesListBuilder) { $this->entityManager = $entityManager; + $this->nodesListBuilder = $nodesListBuilder; } /** @@ -100,6 +103,25 @@ class PartListsController extends AbstractController return $this->redirect($redirect); } + /** + * 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); + } + /** * @Route("/category/{id}/parts", name="part_list_category") * @@ -107,7 +129,17 @@ class PartListsController extends AbstractController */ public function showCategory(Category $category, Request $request, DataTableFactory $dataTable) { - $table = $dataTable->createFromType(PartsDataTable::class, ['category' => $category]) + $formRequest = clone $request; + $formRequest->setMethod('GET'); + $filter = new PartFilter($this->nodesListBuilder); + + //Set the category as default filter and disable to change that constraint value + $filter->getCategory()->setOperator('INCLUDING_CHILDREN')->setValue($category); + $filterForm = $this->createForm(PartFilterType::class, $filter, ['method' => 'GET']); + $this->disableFormFieldAfterCreation($filterForm->get('category')->get('value')); + $filterForm->handleRequest($formRequest); + + $table = $dataTable->createFromType(PartsDataTable::class, ['filter' => $filter]) ->handleRequest($request); if ($table->isCallback()) { @@ -118,6 +150,7 @@ class PartListsController extends AbstractController 'datatable' => $table, 'entity' => $category, 'repo' => $this->entityManager->getRepository(Category::class), + 'filterForm' => $filterForm->createView(), ]); } @@ -269,12 +302,12 @@ class PartListsController extends AbstractController * * @return JsonResponse|Response */ - public function showAll(Request $request, DataTableFactory $dataTable, NodesListBuilder $nodesListBuilder) + public function showAll(Request $request, DataTableFactory $dataTable) { $formRequest = clone $request; $formRequest->setMethod('GET'); - $filter = new PartFilter($nodesListBuilder); + $filter = new PartFilter($this->nodesListBuilder); $filterForm = $this->createForm(PartFilterType::class, $filter, ['method' => 'GET']); $filterForm->handleRequest($formRequest); diff --git a/templates/Parts/lists/category_list.html.twig b/templates/Parts/lists/category_list.html.twig index 676bba99..fd0b0b6d 100644 --- a/templates/Parts/lists/category_list.html.twig +++ b/templates/Parts/lists/category_list.html.twig @@ -8,6 +8,8 @@ {% include "Parts/lists/_info_card.html.twig" with {'header_label': 'category.label'} %} + {% include "Parts/lists/_filter.html.twig" %} + {% include "Parts/lists/_action_bar.html.twig" with {'url_options': {'category': entity.iD}} %} {% include "Parts/lists/_parts_list.html.twig" %}