mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-05 22:35:01 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,5 +1,12 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\MainBundle\Routing\Loader;
|
||||
@@ -8,7 +15,7 @@ use Symfony\Component\Config\Loader\Loader;
|
||||
use Symfony\Component\Routing\RouteCollection;
|
||||
|
||||
/**
|
||||
* Import routes from bundles
|
||||
* Import routes from bundles.
|
||||
*
|
||||
* Routes must be defined in configuration, add an entry
|
||||
* under `chill_main.routing.resources`
|
||||
@@ -30,7 +37,7 @@ class ChillRoutesLoader extends Loader
|
||||
|
||||
foreach ($this->routes as $routeResource) {
|
||||
$collection->addCollection(
|
||||
$this->import($routeResource, NULL)
|
||||
$this->import($routeResource, null)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -41,5 +48,4 @@ class ChillRoutesLoader extends Loader
|
||||
{
|
||||
return 'chill_routes' === $type;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,38 +1,26 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright (C) 2018 Champs Libres Cooperative <info@champs-libres.coop>
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Routing;
|
||||
|
||||
use Knp\Menu\MenuItem;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
interface LocalMenuBuilderInterface
|
||||
{
|
||||
public function buildMenu($menuId, MenuItem $menu, array $parameters);
|
||||
|
||||
/**
|
||||
* return an array of menu ids
|
||||
*
|
||||
* @internal this method is static to 1. keep all config in the class,
|
||||
* return an array of menu ids.
|
||||
*
|
||||
* @internal this method is static to 1. keep all config in the class,
|
||||
* instead of tags arguments; 2. avoid a "supports" method, which could lead
|
||||
* to parsing all instances to get only one or two working instance.
|
||||
*/
|
||||
public static function getMenuIds(): array;
|
||||
|
||||
public function buildMenu($menuId, MenuItem $menu, array $parameters);
|
||||
}
|
||||
|
@@ -1,35 +1,21 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright (C) 2018 Champs Libres Cooperative <info@champs-libres.coop>
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Routing\MenuBuilder;
|
||||
|
||||
use Chill\MainBundle\Routing\LocalMenuBuilderInterface;
|
||||
use Knp\Menu\MenuItem;
|
||||
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
|
||||
use Chill\MainBundle\Security\Authorization\ChillExportVoter;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
class AdminSectionMenuBuilder implements LocalMenuBuilderInterface
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var AuthorizationCheckerInterface
|
||||
*/
|
||||
protected $authorizationChecker;
|
||||
@@ -47,26 +33,26 @@ class AdminSectionMenuBuilder implements LocalMenuBuilderInterface
|
||||
}
|
||||
|
||||
$menu->addChild('Users and permissions', [
|
||||
'route' => 'chill_main_admin_permissions'
|
||||
])
|
||||
'route' => 'chill_main_admin_permissions',
|
||||
])
|
||||
->setExtras([
|
||||
'icons' => ['key'],
|
||||
'order' => 200,
|
||||
'explain' => "Configure permissions for users"
|
||||
'explain' => 'Configure permissions for users',
|
||||
]);
|
||||
|
||||
$menu->addChild('Location and location type', [
|
||||
'route' => 'chill_main_admin_locations'
|
||||
])
|
||||
'route' => 'chill_main_admin_locations',
|
||||
])
|
||||
->setExtras([
|
||||
'icons' => ['key'],
|
||||
'order' => 205,
|
||||
'explain' => "Configure location and location type"
|
||||
'explain' => 'Configure location and location type',
|
||||
]);
|
||||
}
|
||||
|
||||
public static function getMenuIds(): array
|
||||
{
|
||||
return [ 'admin_section', 'admin_index' ];
|
||||
return ['admin_section', 'admin_index'];
|
||||
}
|
||||
}
|
||||
|
@@ -1,28 +1,31 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Routing\MenuBuilder;
|
||||
|
||||
use Knp\Menu\MenuItem;
|
||||
|
||||
class LocationMenuBuilder implements \Chill\MainBundle\Routing\LocalMenuBuilderInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function getMenuIds(): array
|
||||
{
|
||||
return [ 'admin_location' ];
|
||||
}
|
||||
|
||||
public function buildMenu($menuId, MenuItem $menu, array $parameters)
|
||||
{
|
||||
$menu->addChild('Location type list', [
|
||||
'route' => 'chill_crud_main_location_type_index'
|
||||
'route' => 'chill_crud_main_location_type_index',
|
||||
])->setExtras(['order' => 205]);
|
||||
|
||||
$menu->addChild('Location list', [
|
||||
'route' => 'chill_crud_main_location_index'
|
||||
'route' => 'chill_crud_main_location_index',
|
||||
])->setExtras(['order' => 206]);
|
||||
}
|
||||
|
||||
public static function getMenuIds(): array
|
||||
{
|
||||
return ['admin_location'];
|
||||
}
|
||||
}
|
||||
|
@@ -1,43 +1,45 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Routing\MenuBuilder;
|
||||
|
||||
use Knp\Menu\MenuItem;
|
||||
|
||||
class PermissionMenuBuilder implements \Chill\MainBundle\Routing\LocalMenuBuilderInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function getMenuIds(): array
|
||||
{
|
||||
return [ 'admin_permissions' ];
|
||||
}
|
||||
|
||||
public function buildMenu($menuId, MenuItem $menu, array $parameters)
|
||||
{
|
||||
$menu->addChild('Permissions group list', [
|
||||
'route' => 'admin_permissionsgroup'
|
||||
'route' => 'admin_permissionsgroup',
|
||||
])->setExtras([
|
||||
'order' => 300
|
||||
]);
|
||||
'order' => 300,
|
||||
]);
|
||||
|
||||
$menu->addChild('crud.admin_user.index.title', [
|
||||
'route' => 'chill_crud_admin_user_index'
|
||||
'route' => 'chill_crud_admin_user_index',
|
||||
])->setExtras(['order' => 400]);
|
||||
|
||||
$menu->addChild('List circles', [
|
||||
'route' => 'admin_scope'
|
||||
'route' => 'admin_scope',
|
||||
])->setExtras(['order' => 200]);
|
||||
|
||||
$menu->addChild('Center list', [
|
||||
'route' => 'admin_center'
|
||||
'route' => 'admin_center',
|
||||
])->setExtras(['order' => 100]);
|
||||
|
||||
$menu->addChild('User jobs', [
|
||||
'route' => 'chill_crud_admin_user_job_index'
|
||||
'route' => 'chill_crud_admin_user_job_index',
|
||||
])->setExtras(['order' => 150]);
|
||||
}
|
||||
|
||||
public static function getMenuIds(): array
|
||||
{
|
||||
return ['admin_permissions'];
|
||||
}
|
||||
}
|
||||
|
@@ -1,35 +1,22 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2018 Champs Libres Cooperative <info@champs-libres.coop>
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Routing\MenuBuilder;
|
||||
|
||||
use Chill\MainBundle\Routing\LocalMenuBuilderInterface;
|
||||
use Chill\MainBundle\Security\Authorization\ChillExportVoter;
|
||||
use Knp\Menu\MenuItem;
|
||||
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
|
||||
use Chill\MainBundle\Security\Authorization\ChillExportVoter;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
|
||||
/**
|
||||
* Class SectionMenuBuilder
|
||||
*
|
||||
* @package Chill\MainBundle\Routing\MenuBuilder
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* Class SectionMenuBuilder.
|
||||
*/
|
||||
class SectionMenuBuilder implements LocalMenuBuilderInterface
|
||||
{
|
||||
@@ -37,62 +24,56 @@ class SectionMenuBuilder implements LocalMenuBuilderInterface
|
||||
* @var AuthorizationCheckerInterface
|
||||
*/
|
||||
protected $authorizationChecker;
|
||||
|
||||
|
||||
/**
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
|
||||
/**
|
||||
* SectionMenuBuilder constructor.
|
||||
*
|
||||
* @param AuthorizationCheckerInterface $authorizationChecker
|
||||
*/
|
||||
public function __construct(AuthorizationCheckerInterface $authorizationChecker, TranslatorInterface $translator)
|
||||
{
|
||||
$this->authorizationChecker = $authorizationChecker;
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $menuId
|
||||
* @param MenuItem $menu
|
||||
* @param array $parameters
|
||||
*/
|
||||
public function buildMenu($menuId, MenuItem $menu, array $parameters)
|
||||
{
|
||||
$menu->addChild($this->translator->trans('Homepage'), [
|
||||
'route' => 'chill_main_homepage'
|
||||
])
|
||||
'route' => 'chill_main_homepage',
|
||||
])
|
||||
->setExtras([
|
||||
'icons' => ['home'],
|
||||
'order' => 0
|
||||
'order' => 0,
|
||||
]);
|
||||
|
||||
$menu->addChild($this->translator->trans('Global timeline'), [
|
||||
'route' => 'chill_center_timeline',
|
||||
])
|
||||
->setExtras([
|
||||
'order' => 10
|
||||
]
|
||||
);
|
||||
|
||||
->setExtras(
|
||||
[
|
||||
'order' => 10,
|
||||
]
|
||||
);
|
||||
|
||||
if ($this->authorizationChecker->isGranted(ChillExportVoter::EXPORT)) {
|
||||
$menu->addChild($this->translator->trans('Export Menu'), [
|
||||
'route' => 'chill_main_export_index'
|
||||
'route' => 'chill_main_export_index',
|
||||
])
|
||||
->setExtras([
|
||||
'icons' => ['upload'],
|
||||
'order' => 20
|
||||
]);
|
||||
->setExtras([
|
||||
'icons' => ['upload'],
|
||||
'order' => 20,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
|
||||
public static function getMenuIds(): array
|
||||
{
|
||||
return [ 'section' ];
|
||||
return ['section'];
|
||||
}
|
||||
}
|
||||
|
@@ -1,25 +1,16 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright (C) 2018 Champs Libres Cooperative <info@champs-libres.coop>
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Routing\MenuBuilder;
|
||||
|
||||
use Chill\MainBundle\Routing\LocalMenuBuilderInterface;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
||||
use Chill\MainBundle\Routing\LocalMenuBuilderInterface;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
|
||||
class UserMenuBuilder implements LocalMenuBuilderInterface
|
||||
@@ -36,7 +27,6 @@ class UserMenuBuilder implements LocalMenuBuilderInterface
|
||||
$user = $this->security->getUser();
|
||||
|
||||
if ($user instanceof User) {
|
||||
|
||||
if (null !== $user->getCurrentLocation()) {
|
||||
$locationTextMenu = $user->getCurrentLocation()->getName();
|
||||
} else {
|
||||
@@ -50,7 +40,7 @@ class UserMenuBuilder implements LocalMenuBuilderInterface
|
||||
)
|
||||
->setExtras([
|
||||
'order' => -9999999,
|
||||
'icon' => 'map-marker'
|
||||
'icon' => 'map-marker',
|
||||
]);
|
||||
$menu
|
||||
->addChild(
|
||||
@@ -58,7 +48,7 @@ class UserMenuBuilder implements LocalMenuBuilderInterface
|
||||
['route' => 'change_my_password']
|
||||
)
|
||||
->setExtras([
|
||||
'order' => 99999999998
|
||||
'order' => 99999999998,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -66,17 +56,17 @@ class UserMenuBuilder implements LocalMenuBuilderInterface
|
||||
->addChild(
|
||||
'Logout',
|
||||
[
|
||||
'route' => 'logout'
|
||||
'route' => 'logout',
|
||||
]
|
||||
)
|
||||
->setExtras([
|
||||
'order' => 99999999999,
|
||||
'icon' => 'power-off'
|
||||
'icon' => 'power-off',
|
||||
]);
|
||||
}
|
||||
|
||||
public static function getMenuIds(): array
|
||||
{
|
||||
return [ 'user' ];
|
||||
return ['user'];
|
||||
}
|
||||
}
|
||||
|
@@ -1,38 +1,43 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\MainBundle\Routing;
|
||||
|
||||
use Symfony\Component\Routing\RouteCollection;
|
||||
use Symfony\Component\Routing\RouterInterface;
|
||||
use Knp\Menu\FactoryInterface;
|
||||
use Knp\Menu\ItemInterface;
|
||||
use Symfony\Component\Routing\RouteCollection;
|
||||
use Symfony\Component\Routing\RouterInterface;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use function array_merge;
|
||||
use function array_values;
|
||||
|
||||
/**
|
||||
* This class permit to build menu from the routing information
|
||||
* stored in each bundle.
|
||||
*
|
||||
* how to must come here FIXME
|
||||
*
|
||||
* @author julien
|
||||
*/
|
||||
class MenuComposer
|
||||
{
|
||||
|
||||
private RouterInterface $router;
|
||||
private array $localMenuBuilders = [];
|
||||
|
||||
private FactoryInterface $menuFactory;
|
||||
|
||||
private TranslatorInterface $translator;
|
||||
|
||||
private array $localMenuBuilders = [];
|
||||
|
||||
private RouteCollection $routeCollection;
|
||||
|
||||
private RouterInterface $router;
|
||||
|
||||
function __construct(
|
||||
private TranslatorInterface $translator;
|
||||
|
||||
public function __construct(
|
||||
RouterInterface $router,
|
||||
FactoryInterface $menuFactory,
|
||||
TranslatorInterface $translator
|
||||
@@ -42,35 +47,58 @@ class MenuComposer
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the route Collection
|
||||
* This function is needed for testing purpose: routeCollection is not
|
||||
* available as a service (RouterInterface is provided as a service and
|
||||
* added to this class as paramater in __construct)
|
||||
*
|
||||
* @param RouteCollection $routeCollection
|
||||
*/
|
||||
public function setRouteCollection(RouteCollection $routeCollection)
|
||||
public function addLocalMenuBuilder(LocalMenuBuilderInterface $menuBuilder, $menuId)
|
||||
{
|
||||
$this->routeCollection = $routeCollection;
|
||||
$this->localMenuBuilders[$menuId][] = $menuBuilder;
|
||||
}
|
||||
|
||||
public function getMenuFor($menuId, array $parameters = [])
|
||||
{
|
||||
$routes = $this->getRoutesFor($menuId, $parameters);
|
||||
$menu = $this->menuFactory->createItem($menuId);
|
||||
|
||||
// build menu from routes
|
||||
foreach ($routes as $order => $route) {
|
||||
$menu->addChild($this->translator->trans($route['label']), [
|
||||
'route' => $route['key'],
|
||||
'routeParameters' => $parameters['args'],
|
||||
'order' => $order,
|
||||
])
|
||||
->setExtras([
|
||||
//'icon' => $route['icon'],
|
||||
// sf4 check: commented to avoid error: `An exception has been thrown during the rendering of a template ("Notice: Undefined index: icon").`
|
||||
'order' => $order,
|
||||
]);
|
||||
}
|
||||
|
||||
if ($this->hasLocalMenuBuilder($menuId)) {
|
||||
foreach ($this->localMenuBuilders[$menuId] as $builder) {
|
||||
/* @var $builder LocalMenuBuilderInterface */
|
||||
$builder->buildMenu($menuId, $menu, $parameters['args']);
|
||||
}
|
||||
}
|
||||
|
||||
$this->reorderMenu($menu);
|
||||
|
||||
return $menu;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of routes added to $menuId,
|
||||
* The array is aimed to build route with MenuTwig
|
||||
* The array is aimed to build route with MenuTwig.
|
||||
*
|
||||
* @param string $menuId
|
||||
* @param array $parameters see https://redmine.champs-libres.coop/issues/179
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getRoutesFor($menuId, array $parameters = array())
|
||||
public function getRoutesFor($menuId, array $parameters = [])
|
||||
{
|
||||
$routes = array();
|
||||
$routes = [];
|
||||
$routeCollection = $this->router->getRouteCollection();
|
||||
|
||||
foreach ($routeCollection->all() as $routeKey => $route) {
|
||||
if ($route->hasOption('menus')) {
|
||||
|
||||
if (array_key_exists($menuId, $route->getOption('menus'))) {
|
||||
$route = $route->getOption('menus')[$menuId];
|
||||
|
||||
@@ -89,54 +117,29 @@ class MenuComposer
|
||||
return $routes;
|
||||
}
|
||||
|
||||
public function getMenuFor($menuId, array $parameters = array())
|
||||
/**
|
||||
* Return true if the menu has at least one builder.
|
||||
*
|
||||
* This function is a helper to determine if the method `getMenuFor`
|
||||
* should be used, or `getRouteFor`. The method `getMenuFor` should be used
|
||||
* if the result is true (it **does** exists at least one menu builder.
|
||||
*
|
||||
* @param string $menuId
|
||||
*/
|
||||
public function hasLocalMenuBuilder($menuId): bool
|
||||
{
|
||||
$routes = $this->getRoutesFor($menuId, $parameters);
|
||||
$menu = $this->menuFactory->createItem($menuId);
|
||||
|
||||
// build menu from routes
|
||||
foreach ($routes as $order => $route) {
|
||||
$menu->addChild($this->translator->trans($route['label']), [
|
||||
'route' => $route['key'],
|
||||
'routeParameters' => $parameters['args'],
|
||||
'order' => $order
|
||||
])
|
||||
->setExtras([
|
||||
//'icon' => $route['icon'],
|
||||
// sf4 check: commented to avoid error: `An exception has been thrown during the rendering of a template ("Notice: Undefined index: icon").`
|
||||
'order' => $order
|
||||
])
|
||||
;
|
||||
}
|
||||
|
||||
if ($this->hasLocalMenuBuilder($menuId)) {
|
||||
foreach ($this->localMenuBuilders[$menuId] as $builder) {
|
||||
/* @var $builder LocalMenuBuilderInterface */
|
||||
$builder->buildMenu($menuId, $menu, $parameters['args']);
|
||||
}
|
||||
}
|
||||
|
||||
$this->reorderMenu($menu);
|
||||
|
||||
return $menu;
|
||||
return \array_key_exists($menuId, $this->localMenuBuilders);
|
||||
}
|
||||
|
||||
/**
|
||||
* recursive function to resolve the order of a array of routes.
|
||||
* If the order chosen in routing.yml is already in used, find the
|
||||
* first next order available.
|
||||
*
|
||||
* @param array $routes the routes previously added
|
||||
* @param int $order
|
||||
* @return int
|
||||
* Set the route Collection
|
||||
* This function is needed for testing purpose: routeCollection is not
|
||||
* available as a service (RouterInterface is provided as a service and
|
||||
* added to this class as paramater in __construct).
|
||||
*/
|
||||
private function resolveOrder($routes, $order)
|
||||
public function setRouteCollection(RouteCollection $routeCollection)
|
||||
{
|
||||
if (isset($routes[$order])) {
|
||||
return $this->resolveOrder($routes, $order + 1);
|
||||
} else {
|
||||
return $order;
|
||||
}
|
||||
$this->routeCollection = $routeCollection;
|
||||
}
|
||||
|
||||
private function reorderMenu(ItemInterface $menu)
|
||||
@@ -147,8 +150,8 @@ class MenuComposer
|
||||
foreach ($menu->getChildren() as $name => $item) {
|
||||
$order = $item->getExtra('order');
|
||||
|
||||
if ($order !== null) {
|
||||
$ordered[$this->resolveOrder($ordered, $order)] = $name;
|
||||
if (null !== $order) {
|
||||
$ordered[$this->resolveOrder($ordered, $order)] = $name;
|
||||
} else {
|
||||
$unordered = $name;
|
||||
}
|
||||
@@ -156,29 +159,26 @@ class MenuComposer
|
||||
|
||||
ksort($ordered);
|
||||
|
||||
$menus = \array_merge(\array_values($ordered), $unordered);
|
||||
$menus = array_merge(array_values($ordered), $unordered);
|
||||
$menu->reorderChildren($menus);
|
||||
}
|
||||
|
||||
|
||||
public function addLocalMenuBuilder(LocalMenuBuilderInterface $menuBuilder, $menuId)
|
||||
{
|
||||
$this->localMenuBuilders[$menuId][] = $menuBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the menu has at least one builder.
|
||||
* recursive function to resolve the order of a array of routes.
|
||||
* If the order chosen in routing.yml is already in used, find the
|
||||
* first next order available.
|
||||
*
|
||||
* This function is a helper to determine if the method `getMenuFor`
|
||||
* should be used, or `getRouteFor`. The method `getMenuFor` should be used
|
||||
* if the result is true (it **does** exists at least one menu builder.
|
||||
* @param array $routes the routes previously added
|
||||
* @param int $order
|
||||
*
|
||||
* @param string $menuId
|
||||
* @return bool
|
||||
* @return int
|
||||
*/
|
||||
public function hasLocalMenuBuilder($menuId): bool
|
||||
private function resolveOrder($routes, $order)
|
||||
{
|
||||
return \array_key_exists($menuId, $this->localMenuBuilders);
|
||||
}
|
||||
if (isset($routes[$order])) {
|
||||
return $this->resolveOrder($routes, $order + 1);
|
||||
}
|
||||
|
||||
return $order;
|
||||
}
|
||||
}
|
||||
|
@@ -1,27 +1,14 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
* Copyright (C) 2014 Champs Libres Cooperative SCRLFS,
|
||||
* <http://www.champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Routing;
|
||||
|
||||
use Chill\MainBundle\Routing\MenuComposer;
|
||||
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Twig\Environment;
|
||||
@@ -29,90 +16,86 @@ use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
/**
|
||||
* Add the filter 'chill_menu'
|
||||
*
|
||||
* @author Julien Fastré <julien arobase fastre point info>
|
||||
* Add the filter 'chill_menu'.
|
||||
*/
|
||||
class MenuTwig extends AbstractExtension implements ContainerAwareInterface
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
* @var MenuComposer
|
||||
*/
|
||||
private $menuComposer;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var \Symfony\Component\DependencyInjection\ContainerInterface
|
||||
*/
|
||||
private $container;
|
||||
|
||||
|
||||
/**
|
||||
* the default parameters for chillMenu
|
||||
*
|
||||
* @var mixed[]
|
||||
* the default parameters for chillMenu.
|
||||
*
|
||||
* @var mixed[]
|
||||
*/
|
||||
private $defaultParams = array(
|
||||
private $defaultParams = [
|
||||
'layout' => '@ChillMain/Menu/defaultMenu.html.twig',
|
||||
'args' => array(),
|
||||
'activeRouteKey' => null
|
||||
);
|
||||
|
||||
'args' => [],
|
||||
'activeRouteKey' => null,
|
||||
];
|
||||
|
||||
/**
|
||||
* @var MenuComposer
|
||||
*/
|
||||
private $menuComposer;
|
||||
|
||||
public function __construct(MenuComposer $menuComposer)
|
||||
{
|
||||
$this->menuComposer = $menuComposer;
|
||||
}
|
||||
|
||||
public function getFunctions()
|
||||
{
|
||||
return [new TwigFunction('chill_menu',
|
||||
array($this, 'chillMenu'), array(
|
||||
'is_safe' => array('html'),
|
||||
'needs_environment' => true
|
||||
))
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Render a Menu corresponding to $menuId
|
||||
*
|
||||
* Expected params :
|
||||
* Render a Menu corresponding to $menuId.
|
||||
*
|
||||
* Expected params :
|
||||
* - args: the arguments to build the path (i.e: if pattern is /something/{bar}, args must contain {'bar': 'foo'}
|
||||
* - layout: the layout. Absolute path needed (i.e.: ChillXyzBundle:section:foo.html.twig)
|
||||
* - activeRouteKey : the key active, will render the menu differently.
|
||||
*
|
||||
*
|
||||
* @deprecated link: see https://redmine.champs-libres.coop/issues/179 for more informations
|
||||
*
|
||||
*
|
||||
* @param string $menuId
|
||||
* @param mixed[] $params
|
||||
*/
|
||||
public function chillMenu(Environment $env, $menuId, array $params = array())
|
||||
public function chillMenu(Environment $env, $menuId, array $params = [])
|
||||
{
|
||||
$resolvedParams = array_merge($this->defaultParams, $params);
|
||||
|
||||
$layout = $resolvedParams['layout'];
|
||||
unset($resolvedParams['layout']);
|
||||
|
||||
|
||||
if ($this->menuComposer->hasLocalMenuBuilder($menuId) === false) {
|
||||
$resolvedParams['routes'] = $this->menuComposer->getRoutesFor($menuId, $resolvedParams);
|
||||
|
||||
return $env->render($layout, $resolvedParams);
|
||||
} else {
|
||||
$resolvedParams['menus'] = $this->menuComposer->getMenuFor($menuId, $resolvedParams);
|
||||
|
||||
|
||||
return $env->render($layout, $resolvedParams);
|
||||
}
|
||||
$resolvedParams['menus'] = $this->menuComposer->getMenuFor($menuId, $resolvedParams);
|
||||
|
||||
return $env->render($layout, $resolvedParams);
|
||||
}
|
||||
|
||||
|
||||
public function getFunctions()
|
||||
{
|
||||
return [new TwigFunction(
|
||||
'chill_menu',
|
||||
[$this, 'chillMenu'],
|
||||
[
|
||||
'is_safe' => ['html'],
|
||||
'needs_environment' => true,
|
||||
]
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return 'chill_menu';
|
||||
}
|
||||
|
||||
public function setContainer(ContainerInterface $container = null)
|
||||
public function setContainer(?ContainerInterface $container = null)
|
||||
{
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user