CoreController.php 4.1 KB

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