diff --git a/src/CoreBundle/Controller/Admin/SettingsController.php b/src/CoreBundle/Controller/Admin/SettingsController.php index b12ec187922..9a84fd2f334 100644 --- a/src/CoreBundle/Controller/Admin/SettingsController.php +++ b/src/CoreBundle/Controller/Admin/SettingsController.php @@ -152,15 +152,20 @@ public function updateSetting(Request $request, AccessUrlHelper $accessUrlHelper $manager->save($form->getData()); $message = $this->trans('Settings have been successfully updated'); } catch (ValidatorException $validatorException) { - // $message = $this->trans($exception->getMessage(), [], 'validators'); $message = $this->trans($validatorException->getMessage()); $messageType = 'error'; } $this->addFlash($messageType, $message); - if (!empty($keywordFromGet)) { - return $this->redirect($request->headers->get('referer')); + if (!empty($keyword)) { + return $this->redirectToRoute('chamilo_platform_settings_search', [ + 'keyword' => $keyword, + ]); } + + return $this->redirectToRoute('chamilo_platform_settings', [ + 'namespace' => $namespace, + ]); } $schemas = $manager->getSchemas(); diff --git a/src/CoreBundle/Form/DataTransformer/ResourceToIdentifierTransformer.php b/src/CoreBundle/Form/DataTransformer/ResourceToIdentifierTransformer.php index 52e57fc3081..737fdfb0422 100644 --- a/src/CoreBundle/Form/DataTransformer/ResourceToIdentifierTransformer.php +++ b/src/CoreBundle/Form/DataTransformer/ResourceToIdentifierTransformer.php @@ -31,23 +31,34 @@ public function transform($value) return null; } - /* @psalm-suppress ArgumentTypeCoercion */ - Assert::isInstanceOf($value, $this->repository->getClassName()); + if (is_object($value) && method_exists($value, 'getId')) { + return $value; + } + + if (is_numeric($value)) { + return $this->repository->find($value); + } - return PropertyAccess::createPropertyAccessor()->getValue($value, $this->identifier); + return $value; } public function reverseTransform($value) { - if (null === $value) { + if (null === $value || '' === $value) { return null; } - $resource = $this->repository->findOneBy([ - $this->identifier => $value, - ]); + if (is_object($value) && method_exists($value, 'getId')) { + return $value; + } + + $resource = $this->repository->find($value); if (null === $resource) { - throw new TransformationFailedException(\sprintf('Object "%s" with identifier "%s"="%s" does not exist.', $this->repository->getClassName(), $this->identifier, $value)); + throw new TransformationFailedException(sprintf( + 'Object "%s" with identifier "%s" does not exist.', + $this->repository->getClassName(), + $value + )); } return $resource; diff --git a/src/CoreBundle/Settings/CourseSettingsSchema.php b/src/CoreBundle/Settings/CourseSettingsSchema.php index 2eb06e7258c..305c52b0450 100644 --- a/src/CoreBundle/Settings/CourseSettingsSchema.php +++ b/src/CoreBundle/Settings/CourseSettingsSchema.php @@ -130,10 +130,10 @@ public function buildSettings(AbstractSettingsBuilder $builder): void 'course_hide_tools', new ArrayToIdentifierTransformer() ) - /* ->setTransformer( + ->setTransformer( 'course_creation_use_template', - new ResourceToIdentifierTransformer($this->getRepository()) - )*/ + new ResourceToIdentifierTransformer($this->getRepository(), 'id') + ) ; $allowedTypes = [ @@ -229,7 +229,9 @@ public function buildForm(FormBuilderInterface $builder): void 'class' => Course::class, 'placeholder' => 'Choose ...', 'empty_data' => null, - 'data' => null, + 'choice_label' => 'title', + 'choice_value' => 'id', + 'required' => false, ] ) ->add('hide_scorm_export_link', YesNoType::class) diff --git a/src/CoreBundle/Settings/SettingsManager.php b/src/CoreBundle/Settings/SettingsManager.php index 7a7d84f0615..7dfb268a697 100644 --- a/src/CoreBundle/Settings/SettingsManager.php +++ b/src/CoreBundle/Settings/SettingsManager.php @@ -259,13 +259,7 @@ public function load(string $schemaAlias, ?string $namespace = null, bool $ignor foreach ($settingsBuilder->getTransformers() as $parameter => $transformer) { if (\array_key_exists($parameter, $parameters)) { - if ('course_creation_use_template' === $parameter) { - if (empty($parameters[$parameter])) { - $parameters[$parameter] = null; - } - } else { - $parameters[$parameter] = $transformer->reverseTransform($parameters[$parameter]); - } + $parameters[$parameter] = $transformer->reverseTransform($parameters[$parameter]); } } @@ -290,10 +284,8 @@ public function update(SettingsInterface $settings): void // 2. Is defined as an array in class DocumentSettingsSchema // 3. Add transformer for that variable "ArrayToIdentifierTransformer" // 4. Here we recover the transformer and convert the array to string - foreach ($settingsBuilder->getTransformers() as $parameter => $transformer) { - if (\array_key_exists($parameter, $parameters)) { - $parameters[$parameter] = $transformer->transform($parameters[$parameter]); - } + foreach ($parameters as $parameter => $value) { + $parameters[$parameter] = $this->transformToString($value); } $settings->setParameters($parameters); @@ -329,11 +321,6 @@ public function update(SettingsInterface $settings): void ->setAccessUrlLocked(1) ; - // @var ConstraintViolationListInterface $errors - /*$errors = $this->validator->validate($parameter); - if (0 < $errors->count()) { - throw new ValidatorException($errors->get(0)->getMessage()); - }*/ $this->manager->persist($parameter); } } @@ -359,10 +346,8 @@ public function save(SettingsInterface $settings): void // 2. Is defined as an array in class DocumentSettingsSchema // 3. Add transformer for that variable "ArrayToIdentifierTransformer" // 4. Here we recover the transformer and convert the array to string - foreach ($settingsBuilder->getTransformers() as $parameter => $transformer) { - if (\array_key_exists($parameter, $parameters)) { - $parameters[$parameter] = $transformer->transform($parameters[$parameter]); - } + foreach ($parameters as $parameter => $value) { + $parameters[$parameter] = $this->transformToString($value); } $settings->setParameters($parameters); $persistedParameters = $this->repository->findBy([ @@ -373,12 +358,6 @@ public function save(SettingsInterface $settings): void $persistedParametersMap[$parameter->getVariable()] = $parameter; } - // @var SettingsEvent $event - /*$event = $this->eventDispatcher->dispatch( - SettingsEvent::PRE_SAVE, - new SettingsEvent($settings, $parameters) - );*/ - $url = $this->getUrl(); $simpleCategoryName = str_replace('chamilo_core.settings.', '', $namespace); @@ -397,77 +376,11 @@ public function save(SettingsInterface $settings): void ->setAccessUrlLocked(1) ; - // @var ConstraintViolationListInterface $errors - /*$errors = $this->validator->validate($parameter); - if (0 < $errors->count()) { - throw new ValidatorException($errors->get(0)->getMessage()); - }*/ $this->manager->persist($parameter); } - $this->manager->persist($parameter); } $this->manager->flush(); - - // $schemaAlias = $settings->getSchemaAlias(); - // $schemaAliasChamilo = str_replace('chamilo_core.settings.', '', $schemaAlias); - // - // $schema = $this->schemaRegistry->get($schemaAlias); - // - // $settingsBuilder = new SettingsBuilder(); - // $schema->buildSettings($settingsBuilder); - // - // $parameters = $settingsBuilder->resolve($settings->getParameters()); - // - // foreach ($settingsBuilder->getTransformers() as $parameter => $transformer) { - // if (array_key_exists($parameter, $parameters)) { - // $parameters[$parameter] = $transformer->transform($parameters[$parameter]); - // } - // } - // - // /** @var \Sylius\Bundle\SettingsBundle\Event\SettingsEvent $event */ - // $event = $this->eventDispatcher->dispatch( - // SettingsEvent::PRE_SAVE, - // new SettingsEvent($settings) - // ); - // - // /** @var SettingsCurrent $url */ - // $url = $event->getSettings()->getAccessUrl(); - // - // foreach ($parameters as $name => $value) { - // if (isset($persistedParametersMap[$name])) { - // if ($value instanceof Course) { - // $value = $value->getId(); - // } - // $persistedParametersMap[$name]->setValue($value); - // } else { - // $setting = new Settings(); - // $setting->setSchemaAlias($schemaAlias); - // - // $setting - // ->setNamespace($schemaAliasChamilo) - // ->setName($name) - // ->setValue($value) - // ->setUrl($url) - // ->setAccessUrlLocked(0) - // ->setAccessUrlChangeable(1) - // ; - // - // /** @var ConstraintViolationListInterface $errors */ - // /*$errors = $this->->validate($parameter); - // if (0 < $errors->count()) { - // throw new ValidatorException($errors->get(0)->getMessage()); - // }*/ - // $this->manager->persist($setting); - // $this->manager->flush(); - // } - // } - /*$parameters = $settingsBuilder->resolve($settings->getParameters()); - * $settings->setParameters($parameters); - * $this->eventDispatcher->dispatch(SettingsEvent::PRE_SAVE, new SettingsEvent($settings)); - * $this->manager->persist($settings); - * $this->manager->flush(); - * $this->eventDispatcher->dispatch(SettingsEvent::POST_SAVE, new SettingsEvent($settings));*/ } /** @@ -1078,4 +991,25 @@ private function fixCategory($variable, $defaultCategory) return $settings[$variable] ?? $defaultCategory; } + + private function transformToString($value): string + { + if (is_array($value)) { + return implode(',', $value); + } + + if ($value instanceof Course) { + return (string) $value->getId(); + } + + if (is_bool($value)) { + return $value ? 'true' : 'false'; + } + + if (is_null($value)) { + return ''; + } + + return (string) $value; + } }