CRUDController.php 10 KB

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