-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
374 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <[email protected]> | ||
* | ||
* 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\BreadcrumbsBuilderInterface; | ||
use Sonata\AdminBundle\Admin\Pool; | ||
use Sonata\AdminBundle\Templating\TemplateRegistryInterface; | ||
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
final class DashboardAction extends Controller | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $dashboardBlocks; | ||
|
||
/** | ||
* @var BreadcrumbsBuilderInterface | ||
*/ | ||
private $breadcrumbsBuilder; | ||
|
||
/** | ||
* @var TemplateRegistryInterface | ||
*/ | ||
private $templateRegistry; | ||
|
||
/** | ||
* @var Pool | ||
*/ | ||
private $pool; | ||
|
||
public function __construct( | ||
array $dashboardBlocks, | ||
BreadcrumbsBuilderInterface $breadcrumbsBuilder, | ||
TemplateRegistryInterface $templateRegistry, | ||
Pool $pool | ||
) { | ||
$this->dashboardBlocks = $dashboardBlocks; | ||
$this->breadcrumbsBuilder = $breadcrumbsBuilder; | ||
$this->templateRegistry = $templateRegistry; | ||
$this->pool = $pool; | ||
} | ||
|
||
public function __invoke(Request $request) | ||
{ | ||
$blocks = [ | ||
'top' => [], | ||
'left' => [], | ||
'center' => [], | ||
'right' => [], | ||
'bottom' => [], | ||
]; | ||
|
||
foreach ($this->dashboardBlocks as $block) { | ||
$blocks[$block['position']][] = $block; | ||
} | ||
|
||
$parameters = [ | ||
'base_template' => $request->isXmlHttpRequest() ? | ||
$this->templateRegistry->getTemplate('ajax') : | ||
$this->templateRegistry->getTemplate('layout'), | ||
'admin_pool' => $this->pool, | ||
'blocks' => $blocks, | ||
]; | ||
|
||
if (!$request->isXmlHttpRequest()) { | ||
$parameters['breadcrumbs_builder'] = $this->breadcrumbsBuilder; | ||
} | ||
|
||
return $this->render($this->templateRegistry->getTemplate('dashboard'), $parameters); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <[email protected]> | ||
* | ||
* 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\BreadcrumbsBuilderInterface; | ||
use Sonata\AdminBundle\Admin\Pool; | ||
use Sonata\AdminBundle\Search\SearchHandler; | ||
use Sonata\AdminBundle\Templating\TemplateRegistryInterface; | ||
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; | ||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
final class SearchAction extends Controller | ||
{ | ||
/** | ||
* @var Pool | ||
*/ | ||
private $pool; | ||
|
||
/** | ||
* @var SearchHandler | ||
*/ | ||
private $searchHandler; | ||
|
||
/** | ||
* @var TemplateRegistryInterface | ||
*/ | ||
private $templateRegistry; | ||
|
||
/** | ||
* @var BreadcrumbsBuilderInterface | ||
*/ | ||
private $breadcrumbsBuilder; | ||
|
||
public function __construct( | ||
Pool $pool, | ||
SearchHandler $searchHandler, | ||
TemplateRegistryInterface $templateRegistry, | ||
BreadcrumbsBuilderInterface $breadcrumbsBuilder | ||
) { | ||
$this->pool = $pool; | ||
$this->searchHandler = $searchHandler; | ||
$this->templateRegistry = $templateRegistry; | ||
$this->breadcrumbsBuilder = $breadcrumbsBuilder; | ||
} | ||
|
||
/** | ||
* 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. | ||
* | ||
* @throws \RuntimeException | ||
* | ||
* @return JsonResponse|Response | ||
*/ | ||
public function __invoke(Request $request) | ||
{ | ||
if ($request->get('admin') && $request->isXmlHttpRequest()) { | ||
try { | ||
$admin = $this->pool->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'); | ||
} | ||
|
||
$results = []; | ||
|
||
if ($pager = $this->searchHandler->search( | ||
$admin, | ||
$request->get('q'), | ||
$request->get('page'), | ||
$request->get('offset') | ||
)) { | ||
foreach ($pager->getResults() as $result) { | ||
$results[] = [ | ||
'label' => $admin->toString($result), | ||
'link' => $admin->generateObjectUrl('edit', $result), | ||
'id' => $admin->id($result), | ||
]; | ||
} | ||
} | ||
|
||
$response = new JsonResponse([ | ||
'results' => $results, | ||
'page' => $pager ? (int) $pager->getPage() : false, | ||
'total' => $pager ? (int) $pager->getNbResults() : false, | ||
]); | ||
$response->setPrivate(); | ||
|
||
return $response; | ||
} | ||
|
||
return $this->render($this->templateRegistry->getTemplate('search'), [ | ||
'base_template' => $request->isXmlHttpRequest() ? | ||
$this->templateRegistry->getTemplate('ajax') : | ||
$this->templateRegistry->getTemplate('layout'), | ||
'breadcrumbs_builder' => $this->breadcrumbsBuilder, | ||
'admin_pool' => $this->pool, | ||
'query' => $request->get('q'), | ||
'groups' => $this->pool->getDashboardGroups(), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
<services> | ||
<service id="Sonata\AdminBundle\Controller\DashboardAction" public="true"> | ||
<argument>%sonata.admin.configuration.dashboard_blocks%</argument> | ||
<argument type="service" id="sonata.admin.breadcrumbs_builder"/> | ||
<argument type="service" id="sonata.admin.global_template_registry"/> | ||
<argument type="service" id="sonata.admin.pool"/> | ||
</service> | ||
<service id="Sonata\AdminBundle\Controller\SearchAction" public="true"> | ||
<argument type="service" id="sonata.admin.pool"/> | ||
<argument type="service" id="sonata.admin.search.handler"/> | ||
<argument type="service" id="sonata.admin.global_template_registry"/> | ||
<argument type="service" id="sonata.admin.breadcrumbs_builder"/> | ||
</service> | ||
</services> | ||
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.