CoreController.php 4.5 KB

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