CoreController.php 4.3 KB

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