CRUDController.php 7.6 KB

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