CRUDController.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 Sonata\BaseApplicationBundle\Controller;
  11. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  12. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. use Symfony\Component\Form\Form;
  15. use Sonata\BaseApplicationBundle\Tool\DoctrinePager as Pager;
  16. class CRUDController extends Controller
  17. {
  18. protected $admin;
  19. /**
  20. * Sets the Container associated with this Controller.
  21. *
  22. * @param ContainerInterface $container A ContainerInterface instance
  23. */
  24. public function setContainer(ContainerInterface $container = null)
  25. {
  26. $this->container = $container;
  27. $this->configure();
  28. }
  29. public function configure()
  30. {
  31. $this->admin = $this->container
  32. ->get('base_application.admin.pool')
  33. ->getAdminByControllerName(get_class($this));
  34. }
  35. public function getBaseTemplate()
  36. {
  37. if ($this->get('request')->isXmlHttpRequest()) {
  38. return $this->container->getParameter('base_application.templates.ajax');
  39. }
  40. return $this->container->getParameter('base_application.templates.layout');
  41. }
  42. public function listAction()
  43. {
  44. $datagrid = $this->admin->getFilterDatagrid();
  45. $datagrid->setValues($this->get('request')->query->all());
  46. return $this->render($this->admin->getListTemplate(), array(
  47. 'datagrid' => $datagrid,
  48. 'fields' => $this->admin->getListFields(),
  49. 'class_meta_data' => $this->admin->getClassMetaData(),
  50. 'admin' => $this->admin,
  51. 'batch_actions' => $this->admin->getBatchActions(),
  52. 'base_template' => $this->getBaseTemplate(),
  53. ));
  54. }
  55. public function batchActionDelete($idx)
  56. {
  57. $em = $this->admin->getEntityManager();
  58. $query_builder = $em->createQueryBuilder();
  59. $objects = $query_builder
  60. ->select('o')
  61. ->from($this->admin->getClass(), 'o')
  62. ->add('where', $query_builder->expr()->in('o.id', $idx))
  63. ->getQuery()
  64. ->execute();
  65. foreach ($objects as $object) {
  66. $em->remove($object);
  67. }
  68. $em->flush();
  69. // todo : add confirmation flash var
  70. return $this->redirect($this->admin->generateUrl('list'));
  71. }
  72. public function deleteAction($id)
  73. {
  74. // todo
  75. }
  76. public function editAction($id)
  77. {
  78. $this->get('session')->start();
  79. $fields = $this->admin->getFormFields();
  80. if ($id instanceof Form) {
  81. $object = $id->getData();
  82. $form = $id;
  83. } else {
  84. $object = $this->admin->getObject($id);
  85. if (!$object) {
  86. throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
  87. }
  88. $form = $this->admin->getForm($object, $fields);
  89. }
  90. $this->admin->setSubject($object);
  91. return $this->render($this->admin->getEditTemplate(), array(
  92. 'form' => $form,
  93. 'object' => $object,
  94. 'fields' => $fields,
  95. 'form_groups' => $this->admin->getFormGroups(),
  96. 'admin' => $this->admin,
  97. 'base_template' => $this->getBaseTemplate(),
  98. ));
  99. }
  100. public function updateAction()
  101. {
  102. $this->get('session')->start();
  103. if ($this->get('request')->getMethod() != 'POST') {
  104. throw new \RuntimeException('invalid request type, POST expected');
  105. }
  106. $id = $this->get('request')->get('id');
  107. if (is_numeric($id)) {
  108. $object = $this->admin->getObject($id);
  109. if (!$object) {
  110. throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
  111. }
  112. $action = 'edit';
  113. } else {
  114. $object = $this->admin->getNewInstance();
  115. $action = 'create';
  116. }
  117. $fields = $this->admin->getFormFields();
  118. $form = $this->admin->getForm($object, $fields);
  119. $form->bind($this->get('request')->get('data'));
  120. if ($form->isValid()) {
  121. if ($action == 'create') {
  122. $this->admin->preInsert($object);
  123. } else {
  124. $this->admin->preUpdate($object);
  125. }
  126. $this->admin->getEntityManager()->persist($object);
  127. $this->admin->getEntityManager()->flush($object);
  128. if ($action == 'create') {
  129. $this->admin->postInsert($object);
  130. } else {
  131. $this->admin->postUpdate($object);
  132. }
  133. if ($this->get('request')->isXmlHttpRequest()) {
  134. return $this->createResponse('ok');
  135. }
  136. // redirect to edit mode
  137. return $this->redirectTo($object);
  138. }
  139. return $this->forward(sprintf('%s:%s', $this->admin->getBaseControllerName(), $action), array(
  140. 'id' => $form
  141. ));
  142. }
  143. /**
  144. * redirect the user depend on this choice
  145. *
  146. * @param $object
  147. * @return Response
  148. */
  149. public function redirectTo($object) {
  150. $url = false;
  151. if ($this->get('request')->get('btn_update_and_list')) {
  152. $url = $this->admin->generateUrl('list');
  153. }
  154. if ($this->get('request')->get('btn_create_and_create')) {
  155. $url = $this->admin->generateUrl('create');
  156. }
  157. if (!$url) {
  158. $url = $this->admin->generateUrl('edit', array('id' => $object->getId()));
  159. }
  160. return $this->redirect($url);
  161. }
  162. public function batchAction()
  163. {
  164. if ($this->get('request')->getMethod() != 'POST') {
  165. throw new \RuntimeException('invalid request type, POST expected');
  166. }
  167. $action = $this->get('request')->get('action');
  168. $idx = $this->get('request')->get('idx');
  169. if (count($idx) == 0) { // no item selected
  170. // todo : add flash information
  171. return $this->redirect($this->admin->generateUrl('list'));
  172. }
  173. // execute the action, batchActionXxxxx
  174. $final_action = sprintf('batchAction%s', ucfirst($action));
  175. if (!method_exists($this, $final_action)) {
  176. throw new \RuntimeException(sprintf('A `%s::%s` method must be created', get_class($this), $final_action));
  177. }
  178. return call_user_func(array($this, $final_action), $idx);
  179. }
  180. public function createAction($id = null)
  181. {
  182. $this->get('session')->start();
  183. $fields = $this->admin->getFormFields();
  184. if ($id instanceof Form) {
  185. $object = $id->getData();
  186. $form = $id;
  187. } else {
  188. $object = $this->admin->getNewInstance();
  189. $form = $this->admin->getForm($object, $fields);
  190. }
  191. $this->admin->setSubject($object);
  192. return $this->render($this->admin->getEditTemplate(), array(
  193. 'form' => $form,
  194. 'object' => $object,
  195. 'fields' => $fields,
  196. 'form_groups' => $this->admin->getFormGroups(),
  197. 'admin' => $this->admin,
  198. 'base_template' => $this->getBaseTemplate(),
  199. ));
  200. }
  201. public function setConfiguration($configuration)
  202. {
  203. $this->admin = $configuration;
  204. }
  205. public function getConfiguration()
  206. {
  207. return $this->admin;
  208. }
  209. }