123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <?php
- /*
- * This file is part of the Sonata Project package.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Sonata\AdminBundle\Controller;
- use Sonata\AdminBundle\Admin\AdminInterface;
- use Sonata\AdminBundle\Admin\Pool;
- use Sonata\AdminBundle\Search\SearchHandler;
- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
- use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
- use Symfony\Component\HttpFoundation\JsonResponse;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\Response;
- /**
- * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
- */
- class CoreController extends Controller
- {
- /**
- * @return Response
- */
- public function dashboardAction()
- {
- $blocks = array(
- 'top' => array(),
- 'left' => array(),
- 'center' => array(),
- 'right' => array(),
- 'bottom' => array(),
- );
- foreach ($this->container->getParameter('sonata.admin.configuration.dashboard_blocks') as $block) {
- $blocks[$block['position']][] = $block;
- }
- $parameters = array(
- 'base_template' => $this->getBaseTemplate(),
- 'admin_pool' => $this->container->get('sonata.admin.pool'),
- 'blocks' => $blocks,
- );
- if (!$this->getCurrentRequest()->isXmlHttpRequest()) {
- $parameters['breadcrumbs_builder'] = $this->get('sonata.admin.breadcrumbs_builder');
- }
- return $this->render($this->getAdminPool()->getTemplate('dashboard'), $parameters);
- }
- /**
- * The search action first render an empty page, if the query is set, then the template generates
- * some ajax request to retrieve results for each admin. The Ajax query returns a JSON response.
- *
- * @param Request $request
- *
- * @return JsonResponse|Response
- *
- * @throws \RuntimeException
- */
- public function searchAction(Request $request)
- {
- if ($request->get('admin') && $request->isXmlHttpRequest()) {
- try {
- $admin = $this->getAdminPool()->getAdminByAdminCode($request->get('admin'));
- } catch (ServiceNotFoundException $e) {
- throw new \RuntimeException('Unable to find the Admin instance', $e->getCode(), $e);
- }
- if (!$admin instanceof AdminInterface) {
- throw new \RuntimeException('The requested service is not an Admin instance');
- }
- $handler = $this->getSearchHandler();
- $results = array();
- if ($pager = $handler->search($admin, $request->get('q'), $request->get('page'), $request->get('offset'))) {
- foreach ($pager->getResults() as $result) {
- $results[] = array(
- 'label' => $admin->toString($result),
- 'link' => $admin->generateObjectUrl('edit', $result),
- 'id' => $admin->id($result),
- );
- }
- }
- $response = new JsonResponse(array(
- 'results' => $results,
- 'page' => $pager ? (int) $pager->getPage() : false,
- 'total' => $pager ? (int) $pager->getNbResults() : false,
- ));
- $response->setPrivate();
- return $response;
- }
- return $this->render($this->container->get('sonata.admin.pool')->getTemplate('search'), array(
- 'base_template' => $this->getBaseTemplate(),
- 'breadcrumbs_builder' => $this->get('sonata.admin.breadcrumbs_builder'),
- 'admin_pool' => $this->container->get('sonata.admin.pool'),
- 'query' => $request->get('q'),
- 'groups' => $this->getAdminPool()->getDashboardGroups(),
- ));
- }
- /**
- * Get the request object from the container.
- *
- * This method is compatible with both Symfony 2.3 and Symfony 3
- *
- * NEXT_MAJOR: remove this method.
- *
- * @deprecated since 3.0, to be removed in 4.0 and action methods will be adjusted.
- * Use Symfony\Component\HttpFoundation\Request as an action argument
- *
- * @return Request
- */
- public function getRequest()
- {
- @trigger_error(
- 'The '.__METHOD__.' method is deprecated since 3.0 and will be removed in 4.0.'.
- ' Inject the Symfony\Component\HttpFoundation\Request into the actions instead.',
- E_USER_DEPRECATED
- );
- return $this->getCurrentRequest();
- }
- /**
- * @return Pool
- */
- protected function getAdminPool()
- {
- return $this->container->get('sonata.admin.pool');
- }
- /**
- * @return SearchHandler
- */
- protected function getSearchHandler()
- {
- return $this->get('sonata.admin.search.handler');
- }
- /**
- * @return string
- */
- protected function getBaseTemplate()
- {
- if ($this->getCurrentRequest()->isXmlHttpRequest()) {
- return $this->getAdminPool()->getTemplate('ajax');
- }
- return $this->getAdminPool()->getTemplate('layout');
- }
- /**
- * Get the request object from the container.
- *
- * @return Request
- */
- private function getCurrentRequest()
- {
- // NEXT_MAJOR: simplify this when dropping sf < 2.4
- if ($this->container->has('request_stack')) {
- return $this->container->get('request_stack')->getCurrentRequest();
- }
- return $this->container->get('request');
- }
- }
|