CRUDController.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. $this->get('session')->start();
  87. $fields = $this->admin->getFormFields();
  88. if ($id instanceof Form) {
  89. $object = $id->getData();
  90. $form = $id;
  91. } else {
  92. $object = $this->admin->getObject($id);
  93. if (!$object) {
  94. throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
  95. }
  96. $form = $this->admin->getForm($object, $fields);
  97. }
  98. $this->admin->setSubject($object);
  99. return $this->render($this->admin->getEditTemplate(), array(
  100. 'form' => $form,
  101. 'object' => $object,
  102. 'fields' => $fields,
  103. 'form_groups' => $this->admin->getFormGroups(),
  104. 'admin' => $this->admin,
  105. 'base_template' => $this->getBaseTemplate(),
  106. ));
  107. }
  108. public function updateAction()
  109. {
  110. $this->get('session')->start();
  111. if ($this->get('request')->getMethod() != 'POST') {
  112. throw new \RuntimeException('invalid request type, POST expected');
  113. }
  114. $id = $this->get('request')->get('id');
  115. if (is_numeric($id)) {
  116. $object = $this->admin->getObject($id);
  117. if (!$object) {
  118. throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
  119. }
  120. $action = 'edit';
  121. } else {
  122. $object = $this->admin->getNewInstance();
  123. $action = 'create';
  124. }
  125. $fields = $this->admin->getFormFields();
  126. $form = $this->admin->getForm($object, $fields);
  127. $form->bind($this->get('request')->get('data'));
  128. if ($form->isValid()) {
  129. if ($action == 'create') {
  130. $this->admin->preInsert($object);
  131. } else {
  132. $this->admin->preUpdate($object);
  133. }
  134. $this->admin->getEntityManager()->persist($object);
  135. $this->admin->getEntityManager()->flush($object);
  136. if ($action == 'create') {
  137. $this->admin->postInsert($object);
  138. } else {
  139. $this->admin->postUpdate($object);
  140. }
  141. if ($this->get('request')->isXmlHttpRequest()) {
  142. return $this->createResponse(json_encode(array('result' => 'ok', 'object_id' => $object->getId())));
  143. }
  144. // redirect to edit mode
  145. return $this->redirectTo($object);
  146. }
  147. return $this->forward(sprintf('%s:%s', $this->admin->getBaseControllerName(), $action), array(
  148. 'id' => $form
  149. ));
  150. }
  151. /**
  152. * redirect the user depend on this choice
  153. *
  154. * @param $object
  155. * @return Response
  156. */
  157. public function redirectTo($object) {
  158. $url = false;
  159. if ($this->get('request')->get('btn_update_and_list')) {
  160. $url = $this->admin->generateUrl('list');
  161. }
  162. if ($this->get('request')->get('btn_create_and_create')) {
  163. $url = $this->admin->generateUrl('create');
  164. }
  165. if (!$url) {
  166. $url = $this->admin->generateUrl('edit', array('id' => $object->getId()));
  167. }
  168. return $this->redirect($url);
  169. }
  170. public function batchAction()
  171. {
  172. if ($this->get('request')->getMethod() != 'POST') {
  173. throw new \RuntimeException('invalid request type, POST expected');
  174. }
  175. $action = $this->get('request')->get('action');
  176. $idx = $this->get('request')->get('idx');
  177. if (count($idx) == 0) { // no item selected
  178. // todo : add flash information
  179. return $this->redirect($this->admin->generateUrl('list'));
  180. }
  181. // execute the action, batchActionXxxxx
  182. $final_action = sprintf('batchAction%s', ucfirst($action));
  183. if (!method_exists($this, $final_action)) {
  184. throw new \RuntimeException(sprintf('A `%s::%s` method must be created', get_class($this), $final_action));
  185. }
  186. return call_user_func(array($this, $final_action), $idx);
  187. }
  188. public function createAction($id = null)
  189. {
  190. $this->get('session')->start();
  191. $fields = $this->admin->getFormFields();
  192. if ($id instanceof Form) {
  193. $object = $id->getData();
  194. $form = $id;
  195. } else {
  196. $object = $this->admin->getNewInstance();
  197. $form = $this->admin->getForm($object, $fields);
  198. }
  199. $this->admin->setSubject($object);
  200. return $this->render($this->admin->getEditTemplate(), array(
  201. 'form' => $form,
  202. 'object' => $object,
  203. 'fields' => $fields,
  204. 'form_groups' => $this->admin->getFormGroups(),
  205. 'admin' => $this->admin,
  206. 'base_template' => $this->getBaseTemplate(),
  207. ));
  208. }
  209. public function setConfiguration($configuration)
  210. {
  211. $this->admin = $configuration;
  212. }
  213. public function getConfiguration()
  214. {
  215. return $this->admin;
  216. }
  217. }