CRUDController.php 9.8 KB

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