CoreController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. return $this->render($this->getAdminPool()->getTemplate('dashboard'), array(
  44. 'base_template' => $this->getBaseTemplate(),
  45. 'admin_pool' => $this->container->get('sonata.admin.pool'),
  46. 'blocks' => $blocks,
  47. ));
  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. 'admin_pool' => $this->container->get('sonata.admin.pool'),
  92. 'query' => $request->get('q'),
  93. 'groups' => $this->getAdminPool()->getDashboardGroups(),
  94. ));
  95. }
  96. /**
  97. * Get the request object from the container.
  98. *
  99. * This method is compatible with both Symfony 2.3 and Symfony 3
  100. *
  101. * @deprecated Use the Request action argument. This method will be removed
  102. * in SonataAdminBundle 4.0 and the action methods adjusted.
  103. *
  104. * @return Request
  105. */
  106. public function getRequest()
  107. {
  108. if ($this->container->has('request_stack')) {
  109. return $this->container->get('request_stack')->getCurrentRequest();
  110. }
  111. return $this->container->get('request');
  112. }
  113. /**
  114. * @return Pool
  115. */
  116. protected function getAdminPool()
  117. {
  118. return $this->container->get('sonata.admin.pool');
  119. }
  120. /**
  121. * @return SearchHandler
  122. */
  123. protected function getSearchHandler()
  124. {
  125. return $this->get('sonata.admin.search.handler');
  126. }
  127. /**
  128. * @param Request $request
  129. *
  130. * @return string
  131. */
  132. protected function getBaseTemplate()
  133. {
  134. if ($this->getRequest()->isXmlHttpRequest()) {
  135. return $this->getAdminPool()->getTemplate('ajax');
  136. }
  137. return $this->getAdminPool()->getTemplate('layout');
  138. }
  139. }