CRUDController.php 9.0 KB

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