CRUDController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. var_dump($this->get('request')->get('data'));
  117. var_dump($object);
  118. var_dump($this->get('validator')->validate($form));
  119. die;
  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(json_encode(array('result' => 'ok', 'object_id' => $object->getId())));
  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. if ($id instanceof Form) {
  183. $object = $id->getData();
  184. $form = $id;
  185. } else {
  186. $object = $this->admin->getNewInstance();
  187. $form = $this->admin->getForm($object);
  188. }
  189. $this->admin->setSubject($object);
  190. return $this->render($this->admin->getEditTemplate(), array(
  191. 'form' => $form,
  192. 'object' => $object,
  193. 'fields' => $this->admin->getFormFields($form),
  194. 'form_groups' => $this->admin->getFormGroups($form),
  195. 'admin' => $this->admin,
  196. 'base_template' => $this->getBaseTemplate(),
  197. ));
  198. }
  199. public function setConfiguration($configuration)
  200. {
  201. $this->admin = $configuration;
  202. }
  203. public function getConfiguration()
  204. {
  205. return $this->admin;
  206. }
  207. }