CoreController.php 3.8 KB

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