Skip to content

Commit

Permalink
Merge branch 'release-49.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Feb 2, 2022
2 parents d02561c + 1d1e715 commit 27f8908
Show file tree
Hide file tree
Showing 13 changed files with 264 additions and 31 deletions.
35 changes: 26 additions & 9 deletions actions/form/class.SimpleProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,7 @@ protected function initElements()

$index = $this->getIndex();

$propertyProperties = array_merge(
tao_helpers_form_GenerisFormFactory::getDefaultProperties(),
[
new core_kernel_classes_Property(GenerisRdf::PROPERTY_ALIAS),
new core_kernel_classes_Property(GenerisRdf::PROPERTY_IS_LG_DEPENDENT),
new core_kernel_classes_Property(TaoOntology::PROPERTY_GUI_ORDER),
$this->getProperty(ValidationRuleRegistry::PROPERTY_VALIDATION_RULE)
]
);
$propertyProperties = $this->getSchemaProperties();
$values = $property->getPropertiesValues($propertyProperties);

$elementNames = [];
Expand Down Expand Up @@ -106,6 +98,13 @@ protected function initElements()
if ($propertyProperty->getUri() == OntologyRdfs::RDFS_LABEL) {
$element->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty'));
}
if (
$propertyProperty->getUri() === GenerisRdf::PROPERTY_IS_STATISTICAL &&
empty($values[$propertyProperty->getUri()])
) {
$element->setValue(tao_helpers_Uri::encode(GenerisRdf::GENERIS_FALSE));
}

$this->form->addElement($element);
$elementNames[] = $element->getName();
}
Expand Down Expand Up @@ -280,6 +279,24 @@ private function disableValues(core_kernel_classes_Property $property, Validator
$element->setDisabledValues(array_unique($disabledValues));
}

private function getSchemaProperties(): array
{
return array_merge(
tao_helpers_form_GenerisFormFactory::getDefaultProperties(),
array_filter(
[
$this->getProperty(GenerisRdf::PROPERTY_ALIAS),
$this->getProperty(GenerisRdf::PROPERTY_IS_LG_DEPENDENT),
$this->getFeatureFlagChecker()->isEnabled('FEATURE_FLAG_STATISTIC_METADATA_IMPORT')
? $this->getProperty(GenerisRdf::PROPERTY_IS_STATISTICAL)
: null,
$this->getProperty(TaoOntology::PROPERTY_GUI_ORDER),
$this->getProperty(ValidationRuleRegistry::PROPERTY_VALIDATION_RULE)
]
)
);
}

private function getDependsOnPropertyFormFieldFactory(): DependsOnPropertyFormFieldFactory
{
return $this->getContainer()->get(DependsOnPropertyFormFieldFactory::class);
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"oat-sa/jig": "~0.1",
"oat-sa/composer-npm-bridge": "~0.4.2||dev-master",
"oat-sa/oatbox-extension-installer": "~1.1||dev-master",
"oat-sa/generis": ">=15.14.0",
"oat-sa/generis": ">=15.15.0",
"composer/package-versions-deprecated": "^1.11",
"paragonie/random_compat": "^2.0",
"phpdocumentor/reflection-docblock": "2.*",
Expand Down
13 changes: 13 additions & 0 deletions helpers/form/ElementMapFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,26 @@ public function create(core_kernel_classes_Property $property): ?tao_helpers_for
}
}

if ($this->isBlockedForModification($property)) {
$element->disable();
}

foreach (ValidationRuleRegistry::getRegistry()->getValidators($property) as $validator) {
$element->addValidator($validator);
}

return $element;
}

private function isBlockedForModification(core_kernel_classes_Property $property): bool
{
if ($this->getFeatureFlagChecker()->isEnabled('FEATURE_FLAG_STATISTIC_METADATA_IMPORT')) {
return $property->isStatistical();
}

return false;
}

