CoreController.php 4.4 KB

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