CoreController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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(Request $request = null)
  46. {
  47. // to be BC
  48. if (null === $request) {
  49. $request = $this->getRequest();
  50. }
  51. if ($request->isXmlHttpRequest()) {
  52. return $this->getAdminPool()->getTemplate('ajax');
  53. }
  54. return $this->getAdminPool()->getTemplate('layout');
  55. }
  56. /**
  57. * @param Request $request
  58. *
  59. * @return Response
  60. */
  61. public function dashboardAction(Request $request)
  62. {
  63. $blocks = array(
  64. 'top' => array(),
  65. 'left' => array(),
  66. 'center' => array(),
  67. 'right' => array(),
  68. 'bottom' => array(),
  69. );
  70. foreach ($this->container->getParameter('sonata.admin.configuration.dashboard_blocks') as $block) {
  71. $blocks[$block['position']][] = $block;
  72. }
  73. return $this->render($this->getAdminPool()->getTemplate('dashboard'), array(
  74. 'base_template' => $this->getBaseTemplate($request),
  75. 'admin_pool' => $this->container->get('sonata.admin.pool'),
  76. 'blocks' => $blocks,
  77. ));
  78. }
  79. /**
  80. * The search action first render an empty page, if the query is set, then the template generates
  81. * some ajax request to retrieve results for each admin. The Ajax query returns a JSON response.
  82. *
  83. * @param Request $request
  84. *
  85. * @return JsonResponse|Response
  86. *
  87. * @throws \RuntimeException
  88. */
  89. public function searchAction(Request $request)
  90. {
  91. if ($request->get('admin') && $request->isXmlHttpRequest()) {
  92. try {
  93. $admin = $this->getAdminPool()->getAdminByAdminCode($request->get('admin'));
  94. } catch (ServiceNotFoundException $e) {
  95. throw new \RuntimeException('Unable to find the Admin instance', $e->getCode(), $e);
  96. }
  97. if (!$admin instanceof AdminInterface) {
  98. throw new \RuntimeException('The requested service is not an Admin instance');
  99. }
  100. $handler = $this->getSearchHandler();
  101. $results = array();
  102. if ($pager = $handler->search($admin, $request->get('q'), $request->get('page'), $request->get('offset'))) {
  103. foreach ($pager->getResults() as $result) {
  104. $results[] = array(
  105. 'label' => $admin->toString($result),
  106. 'link' => $admin->generateObjectUrl('edit', $result),
  107. 'id' => $admin->id($result),
  108. );
  109. }
  110. }
  111. $response = new JsonResponse(array(
  112. 'results' => $results,
  113. 'page' => $pager ? (int) $pager->getPage() : false,
  114. 'total' => $pager ? (int) $pager->getNbResults() : false,
  115. ));
  116. $response->setPrivate();
  117. return $response;
  118. }
  119. return $this->render($this->container->get('sonata.admin.pool')->getTemplate('search'), array(
  120. 'base_template' => $this->getBaseTemplate($request),
  121. 'admin_pool' => $this->container->get('sonata.admin.pool'),
  122. 'query' => $request->get('q'),
  123. 'groups' => $this->getAdminPool()->getDashboardGroups(),
  124. ));
  125. }
  126. }