CRUDController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. if ($id instanceof Form) {
  79. $object = $id->getData();
  80. $form = $id;
  81. } else {
  82. $object = $this->admin->getObject($id);
  83. if (!$object) {
  84. throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
  85. }
  86. $form = $this->admin->getForm($object);
  87. }
  88. $this->admin->setSubject($object);
  89. return $this->render($this->admin->getEditTemplate(), array(
  90. 'form' => $form,
  91. 'object' => $object,
  92. 'fields' => $this->admin->getFormFields($form),
  93. 'form_groups' => $this->admin->getFormGroups($form),
  94. 'admin' => $this->admin,
  95. 'base_template' => $this->getBaseTemplate(),
  96. ));
  97. }
  98. public function updateAction()
  99. {
  100. if ($this->get('request')->getMethod() != 'POST') {
  101. throw new \RuntimeException('invalid request type, POST expected');
  102. }
  103. $id = $this->get('request')->get('id');
  104. if (is_numeric($id)) {
  105. $object = $this->admin->getObject($id);
  106. if (!$object) {
  107. throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
  108. }
  109. $action = 'edit';
  110. } else {
  111. $object = $this->admin->getNewInstance();
  112. $action = 'create';
  113. }
  114. $form = $this->admin->getForm($object);
  115. $form->bind($this->get('request')->get('data'));
  116. if ($form->isValid()) {
  117. if ($action == 'create') {
  118. $this->admin->preInsert($object);
  119. } else {
  120. $this->admin->preUpdate($object);
  121. }
  122. $this->admin->getEntityManager()->persist($object);
  123. $this->admin->getEntityManager()->flush($object);
  124. if ($action == 'create') {
  125. $this->admin->postInsert($object);
  126. } else {
  127. $this->admin->postUpdate($object);
  128. }
  129. if ($this->get('request')->isXmlHttpRequest()) {
  130. return $this->createResponse(json_encode(array('result' => 'ok', 'object_id' => $object->getId())));
  131. }
  132. // redirect to edit mode
  133. return $this->redirectTo($object);
  134. }
  135. return $this->forward(sprintf('%s:%s', $this->admin->getBaseControllerName(), $action), array(
  136. 'id' => $form
  137. ));
  138. }
  139. /**
  140. * redirect the user depend on this choice
  141. *
  142. * @param $object
  143. * @return Response
  144. */
  145. public function redirectTo($object) {
  146. $url = false;
  147. if ($this->get('request')->get('btn_update_and_list')) {
  148. $url = $this->admin->generateUrl('list');
  149. }
  150. if ($this->get('request')->get('btn_create_and_create')) {
  151. $url = $this->admin->generateUrl('create');
  152. }
  153. if (!$url) {
  154. $url = $this->admin->generateUrl('edit', array('id' => $object->getId()));
  155. }
  156. return $this->redirect($url);
  157. }
  158. public function batchAction()
  159. {
  160. if ($this->get('request')->getMethod() != 'POST') {
  161. throw new \RuntimeException('invalid request type, POST expected');
  162. }
  163. $action = $this->get('request')->get('action');
  164. $idx = $this->get('request')->get('idx');
  165. if (count($idx) == 0) { // no item selected
  166. // todo : add flash information
  167. return $this->redirect($this->admin->generateUrl('list'));
  168. }
  169. // execute the action, batchActionXxxxx
  170. $final_action = sprintf('batchAction%s', ucfirst($action));
  171. if (!method_exists($this, $final_action)) {
  172. throw new \RuntimeException(sprintf('A `%s::%s` method must be created', get_class($this), $final_action));
  173. }
  174. return call_user_func(array($this, $final_action), $idx);
  175. }
  176. public function createAction($id = null)
  177. {
  178. if ($id instanceof Form) {
  179. $object = $id->getData();
  180. $form = $id;
  181. } else {
  182. $object = $this->admin->getNewInstance();
  183. $form = $this->admin->getForm($object);
  184. }
  185. $this->admin->setSubject($object);
  186. return $this->render($this->admin->getEditTemplate(), array(
  187. 'form' => $form,
  188. 'object' => $object,
  189. 'fields' => $this->admin->getFormFields($form),
  190. 'form_groups' => $this->admin->getFormGroups($form),
  191. 'admin' => $this->admin,
  192. 'base_template' => $this->getBaseTemplate(),
  193. ));
  194. }
  195. public function setConfiguration($configuration)
  196. {
  197. $this->admin = $configuration;
  198. }
  199. public function getConfiguration()
  200. {
  201. return $this->admin;
  202. }
  203. }