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 flavien-metivier |
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 Request $request |
41
|
|
|
* @param string $repositoryName |
42
|
|
|
* |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
1 |
|
protected function getCollectionPaginated(Request $request, $repositoryName) |
46
|
|
|
{ |
47
|
1 |
|
$limit = $request->query->getInt('limit', 10); |
48
|
1 |
|
$page = $request->query->getInt('page', 1); |
49
|
1 |
|
$sorting = $request->query->get('sorting', array()); |
50
|
1 |
|
$searching = $request->query->get('searching', array()); |
51
|
|
|
|
52
|
1 |
|
$collectionPager = $this->get('doctrine.orm.entity_manager') |
53
|
1 |
|
->getRepository($repositoryName) |
54
|
1 |
|
->findAllPaginated($limit, $page, $sorting, $searching); |
55
|
|
|
|
56
|
1 |
|
$pagerFactory = new PagerfantaFactory(); |
57
|
|
|
|
58
|
1 |
|
return $pagerFactory->createRepresentation( |
59
|
|
|
$collectionPager, |
60
|
1 |
|
new Route($request->get('_route'), array( |
61
|
1 |
|
'limit' => $limit, |
62
|
1 |
|
'page' => $page, |
63
|
1 |
|
'sorting' => $sorting, |
64
|
1 |
|
'searching' => $searching, |
65
|
|
|
)) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param Request $request |
71
|
|
|
* @param string $repositoryName |
72
|
|
|
* |
73
|
|
|
* @return mixed|\FOS\RestBundle\View\View |
74
|
|
|
*/ |
75
|
2 |
|
protected function getAnElement(Request $request, $repositoryName) |
76
|
|
|
{ |
77
|
2 |
|
$anElement = $this->get('doctrine.orm.entity_manager') |
78
|
2 |
|
->getRepository($repositoryName) |
79
|
2 |
|
->find($request->get('id')); |
80
|
|
|
|
81
|
2 |
|
if (empty($anElement)) { |
82
|
1 |
|
return View::create(['message' => 'Element not found'], Response::HTTP_NOT_FOUND); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
return $anElement; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param Request $request |
90
|
|
|
* @param mixed $element |
91
|
|
|
* @param string $formClassName |
92
|
|
|
* |
93
|
|
|
* @return mixed |
94
|
|
|
*/ |
95
|
2 |
|
protected function createAnElement(Request $request, $element, $formClassName) |
96
|
|
|
{ |
97
|
2 |
|
$form = $this->createForm($formClassName, $element); |
98
|
|
|
|
99
|
2 |
|
$form->submit($request->request->all(), false); |
100
|
|
|
|
101
|
2 |
|
return $this->checkFormAndPersist($form, $element); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param Request $request |
106
|
|
|
* @param string $repositoryName |
107
|
|
|
*/ |
108
|
2 |
|
protected function removeAnElement(Request $request, $repositoryName) |
109
|
|
|
{ |
110
|
2 |
|
$em = $this->get('doctrine.orm.entity_manager'); |
111
|
2 |
|
$element = $em->getRepository($repositoryName) |
112
|
2 |
|
->find($request->get('id')); |
113
|
|
|
|
114
|
2 |
|
if ($element) { |
115
|
1 |
|
$em->remove($element); |
116
|
1 |
|
$em->flush(); |
117
|
|
|
} |
118
|
2 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param Request $request |
122
|
|
|
* @param bool $clearMissing |
123
|
|
|
* |
124
|
|
|
* @return mixed|Form|\FOS\RestBundle\View\View |
125
|
|
|
*/ |
126
|
6 |
|
protected function updateAnElement(Request $request, $clearMissing, $repositoryName, $formClassName) |
127
|
|
|
{ |
128
|
6 |
|
$element = $this->get('doctrine.orm.entity_manager') |
129
|
6 |
|
->getRepository($repositoryName) |
130
|
6 |
|
->find($request->get('id')); |
131
|
|
|
|
132
|
6 |
|
if (empty($element)) { |
133
|
2 |
|
return View::create(['message' => 'Element not found'], Response::HTTP_NOT_FOUND); |
134
|
|
|
} |
135
|
|
|
|
136
|
4 |
|
$form = $this->createForm($formClassName, $element); |
137
|
|
|
|
138
|
4 |
|
$form->submit($request->request->all(), $clearMissing); |
139
|
|
|
|
140
|
4 |
|
return $this->checkFormAndPersist($form, $element); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param Form $form |
145
|
|
|
* @param mixed $element |
146
|
|
|
* |
147
|
|
|
* @return mixed|Form |
148
|
|
|
*/ |
149
|
6 |
|
private function checkFormAndPersist(Form $form, $element) |
150
|
|
|
{ |
151
|
6 |
|
if ($form->isSubmitted() && $form->isValid()) { |
152
|
3 |
|
$em = $this->get('doctrine.orm.entity_manager'); |
153
|
3 |
|
$em->persist($element); |
154
|
3 |
|
$em->flush(); |
155
|
|
|
|
156
|
3 |
|
return $element; |
157
|
|
|
} |
158
|
|
|
|
159
|
3 |
|
return $form; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|