1
+ <?php
2
+
3
+ /*
4
+ * This file is part of the Sonata package.
5
+ *
6
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
7
+ *
8
+ * For the full copyright and license information, please view the LICENSE
9
+ * file that was distributed with this source code.
10
+ */
11
+
12
+ namespace Bundle \BaseApplicationBundle \Controller ;
13
+
14
+ use Symfony \Bundle \FrameworkBundle \Controller \Controller ;
15
+ use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
16
+
17
+ use Symfony \Component \Form \Form ;
18
+
19
+
20
+ use Bundle \BaseApplicationBundle \Tool \DoctrinePager as Pager ;
21
+
22
+ class CRUDController extends Controller
23
+ {
24
+ protected $ class ;
25
+
26
+ protected $ list_fields = false ;
27
+
28
+ protected $ form_fields = false ;
29
+
30
+ protected $ base_route = '' ;
31
+
32
+ protected $ base_controller_name ;
33
+
34
+ public function getClass ()
35
+ {
36
+ return $ this ->class ;
37
+ }
38
+
39
+ public function getEntityManager ()
40
+ {
41
+ return $ this ->get ('doctrine.orm.default_entity_manager ' );
42
+ }
43
+
44
+ public function getClassMetaData ()
45
+ {
46
+ $ em = $ this ->getEntityManager ();
47
+
48
+ return $ em ->getClassMetaData ($ this ->getClass ());
49
+ }
50
+
51
+ public function getListQueryBuilder ()
52
+ {
53
+ $ em = $ this ->getEntityManager ();
54
+ $ repository = $ em ->getRepository ($ this ->getClass ());
55
+
56
+ $ query_buidler = $ repository
57
+ ->createQueryBuilder ('c ' );
58
+
59
+ return $ query_buidler ;
60
+ }
61
+
62
+ public function getUrls ()
63
+ {
64
+ return array (
65
+ 'list ' => array (
66
+ 'url ' => $ this ->base_route .'_list ' ,
67
+ 'params ' => array (),
68
+ ),
69
+ 'create ' => array (
70
+ 'url ' => $ this ->base_route .'_create ' ,
71
+ 'params ' => array (),
72
+ ),
73
+ 'update ' => array (
74
+ 'url ' => $ this ->base_route .'_update ' ,
75
+ 'params ' => array ()
76
+ ),
77
+ 'delete ' => array (
78
+ 'url ' => $ this ->base_route .'_delete ' ,
79
+ 'params ' => array ()
80
+ ),
81
+ 'edit ' => array (
82
+ 'url ' => $ this ->base_route .'_edit ' ,
83
+ 'params ' => array ()
84
+ )
85
+ );
86
+ }
87
+
88
+ public function getUrl ($ name )
89
+ {
90
+ $ urls = $ this ->getUrls ();
91
+
92
+ if (!isset ($ urls [$ name ])) {
93
+ return false ;
94
+ }
95
+
96
+ return $ urls [$ name ];
97
+ }
98
+
99
+ public function listAction ()
100
+ {
101
+
102
+ $ pager = new Pager ($ this ->getClass ());
103
+
104
+ $ url = $ this ->getUrl ('list ' );
105
+
106
+ $ pager ->setRouter ($ this ->get ('router ' ));
107
+ $ pager ->setRoute ($ url ['url ' ]);
108
+
109
+ $ pager ->setQueryBuilder ($ this ->getListQueryBuilder ());
110
+ $ pager ->setPage ($ this ->get ('request ' )->get ('page ' , 1 ));
111
+ $ pager ->init ();
112
+
113
+ return $ this ->render ($ this ->getListTemplate (), array (
114
+ 'pager ' => $ pager ,
115
+ 'fields ' => $ this ->getListFields (),
116
+ 'class_meta_data ' => $ this ->getClassMetaData (),
117
+ 'urls ' => $ this ->getUrls ()
118
+ ));
119
+
120
+ }
121
+
122
+ public function getListTemplate ()
123
+ {
124
+ return 'BaseApplicationBundle:CRUD:list.twig ' ;
125
+ }
126
+
127
+ public function getEditTemplate ()
128
+ {
129
+ return 'BaseApplicationBundle:CRUD:edit.twig ' ;
130
+ }
131
+
132
+ public function getReflectionFields ()
133
+ {
134
+ return $ this ->getClassMetaData ()->reflFields ;
135
+ }
136
+
137
+ /**
138
+ * make sure the base field are set in the correct format
139
+ *
140
+ * @param $selected_fields
141
+ * @return array
142
+ */
143
+ public function getBaseFields ($ selected_fields )
144
+ {
145
+ // if nothing is defined we display all fields
146
+ if (!$ selected_fields ) {
147
+ $ selected_fields = array_keys ($ this ->getClassMetaData ()->reflFields );
148
+ }
149
+
150
+ $ metadata = $ this ->getClassMetaData ();
151
+
152
+ // make sure we works with array
153
+ $ fields = array ();
154
+ foreach ($ selected_fields as $ name => $ options ) {
155
+ if (is_array ($ options )) {
156
+ $ fields [$ name ] = $ options ;
157
+ } else {
158
+ $ fields [$ options ] = array ();
159
+ $ name = $ options ;
160
+ }
161
+
162
+ if (isset ($ metadata ->fieldMappings [$ name ])) {
163
+ $ fields [$ name ] = array_merge (
164
+ $ metadata ->fieldMappings [$ name ],
165
+ $ fields [$ name ]
166
+ );
167
+ }
168
+
169
+ if (isset ($ metadata ->reflFields [$ name ])) {
170
+ $ fields [$ name ]['reflection ' ] =& $ metadata ->reflFields [$ name ];
171
+ }
172
+ }
173
+
174
+ return $ fields ;
175
+ }
176
+
177
+ public function getFormFields ()
178
+ {
179
+ $ this ->form_fields = $ this ->getBaseFields ($ this ->form_fields );
180
+
181
+ foreach ($ this ->form_fields as $ name => $ options ) {
182
+ if (!isset ($ this ->form_fields [$ name ]['template ' ])) {
183
+ $ this ->form_fields [$ name ]['template ' ] = sprintf ('BaseApplicationBundle:CRUD:edit_%s.twig ' , $ this ->form_fields [$ name ]['type ' ]);
184
+ }
185
+
186
+ if (isset ($ this ->form_fields [$ name ]['id ' ])) {
187
+ unset($ this ->form_fields [$ name ]);
188
+ }
189
+ }
190
+
191
+ return $ this ->form_fields ;
192
+ }
193
+
194
+ public function getListFields ()
195
+ {
196
+
197
+ $ this ->list_fields = $ this ->getBaseFields ($ this ->list_fields );
198
+
199
+ foreach ($ this ->list_fields as $ name => $ options ) {
200
+ if (!isset ($ this ->list_fields [$ name ]['type ' ])) {
201
+ $ this ->list_fields [$ name ]['type ' ] = 'string ' ;
202
+ }
203
+
204
+ if (!isset ($ this ->list_fields [$ name ]['template ' ])) {
205
+ $ this ->list_fields [$ name ]['template ' ] = sprintf ('BaseApplicationBundle:CRUD:list_%s.twig ' , $ this ->list_fields [$ name ]['type ' ]);
206
+ }
207
+
208
+ if (isset ($ this ->list_fields [$ name ]['id ' ])) {
209
+ $ this ->list_fields [$ name ]['template ' ] = 'BaseApplicationBundle:CRUD:list_identifier.twig ' ;
210
+ }
211
+ }
212
+
213
+ return $ this ->list_fields ;
214
+ }
215
+
216
+ public function deleteAction ($ id )
217
+ {
218
+
219
+ }
220
+
221
+ public function editAction ($ id )
222
+ {
223
+
224
+ $ this ->get ('session ' )->start ();
225
+
226
+ $ fields = $ this ->getFormFields ();
227
+
228
+ if ($ id instanceof Form) {
229
+ $ object = $ id ->getData ();
230
+ $ form = $ id ;
231
+ } else {
232
+ $ object = $ this ->get ('doctrine.orm.default_entity_manager ' )->find ($ this ->getClass (), $ id );
233
+
234
+ if (!$ object ) {
235
+ throw new NotFoundHttpException (sprintf ('unable to find the object with id : %s ' , $ id ));
236
+ }
237
+
238
+ $ form = $ this ->getForm ($ object , $ fields );
239
+ }
240
+
241
+ return $ this ->render ($ this ->getEditTemplate (), array (
242
+ 'form ' => $ form ,
243
+ 'object ' => $ object ,
244
+ 'fields ' => $ fields ,
245
+ 'urls ' => $ this ->getUrls ()
246
+ ));
247
+ }
248
+
249
+ public function getForm ($ object , $ fields )
250
+ {
251
+
252
+ $ form = new Form ('data ' , $ object , $ this ->get ('validator ' ));
253
+
254
+ foreach ($ fields as $ name => $ description ) {
255
+
256
+ switch ($ description ['type ' ]) {
257
+ case 'string ' :
258
+ $ field = new \Symfony \Component \Form \TextField ($ name );
259
+ break ;
260
+
261
+ case 'text ' :
262
+ $ field = new \Symfony \Component \Form \TextareaField ($ name );
263
+ break ;
264
+
265
+ case 'boolean ' :
266
+ $ field = new \Symfony \Component \Form \CheckboxField ($ name );
267
+ break ;
268
+
269
+ case 'integer ' :
270
+ $ field = new \Symfony \Component \Form \IntegerField ($ name );
271
+ break ;
272
+
273
+ case 'decimal ' :
274
+ $ field = new \Symfony \Component \Form \NumberField ($ name );
275
+ break ;
276
+
277
+ case 'datetime ' :
278
+ $ field = new \Symfony \Component \Form \DateTimeField ($ name );
279
+ break ;
280
+
281
+ case 'date ' :
282
+ $ field = new \Symfony \Component \Form \DateField ($ name );
283
+ break ;
284
+
285
+ case 'array ' :
286
+ $ field = new \Symfony \Component \Form \FieldGroup ($ name );
287
+
288
+ $ values = $ description ['reflection ' ]->getValue ($ object );
289
+
290
+ foreach ((array )$ values as $ k => $ v ) {
291
+ $ field ->add (new \Symfony \Component \Form \TextField ($ k ));
292
+ }
293
+ }
294
+
295
+ $ form ->add ($ field );
296
+
297
+ }
298
+
299
+ return $ form ;
300
+ }
301
+
302
+ public function updateAction ()
303
+ {
304
+
305
+ $ this ->get ('session ' )->start ();
306
+
307
+ if ($ this ->get ('request ' )->getMethod () != 'POST ' ) {
308
+ throw new \RuntimeException ('invalid request type, POST expected ' );
309
+ }
310
+
311
+ $ id = $ this ->get ('request ' )->get ('id ' );
312
+
313
+ if (is_numeric ($ id )) {
314
+ $ object = $ this ->get ('doctrine.orm.default_entity_manager ' )->find ($ this ->getClass (), $ id );
315
+
316
+ if (!$ object ) {
317
+ throw new NotFoundHttpException (sprintf ('unable to find the object with id : %s ' , $ id ));
318
+ }
319
+
320
+ $ action = 'edit ' ;
321
+ } else {
322
+ $ object = new $ this ->getClass ();
323
+ $ action = 'create ' ;
324
+ }
325
+
326
+ $ fields = $ this ->getFormFields ();
327
+ $ form = $ this ->getForm ($ object , $ fields );
328
+
329
+ $ form ->bind ($ this ->get ('request ' )->get ('data ' ));
330
+
331
+ if ($ form ->isValid ()) {
332
+
333
+ $ this ->getEntityManager ()->persist ($ object );
334
+ $ this ->getEntityManager ()->flush ($ object );
335
+
336
+ // redirect to edit mode
337
+ $ url = $ this ->getUrl ('edit ' );
338
+
339
+ return $ this ->redirect ($ this ->generateUrl ($ url ['url ' ], array ('id ' => $ object ->getId ())));
340
+ }
341
+
342
+ return $ this ->forward (sprintf ('%s:%s ' , $ this ->getBaseControllerName (), $ action ), array (
343
+ 'id ' => $ form
344
+ ));
345
+ }
346
+
347
+ public function createAction ()
348
+ {
349
+
350
+ }
351
+
352
+ public function setBaseControllerName ($ base_controller_name )
353
+ {
354
+ $ this ->base_controller_name = $ base_controller_name ;
355
+ }
356
+
357
+ public function getBaseControllerName ()
358
+ {
359
+ return $ this ->base_controller_name ;
360
+ }
361
+
362
+ public function setBaseRoute ($ base_route )
363
+ {
364
+ $ this ->base_route = $ base_route ;
365
+ }
366
+
367
+ public function getBaseRoute ()
368
+ {
369
+ return $ this ->base_route ;
370
+ }
371
+ }
0 commit comments