1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Zikula package. |
7
|
|
|
* |
8
|
|
|
* Copyright Zikula Foundation - https://ziku.la/ |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Zikula\CategoriesModule\Controller; |
15
|
|
|
|
16
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
17
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
18
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
20
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
21
|
|
|
use Zikula\Bundle\CoreBundle\Controller\AbstractController; |
22
|
|
|
use Zikula\Bundle\FormExtensionBundle\Form\Type\DeletionType; |
23
|
|
|
use Zikula\CategoriesModule\Entity\CategoryRegistryEntity; |
24
|
|
|
use Zikula\CategoriesModule\Entity\RepositoryInterface\CategoryRegistryRepositoryInterface; |
25
|
|
|
use Zikula\CategoriesModule\Form\Type\CategoryRegistryType; |
26
|
|
|
use Zikula\ExtensionsModule\Api\ApiInterface\CapabilityApiInterface; |
27
|
|
|
use Zikula\ExtensionsModule\Api\CapabilityApi; |
28
|
|
|
use Zikula\PermissionsModule\Annotation\PermissionCheck; |
29
|
|
|
use Zikula\ThemeModule\Engine\Annotation\Theme; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Controller for handling category registries. |
33
|
|
|
* |
34
|
|
|
* @Route("/registry") |
35
|
|
|
* @PermissionCheck("admin") |
36
|
|
|
*/ |
37
|
|
|
class RegistryController extends AbstractController |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @Route("/edit/{id}", requirements={"id" = "^[1-9]\d*$"}, defaults={"id" = null}) |
41
|
|
|
* @Theme("admin") |
42
|
|
|
* @Template("@ZikulaCategoriesModule/Registry/edit.html.twig") |
43
|
|
|
* |
44
|
|
|
* Creates or edits a category registry. |
45
|
|
|
* |
46
|
|
|
* @return array|RedirectResponse |
47
|
|
|
*/ |
48
|
|
|
public function editAction( |
49
|
|
|
Request $request, |
50
|
|
|
EntityManagerInterface $entityManager, |
51
|
|
|
CapabilityApiInterface $capabilityApi, |
52
|
|
|
CategoryRegistryRepositoryInterface $registryRepository, |
53
|
|
|
CategoryRegistryEntity $registryEntity = null |
54
|
|
|
) { |
55
|
|
|
if (null === $registryEntity) { |
56
|
|
|
$registryEntity = new CategoryRegistryEntity(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$form = $this->createForm(CategoryRegistryType::class, $registryEntity, [ |
60
|
|
|
'categorizableModules' => $this->getCategorizableModules($capabilityApi) |
61
|
|
|
]); |
62
|
|
|
$form->handleRequest($request); |
63
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
64
|
|
|
if ($form->get('save')->isClicked()) { |
|
|
|
|
65
|
|
|
$entityManager->persist($registryEntity); |
66
|
|
|
$entityManager->flush(); |
67
|
|
|
$this->addFlash('success', 'Done! Registry updated.'); |
68
|
|
|
} elseif ($form->get('cancel')->isClicked()) { |
69
|
|
|
$this->addFlash('status', 'Operation cancelled.'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->redirectToRoute('zikulacategoriesmodule_registry_edit'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return [ |
76
|
|
|
'form' => $form->createView(), |
77
|
|
|
'registries' => $registryRepository->findAll() |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function getCategorizableModules(CapabilityApiInterface $capabilityApi): array |
82
|
|
|
{ |
83
|
|
|
$modules = $capabilityApi->getExtensionsCapableOf(CapabilityApi::CATEGORIZABLE); |
84
|
|
|
$moduleOptions = []; |
85
|
|
|
foreach ($modules as $module) { |
86
|
|
|
$moduleName = $module->getName(); |
87
|
|
|
$moduleOptions[$moduleName] = $moduleName; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $moduleOptions; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @Route("/delete/{id}", requirements={"id" = "^[1-9]\d*$"}) |
95
|
|
|
* @Theme("admin") |
96
|
|
|
* @Template("@ZikulaCategoriesModule/Registry/delete.html.twig") |
97
|
|
|
* |
98
|
|
|
* Deletes a category registry. |
99
|
|
|
* |
100
|
|
|
* @return array|RedirectResponse |
101
|
|
|
*/ |
102
|
|
|
public function deleteAction( |
103
|
|
|
Request $request, |
104
|
|
|
EntityManagerInterface $entityManager, |
105
|
|
|
CategoryRegistryEntity $registry |
106
|
|
|
) { |
107
|
|
|
$form = $this->createForm(DeletionType::class); |
108
|
|
|
$form->handleRequest($request); |
109
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
110
|
|
|
if ($form->get('delete')->isClicked()) { |
111
|
|
|
$entityManager->remove($registry); |
112
|
|
|
$entityManager->flush(); |
113
|
|
|
$this->addFlash('success', 'Done! Registry entry deleted.'); |
114
|
|
|
} elseif ($form->get('cancel')->isClicked()) { |
115
|
|
|
$this->addFlash('status', 'Operation cancelled.'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $this->redirectToRoute('zikulacategoriesmodule_registry_edit'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return [ |
122
|
|
|
'form' => $form->createView(), |
123
|
|
|
'registry' => $registry |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|