1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Superdesk Web Publisher Core Bundle. |
5
|
|
|
* |
6
|
|
|
* Copyright 2015 Sourcefabric z.u. and contributors. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please see the |
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @copyright 2015 Sourcefabric z.ú |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace SWP\Bundle\CoreBundle\Controller; |
16
|
|
|
|
17
|
|
|
use FOS\RestBundle\Controller\FOSRestController; |
18
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc; |
19
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; |
20
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
21
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
22
|
|
|
use SWP\Component\Common\Criteria\Criteria; |
23
|
|
|
use SWP\Component\Common\Pagination\PaginationData; |
24
|
|
|
use SWP\Component\Common\Response\ResourcesListResponse; |
25
|
|
|
use SWP\Component\Common\Response\ResponseContext; |
26
|
|
|
use SWP\Component\Common\Response\SingleResourceResponse; |
27
|
|
|
use SWP\Bundle\TemplatesSystemBundle\Form\Type\WidgetType; |
28
|
|
|
use SWP\Bundle\CoreBundle\Model\WidgetModel; |
29
|
|
|
use Symfony\Component\Finder\Finder; |
30
|
|
|
use Symfony\Component\HttpFoundation\Request; |
31
|
|
|
use Symfony\Component\HttpKernel\Exception\ConflictHttpException; |
32
|
|
|
|
33
|
|
|
class WidgetController extends FOSRestController |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Lists all registered widgets. |
37
|
|
|
* |
38
|
|
|
* @ApiDoc( |
39
|
|
|
* resource=true, |
40
|
|
|
* description="Lists all registered widgets", |
41
|
|
|
* statusCodes={ |
42
|
|
|
* 200="Returned on success." |
43
|
|
|
* }, |
44
|
|
|
* filters={ |
45
|
|
|
* {"name"="sorting", "dataType"="string", "pattern"="[updatedAt]=asc|desc"} |
46
|
|
|
* } |
47
|
|
|
* ) |
48
|
2 |
|
* @Route("/api/{version}/templates/widgets/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_templates_list_widgets") |
49
|
|
|
* @Method("GET") |
50
|
2 |
|
* @Cache(expires="10 minutes", public=true) |
51
|
2 |
|
*/ |
52
|
2 |
|
public function listAction(Request $request) |
53
|
2 |
|
{ |
54
|
2 |
|
$repository = $this->get('swp.repository.widget_model'); |
55
|
2 |
|
|
56
|
|
|
$widgets = $repository->getPaginatedByCriteria(new Criteria(), $request->query->get('sorting', []), new PaginationData($request)); |
57
|
|
|
|
58
|
2 |
|
return new ResourcesListResponse($widgets); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
2 |
|
* Lists all theme widgets templates. |
63
|
|
|
* |
64
|
|
|
* @ApiDoc( |
65
|
|
|
* resource=true, |
66
|
|
|
* description="Lists all theme widgets templates", |
67
|
|
|
* statusCodes={ |
68
|
|
|
* 200="Returned on success." |
69
|
|
|
* } |
70
|
|
|
* ) |
71
|
|
|
* @Route("/api/{version}/templates/widgets/templates/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_templates_list_widgets_templates") |
72
|
|
|
* @Method("GET") |
73
|
|
|
* Cache(expires="10 minutes", public=true) |
74
|
|
|
*/ |
75
|
|
|
public function listTemplatesAction(Request $request) |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$themeContext = $this->container->get('sylius.context.theme'); |
78
|
|
|
$path = $themeContext->getTheme()->getPath().DIRECTORY_SEPARATOR.'views'.DIRECTORY_SEPARATOR.'widgets'; |
79
|
|
|
|
80
|
5 |
|
if (!file_exists($path)) { |
81
|
|
|
return new SingleResourceResponse([]); |
82
|
5 |
|
} |
83
|
|
|
|
84
|
|
|
$files = Finder::create() |
85
|
|
|
->files() |
86
|
5 |
|
->depth(0) |
87
|
5 |
|
->in($path) |
88
|
|
|
->sortByName(); |
89
|
5 |
|
|
90
|
2 |
|
$templates = []; |
91
|
|
|
|
92
|
|
|
foreach ($files as $file) { |
93
|
|
|
$templates[] = [ |
94
|
5 |
|
'name' => $file->getFilename(), |
95
|
2 |
|
'modified' => date('Y-m-d H:i:s', $file->getMTime()), |
96
|
|
|
]; |
97
|
|
|
} |
98
|
3 |
|
|
99
|
|
|
return new SingleResourceResponse($templates); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get single widget. |
104
|
|
|
* |
105
|
|
|
* @ApiDoc( |
106
|
|
|
* resource=true, |
107
|
|
|
* description="Get single widget", |
108
|
|
|
* statusCodes={ |
109
|
|
|
* 200="Returned on success.", |
110
|
|
|
* 404="Widget not found", |
111
|
|
|
* 422="Widget id is not number" |
112
|
|
|
* } |
113
|
|
|
* ) |
114
|
|
|
* @Route("/api/{version}/templates/widgets/{id}", requirements={"id"="\d+"}, options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_templates_get_widget") |
115
|
|
|
* @Method("GET") |
116
|
2 |
|
*/ |
117
|
|
|
public function getAction($id) |
118
|
2 |
|
{ |
119
|
|
|
return new SingleResourceResponse($this->findOr404($id)); |
120
|
2 |
|
} |
121
|
2 |
|
|
122
|
2 |
|
/** |
123
|
2 |
|
* Create new widget. |
124
|
2 |
|
* |
125
|
2 |
|
* Note: |
126
|
|
|
* |
127
|
2 |
|
* Widget Type can be widget ID or his class (string). |
128
|
|
|
* Example: "SWP\\Bundle\\CoreBundle\\Widget\\ContentListWidget" |
129
|
|
|
* |
130
|
|
|
* @ApiDoc( |
131
|
|
|
* resource=true, |
132
|
|
|
* description="Create new widget", |
133
|
|
|
* statusCodes={ |
134
|
|
|
* 201="Returned on success.", |
135
|
|
|
* 400="Returned when form have errors" |
136
|
|
|
* }, |
137
|
|
|
* input="SWP\Bundle\TemplatesSystemBundle\Form\Type\WidgetType" |
138
|
|
|
* ) |
139
|
|
|
* @Route("/api/{version}/templates/widgets/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_templates_create_widget") |
140
|
|
|
* @Method("POST") |
141
|
|
|
*/ |
142
|
|
View Code Duplication |
public function createAction(Request $request) |
|
|
|
|
143
|
|
|
{ |
144
|
|
|
$widget = new WidgetModel(); |
145
|
|
|
$form = $this->createForm(WidgetType::class, $widget); |
146
|
|
|
$form->handleRequest($request); |
147
|
|
|
if ($form->isValid()) { |
148
|
1 |
|
$this->ensureWidgetDontExists($widget->getName()); |
149
|
|
|
$this->getWidgetRepository()->add($widget); |
150
|
1 |
|
|
151
|
|
|
return new SingleResourceResponse($widget, new ResponseContext(201)); |
152
|
|
|
} |
153
|
|
|
|
154
|
1 |
|
return new SingleResourceResponse($form, new ResponseContext(400)); |
155
|
1 |
|
} |
156
|
|
|
|
157
|
1 |
|
/** |
158
|
|
|
* Delete single widget. |
159
|
|
|
* |
160
|
|
|
* @ApiDoc( |
161
|
1 |
|
* resource=true, |
162
|
|
|
* description="Delete single widget", |
163
|
|
|
* statusCodes={ |
164
|
|
|
* 204="Returned on success.", |
165
|
1 |
|
* 404="Widget not found", |
166
|
1 |
|
* 422="Widget id is not number" |
167
|
|
|
* } |
168
|
1 |
|
* ) |
169
|
|
|
* @Route("/api/{version}/templates/widgets/{id}", requirements={"id"="\d+"}, options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_templates_delete_widget") |
170
|
|
|
* @Method("DELETE") |
171
|
|
|
*/ |
172
|
|
|
public function deleteAction($id) |
173
|
|
|
{ |
174
|
|
|
$entityManager = $this->get('doctrine')->getManager(); |
175
|
|
|
$widget = $this->findOr404($id); |
176
|
|
|
|
177
|
|
|
foreach ($widget->getContainers() as $containerWidget) { |
178
|
|
|
$entityManager->remove($containerWidget); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$this->getWidgetRepository()->remove($widget); |
182
|
|
|
|
183
|
|
|
return new SingleResourceResponse(null, new ResponseContext(204)); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Update single widget. |
188
|
1 |
|
* |
189
|
|
|
* @ApiDoc( |
190
|
1 |
|
* resource=true, |
191
|
|
|
* description="Update single widget", |
192
|
|
|
* statusCodes={ |
193
|
|
|
* 201="Returned on success.", |
194
|
1 |
|
* 404="Widget not found", |
195
|
1 |
|
* 422="Widget id is not number", |
196
|
|
|
* 405="Method Not Allowed" |
197
|
1 |
|
* }, |
198
|
|
|
* input="SWP\Bundle\TemplatesSystemBundle\Form\Type\WidgetType" |
199
|
|
|
* ) |
200
|
|
|
* @Route("/api/{version}/templates/widgets/{id}", requirements={"id"="\d+"}, options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_templates_update_widget") |
201
|
1 |
|
* @Method("PATCH") |
202
|
1 |
|
*/ |
203
|
|
View Code Duplication |
public function updateAction(Request $request, $id) |
|
|
|
|
204
|
|
|
{ |
205
|
1 |
|
$widget = $this->findOr404($id); |
206
|
1 |
|
$form = $this->createForm(WidgetType::class, $widget, [ |
207
|
1 |
|
'method' => $request->getMethod(), |
208
|
1 |
|
]); |
209
|
|
|
|
210
|
1 |
|
$form->handleRequest($request); |
211
|
|
|
if ($form->isValid()) { |
212
|
|
|
$this->getWidgetRepository()->add($widget); |
213
|
|
|
|
214
|
|
|
return new SingleResourceResponse($widget, new ResponseContext(201)); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return new SingleResourceResponse($form); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
private function findOr404(int $id) |
221
|
|
|
{ |
222
|
|
|
if (null === $widget = $this->getWidgetRepository()->findOneById($id)) { |
223
|
|
|
throw $this->createNotFoundException(sprintf('Widget with id "%s" was not found.', $id)); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return $widget; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
private function ensureWidgetDontExists(string $name) |
230
|
|
|
{ |
231
|
|
|
if (null !== $this->getWidgetRepository()->findOneByName($name)) { |
232
|
|
|
throw new ConflictHttpException(sprintf('Widget with name "%s" already exists.', $name)); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
private function getWidgetRepository() |
237
|
|
|
{ |
238
|
|
|
return $this->get('swp.repository.widget_model'); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.