CRUDController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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->getFilterDatagrid();
  53. $datagrid->setValues($this->get('request')->query->all());
  54. return $this->render($this->admin->getListTemplate(), array(
  55. 'datagrid' => $datagrid,
  56. 'fields' => $this->admin->getListFields(),
  57. 'class_meta_data' => $this->admin->getClassMetaData(),
  58. 'admin' => $this->admin,
  59. 'batch_actions' => $this->admin->getBatchActions(),
  60. 'base_template' => $this->getBaseTemplate(),
  61. ));
  62. }
  63. public function batchActionDelete($idx)
  64. {
  65. $em = $this->admin->getEntityManager();
  66. $query_builder = $em->createQueryBuilder();
  67. $objects = $query_builder
  68. ->select('o')
  69. ->from($this->admin->getClass(), 'o')
  70. ->add('where', $query_builder->expr()->in('o.id', $idx))
  71. ->getQuery()
  72. ->execute();
  73. foreach ($objects as $object) {
  74. $em->remove($object);
  75. }
  76. $em->flush();
  77. // todo : add confirmation flash var
  78. return $this->redirect($this->admin->generateUrl('list'));
  79. }
  80. public function deleteAction($id)
  81. {
  82. // todo
  83. }
  84. public function editAction($id)
  85. {
  86. if ($id instanceof Form) {
  87. $object = $id->getData();
  88. $form = $id;
  89. } else {
  90. $object = $this->admin->getObject($id);
  91. if (!$object) {
  92. throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
  93. }
  94. $form = $this->admin->getForm($object);
  95. }
  96. $this->admin->setSubject($object);
  97. return $this->render($this->admin->getEditTemplate(), array(
  98. 'form' => $form,
  99. 'object' => $object,
  100. 'fields' => $this->admin->getFormFields($form),
  101. 'form_groups' => $this->admin->getFormGroups($form),
  102. 'admin' => $this->admin,
  103. 'base_template' => $this->getBaseTemplate(),
  104. ));
  105. }
  106. public function updateAction()
  107. {
  108. if ($this->get('request')->getMethod() != 'POST') {
  109. throw new \RuntimeException('invalid request type, POST expected');
  110. }
  111. $id = $this->get('request')->get('id');
  112. if (is_numeric($id)) {
  113. $object = $this->admin->getObject($id);
  114. if (!$object) {
  115. throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
  116. }
  117. $action = 'edit';
  118. } else {
  119. $object = $this->admin->getNewInstance();
  120. $action = 'create';
  121. }
  122. $form = $this->admin->getForm($object);
  123. $form->bind($this->get('request')->get('data'));
  124. if ($form->isValid()) {
  125. if ($action == 'create') {
  126. $this->admin->preInsert($object);
  127. } else {
  128. $this->admin->preUpdate($object);
  129. }
  130. $this->admin->getEntityManager()->persist($object);
  131. $this->admin->getEntityManager()->flush($object);
  132. if ($action == 'create') {
  133. $this->admin->postInsert($object);
  134. } else {
  135. $this->admin->postUpdate($object);
  136. }
  137. if ($this->get('request')->isXmlHttpRequest()) {
  138. return $this->createResponse(json_encode(array('result' => 'ok', 'object_id' => $object->getId())));
  139. }
  140. // redirect to edit mode
  141. return $this->redirectTo($object);
  142. }
  143. return $this->forward(sprintf('%s:%s', $this->admin->getBaseControllerName(), $action), array(
  144. 'id' => $form
  145. ));
  146. }
  147. /**
  148. * redirect the user depend on this choice
  149. *
  150. * @param $object
  151. * @return Response
  152. */
  153. public function redirectTo($object) {
  154. $url = false;
  155. if ($this->get('request')->get('btn_update_and_list')) {
  156. $url = $this->admin->generateUrl('list');
  157. }
  158. if ($this->get('request')->get('btn_create_and_create')) {
  159. $url = $this->admin->generateUrl('create');
  160. }
  161. if (!$url) {
  162. $url = $this->admin->generateUrl('edit', array('id' => $object->getId()));
  163. }
  164. return $this->redirect($url);
  165. }
  166. public function batchAction()
  167. {
  168. if ($this->get('request')->getMethod() != 'POST') {
  169. throw new \RuntimeException('invalid request type, POST expected');
  170. }
  171. $action = $this->get('request')->get('action');
  172. $idx = $this->get('request')->get('idx');
  173. if (count($idx) == 0) { // no item selected
  174. // todo : add flash information
  175. return $this->redirect($this->admin->generateUrl('list'));
  176. }
  177. // execute the action, batchActionXxxxx
  178. $final_action = sprintf('batchAction%s', ucfirst($action));
  179. if (!method_exists($this, $final_action)) {
  180. throw new \RuntimeException(sprintf('A `%s::%s` method must be created', get_class($this), $final_action));
  181. }
  182. return call_user_func(array($this, $final_action), $idx);
  183. }
  184. public function createAction($id = null)
  185. {
  186. if ($id instanceof Form) {
  187. $object = $id->getData();
  188. $form = $id;
  189. } else {
  190. $object = $this->admin->getNewInstance();
  191. $form = $this->admin->getForm($object);
  192. }
  193. $this->admin->setSubject($object);
  194. return $this->render($this->admin->getEditTemplate(), array(
  195. 'form' => $form,
  196. 'object' => $object,
  197. 'fields' => $this->admin->getFormFields($form),
  198. 'form_groups' => $this->admin->getFormGroups($form),
  199. 'admin' => $this->admin,
  200. 'base_template' => $this->getBaseTemplate(),
  201. ));
  202. }
  203. public function setConfiguration($configuration)
  204. {
  205. $this->admin = $configuration;
  206. }
  207. public function getConfiguration()
  208. {
  209. return $this->admin;
  210. }
  211. }