CRUDController.php 9.9 KB

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