private function isList($range): bool
{
if (!$range->isClass()) {
Expand Down
5 changes: 5 additions & 0 deletions helpers/form/class.FormElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,11 @@ public function disable()
$this->addAttribute('disabled', 'disabled');
}

public function isDisabled(): bool
{
return isset($this->attributes['disabled']) && $this->attributes['disabled'] === 'disabled';
}

/**
* Short description of method renderAttributes
*
Expand Down
36 changes: 32 additions & 4 deletions helpers/form/elements/xhtml/class.Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class tao_helpers_form_elements_xhtml_Calendar extends tao_helpers_form_elements
* Rendering of the XHTML implementation of the Calendar Widget.
*
* @author Bertrand Chevrier, <[email protected]>
* @return The XHTML stream of the Calendar Widget.
* @return string The XHTML stream of the Calendar Widget.
*/
public function render()
{
Expand All @@ -45,6 +45,23 @@ public function render()
$uniqueId = uniqid('calendar_');
$elementId = tao_helpers_Display::TextCleaner($this->getDescription()) . '_' . $uniqueId;

if ($this->isDisabled()) {
return $returnValue . sprintf(
'<input type="text"
name="%s"
id="%s"
data-testid="%s"
value="%s"
%s
>',
$this->name,
$elementId,
$this->getDescription(),
$this->getDateOutput(),
$this->renderAttributes()
);
}

if (! isset($this->attributes['size'])) {
$this->attributes['size'] = 20;
}
Expand All @@ -53,12 +70,12 @@ public function render()
$returnValue .= $this->renderAttributes();

if (! empty($this->value)) {
$timeStamp = is_numeric($this->getRawValue()) ? $this->getRawValue() : $this->getEvaluatedValue();
$returnValue .= ' value="' . _dh(tao_helpers_Date::displayeDate($timeStamp, tao_helpers_Date::FORMAT_DATEPICKER)) . '"';
$returnValue .= ' value="' . $this->getDateOutput() . '"';
}

$returnValue .= ' /></div>';

return (string) $returnValue;
return $returnValue;
}

public function getEvaluatedValue()
Expand All @@ -77,4 +94,15 @@ public function getEvaluatedValue()

return $returnValue;
}

