CoreController.php 5.4 KB

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