CoreController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project 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\AdminBundle\Controller;
  11. use Sonata\AdminBundle\Admin\AdminInterface;
  12. use Sonata\AdminBundle\Admin\Pool;
  13. use Sonata\AdminBundle\Search\SearchHandler;
  14. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  15. use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
  16. use Symfony\Component\HttpFoundation\JsonResponse;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. /**
  20. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  21. */
  22. class CoreController extends Controller
  23. {
  24. /**
  25. * @return Response
  26. */
  27. public function dashboardAction()
  28. {
  29. $blocks = array(
  30. 'top' => array(),
  31. 'left' => array(),
  32. 'center' => array(),
  33. 'right' => array(),
  34. 'bottom' => array(),
  35. );
  36. foreach ($this->container->getParameter('sonata.admin.configuration.dashboard_blocks') as $block) {
  37. $blocks[$block['position']][] = $block;
  38. }
  39. $parameters = array(
  40. 'base_template' => $this->getBaseTemplate(),
  41. 'admin_pool' => $this->container->get('sonata.admin.pool'),
  42. 'blocks' => $blocks,
  43. );
  44. if (!$this->getCurrentRequest()->isXmlHttpRequest()) {
  45. $parameters['breadcrumbs_builder'] = $this->get('sonata.admin.breadcrumbs_builder');
  46. }
  47. return $this->render($this->getAdminPool()->getTemplate('dashboard'), $parameters);
  48. }
  49. /**
  50. * The search action first render an empty page, if the query is set, then the template generates
  51. * some ajax request to retrieve results for each admin. The Ajax query returns a JSON response.
  52. *
  53. * @param Request $request
  54. *
  55. * @return JsonResponse|Response
  56. *
  57. * @throws \RuntimeException
  58. */
  59. public function searchAction(Request $request)
  60. {
  61. if ($request->get('admin') && $request->isXmlHttpRequest()) {
  62. try {
  63. $admin = $this->getAdminPool()->getAdminByAdminCode($request->get('admin'));
  64. } catch (ServiceNotFoundException $e) {
  65. throw new \RuntimeException('Unable to find the Admin instance', $e->getCode(), $e);
  66. }
  67. if (!$admin instanceof AdminInterface) {
  68. throw new \RuntimeException('The requested service is not an Admin instance');
  69. }
  70. $handler = $this->getSearchHandler();
  71. $results = array();
  72. if ($pager = $handler->search($admin, $request->get('q'), $request->get('page'), $request->get('offset'))) {
  73. foreach ($pager->getResults() as $result) {
  74. $results[] = array(
  75. 'label' => $admin->toString($result),
  76. 'link' => $admin->generateObjectUrl('edit', $result),
  77. 'id' => $admin->id($result),
  78. );
  79. }
  80. }
  81. $response = new JsonResponse(array(
  82. 'results' => $results,
  83. 'page' => $pager ? (int) $pager->getPage() : false,
  84. 'total' => $pager ? (int) $pager->getNbResults() : false,
  85. ));
  86. $response->setPrivate();
  87. return $response;
  88. }
  89. return $this->render($this->container->get('sonata.admin.pool')->getTemplate('search'), array(
  90. 'base_template' => $this->getBaseTemplate(),
  91. 'breadcrumbs_builder' => $this->get('sonata.admin.breadcrumbs_builder'),
  92. 'admin_pool' => $this->container->get('sonata.admin.pool'),
  93. 'query' => $request->get('q'),
  94. 'groups' => $this->getAdminPool()->getDashboardGroups(),
  95. ));
  96. }
  97. /**
  98. * Get the request object from the container.
  99. *
  100. * This method is compatible with both Symfony 2.3 and Symfony 3
  101. *
  102. * NEXT_MAJOR: remove this method.
  103. *
  104. * @deprecated since 3.0, to be removed in 4.0 and action methods will be adjusted.
  105. * Use Symfony\Component\HttpFoundation\Request as an action argument
  106. *
  107. * @return Request
  108. */
  109. public function getRequest()
  110. {
  111. @trigger_error(
  112. 'The '.__METHOD__.' method is deprecated since 3.0 and will be removed in 4.0.'.
  113. ' Inject the Symfony\Component\HttpFoundation\Request into the actions instead.',
  114. E_USER_DEPRECATED
  115. );
  116. return $this->getCurrentRequest();
  117. }
  118. /**
  119. * @return Pool
  120. */
  121. protected function getAdminPool()
  122. {
  123. return $this->container->get('sonata.admin.pool');
  124. }
  125. /**
  126. * @return SearchHandler
  127. */
  128. protected function getSearchHandler()
  129. {
  130. return $this->get('sonata.admin.search.handler');
  131. }
  132. /**
  133. * @return string
  134. */
  135. protected function getBaseTemplate()
  136. {
  137. if ($this->getCurrentRequest()->isXmlHttpRequest()) {
  138. return $this->getAdminPool()->getTemplate('ajax');
  139. }
  140. return $this->getAdminPool()->getTemplate('layout');
  141. }
  142. /**
  143. * Get the request object from the container.
  144. *
  145. * @return Request
  146. */
  147. private function getCurrentRequest()
  148. {
  149. // NEXT_MAJOR: simplify this when dropping sf < 2.4
  150. if ($this->container->has('request_stack')) {
  151. return $this->container->get('request_stack')->getCurrentRequest();
  152. }
  153. return $this->container->get('request');
  154. }
  155. }