|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Sulu. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) MASSIVE ART WebServices GmbH |
|
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 Sulu\Bundle\CommentBundle\Controller; |
|
13
|
|
|
|
|
14
|
|
|
use FOS\RestBundle\Controller\Annotations\Get; |
|
15
|
|
|
use FOS\RestBundle\Routing\ClassResourceInterface; |
|
16
|
|
|
use Sulu\Bundle\CommentBundle\Entity\ThreadInterface; |
|
17
|
|
|
use Sulu\Component\Rest\Exception\EntityNotFoundException; |
|
18
|
|
|
use Sulu\Component\Rest\ListBuilder\FieldDescriptorInterface; |
|
19
|
|
|
use Sulu\Component\Rest\ListBuilder\ListRepresentation; |
|
20
|
|
|
use Sulu\Component\Rest\RequestParametersTrait; |
|
21
|
|
|
use Sulu\Component\Rest\RestController; |
|
22
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
23
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Provides an api for threads. |
|
27
|
|
|
*/ |
|
28
|
|
|
class ThreadController extends RestController implements ClassResourceInterface |
|
29
|
|
|
{ |
|
30
|
|
|
use RequestParametersTrait; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Returns list of field-descriptors. |
|
34
|
|
|
* |
|
35
|
|
|
* @Get("/threads/fields") |
|
36
|
|
|
* |
|
37
|
|
|
* @return Response |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getFieldsAction() |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->handleView($this->view($this->getFieldDescriptors())); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Returns list of threads. |
|
46
|
|
|
* |
|
47
|
|
|
* @param Request $request |
|
48
|
|
|
* |
|
49
|
|
|
* @return Response |
|
50
|
|
|
*/ |
|
51
|
3 |
|
public function cgetAction(Request $request) |
|
52
|
|
|
{ |
|
53
|
3 |
|
$restHelper = $this->get('sulu_core.doctrine_rest_helper'); |
|
54
|
3 |
|
$factory = $this->get('sulu_core.doctrine_list_builder_factory'); |
|
55
|
3 |
|
$listBuilder = $factory->create($this->getParameter('sulu.model.thread.class')); |
|
56
|
|
|
|
|
57
|
3 |
|
$fieldDescriptors = $this->getFieldDescriptors(); |
|
58
|
3 |
|
$restHelper->initializeListBuilder($listBuilder, $fieldDescriptors); |
|
59
|
|
|
|
|
60
|
3 |
View Code Duplication |
foreach ($request->query->all() as $filterKey => $filterValue) { |
|
|
|
|
|
|
61
|
3 |
|
if (isset($fieldDescriptors[$filterKey])) { |
|
62
|
3 |
|
$listBuilder->where($fieldDescriptors[$filterKey], $filterValue); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
3 |
|
$typeParameter = $request->get('types'); |
|
67
|
3 |
|
if ($typeParameter) { |
|
68
|
1 |
|
$listBuilder->in($fieldDescriptors['type'], array_filter(explode(',', $typeParameter))); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
3 |
|
$items = $listBuilder->execute(); |
|
72
|
3 |
|
$list = new ListRepresentation( |
|
73
|
|
|
$items, |
|
74
|
3 |
|
'threads', |
|
75
|
3 |
|
'get_threads', |
|
76
|
3 |
|
$request->query->all(), |
|
77
|
3 |
|
$listBuilder->getCurrentPage(), |
|
78
|
3 |
|
$listBuilder->getLimit(), |
|
79
|
3 |
|
$listBuilder->count() |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
3 |
|
return $this->handleView($this->view($list, 200)); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Returns single thread. |
|
87
|
|
|
* |
|
88
|
|
|
* @param int $id |
|
89
|
|
|
* |
|
90
|
|
|
* @return Response |
|
91
|
|
|
* |
|
92
|
|
|
* @throws EntityNotFoundException |
|
93
|
|
|
*/ |
|
94
|
1 |
|
public function getAction($id) |
|
95
|
|
|
{ |
|
96
|
1 |
|
$thread = $this->get('sulu.repository.thread')->findThreadById($id); |
|
97
|
1 |
|
if (!$thread) { |
|
98
|
|
|
throw new EntityNotFoundException(ThreadInterface::class, $id); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
1 |
|
return $this->handleView($this->view($thread)); |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Update thread. |
|
106
|
|
|
* |
|
107
|
|
|
* @param int $id |
|
108
|
|
|
* @param Request $request |
|
109
|
|
|
* |
|
110
|
|
|
* @return Response |
|
111
|
|
|
* |
|
112
|
|
|
* @throws EntityNotFoundException |
|
113
|
|
|
*/ |
|
114
|
1 |
View Code Duplication |
public function putAction($id, Request $request) |
|
|
|
|
|
|
115
|
|
|
{ |
|
116
|
|
|
/** @var ThreadInterface $thread */ |
|
117
|
1 |
|
$thread = $this->get('sulu.repository.thread')->findThreadById($id); |
|
118
|
1 |
|
if (!$thread) { |
|
119
|
|
|
throw new EntityNotFoundException(ThreadInterface::class, $id); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
1 |
|
$thread->setTitle($request->request->get('title')); |
|
123
|
|
|
|
|
124
|
1 |
|
$this->get('sulu_comment.manager')->updateThread($thread); |
|
125
|
1 |
|
$this->get('doctrine.orm.entity_manager')->flush(); |
|
126
|
|
|
|
|
127
|
1 |
|
return $this->handleView($this->view($thread)); |
|
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Delete thread identified by id. |
|
132
|
|
|
* |
|
133
|
|
|
* @param int $id |
|
134
|
|
|
* |
|
135
|
|
|
* @return Response |
|
136
|
|
|
*/ |
|
137
|
1 |
View Code Duplication |
public function deleteAction($id) |
|
|
|
|
|
|
138
|
|
|
{ |
|
139
|
1 |
|
$this->get('sulu_comment.manager')->deleteThreads([$id]); |
|
140
|
1 |
|
$this->get('doctrine.orm.entity_manager')->flush(); |
|
141
|
|
|
|
|
142
|
1 |
|
return $this->handleView($this->view(null, 204)); |
|
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Delete multiple threads identified by ids parameter. |
|
147
|
|
|
* |
|
148
|
|
|
* @param Request $request |
|
149
|
|
|
* |
|
150
|
|
|
* @return Response |
|
151
|
|
|
*/ |
|
152
|
1 |
View Code Duplication |
public function cdeleteAction(Request $request) |
|
|
|
|
|
|
153
|
|
|
{ |
|
154
|
1 |
|
$ids = array_filter(explode(',', $request->get('ids', ''))); |
|
155
|
|
|
|
|
156
|
1 |
|
if (0 === count($ids)) { |
|
157
|
|
|
return $this->handleView($this->view(null, 204)); |
|
|
|
|
|
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
1 |
|
$this->get('sulu_comment.manager')->deleteThreads($ids); |
|
161
|
1 |
|
$this->get('doctrine.orm.entity_manager')->flush(); |
|
162
|
|
|
|
|
163
|
1 |
|
return $this->handleView($this->view(null, 204)); |
|
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Returns array of field-descriptors. |
|
168
|
|
|
* |
|
169
|
|
|
* @return FieldDescriptorInterface[] |
|
170
|
|
|
*/ |
|
171
|
3 |
|
private function getFieldDescriptors() |
|
172
|
|
|
{ |
|
173
|
3 |
|
return $this->get('sulu_core.list_builder.field_descriptor_factory') |
|
174
|
3 |
|
->getFieldDescriptorForClass($this->getParameter('sulu.model.thread.class')); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: