1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace QualityCode\ApiFeaturesBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
7
|
|
|
use FOS\RestBundle\View\View; |
8
|
|
|
use Hateoas\Configuration\Route; |
9
|
|
|
use Hateoas\Representation\Factory\PagerfantaFactory; |
10
|
|
|
use Symfony\Component\Form\Form; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Description of controllerBasicFeatures. |
14
|
|
|
* |
15
|
|
|
* @author fmetivier |
16
|
|
|
*/ |
17
|
|
|
trait ControllerBasicFeaturesTrait |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Gets a container service by its id. |
21
|
|
|
* |
22
|
|
|
* @param string $id The service id |
23
|
|
|
* |
24
|
|
|
* @return object The service |
25
|
|
|
*/ |
26
|
|
|
abstract protected function get($id); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Creates and returns a Form instance from the type of the form. |
30
|
|
|
* |
31
|
|
|
* @param string $type The fully qualified class name of the form type |
32
|
|
|
* @param mixed $data The initial data for the form |
33
|
|
|
* @param array $options Options for the form |
34
|
|
|
* |
35
|
|
|
* @return Form |
36
|
|
|
*/ |
37
|
|
|
abstract protected function createForm($type, $data = null, array $options = array()); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $repositoryName |
41
|
|
|
* |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
|
|
protected function getCollection($repositoryName) |
45
|
|
|
{ |
46
|
|
|
$collection = $this->get('doctrine.orm.entity_manager') |
47
|
|
|
->getRepository($repositoryName) |
48
|
|
|
->findAll(); |
49
|
|
|
|
50
|
|
|
return $collection; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param Request $request |
55
|
|
|
* @param string $repositoryName |
56
|
|
|
* |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
protected function getCollectionPaginated(Request $request, $repositoryName) |
60
|
|
|
{ |
61
|
|
|
$limit = $request->query->getInt('limit', 10); |
62
|
|
|
$page = $request->query->getInt('page', 1); |
63
|
|
|
$sorting = $request->query->get('sorting', array()); |
64
|
|
|
$searching = $request->query->get('searching', array()); |
65
|
|
|
|
66
|
|
|
$collectionPager = $this->get('doctrine.orm.entity_manager') |
67
|
|
|
->getRepository($repositoryName) |
68
|
|
|
->findAllPaginated($limit, $page, $sorting, $searching); |
69
|
|
|
|
70
|
|
|
$pagerFactory = new PagerfantaFactory(); |
71
|
|
|
|
72
|
|
|
return $pagerFactory->createRepresentation( |
73
|
|
|
$collectionPager, |
74
|
|
|
new Route($request->get('_route'), array( |
75
|
|
|
'limit' => $limit, |
76
|
|
|
'page' => $page, |
77
|
|
|
'sorting' => $sorting, |
78
|
|
|
'searching' => $searching, |
79
|
|
|
)) |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param Request $request |
85
|
|
|
* @param string $repositoryName |
86
|
|
|
* |
87
|
|
|
* @return mixed|\FOS\RestBundle\View\View |
88
|
|
|
*/ |
89
|
|
|
protected function getAnElement(Request $request, $repositoryName) |
90
|
|
|
{ |
91
|
|
|
$anElement = $this->get('doctrine.orm.entity_manager') |
92
|
|
|
->getRepository($repositoryName) |
93
|
|
|
->find($request->get('id')); |
94
|
|
|
|
95
|
|
|
if (empty($anElement)) { |
96
|
|
|
return View::create(['message' => 'Element not found'], Response::HTTP_NOT_FOUND); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $anElement; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param Request $request |
104
|
|
|
* @param mixed $element |
105
|
|
|
* @param string $formClassName |
106
|
|
|
* |
107
|
|
|
* @return mixed |
108
|
|
|
*/ |
109
|
|
|
protected function createAnElement(Request $request, $element, $formClassName) |
110
|
|
|
{ |
111
|
|
|
$form = $this->createForm($formClassName, $element); |
112
|
|
|
|
113
|
|
|
$form->submit($request->request->all(), false); |
114
|
|
|
|
115
|
|
|
return $this->checkFormAndPersist($form, $element); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param Request $request |
120
|
|
|
* @param string $repositoryName |
121
|
|
|
*/ |
122
|
|
|
protected function removeAnElement(Request $request, $repositoryName) |
123
|
|
|
{ |
124
|
|
|
$em = $this->get('doctrine.orm.entity_manager'); |
125
|
|
|
$element = $em->getRepository($repositoryName) |
126
|
|
|
->find($request->get('id')); |
127
|
|
|
|
128
|
|
|
if ($element) { |
129
|
|
|
$em->remove($element); |
130
|
|
|
$em->flush(); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param Request $request |
136
|
|
|
* @param bool $clearMissing |
137
|
|
|
* |
138
|
|
|
* @return mixed|Form|\FOS\RestBundle\View\View |
139
|
|
|
*/ |
140
|
|
|
protected function updateAnElement(Request $request, $clearMissing, $repositoryName, $formClassName) |
141
|
|
|
{ |
142
|
|
|
$element = $this->get('doctrine.orm.entity_manager') |
143
|
|
|
->getRepository($repositoryName) |
144
|
|
|
->find($request->get('id')); |
145
|
|
|
|
146
|
|
|
if (empty($element)) { |
147
|
|
|
return View::create(['message' => 'Element not found'], Response::HTTP_NOT_FOUND); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$form = $this->createForm($formClassName, $element); |
151
|
|
|
|
152
|
|
|
$form->submit($request->request->all(), $clearMissing); |
153
|
|
|
|
154
|
|
|
return $this->checkFormAndPersist($form, $element); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param Form $form |
159
|
|
|
* @param mixed $element |
160
|
|
|
* |
161
|
|
|
* @return mixed|Form |
162
|
|
|
*/ |
163
|
|
|
private function checkFormAndPersist(Form $form, $element) |
164
|
|
|
{ |
165
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
166
|
|
|
$em = $this->get('doctrine.orm.entity_manager'); |
167
|
|
|
$em->persist($element); |
168
|
|
|
$em->flush(); |
169
|
|
|
|
170
|
|
|
return $element; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $form; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|