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