Skip to content

Commit

Permalink
MetaData: controlled vocabs in editor
Browse files Browse the repository at this point in the history
  • Loading branch information
schmitz-ilias committed Oct 2, 2024
1 parent 0fe79e8 commit 268dd7f
Show file tree
Hide file tree
Showing 45 changed files with 1,087 additions and 412 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@
use ILIAS\UI\Component\Input\Field\Factory as UIFactory;
use ILIAS\MetaData\Repository\Validation\Dictionary\DictionaryInterface as ConstraintDictionary;
use ILIAS\MetaData\Editor\Presenter\PresenterInterface;
use ILIAS\MetaData\Vocabularies\Slots\Identifier as SlotIdentifier;
use ILIAS\MetaData\Editor\Full\Services\Inputs\WithoutConditions\InputHelper;

abstract class BaseConditionFactory
{
use InputHelper;

protected UIFactory $ui_factory;
protected PresenterInterface $presenter;
protected ConstraintDictionary $constraint_dictionary;
Expand All @@ -46,49 +50,22 @@ public function __construct(
$this->types = $types;
}

abstract protected function conditionInput(
abstract public function getConditionInput(
ElementInterface $element,
ElementInterface $context_element,
ElementInterface ...$conditional_elements
ElementInterface $conditional_element
): FormInput;

final public function getConditionInput(
ElementInterface $element,
ElementInterface $context_element,
ElementInterface ...$conditional_elements
): FormInput {
$input = $this->conditionInput(
$element,
$context_element,
...$conditional_elements
);

return $this->finishInput($element, $context_element, $input);
}

final protected function getInputInCondition(
protected function getInputInCondition(
ElementInterface $element,
ElementInterface $context_element,
string $condition_value
SlotIdentifier $conditional_slot
): FormInput {
$input_factory = $this->types->factory($element->getDefinition()->dataType());
return $input_factory->getInputInCondition(
$element,
$context_element,
$condition_value
);
}

final protected function finishInput(
ElementInterface $element,
ElementInterface $context_element,
FormInput $input
): FormInput {
$input_factory = $this->types->factory($element->getDefinition()->dataType());
return $input_factory->finishInput(
$element,
$context_element,
$input
$conditional_slot
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

namespace ILIAS\MetaData\Editor\Full\Services\Inputs\Conditions;

use ILIAS\MetaData\Vocabularies\VocabulariesInterface;
use ILIAS\UI\Component\Input\Field\Factory as UIFactory;
use ILIAS\MetaData\Editor\Presenter\PresenterInterface;
use ILIAS\MetaData\Repository\Validation\Dictionary\DictionaryInterface as ConstraintDictionary;
Expand All @@ -30,6 +29,8 @@
use ILIAS\MetaData\Editor\Full\Services\Inputs\WithoutConditions\FactoryWithoutConditionTypesService;
use ILIAS\MetaData\Editor\Full\Services\Inputs\WithoutConditions\BaseFactory;
use ILIAS\MetaData\DataHelper\DataHelperInterface;
use ILIAS\MetaData\Vocabularies\ElementHelper\ElementHelperInterface;
use ILIAS\MetaData\Vocabularies\Slots\HandlerInterface as VocabSlotHandler;

class FactoryWithConditionTypesService
{
Expand All @@ -40,26 +41,29 @@ public function __construct(
UIFactory $ui_factory,
PresenterInterface $presenter,
ConstraintDictionary $constraint_dictionary,
VocabulariesInterface $vocabularies,
Refinery $refinery,
PathFactory $path_factory,
DataHelperInterface $data_helper
DataHelperInterface $data_helper,
ElementHelperInterface $element_vocab_helper,
VocabSlotHandler $vocab_slot_handler
) {
$this->types_without_conditions = new FactoryWithoutConditionTypesService(
$ui_factory,
$presenter,
$constraint_dictionary,
$vocabularies,
$refinery,
$data_helper
$data_helper,
$element_vocab_helper,
$path_factory
);
$this->vocab_value = new VocabValueConditionFactory(
$ui_factory,
$presenter,
$constraint_dictionary,
$this->types_without_conditions,
$vocabularies,
$path_factory
$path_factory,
$element_vocab_helper,
$vocab_slot_handler
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,68 +22,110 @@

use ILIAS\UI\Component\Input\Container\Form\FormInput;
use ILIAS\MetaData\Elements\ElementInterface;
use ILIAS\MetaData\Vocabularies\VocabulariesInterface;
use ILIAS\UI\Component\Input\Field\Factory as UIFactory;
use ILIAS\MetaData\Repository\Validation\Dictionary\DictionaryInterface as ConstraintDictionary;
use ILIAS\MetaData\Editor\Presenter\PresenterInterface;
use ILIAS\MetaData\Paths\FactoryInterface as PathFactory;
use ILIAS\MetaData\Editor\Full\Services\Inputs\WithoutConditions\FactoryWithoutConditionTypesService;
use ILIAS\MetaData\Vocabularies\ElementHelper\ElementHelperInterface;
use ILIAS\MetaData\Vocabularies\Slots\HandlerInterface as VocabSlotHandler;
use ILIAS\MetaData\Elements\Data\Type;
use ILIAS\MetaData\Vocabularies\VocabularyInterface;

class VocabValueConditionFactory extends BaseConditionFactory
{
protected VocabulariesInterface $vocabularies;
protected PathFactory $path_factory;
protected ElementHelperInterface $element_vocab_helper;
protected VocabSlotHandler $vocab_slot_handler;

public function __construct(
UIFactory $ui_factory,
PresenterInterface $presenter,
ConstraintDictionary $constraint_dictionary,
FactoryWithoutConditionTypesService $types,
VocabulariesInterface $vocabularies,
PathFactory $path_factory
PathFactory $path_factory,
ElementHelperInterface $element_vocab_helper,
VocabSlotHandler $vocab_slot_handler
) {
parent::__construct($ui_factory, $presenter, $constraint_dictionary, $types);
$this->vocabularies = $vocabularies;
$this->path_factory = $path_factory;
$this->element_vocab_helper = $element_vocab_helper;
$this->vocab_slot_handler = $vocab_slot_handler;
}

protected function conditionInput(
public function getConditionInput(
ElementInterface $element,
ElementInterface $context_element,
ElementInterface ...$conditional_elements
ElementInterface $conditional_element
): FormInput {
$slot = $this->element_vocab_helper->slotForElement($element);
$unique_path_to_conditional_element = $this->path_factory->toElement($conditional_element, true);
$path_for_conditional_slot = $this->path_factory->toElement($conditional_element);
$path_for_condition = $this->path_factory->betweenElements($conditional_element, $element);

$data = $this->getDataFromElementOrConstraint($element);
$conditional_data = $this->getDataFromElementOrConstraint($conditional_element);

$groups = [];
foreach ($this->vocabularies->vocabulariesForElement($element) as $vocab) {
foreach ($this->element_vocab_helper->vocabulariesForSlot($slot) as $vocab) {
$labels_by_value = $this->getLabelsByValueForVocabulary($vocab);
foreach ($vocab->values() as $value) {
$inputs = [];
foreach ($conditional_elements as $conditional_element) {
$input = $this->getInputInCondition(
$conditional_element,
$context_element,
$value
);
$path_string = $this->path_factory->toElement($conditional_element, true)
->toString();
$inputs[$path_string] = $this->ui_factory->group(
[$input]
);
}
if (!isset($default_value)) {
$default_value = $value;
$conditional_slot = $this->vocab_slot_handler->identiferFromPathAndCondition(
$path_for_conditional_slot,
$path_for_condition,
$value,
);

$input = $this->getInputInCondition(
$conditional_element,
$context_element,
$conditional_slot
);

if ($data === $value && isset($conditional_data)) {
$input = $input->withValue($conditional_data);
}

$groups[$value] = $this->ui_factory->group(
$inputs,
$this->presenter->data()->vocabularyValue($value)
[$unique_path_to_conditional_element->toString() => $input],
$labels_by_value[$value] ?? ''
);
}
}
$input = $this->ui_factory->switchableGroup(

$radios = $this->ui_factory->switchableGroup(
$groups,
'placeholder'
$this->getInputLabelFromElement($this->presenter, $element, $context_element)
);
if (isset($default_value)) {
return $input->withValue($default_value);
if (isset($data)) {
$radios = $radios->withValue($data);
}
return $this->addConstraintsFromElement(
$this->constraint_dictionary,
$element,
$radios
);
}

protected function getLabelsByValueForVocabulary(VocabularyInterface $vocabulary): array
{
$presentable_labels = $this->presenter->data()->vocabularyValues(
$vocabulary->slot(),
...$vocabulary->values()
);
$labels_by_value = [];
foreach ($presentable_labels as $labelled_value) {
$labels_by_value[$labelled_value->value()] = $labelled_value->label();
}
return $labels_by_value;
}

protected function getDataFromElementOrConstraint(ElementInterface $element): ?string
{
$data = null;
if ($element->getData()->type() !== Type::NULL) {
$data = $element->getData()->value();
}
return $input;
return $this->getPresetValueFromConstraints($this->constraint_dictionary, $element) ?? $data;
}
}
Loading

0 comments on commit 268dd7f

Please sign in to comment.