1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivoaz ContentEditable bundle. |
5
|
|
|
* |
6
|
|
|
* (c) Ivo Azirjans <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivoaz\Bundle\ContentEditableBundle\Controller; |
13
|
|
|
|
14
|
|
|
use Ivoaz\Bundle\ContentEditableBundle\Form\Type\ContentBatchType; |
15
|
|
|
use Ivoaz\Bundle\ContentEditableBundle\Form\Type\ContentType; |
16
|
|
|
use Ivoaz\Bundle\ContentEditableBundle\Manager\ContentManagerInterface; |
17
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
18
|
|
|
use Symfony\Component\Form\FormInterface; |
19
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
22
|
|
|
use Symfony\Component\Validator\Constraints; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Ivo Azirjans <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class ContentController |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var ContentManagerInterface |
31
|
|
|
*/ |
32
|
|
|
private $manager; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var FormFactoryInterface |
36
|
|
|
*/ |
37
|
|
|
private $formFactory; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var AuthorizationCheckerInterface |
41
|
|
|
*/ |
42
|
|
|
private $authorizationChecker; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param ContentManagerInterface $manager |
46
|
|
|
* @param FormFactoryInterface $formFactory |
47
|
|
|
* @param AuthorizationCheckerInterface $authorizationChecker |
48
|
|
|
*/ |
49
|
8 |
|
public function __construct( |
50
|
|
|
ContentManagerInterface $manager, |
51
|
|
|
FormFactoryInterface $formFactory, |
52
|
|
|
AuthorizationCheckerInterface $authorizationChecker |
53
|
|
|
) { |
54
|
8 |
|
$this->manager = $manager; |
55
|
8 |
|
$this->formFactory = $formFactory; |
56
|
8 |
|
$this->authorizationChecker = $authorizationChecker; |
57
|
8 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param Request $request |
61
|
|
|
* |
62
|
|
|
* @return JsonResponse |
63
|
|
|
*/ |
64
|
4 |
|
public function updateAction(Request $request) |
65
|
|
|
{ |
66
|
4 |
|
if (!$this->isAuthorized()) { |
67
|
1 |
|
return new JsonResponse(null, JsonResponse::HTTP_FORBIDDEN); |
68
|
|
|
} |
69
|
|
|
|
70
|
3 |
|
$id = $request->get('id'); |
71
|
3 |
|
$content = $this->manager->find($id); |
72
|
|
|
|
73
|
3 |
|
if (null === $content) { |
74
|
1 |
|
return $this->createErrorResponse([$this->createContentNotFoundError($id)]); |
75
|
|
|
} |
76
|
|
|
|
77
|
2 |
|
$form = $this->formFactory->create(ContentType::class); |
78
|
2 |
|
$form->handleRequest($request); |
79
|
|
|
|
80
|
2 |
|
if (!$form->isValid()) { |
81
|
1 |
|
$errors = $this->getErrors($form); |
82
|
|
|
|
83
|
1 |
|
return $this->createErrorResponse($errors); |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
$form->getData()->update($content); |
87
|
1 |
|
$this->manager->update($content); |
88
|
|
|
|
89
|
1 |
|
return new JsonResponse(null, JsonResponse::HTTP_NO_CONTENT); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param Request $request |
94
|
|
|
* |
95
|
|
|
* @return JsonResponse |
96
|
|
|
*/ |
97
|
4 |
|
public function batchUpdateAction(Request $request) |
98
|
|
|
{ |
99
|
4 |
|
if (!$this->isAuthorized()) { |
100
|
1 |
|
return new JsonResponse(null, JsonResponse::HTTP_FORBIDDEN); |
101
|
|
|
} |
102
|
|
|
|
103
|
3 |
|
$form = $this->formFactory->create(BatchType::class); |
104
|
3 |
|
$form->handleRequest($request); |
105
|
|
|
|
106
|
3 |
|
if (!$form->isValid()) { |
107
|
1 |
|
$errors = $this->getErrors($form); |
108
|
|
|
|
109
|
1 |
|
return $this->createErrorResponse($errors); |
110
|
|
|
} |
111
|
|
|
|
112
|
2 |
|
$errors = []; |
113
|
2 |
|
$contents = []; |
114
|
|
|
|
115
|
2 |
|
foreach ($form->getData()->contents as $data) { |
116
|
2 |
|
$content = $this->manager->find($data->id); |
117
|
|
|
|
118
|
2 |
|
if (null === $content) { |
119
|
1 |
|
$errors[] = $this->createContentNotFoundError($data->id); |
120
|
|
|
|
121
|
1 |
|
continue; |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
$data->update($content); |
125
|
1 |
|
$contents[] = $content; |
126
|
2 |
|
} |
127
|
|
|
|
128
|
2 |
|
if (0 !== count($errors)) { |
129
|
1 |
|
return $this->createErrorResponse($errors); |
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
foreach ($contents as $content) { |
133
|
1 |
|
$this->manager->update($content); |
134
|
1 |
|
} |
135
|
|
|
|
136
|
1 |
|
return new JsonResponse(null, JsonResponse::HTTP_NO_CONTENT); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param FormInterface $form |
141
|
|
|
* |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
2 |
|
private function getErrors(FormInterface $form) |
145
|
|
|
{ |
146
|
2 |
|
$errors = []; |
147
|
|
|
|
148
|
2 |
|
foreach ($form->getErrors(true, true) as $error) { |
149
|
2 |
|
$errors[] = ['title' => $error->getMessage()]; |
150
|
2 |
|
} |
151
|
|
|
|
152
|
2 |
|
return $errors; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param array $errors |
157
|
|
|
* @param int $status |
158
|
|
|
* |
159
|
|
|
* @return JsonResponse |
160
|
|
|
*/ |
161
|
4 |
|
private function createErrorResponse(array $errors, $status = 400) |
162
|
|
|
{ |
163
|
4 |
|
return new JsonResponse(['errors' => $errors], $status); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return bool |
168
|
|
|
*/ |
169
|
8 |
|
private function isAuthorized() |
170
|
|
|
{ |
171
|
8 |
|
return $this->authorizationChecker->isGranted('ROLE_ADMIN'); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param $id |
176
|
|
|
* |
177
|
|
|
* @return array |
178
|
|
|
*/ |
179
|
2 |
|
private function createContentNotFoundError($id) |
180
|
|
|
{ |
181
|
2 |
|
return ['title' => sprintf('Content with id "%s" was not found.', $id)]; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|