CoreController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. * Class CoreController.
  21. *
  22. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  23. */
  24. class CoreController extends Controller
  25. {
  26. /**
  27. * @param Request $request
  28. *
  29. * @return Response
  30. */
  31. public function dashboardAction()
  32. {
  33. $blocks = array(
  34. 'top' => array(),
  35. 'left' => array(),
  36. 'center' => array(),
  37. 'right' => array(),
  38. 'bottom' => array(),
  39. );
  40. foreach ($this->container->getParameter('sonata.admin.configuration.dashboard_blocks') as $block) {
  41. $blocks[$block['position']][] = $block;
  42. }
  43. $parameters = array(
  44. 'base_template' => $this->getBaseTemplate(),
  45. 'admin_pool' => $this->container->get('sonata.admin.pool'),
  46. 'blocks' => $blocks,
  47. );
  48. if (!$this->getRequest()->isXmlHttpRequest()) {
  49. $parameters['breadcrumbs_builder'] = $this->get('sonata.admin.breadcrumbs_builder');
  50. }
  51. return $this->render($this->getAdminPool()->getTemplate('dashboard'), $parameters);
  52. }
  53. /**
  54. * The search action first render an empty page, if the query is set, then the template generates
  55. * some ajax request to retrieve results for each admin. The Ajax query returns a JSON response.
  56. *
  57. * @param Request $request
  58. *
  59. * @return JsonResponse|Response
  60. *
  61. * @throws \RuntimeException
  62. */
  63. public function searchAction(Request $request)
  64. {
  65. if ($request->get('admin') && $request->isXmlHttpRequest()) {
  66. try {
  67. $admin = $this->getAdminPool()->getAdminByAdminCode($request->get('admin'));
  68. } catch (ServiceNotFoundException $e) {
  69. throw new \RuntimeException('Unable to find the Admin instance', $e->getCode(), $e);
  70. }
  71. if (!$admin instanceof AdminInterface) {
  72. throw new \RuntimeException('The requested service is not an Admin instance');
  73. }
  74. $handler = $this->getSearchHandler();
  75. $results = array();
  76. if ($pager = $handler->search($admin, $request->get('q'), $request->get('page'), $request->get('offset'))) {
  77. foreach ($pager->getResults() as $result) {
  78. $results[] = array(
  79. 'label' => $admin->toString($result),
  80. 'link' => $admin->generateObjectUrl('edit', $result),
  81. 'id' => $admin->id($result),
  82. );
  83. }
  84. }
  85. $response = new JsonResponse(array(
  86. 'results' => $results,
  87. 'page' => $pager ? (int) $pager->getPage() : false,
  88. 'total' => $pager ? (int) $pager->getNbResults() : false,
  89. ));
  90. $response->setPrivate();
  91. return $response;
  92. }
  93. return $this->render($this->container->get('sonata.admin.pool')->getTemplate('search'), array(
  94. 'base_template' => $this->getBaseTemplate(),
  95. 'breadcrumbs_builder' => $this->get('sonata.admin.breadcrumbs_builder'),
  96. 'admin_pool' => $this->container->get('sonata.admin.pool'),
  97. 'query' => $request->get('q'),
  98. 'groups' => $this->getAdminPool()->getDashboardGroups(),
  99. ));
  100. }
  101. /**
  102. * Get the request object from the container.
  103. *
  104. * This method is compatible with both Symfony 2.3 and Symfony 3
  105. *
  106. * @deprecated Use the Request action argument. This method will be removed
  107. * in SonataAdminBundle 4.0 and the action methods adjusted
  108. *
  109. * @return Request
  110. */
  111. public function getRequest()
  112. {
  113. if ($this->container->has('request_stack')) {
  114. return $this->container->get('request_stack')->getCurrentRequest();
  115. }
  116. return $this->container->get('request');
  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. * @param Request $request
  134. *
  135. * @return string
  136. */
  137. protected function getBaseTemplate()
  138. {
  139. if ($this->getRequest()->isXmlHttpRequest()) {
  140. return $this->getAdminPool()->getTemplate('ajax');
  141. }
  142. return $this->getAdminPool()->getTemplate('layout');
  143. }
  144. }