From b4801734d37531d1193d8850ba54b669bf2979da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 21 Aug 2018 16:27:33 +0200 Subject: [PATCH] remove accompanying period if not used --- ChillPersonBundle.php | 3 + DependencyInjection/ChillPersonExtension.php | 9 +- ...AccompanyingPeriodTimelineCompilerPass.php | 66 +++++++++++++++ DependencyInjection/Configuration.php | 12 ++- Menu/PersonMenuBuilder.php | 84 +++++++++++++++++++ Resources/config/routing.yml | 10 --- Resources/config/services/menu.yml | 7 ++ Resources/config/validation.yml | 4 +- 8 files changed, 181 insertions(+), 14 deletions(-) create mode 100644 DependencyInjection/CompilerPass/AccompanyingPeriodTimelineCompilerPass.php create mode 100644 Menu/PersonMenuBuilder.php diff --git a/ChillPersonBundle.php b/ChillPersonBundle.php index 93576c0d4..18bf8fb79 100644 --- a/ChillPersonBundle.php +++ b/ChillPersonBundle.php @@ -5,6 +5,7 @@ namespace Chill\PersonBundle; use Symfony\Component\HttpKernel\Bundle\Bundle; use Symfony\Component\DependencyInjection\ContainerBuilder; use Chill\PersonBundle\Widget\PersonListWidgetFactory; +use Chill\PersonBundle\DependencyInjection\CompilerPass\AccompanyingPeriodTimelineCompilerPass; class ChillPersonBundle extends Bundle { @@ -14,5 +15,7 @@ class ChillPersonBundle extends Bundle $container->getExtension('chill_main') ->addWidgetFactory(new PersonListWidgetFactory()); + + $container->addCompilerPass(new AccompanyingPeriodTimelineCompilerPass()); } } diff --git a/DependencyInjection/ChillPersonExtension.php b/DependencyInjection/ChillPersonExtension.php index 29fe2d828..b1fb57099 100644 --- a/DependencyInjection/ChillPersonExtension.php +++ b/DependencyInjection/ChillPersonExtension.php @@ -73,7 +73,14 @@ class ChillPersonExtension extends Extension implements PrependExtensionInterfac $container->setParameter('chill_person.person_fields', $config); foreach ($config as $key => $value) { - $container->setParameter('chill_person.person_fields.'.$key, $value); + switch($key) { + case 'accompanying_period': + $container->setParameter('chill_person.accompanying_period', $value); + break; + default: + $container->setParameter('chill_person.person_fields.'.$key, $value); + break; + } } } diff --git a/DependencyInjection/CompilerPass/AccompanyingPeriodTimelineCompilerPass.php b/DependencyInjection/CompilerPass/AccompanyingPeriodTimelineCompilerPass.php new file mode 100644 index 000000000..1ee7782d3 --- /dev/null +++ b/DependencyInjection/CompilerPass/AccompanyingPeriodTimelineCompilerPass.php @@ -0,0 +1,66 @@ + + * + * 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 . + */ +namespace Chill\PersonBundle\DependencyInjection\CompilerPass; + +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; + +/** + * Remove services which add AccompanyingPeriod to timeline if + * accompanying_periods are set to `hidden` + * + */ +class AccompanyingPeriodTimelineCompilerPass implements CompilerPassInterface +{ + public function process(ContainerBuilder $container) + { + // remove services when accompanying period are hidden + if ($container->getParameter('chill_person.accompanying_period') !== 'hidden') { + return; + } + + $definitions = [ + 'chill.person.timeline.accompanying_period_opening', + 'chill.person.timeline.accompanying_period_closing' + ]; + + foreach($definitions as $definition) { + $container + ->removeDefinition($definition) + ; + } + + $definition = $container->getDefinition('chill.main.timeline_builder'); + + // we have to remove all methods call, and re-add them if not linked + // to this service + $calls = $definition->getMethodCalls(); + + foreach($calls as list($method, $arguments)) { + if ($method !== 'addProvider') { + continue; + } + + $definition->removeMethodCall('addProvider'); + + if (FALSE === \in_array($arguments[1], $definitions)) { + $definition->addMethodCall($method, $arguments); + } + } + } +} diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 860a8b703..b4a8de3ca 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -74,6 +74,7 @@ class Configuration implements ConfigurationInterface ->append($this->addFieldNode('marital_status')) ->append($this->addFieldNode('spoken_languages')) ->append($this->addFieldNode('address')) + ->append($this->addFieldNode('accompanying_period')) ->end() //children for 'person_fields', parent = array 'person_fields' ->end() // person_fields, parent = children of root ->end() // children of 'root', parent = root @@ -87,11 +88,20 @@ class Configuration implements ConfigurationInterface { $tree = new TreeBuilder(); $node = $tree->root($key, 'enum'); + + switch($key) { + case 'accompanying_period': + $info = "If the accompanying periods are shown"; + break; + default: + $info = "If the field $key must be shown"; + break; + } $node ->values(array('hidden', 'visible')) ->defaultValue('visible') - ->info("If the field $key must be shown") + ->info($info) ->end(); return $node; diff --git a/Menu/PersonMenuBuilder.php b/Menu/PersonMenuBuilder.php new file mode 100644 index 000000000..5cefe6b96 --- /dev/null +++ b/Menu/PersonMenuBuilder.php @@ -0,0 +1,84 @@ + + * + * 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 . + */ +namespace Chill\PersonBundle\Menu; + +use Chill\MainBundle\Routing\LocalMenuBuilderInterface; +use Knp\Menu\MenuItem; +use Symfony\Component\Translation\TranslatorInterface; + +/** + * Add menu entrie to person menu. + * + * Menu entries added : + * + * - person details ; + * - accompanying period (if `visible`) + * + */ +class PersonMenuBuilder implements LocalMenuBuilderInterface +{ + /** + * + * @var string 'visible' or 'hidden' + */ + protected $showAccompanyingPeriod; + + /** + * + * @var TranslatorInterface + */ + protected $translator; + + public function __construct( + $showAccompanyingPeriod, + TranslatorInterface $translator + ) { + $this->showAccompanyingPeriod = $showAccompanyingPeriod; + $this->translator = $translator; + } + + public function buildMenu($menuId, MenuItem $menu, array $parameters) + { + $menu->addChild($this->translator->trans('Person details'), [ + 'route' => 'chill_person_view', + 'routeParameters' => [ + 'person_id' => $parameters['person']->getId() + ] + ]) + ->setExtras([ + 'order' => 50 + ]); + + if ($this->showAccompanyingPeriod === 'visible') { + $menu->addChild($this->translator->trans('Accompanying period list'), [ + 'route' => 'chill_person_accompanying_period_list', + 'routeParameters' => [ + 'person_id' => $parameters['person']->getId() + ] + ]) + ->setExtras([ + 'order' => 100 + ]); + } + } + + public static function getMenuIds(): array + { + return [ 'person' ]; + } +} diff --git a/Resources/config/routing.yml b/Resources/config/routing.yml index 56244cae3..0450b3979 100644 --- a/Resources/config/routing.yml +++ b/Resources/config/routing.yml @@ -1,11 +1,6 @@ chill_person_view: path: /{_locale}/person/{person_id}/general defaults: { _controller: ChillPersonBundle:Person:view } - options: - menus: - person: - order: 50 - label: Person details chill_person_general_edit: path: /{_locale}/person/{person_id}/general/edit @@ -39,11 +34,6 @@ chill_person_search: chill_person_accompanying_period_list: path: /{_locale}/person/{person_id}/accompanying-period defaults: { _controller: ChillPersonBundle:AccompanyingPeriod:list } - options: - menus: - person: - order: 100 - label: Accompanying period list chill_person_accompanying_period_create: path: /{_locale}/person/{person_id}/accompanying-period/create diff --git a/Resources/config/services/menu.yml b/Resources/config/services/menu.yml index 09b24d86b..8758d8d93 100644 --- a/Resources/config/services/menu.yml +++ b/Resources/config/services/menu.yml @@ -4,3 +4,10 @@ services: $authorizationChecker: '@Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface' tags: - { name: 'chill.menu_builder' } + + Chill\PersonBundle\Menu\PersonMenuBuilder: + arguments: + $showAccompanyingPeriod: '%chill_person.accompanying_period%' + $translator: '@Symfony\Component\Translation\TranslatorInterface' + tags: + - { name: 'chill.menu_builder' } diff --git a/Resources/config/validation.yml b/Resources/config/validation.yml index a430847e5..814db0ee4 100644 --- a/Resources/config/validation.yml +++ b/Resources/config/validation.yml @@ -42,7 +42,7 @@ Chill\PersonBundle\Entity\Person: message: 'Invalid phone number: it should begin with the international prefix starting with "+", hold only digits and be smaller than 20 characters. Ex: +33123456789' - Chill\MainBundle\Validation\Constraint\PhonenumberConstraint: type: landline - groups: [ general ] + groups: [ general, creation ] mobilenumber: - Regex: pattern: '/^([\+{1}])([0-9\s*]{4,20})$/' @@ -50,7 +50,7 @@ Chill\PersonBundle\Entity\Person: message: 'Invalid phone number: it should begin with the international prefix starting with "+", hold only digits and be smaller than 20 characters. Ex: +33623456789' - Chill\MainBundle\Validation\Constraint\PhonenumberConstraint: type: mobile - groups: [ general ] + groups: [ general, creation ] constraints: