|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controller\Admin; |
|
4
|
|
|
|
|
5
|
|
|
use App\Command\DeleteCategoryCommand; |
|
6
|
|
|
use App\Repository\CategoryRepository; |
|
7
|
|
|
use Psr\Log\LoggerInterface; |
|
8
|
|
|
use Ramsey\Uuid\Uuid; |
|
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
13
|
|
|
use Symfony\Component\Messenger\MessageBusInterface; |
|
14
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
15
|
|
|
use Symfony\Component\Form\FormError; |
|
16
|
|
|
use Knp\Component\Pager\PaginatorInterface; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
18
|
|
|
use App\Service\CategoryService; |
|
19
|
|
|
use Symfony\Component\Form\FormInterface; |
|
20
|
|
|
use Swagger\Annotations as SWG; |
|
21
|
|
|
use App\Traits\RequestQueryTrait; |
|
22
|
|
|
|
|
23
|
|
|
class AdminCategoryController extends AbstractController |
|
24
|
|
|
{ |
|
25
|
|
|
use RequestQueryTrait; |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var MessageBusInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $commandBus; |
|
31
|
|
|
/** |
|
32
|
|
|
* @var LoggerInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private $logger; |
|
35
|
|
|
/** |
|
36
|
|
|
* @var CategoryRepository |
|
37
|
|
|
*/ |
|
38
|
|
|
private $repository; |
|
39
|
|
|
/** |
|
40
|
|
|
* @var CategoryService |
|
41
|
|
|
*/ |
|
42
|
|
|
private $categoryService; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param MessageBusInterface $commandBus |
|
46
|
|
|
* @param LoggerInterface $logger |
|
47
|
|
|
* @param CategoryRepository $repository |
|
48
|
|
|
* @param CategoryService $categoryService |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct( |
|
51
|
|
|
MessageBusInterface $commandBus, |
|
52
|
|
|
LoggerInterface $logger, |
|
53
|
|
|
CategoryRepository $repository, |
|
54
|
|
|
CategoryService $categoryService |
|
55
|
|
|
) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->commandBus = $commandBus; |
|
58
|
|
|
$this->logger = $logger; |
|
59
|
|
|
$this->repository = $repository; |
|
60
|
|
|
$this->categoryService = $categoryService; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* |
|
65
|
|
|
* @Route("/admin/category/{id}", name="admin_category", defaults={"id"=null}, methods={"GET","POST"}) |
|
66
|
|
|
* @param string|null $id |
|
67
|
|
|
* @param Request $rawRequest |
|
68
|
|
|
* @return Response|RedirectResponse |
|
69
|
|
|
*/ |
|
70
|
|
|
public function createOrEditCategory(?string $id, Request $rawRequest): Response |
|
71
|
|
|
{ |
|
72
|
|
|
$form = $this->getForm($id); |
|
73
|
|
|
$form->handleRequest($rawRequest); |
|
74
|
|
|
|
|
75
|
|
|
try { |
|
76
|
|
|
if (!empty($id)) { |
|
77
|
|
|
$this->repository->getCategory(Uuid::fromString($id)); |
|
78
|
|
|
} |
|
79
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
80
|
|
|
$categoryDTO = $form->getData(); |
|
81
|
|
|
$categoryDTO->setId($id); |
|
82
|
|
|
$command = $this->categoryService->getCommand($categoryDTO); |
|
83
|
|
|
$this->commandBus->dispatch($command); |
|
84
|
|
|
$this->addFlash('success','Your changes were saved!'); |
|
85
|
|
|
return $this->redirectToRoute('admin_category', ['id' => $command->getId()]); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
} catch (\Exception $e) { |
|
88
|
|
|
$this->addFlash('danger','Error while saving changes'); |
|
89
|
|
|
$error = new FormError("There is an error: ".$e->getMessage()); |
|
90
|
|
|
$form->addError($error); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $this->render('admin/category/form.html.twig', [ |
|
94
|
|
|
'form' => $form->createView(), |
|
95
|
|
|
'id' => $id, |
|
96
|
|
|
]); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @Route("/admin/categories/list", name="admin_categories", methods={"GET"}) |
|
101
|
|
|
* @param PaginatorInterface $paginator |
|
102
|
|
|
* @param Request $request |
|
103
|
|
|
* @return Response |
|
104
|
|
|
*/ |
|
105
|
|
|
public function categoryList(PaginatorInterface $paginator, Request $request): Response |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->render('admin/category/list.html.twig', [ |
|
108
|
|
|
'pagination' => $paginator->paginate( |
|
109
|
|
|
$this->repository->listAllCategories( |
|
110
|
|
|
$request->query->getAlnum('sort', 'name'), |
|
111
|
|
|
$request->query->getAlnum('dir', 'asc') |
|
112
|
|
|
), $request->query->getInt('page', 1),10) |
|
113
|
|
|
]); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* |
|
118
|
|
|
* @Route("/admin/delete/category/{id}", name="admin_delete_category", methods={"GET"}) |
|
119
|
|
|
* @param string $id |
|
120
|
|
|
* @param Request $rawRequest |
|
121
|
|
|
* @return Response|RedirectResponse |
|
122
|
|
|
*/ |
|
123
|
|
|
public function deleteCategory(string $id, Request $rawRequest): Response |
|
|
|
|
|
|
124
|
|
|
{ |
|
125
|
|
|
try { |
|
126
|
|
|
$categoryId = Uuid::fromString($id); |
|
127
|
|
|
$command = new DeleteCategoryCommand($categoryId); |
|
128
|
|
|
$this->commandBus->dispatch($command); |
|
129
|
|
|
$this->addFlash('success','Category deleted!'); |
|
130
|
|
|
} catch (\Exception $e) { |
|
131
|
|
|
$this->addFlash('danger','Error while deleting category: '.$e->getMessage()); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $this->redirectToRoute('admin_categories'); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @Route("/autocomplete/categories", name="admin_categories_autocomplete", methods={"GET"}) |
|
139
|
|
|
* |
|
140
|
|
|
* @SWG\Tag(name="Autocomplete") |
|
141
|
|
|
* @SWG\Get( |
|
142
|
|
|
* @SWG\Parameter(name="q", in="path", type="string", description="Search query") |
|
143
|
|
|
* ) |
|
144
|
|
|
* @SWG\Response( |
|
145
|
|
|
* response="200", |
|
146
|
|
|
* description="Matched categories" |
|
147
|
|
|
* ) |
|
148
|
|
|
* |
|
149
|
|
|
* @param Request $request |
|
150
|
|
|
* @return JsonResponse |
|
151
|
|
|
*/ |
|
152
|
|
|
public function autocompleteAction(Request $request): JsonResponse |
|
153
|
|
|
{ |
|
154
|
|
|
$result = array(); |
|
155
|
|
|
$categories = $this->repository->autocomplete($this->getFromRequest($request, 'q', true)); |
|
156
|
|
|
|
|
157
|
|
|
foreach ($categories as $category) { |
|
158
|
|
|
$result[] = ['id' => $category->getId()->toString(), 'text'=>$category->getName()]; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return $this->json(['categories' => $result], 200); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param string|null $id |
|
166
|
|
|
* @return FormInterface |
|
167
|
|
|
*/ |
|
168
|
|
|
private function getForm($id = null): FormInterface |
|
169
|
|
|
{ |
|
170
|
|
|
if (empty($id)) { |
|
171
|
|
|
return $this->createForm(\App\Form\Type\CategoryType::class); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
$category = $this->repository->getCategory(Uuid::fromString($id)); |
|
175
|
|
|
$categoryDTO = $this->categoryService->fillCategoryDTO($category); |
|
176
|
|
|
return $this->createForm(\App\Form\Type\CategoryType::class, $categoryDTO); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|