private function getDateOutput(): string
{
if (empty($this->value)) {
return '';
}

$timeStamp = is_numeric($this->getRawValue()) ? $this->getRawValue() : $this->getEvaluatedValue();

return _dh(tao_helpers_Date::displayeDate($timeStamp, tao_helpers_Date::FORMAT_DATEPICKER));
}
}
4 changes: 3 additions & 1 deletion models/classes/Csv/Resource/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public function __construct(LeagueCsvReader $reader)
*/
public function getHeader(): array
{
return $this->reader->getHeader();
$headers = $this->reader->getHeader();

return array_map('trim', $headers);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public function extract(array $header): array
$this->metadataPropertiesValidator->validateMetadataExistence($aliases, $metadataProperties);
$this->metadataPropertiesValidator->validateMetadataUniqueness($metadataProperties);
$this->metadataPropertiesValidator->validateMetadataTypes($metadataProperties);
$this->metadataPropertiesValidator->validateMetadataIsStatistical($metadataProperties);

return $uniqueMetadataProperties;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public function process(File $file): Report
private function bindProperties(core_kernel_classes_Resource $resource, array $values): void
{
$binder = $this->dataBinder ?? new tao_models_classes_dataBinding_GenerisInstanceDataBinder($resource);
$binder->forceModification();
$binder->bind($values);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@
use oat\tao\model\StatisticalMetadata\Contract\Header;
use oat\tao\model\StatisticalMetadata\Import\Exception\HeaderValidationException;
use oat\tao\model\StatisticalMetadata\Import\Exception\AggregatedValidationException;
use tao_helpers_form_elements_Htmlarea;
use tao_helpers_form_elements_Textarea;
use tao_helpers_form_elements_Textbox;

class MetadataPropertiesValidator
{
private const ALLOWED_WIDGETS = [
'http://www.tao.lu/datatypes/WidgetDefinitions.rdf#TextBox',
'http://www.tao.lu/datatypes/WidgetDefinitions.rdf#TextArea',
'http://www.tao.lu/datatypes/WidgetDefinitions.rdf#HTMLArea',
tao_helpers_form_elements_Textbox::WIDGET_ID,
tao_helpers_form_elements_Textarea::WIDGET_ID,
tao_helpers_form_elements_Htmlarea::WIDGET_ID
];

/**
Expand Down Expand Up @@ -85,7 +88,10 @@ public function validateMetadataUniqueness(array $metadataProperties): void
}

if (!array_key_exists($alias, $exceptions)) {
$exceptions[$alias] = $this->buildHeaderException($alias, 'Property referenced by "%s" is not unique');
$exceptions[$alias] = $this->buildHeaderException(
$alias ?? $metadataProperty->getLabel(),
'Property referenced by "%s" is not unique'
);
}
}

Expand All @@ -108,9 +114,8 @@ public function validateMetadataTypes(array $metadataProperties): void

foreach ($metadataProperties as $metadataProperty) {
if (!in_array($metadataProperty->getWidget()->getUri(), self::ALLOWED_WIDGETS, true)) {
$alias = $metadataProperty->getAlias();
$exceptions[] = $this->buildHeaderException(
$alias,
$metadataProperty->getAlias() ?? $metadataProperty->getLabel(),
'Property referenced by "%s" has invalid input type - only TEXT is allowed'
);
}
Expand All @@ -123,6 +128,32 @@ public function validateMetadataTypes(array $metadataProperties): void
);
}

/**
* @param core_kernel_classes_Property[] $metadataProperties
*
* @throws AggregatedValidationException
* @throws HeaderValidationException
*/
public function validateMetadataIsStatistical(array $metadataProperties): void
{
$exceptions = [];

foreach ($metadataProperties as $metadataProperty) {
if (!$metadataProperty->isStatistical()) {
$exceptions[] = $this->buildHeaderException(
$metadataProperty->getAlias() ?? $metadataProperty->getLabel(),
'Property referenced by "%s" must be "statistical"'
);
}
}

$this->throwErrorOrAggregatedException(
$exceptions,
$metadataProperties,
'None of properties referenced by "%s" columns are statistical'
);
}

private function buildHeaderException(string $alias, string $message): HeaderValidationException
{
$column = Header::METADATA_PREFIX . $alias;
Expand Down
54 changes: 52 additions & 2 deletions models/classes/dataBinding/class.GenerisInstanceDataBinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
use oat\tao\model\event\MetadataModified;
use oat\oatbox\event\EventManager;
use oat\oatbox\service\ServiceManager;
use oat\tao\model\featureFlag\FeatureFlagChecker;
use oat\tao\model\featureFlag\FeatureFlagCheckerInterface;

/**
* A data binder focusing on binding a source of data to a Generis instance
Expand All @@ -44,6 +46,12 @@ class tao_models_classes_dataBinding_GenerisInstanceDataBinder extends tao_model
/** @var EventManager */
private $eventManager;

/** @var ServiceManager */
private $serviceManager;

/** @var bool */
private $forceModification = false;

/**
* Creates a new instance of binder.
*
Expand All @@ -57,11 +65,21 @@ public function __construct(core_kernel_classes_Resource $targetInstance)
$this->targetInstance = $targetInstance;
}

public function withServiceManager(ServiceManager $serviceManager): void
{
$this->serviceManager = $serviceManager;
}

public function withEventManager(EventManager $eventManager): void
{
$this->eventManager = $eventManager;
}

public function forceModification(): void
{
$this->forceModification = true;
}

/**
* Returns the target instance.
*
Expand Down Expand Up @@ -111,6 +129,11 @@ public function bind($data)
}

$prop = new core_kernel_classes_Property($propertyUri);

if ($this->isBlockedForModification($prop)) {
continue;
}

$values = $instance->getPropertyValuesCollection($prop);
if ($values->count() > 0) {
if (is_array($propertyValue)) {
Expand Down Expand Up @@ -160,17 +183,44 @@ public function bind($data)
}
}

private function isBlockedForModification(core_kernel_classes_Property $property): bool
{
if ($this->forceModification) {
return false;
}

if ($this->getFeatureFlagChecker()->isEnabled('FEATURE_FLAG_STATISTIC_METADATA_IMPORT')) {
return $property->isStatistical();
}

return false;
}

private function isEmptyValue(string $value): bool
{
return strlen(trim($value)) === 0;
}

private function getEventManager(): EventManager
{
if ($this->eventManager === null) {
$this->eventManager = ServiceManager::getServiceManager()->get(EventManager::SERVICE_ID);
if (!isset($this->eventManager)) {
$this->eventManager = $this->getServiceManager()->get(EventManager::SERVICE_ID);
}

return $this->eventManager;
}

private function getFeatureFlagChecker(): FeatureFlagCheckerInterface
{
return $this->getServiceManager()->get(FeatureFlagChecker::class);
}

private function getServiceManager(): ServiceManager
{
if (!isset($this->serviceManager)) {
$this->serviceManager = ServiceManager::getServiceManager();
}

return $this->serviceManager;
}
}
Loading

0 comments on commit 27f8908

Please sign in to comment.