CRUDController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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\Sonata\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\Sonata\BaseApplicationBundle\Tool\DoctrinePager as Pager;
  15. class CRUDController extends Controller
  16. {
  17. protected $configuration;
  18. /**
  19. * Sets the Container associated with this Controller.
  20. *
  21. * @param ContainerInterface $container A ContainerInterface instance
  22. */
  23. public function setContainer(\Symfony\Component\DependencyInjection\ContainerInterface $container = null)
  24. {
  25. $this->container = $container;
  26. $this->configure();
  27. }
  28. public function configure()
  29. {
  30. $this->configuration = $this->container
  31. ->get('base_application.admin.pool')
  32. ->getAdminConfigurationByControllerName(get_class($this));
  33. }
  34. public function getBaseTemplate()
  35. {
  36. if($this->get('request')->isXmlHttpRequest()) {
  37. return 'BaseApplicationBundle::ajax_layout.twig';
  38. }
  39. return 'BaseApplicationBundle::standard_layout.twig';
  40. }
  41. public function listAction()
  42. {
  43. $datagrid = $this->configuration->getFilterDatagrid();
  44. $datagrid->setValues($this->get('request')->query->all());
  45. return $this->render($this->configuration->getListTemplate(), array(
  46. 'datagrid' => $datagrid,
  47. 'fields' => $this->configuration->getListFields(),
  48. 'class_meta_data' => $this->configuration->getClassMetaData(),
  49. 'configuration' => $this->configuration,
  50. 'batch_actions' => $this->configuration->getBatchActions(),
  51. 'base_template' => $this->getBaseTemplate(),
  52. ));
  53. }
  54. public function batchActionDelete($idx)
  55. {
  56. $em = $this->configuration->getEntityManager();
  57. $query_builder = $em->createQueryBuilder();
  58. $objects = $query_builder
  59. ->select('o')
  60. ->from($this->configuration->getClass(), 'o')
  61. ->add('where', $query_builder->expr()->in('o.id', $idx))
  62. ->getQuery()
  63. ->execute();
  64. foreach($objects as $object) {
  65. $em->remove($object);
  66. }
  67. $em->flush();
  68. // todo : add confirmation flash var
  69. return $this->redirect($this->configuration->generateUrl('list'));
  70. }
  71. public function deleteAction($id)
  72. {
  73. // todo
  74. }
  75. public function editAction($id)
  76. {
  77. $this->get('session')->start();
  78. $fields = $this->configuration->getFormFields();
  79. if($id instanceof Form) {
  80. $object = $id->getData();
  81. $form = $id;
  82. } else {
  83. $object = $this->configuration->getObject($id);
  84. if(!$object) {
  85. throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
  86. }
  87. $form = $this->configuration->getForm($object, $fields);
  88. }
  89. return $this->render($this->configuration->getEditTemplate(), array(
  90. 'form' => $form,
  91. 'object' => $object,
  92. 'fields' => $fields,
  93. 'configuration' => $this->configuration,
  94. 'base_template' => $this->getBaseTemplate(),
  95. ));
  96. }
  97. public function updateAction()
  98. {
  99. $this->get('session')->start();
  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->configuration->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. $class = $this->configuration->getClass();
  112. $object = new $class;
  113. $action = 'create';
  114. }
  115. $fields = $this->configuration->getFormFields();
  116. $form = $this->configuration->getForm($object, $fields);
  117. $form->bind($this->get('request')->get('data'));
  118. if($form->isValid()) {
  119. $this->configuration->getEntityManager()->persist($object);
  120. $this->configuration->getEntityManager()->flush($object);
  121. if($this->get('request')->isXmlHttpRequest()) {
  122. return $this->createResponse('ok');
  123. }
  124. // redirect to edit mode
  125. return $this->redirect($this->configuration->generateUrl('edit', array('id' => $object->getId())));
  126. }
  127. return $this->forward(sprintf('%s:%s', $this->configuration->getBaseControllerName(), $action), array(
  128. 'id' => $form
  129. ));
  130. }
  131. public function batchAction()
  132. {
  133. if($this->get('request')->getMethod() != 'POST') {
  134. throw new \RuntimeException('invalid request type, POST expected');
  135. }
  136. $action = $this->get('request')->get('action');
  137. $idx = $this->get('request')->get('idx');
  138. if(count($idx) == 0) { // no item selected
  139. // todo : add flash information
  140. return $this->redirect($this->configuration->generateUrl('list'));
  141. }
  142. // execute the action, batchActionXxxxx
  143. $final_action = sprintf('batchAction%s', ucfirst($action));
  144. if(!method_exists($this, $final_action)) {
  145. throw new \RuntimeException(sprintf('A `%s::%s` method must be created', get_class($this), $final_action));
  146. }
  147. return call_user_func(array($this, $final_action), $idx);
  148. }
  149. public function createAction($id = null)
  150. {
  151. $this->get('session')->start();
  152. $fields = $this->configuration->getFormFields();
  153. if($id instanceof Form) {
  154. $object = $id->getData();
  155. $form = $id;
  156. } else {
  157. $class = $this->configuration->getClass();
  158. $object = new $class;
  159. $form = $this->configuration->getForm($object, $fields);
  160. }
  161. return $this->render($this->configuration->getEditTemplate(), array(
  162. 'form' => $form,
  163. 'object' => $object,
  164. 'fields' => $fields,
  165. 'configuration' => $this->configuration,
  166. 'base_template' => $this->getBaseTemplate(),
  167. ));
  168. }
  169. public function setConfiguration($configuration)
  170. {
  171. $this->configuration = $configuration;
  172. }
  173. public function getConfiguration()
  174. {
  175. return $this->configuration;
  176. }
  177. }