remove accompanying period if not used

This commit is contained in:
Julien Fastré 2018-08-21 16:27:33 +02:00
parent c21b87b9c6
commit b4801734d3
8 changed files with 181 additions and 14 deletions

View File

@ -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());
}
}

View File

@ -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;
}
}
}

View File

@ -0,0 +1,66 @@
<?php
/*
* Copyright (C) 2018 Champs-Libres <info@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/>.
*/
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);
}
}
}
}

View File

@ -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;

View File

@ -0,0 +1,84 @@
<?php
/*
* Copyright (C) 2018 Champs-Libres <info@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/>.
*/
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' ];
}
}

View File

@ -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

View File

@ -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' }

View File

@ -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: