diff --git a/CHANGELOG.md b/CHANGELOG.md index d635f2e06..e0d455e72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to * [workflow]: Fixed: the notification is sent when the user is added to the first step. +* [budget] Feature: allow to desactivate some charges and resources, adding an `active` key in the configuration ## Test releases diff --git a/src/Bundle/ChillBudgetBundle/Config/ConfigRepository.php b/src/Bundle/ChillBudgetBundle/Config/ConfigRepository.php index 73f6fb355..e498ba5a3 100644 --- a/src/Bundle/ChillBudgetBundle/Config/ConfigRepository.php +++ b/src/Bundle/ChillBudgetBundle/Config/ConfigRepository.php @@ -29,44 +29,58 @@ class ConfigRepository $this->charges = $charges; } - public function getChargesKeys(): array + public function getChargesKeys(bool $onlyActive = false): array { - return array_map(static function ($element) { return $element['key']; }, $this->charges); + return array_map(static function ($element) { return $element['key']; }, $this->getCharges($onlyActive)); } /** * @return array where keys are the resource'key and label the ressource label */ - public function getChargesLabels() + public function getChargesLabels(bool $onlyActive = false) { $charges = []; - foreach ($this->charges as $definition) { + foreach ($this->getCharges($onlyActive) as $definition) { $charges[$definition['key']] = $this->normalizeLabel($definition['labels']); } return $charges; } - public function getResourcesKeys(): array + public function getResourcesKeys(bool $onlyActive = false): array { - return array_map(static function ($element) { return $element['key']; }, $this->resources); + return array_map(static function ($element) { return $element['key']; }, $this->getResources($onlyActive)); } /** * @return array where keys are the resource'key and label the ressource label */ - public function getResourcesLabels() + public function getResourcesLabels(bool $onlyActive = false) { $resources = []; - foreach ($this->resources as $definition) { + foreach ($this->getResources($onlyActive) as $definition) { $resources[$definition['key']] = $this->normalizeLabel($definition['labels']); } return $resources; } + private function getCharges(bool $onlyActive = false): array + { + return $onlyActive ? + array_filter($this->charges, function ($el) { return $el['active']; }) + : $this->charges; + } + + private function getResources(bool $onlyActive = false): array + { + return $onlyActive ? + array_filter($this->resources, function ($el) { return $el['active']; }) + : $this->resources; + } + private function normalizeLabel($labels) { $normalizedLabels = []; diff --git a/src/Bundle/ChillBudgetBundle/DependencyInjection/Configuration.php b/src/Bundle/ChillBudgetBundle/DependencyInjection/Configuration.php index a18e17ae6..710fe43f0 100644 --- a/src/Bundle/ChillBudgetBundle/DependencyInjection/Configuration.php +++ b/src/Bundle/ChillBudgetBundle/DependencyInjection/Configuration.php @@ -14,11 +14,6 @@ namespace Chill\BudgetBundle\DependencyInjection; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; -/** - * This is the class that validates and merges configuration from your app/config files. - * - * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/configuration.html} - */ class Configuration implements ConfigurationInterface { public function getConfigTreeBuilder() @@ -37,6 +32,7 @@ class Configuration implements ConfigurationInterface ->info('the key stored in database') ->example('salary') ->end() + ->booleanNode('active')->defaultTrue()->end() ->arrayNode('labels')->isRequired()->requiresAtLeastOneElement() ->arrayPrototype() ->children() @@ -59,6 +55,7 @@ class Configuration implements ConfigurationInterface ->info('the key stored in database') ->example('salary') ->end() + ->booleanNode('active')->defaultTrue()->end() ->arrayNode('labels')->isRequired()->requiresAtLeastOneElement() ->arrayPrototype() ->children() diff --git a/src/Bundle/ChillBudgetBundle/Form/ChargeType.php b/src/Bundle/ChillBudgetBundle/Form/ChargeType.php index 1e054d86b..85e7e45aa 100644 --- a/src/Bundle/ChillBudgetBundle/Form/ChargeType.php +++ b/src/Bundle/ChillBudgetBundle/Form/ChargeType.php @@ -103,7 +103,7 @@ class ChargeType extends AbstractType private function getTypes() { $charges = $this->configRepository - ->getChargesLabels(); + ->getChargesLabels(true); // rewrite labels to filter in language foreach ($charges as $key => $labels) { diff --git a/src/Bundle/ChillBudgetBundle/Form/ResourceType.php b/src/Bundle/ChillBudgetBundle/Form/ResourceType.php index 51e3f6799..5ad388a39 100644 --- a/src/Bundle/ChillBudgetBundle/Form/ResourceType.php +++ b/src/Bundle/ChillBudgetBundle/Form/ResourceType.php @@ -87,7 +87,7 @@ class ResourceType extends AbstractType private function getTypes() { $resources = $this->configRepository - ->getResourcesLabels(); + ->getResourcesLabels(true); // rewrite labels to filter in language foreach ($resources as $key => $labels) {