|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* AbstractController controller des méthodes communes. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP Version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* @author Quétier Laurent <[email protected]> |
|
8
|
|
|
* @copyright 2014 Dev-Int GLSR |
|
9
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
10
|
|
|
* |
|
11
|
|
|
* @version since 1.0.0 |
|
12
|
|
|
* |
|
13
|
|
|
* @link https://github.com/Dev-Int/glsr |
|
14
|
|
|
*/ |
|
15
|
|
|
namespace AppBundle\Controller; |
|
16
|
|
|
|
|
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
18
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
19
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Abstract controller. |
|
23
|
|
|
* |
|
24
|
|
|
* @category Controller |
|
25
|
|
|
*/ |
|
26
|
|
|
abstract class AbstractController extends Controller |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Lists all items entity. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $entityName Name of Entity |
|
32
|
|
|
* @return array |
|
33
|
|
|
*/ |
|
34
|
|
|
public function abstractIndexAction($entityName) |
|
35
|
|
|
{ |
|
36
|
|
|
$etm = $this->getDoctrine()->getManager(); |
|
37
|
|
|
$entities = $etm->getRepository('AppBundle:'.$entityName)->findAll(); |
|
38
|
|
|
|
|
39
|
|
|
return array( |
|
40
|
|
|
'entities' => $entities, |
|
41
|
|
|
'ctEntity' => count($entities), |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Finds and displays an item entity. |
|
47
|
|
|
* |
|
48
|
|
|
* @param Object $entity Entity |
|
49
|
|
|
* @param string $entityName Name of Entity |
|
50
|
|
|
* @return array |
|
51
|
|
|
*/ |
|
52
|
|
|
public function abstractShowAction($entity, $entityName) |
|
53
|
|
|
{ |
|
54
|
|
|
$deleteForm = $this->createDeleteForm($entity->getId(), $entityName.'_delete'); |
|
55
|
|
|
|
|
56
|
|
|
return array( |
|
57
|
|
|
$entityName => $entity, |
|
58
|
|
|
'delete_form' => $deleteForm->createView(), |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Displays a form to create a new item entity. |
|
64
|
|
|
* |
|
65
|
|
|
* @param Object $entity Entity |
|
66
|
|
|
* @param string $entityPath Path of Entity |
|
67
|
|
|
* @param string $typePath Path of FormType |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
|
|
public function abstractNewAction($entity, $entityPath, $typePath) |
|
71
|
|
|
{ |
|
72
|
|
|
$etm = $this->getDoctrine()->getManager(); |
|
73
|
|
|
$ctEntity = count($etm->getRepository('AppBundle:'.$entity)->findAll()); |
|
74
|
|
|
|
|
75
|
|
|
$entityNew = $etm->getClassMetadata($entityPath)->newInstance(); |
|
76
|
|
|
$form = $this->createForm(new $typePath(), $entityNew); |
|
77
|
|
|
|
|
78
|
|
|
$return = array(strtolower($entity) => $entityNew, 'form' => $form->createView(),); |
|
79
|
|
|
|
|
80
|
|
|
if ($ctEntity >= 1) { |
|
81
|
|
|
$return = $this->redirectToRoute('_home'); |
|
82
|
|
|
$this->addFlash('danger', 'gestock.settings.'.strtolower($entity).'.add2'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $return; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Creates a new item entity. |
|
90
|
|
|
* |
|
91
|
|
|
* @param Request $request Request in progress |
|
92
|
|
|
* @param Object $entity Entity |
|
93
|
|
|
* @param string $entityPath Path of Entity |
|
94
|
|
|
* @param string $typePath Path of FormType |
|
95
|
|
|
* @return array |
|
96
|
|
|
*/ |
|
97
|
|
|
public function abstractCreateAction(Request $request, $entity, $entityPath, $typePath) |
|
98
|
|
|
{ |
|
99
|
|
|
$etm = $this->getDoctrine()->getManager(); |
|
100
|
|
|
$entityNew = $etm->getClassMetadata($entityPath)->newInstance(); |
|
101
|
|
|
$form = $this->createForm(new $typePath(), $entityNew); |
|
102
|
|
|
if ($form->handleRequest($request)->isValid()) { |
|
103
|
|
|
$etm = $this->getDoctrine()->getManager(); |
|
104
|
|
|
$etm->persist($entityNew); |
|
105
|
|
|
$etm->flush(); |
|
106
|
|
|
|
|
107
|
|
|
return $this->redirectToRoute($entity.'_show', array('id' => $entityNew->getId())); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return array( |
|
111
|
|
|
$entity => $entityNew, |
|
112
|
|
|
'form' => $form->createView(), |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Displays a form to edit an existing item entity. |
|
118
|
|
|
* |
|
119
|
|
|
* @param Object $entity Entity |
|
120
|
|
|
* @param string $entityName Name of Entity |
|
121
|
|
|
* @param string $typePath Path of FormType |
|
122
|
|
|
* @return array |
|
123
|
|
|
*/ |
|
124
|
|
|
public function abstractEditAction($entity, $entityName, $typePath) |
|
125
|
|
|
{ |
|
126
|
|
|
$editForm = $this->createForm(new $typePath(), $entity, array( |
|
127
|
|
|
'action' => $this->generateUrl($entityName.'_update', array('id' => $entity->getId())), |
|
128
|
|
|
'method' => 'PUT', |
|
129
|
|
|
)); |
|
130
|
|
|
$deleteForm = $this->createDeleteForm($entity->getId(), $entityName.'_delete'); |
|
131
|
|
|
|
|
132
|
|
|
return array( |
|
133
|
|
|
$entityName => $entity, |
|
134
|
|
|
'edit_form' => $editForm->createView(), |
|
135
|
|
|
'delete_form' => $deleteForm->createView(), |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Edits an existing item entity. |
|
141
|
|
|
* |
|
142
|
|
|
* @param Object $entity Entity |
|
143
|
|
|
* @param Request $request Request in progress |
|
144
|
|
|
* @param string $entityName Name of Entity |
|
145
|
|
|
* @param string $typePath Path of FormType |
|
146
|
|
|
* @return array |
|
147
|
|
|
*/ |
|
148
|
|
|
public function abstractUpdateAction($entity, Request $request, $entityName, $typePath) |
|
149
|
|
|
{ |
|
150
|
|
|
$editForm = $this->createForm(new $typePath(), $entity, array( |
|
151
|
|
|
'action' => $this->generateUrl($entityName.'_update', array('id' => $entity->getId())), |
|
152
|
|
|
'method' => 'PUT', |
|
153
|
|
|
)); |
|
154
|
|
|
if ($editForm->handleRequest($request)->isValid()) { |
|
155
|
|
|
$this->getDoctrine()->getManager()->flush(); |
|
156
|
|
|
$this->addFlash('info', 'gestock.settings.'.$entityName.'.edit_ok'); |
|
157
|
|
|
|
|
158
|
|
|
return $this->redirectToRoute($entityName.'_edit', array('id' => $entity->getId())); |
|
159
|
|
|
} |
|
160
|
|
|
$deleteForm = $this->createDeleteForm($entity->getId(), $entityName.'_delete'); |
|
161
|
|
|
|
|
162
|
|
|
return array( |
|
163
|
|
|
$entityName => $entity, |
|
164
|
|
|
'edit_form' => $editForm->createView(), |
|
165
|
|
|
'delete_form' => $deleteForm->createView(), |
|
166
|
|
|
); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Deletes an item entity. |
|
171
|
|
|
* |
|
172
|
|
|
* @param Object $entity Entity |
|
173
|
|
|
* @param Request $request Request in progress |
|
174
|
|
|
* @param string $entityName Name of Entity |
|
175
|
|
|
* @return array |
|
176
|
|
|
*/ |
|
177
|
|
|
public function abstractDeleteAction($entity, Request $request, $entityName) |
|
178
|
|
|
{ |
|
179
|
|
|
$form = $this->createDeleteForm($entity->getId(), $entityName.'_delete'); |
|
180
|
|
|
if ($form->handleRequest($request)->isValid()) { |
|
181
|
|
|
$etm = $this->getDoctrine()->getManager(); |
|
182
|
|
|
$etm->remove($entity); |
|
183
|
|
|
$etm->flush(); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* SetOrder for the SortAction in views. |
|
189
|
|
|
* |
|
190
|
|
|
* @param string $name session name |
|
191
|
|
|
* @param string $field field name |
|
192
|
|
|
* @param string $type sort type ("ASC"/"DESC") |
|
193
|
|
|
*/ |
|
194
|
|
|
protected function setOrder($name, $field, $type = 'ASC') |
|
195
|
|
|
{ |
|
196
|
|
|
$this->getRequest()->getSession()->set('sort.'.$name, array('field' => $field, 'type' => $type)); |
|
|
|
|
|
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* GetOrder for the SortAction in views. |
|
201
|
|
|
* |
|
202
|
|
|
* @param string $name |
|
203
|
|
|
* |
|
204
|
|
|
* @return array |
|
205
|
|
|
*/ |
|
206
|
|
|
protected function getOrder($name) |
|
207
|
|
|
{ |
|
208
|
|
|
$session = $this->getRequest()->getSession(); |
|
|
|
|
|
|
209
|
|
|
|
|
210
|
|
|
return $session->has('sort.' . $name) ? $session->get('sort.' . $name) : null; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* AddQueryBuilderSort for the SortAction in views. |
|
215
|
|
|
* |
|
216
|
|
|
* @param QueryBuilder $qb |
|
217
|
|
|
* @param string $name |
|
218
|
|
|
*/ |
|
219
|
|
|
protected function addQueryBuilderSort(QueryBuilder $qb, $name) |
|
|
|
|
|
|
220
|
|
|
{ |
|
221
|
|
|
$alias = current($qb->getDQLPart('from'))->getAlias(); |
|
222
|
|
|
if (is_array($order = $this->getOrder($name))) { |
|
223
|
|
|
$qb->orderBy($alias . '.' . $order['field'], $order['type']); |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Create Delete form. |
|
229
|
|
|
* |
|
230
|
|
|
* @param int $id |
|
231
|
|
|
* @param string $route |
|
232
|
|
|
* |
|
233
|
|
|
* @return \Symfony\Component\Form\Form |
|
234
|
|
|
*/ |
|
235
|
|
|
protected function createDeleteForm($id, $route) |
|
236
|
|
|
{ |
|
237
|
|
|
return $this->createFormBuilder(null, array('attr' => array('id' => 'delete'))) |
|
238
|
|
|
->setAction($this->generateUrl($route, array('id' => $id))) |
|
239
|
|
|
->setMethod('DELETE') |
|
240
|
|
|
->getForm() |
|
241
|
|
|
; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Test Inventory. |
|
246
|
|
|
* |
|
247
|
|
|
* @return string|null |
|
248
|
|
|
*/ |
|
249
|
|
|
protected function testInventory() |
|
250
|
|
|
{ |
|
251
|
|
|
$url = null; |
|
252
|
|
|
$etm = $this->getDoctrine()->getManager(); |
|
253
|
|
|
$inventories = $etm->getRepository('AppBundle:Inventory')->getInventory(); |
|
254
|
|
|
|
|
255
|
|
|
if (empty($inventories)) { |
|
256
|
|
|
$url = 'gs_install_st7'; |
|
257
|
|
|
} else { |
|
258
|
|
|
foreach ($inventories as $inventory) { |
|
259
|
|
|
if ($inventory->getstatus() === 1 || $inventory->getStatus() === 2) { |
|
260
|
|
|
$message = $this->get('translator') |
|
261
|
|
|
->trans('yet', array(), 'gs_inventories'); |
|
262
|
|
|
$this->addFlash('danger', $message); |
|
263
|
|
|
$url = 'inventory'; |
|
264
|
|
|
break; |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
return $url; |
|
270
|
|
|
} |
|
271
|
|
|
} |
|
272
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.