CoreController.php 4.8 KB

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