From 34c825e4ea7ba991beb33d9414168babaa751a64 Mon Sep 17 00:00:00 2001 From: Elorfin Date: Wed, 9 Feb 2022 08:47:51 +0100 Subject: [PATCH] fixes #1963 --- src/main/core/API/Validator/UserValidator.php | 9 +++- src/main/core/Manager/FacetManager.php | 41 +++++++++++++++++++ .../Resources/config/services/validator.yml | 1 + 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/main/core/API/Validator/UserValidator.php b/src/main/core/API/Validator/UserValidator.php index a607e6520fb..4760e41c253 100644 --- a/src/main/core/API/Validator/UserValidator.php +++ b/src/main/core/API/Validator/UserValidator.php @@ -12,6 +12,7 @@ use Claroline\CoreBundle\Entity\Role; use Claroline\CoreBundle\Entity\User; use Claroline\CoreBundle\Library\Configuration\PlatformConfigurationHandler; +use Claroline\CoreBundle\Manager\FacetManager; use Claroline\CoreBundle\Manager\Organization\OrganizationManager; use Claroline\CoreBundle\Manager\UserManager; use Claroline\CoreBundle\Manager\Workspace\WorkspaceManager; @@ -36,6 +37,8 @@ class UserValidator implements ValidatorInterface private $workspaceManager; /** @var OrganizationManager */ private $organizationManager; + /** @var FacetManager */ + private $facetManager; /** @var ProfileSerializer */ private $profileSerializer; @@ -49,6 +52,7 @@ public function __construct( UserManager $manager, WorkspaceManager $workspaceManager, OrganizationManager $organizationManager, + FacetManager $facetManager, ProfileSerializer $profileSerializer ) { $this->authorization = $authorization; @@ -58,6 +62,7 @@ public function __construct( $this->manager = $manager; $this->workspaceManager = $workspaceManager; $this->organizationManager = $organizationManager; + $this->facetManager = $facetManager; $this->profileSerializer = $profileSerializer; $this->roleRepo = $om->getRepository(Role::class); @@ -135,11 +140,13 @@ public function validate($data, $mode, array $options = []) // todo validate Facet values if (in_array(Options::VALIDATE_FACET, $options)) { $facets = $this->profileSerializer->serialize([Options::REGISTRATION]); + $allFields = []; $required = []; foreach ($facets as $facet) { foreach ($facet['sections'] as $section) { foreach ($section['fields'] as $field) { + $allFields[] = $field; if ($field['required']) { $required[] = $field; } @@ -148,7 +155,7 @@ public function validate($data, $mode, array $options = []) } foreach ($required as $field) { - if (!ArrayUtils::has($data, 'profile.'.$field['id'])) { + if ($this->facetManager->isFieldDisplayed($field, $allFields, $data) && !ArrayUtils::has($data, 'profile.'.$field['id'])) { $errors[] = [ 'path' => 'profile/'.$field['id'], 'message' => 'The field '.$field['label'].' is required', diff --git a/src/main/core/Manager/FacetManager.php b/src/main/core/Manager/FacetManager.php index 34cca0113ba..6280d1869c7 100644 --- a/src/main/core/Manager/FacetManager.php +++ b/src/main/core/Manager/FacetManager.php @@ -2,6 +2,7 @@ namespace Claroline\CoreBundle\Manager; +use Claroline\AppBundle\API\Utils\ArrayUtils; use Claroline\CoreBundle\Event\CatalogEvents\FacetEvents; use Claroline\CoreBundle\Event\Facet\GetFacetValueEvent; use Claroline\CoreBundle\Event\Facet\SetFacetValueEvent; @@ -42,4 +43,44 @@ public function deserializeFieldValue($object, $type, $value = null) return $event->getFormattedValue(); } + + public function isFieldDisplayed(array $fieldDef, array $allFields, array $data): bool + { + $condition = $fieldDef['display']['condition']; + + if (!empty($condition)) { + $parentField = null; + + foreach ($allFields as $searchedField) { + if ($searchedField['id'] === $condition['field']) { + $parentField = $searchedField; + } + } + + if ($parentField) { + $parentValue = ArrayUtils::get($data, 'profile.'.$parentField['id']); + + $displayed = false; + + switch ($condition['comparator']) { + case 'equal': + $displayed = $parentValue === $condition['value']; + break; + case 'different': + $displayed = $parentValue !== $condition['value']; + break; + case 'empty': + $displayed = empty($parentValue); + break; + case 'not_empty': + $displayed = !empty($parentValue); + break; + } + + return $displayed; + } + } + + return true; + } } diff --git a/src/main/core/Resources/config/services/validator.yml b/src/main/core/Resources/config/services/validator.yml index 004a4941ac0..cbe4585fe0c 100644 --- a/src/main/core/Resources/config/services/validator.yml +++ b/src/main/core/Resources/config/services/validator.yml @@ -21,6 +21,7 @@ services: - '@Claroline\CoreBundle\Manager\UserManager' - '@Claroline\CoreBundle\Manager\Workspace\WorkspaceManager' - '@Claroline\CoreBundle\Manager\Organization\OrganizationManager' + - '@Claroline\CoreBundle\Manager\FacetManager' - '@Claroline\CoreBundle\API\Serializer\User\ProfileSerializer' Claroline\CoreBundle\API\Validator\GroupValidator: