Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 319 |
|
0.00% |
0 / 29 |
CRAP | |
0.00% |
0 / 1 |
| tao_actions_PropertiesAuthoring | |
0.00% |
0 / 319 |
|
0.00% |
0 / 29 |
10302 | |
0.00% |
0 / 1 |
| getEventManager | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| index | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
30 | |||
| addClassProperty | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| removeClassProperty | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| removePropertyIndex | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
20 | |||
| addPropertyIndex | |
0.00% |
0 / 49 |
|
0.00% |
0 / 1 |
72 | |||
| getCurrentClass | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
30 | |||
| getCurrentInstance | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| getClassForm | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
42 | |||
| populateSubmittedProperties | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
132 | |||
| getDependsOnPropertyOptions | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| saveSimpleProperty | |
0.00% |
0 / 35 |
|
0.00% |
0 / 1 |
132 | |||
| savePropertyIndex | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
30 | |||
| bindProperties | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| extractClassData | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| extractPropertyData | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| saveProperties | |
0.00% |
0 / 40 |
|
0.00% |
0 / 1 |
72 | |||
| getDecodedPropertyValue | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| setDependsOnProperty | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
| invalidatePropertyCache | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
42 | |||
| isElasticSearchEnabled | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getFeatureFlagChecker | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getParentPropertyListCachedRepository | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getPropertyChangedValidator | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getPropertyTypeValidator | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getPropertyListValidator | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDependsOnPropertyRepository | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDependsOnPropertySynchronizer | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getClassFormValidators | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2015-2021 Open Assessment Technologies S.A. |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | use oat\generis\model\WidgetRdf; |
| 24 | use oat\generis\model\GenerisRdf; |
| 25 | use oat\oatbox\event\EventManager; |
| 26 | use oat\oatbox\validator\ValidatorInterface; |
| 27 | use oat\tao\helpers\form\Factory\AbstractElementPropertyListValuesFactory; |
| 28 | use oat\tao\helpers\form\Factory\ElementPropertyTypeFactory; |
| 29 | use oat\tao\model\dto\OldProperty; |
| 30 | use oat\generis\model\OntologyRdfs; |
| 31 | use oat\oatbox\log\LoggerAwareTrait; |
| 32 | use oat\generis\model\OntologyAwareTrait; |
| 33 | use oat\tao\model\Lists\Business\Validation\PropertyListValidator; |
| 34 | use oat\tao\model\Lists\Business\Validation\PropertyTypeValidator; |
| 35 | use oat\tao\model\search\tasks\IndexTrait; |
| 36 | use oat\tao\model\search\index\OntologyIndex; |
| 37 | use oat\tao\model\event\ClassFormUpdatedEvent; |
| 38 | use oat\tao\helpers\form\ValidationRuleRegistry; |
| 39 | use oat\tao\model\featureFlag\FeatureFlagChecker; |
| 40 | use oat\tao\model\event\ClassPropertiesChangedEvent; |
| 41 | use oat\tao\model\search\index\OntologyIndexService; |
| 42 | use oat\tao\model\validator\PropertyChangedValidator; |
| 43 | use oat\tao\model\AdvancedSearch\AdvancedSearchChecker; |
| 44 | use oat\tao\model\featureFlag\FeatureFlagCheckerInterface; |
| 45 | use oat\generis\model\resource\DependsOnPropertyCollection; |
| 46 | use oat\tao\model\ClassProperty\RemoveClassPropertyService; |
| 47 | use oat\tao\model\ClassProperty\AddClassPropertyFormFactory; |
| 48 | use oat\tao\model\Lists\Business\Service\RemoteSourcedListOntology; |
| 49 | use oat\tao\model\Lists\Business\Service\DependsOnPropertySynchronizer; |
| 50 | use oat\tao\model\Lists\DataAccess\Repository\DependsOnPropertyRepository; |
| 51 | use oat\tao\model\Lists\Business\Domain\DependsOnPropertySynchronizerContext; |
| 52 | use oat\tao\model\Lists\Business\Contract\DependsOnPropertyRepositoryInterface; |
| 53 | use oat\tao\model\Lists\Business\Contract\DependsOnPropertySynchronizerInterface; |
| 54 | use oat\tao\model\Lists\DataAccess\Repository\ParentPropertyListCachedRepository; |
| 55 | |
| 56 | /** |
| 57 | * Regrouping all actions related to authoring |
| 58 | * of properties |
| 59 | */ |
| 60 | class tao_actions_PropertiesAuthoring extends tao_actions_CommonModule |
| 61 | { |
| 62 | use OntologyAwareTrait; |
| 63 | use LoggerAwareTrait; |
| 64 | use IndexTrait; |
| 65 | |
| 66 | /** |
| 67 | * @return EventManager |
| 68 | */ |
| 69 | protected function getEventManager(): EventManager |
| 70 | { |
| 71 | return $this->getServiceLocator()->get(EventManager::SERVICE_ID); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @requiresRight id READ |
| 76 | */ |
| 77 | public function index(): void |
| 78 | { |
| 79 | $this->defaultData(); |
| 80 | $class = $this->getClass($this->getRequestParameter('id')); |
| 81 | |
| 82 | $myForm = $this->getClassForm($class); |
| 83 | if ($myForm->isSubmited()) { |
| 84 | if ($myForm->isValid()) { |
| 85 | if ($class instanceof core_kernel_classes_Resource) { |
| 86 | $this->setData("selectNode", tao_helpers_Uri::encode($class->getUri())); |
| 87 | $properties = $this->hasRequestParameter('properties') |
| 88 | ? $this->getRequestParameter('properties') |
| 89 | : []; |
| 90 | $this->getEventManager()->trigger(new ClassFormUpdatedEvent($class, $properties)); |
| 91 | } |
| 92 | $this->setData('message', __('%s Class saved', $class->getLabel())); |
| 93 | $this->setData('reload', false); |
| 94 | } |
| 95 | } |
| 96 | $this->setData('formTitle', __('Manage class schema')); |
| 97 | $this->setData('myForm', $myForm->render()); |
| 98 | $this->setView('form.tpl', 'tao'); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Render the add property sub form. |
| 103 | * |
| 104 | * @requiresRight id WRITE |
| 105 | */ |
| 106 | public function addClassProperty(AddClassPropertyFormFactory $addClassPropertyFormFactory): void |
| 107 | { |
| 108 | if (!$this->isXmlHttpRequest()) { |
| 109 | throw new common_exception_BadRequest('wrong request mode'); |
| 110 | } |
| 111 | |
| 112 | $myForm = $addClassPropertyFormFactory->add( |
| 113 | $this->getPsrRequest(), |
| 114 | $this->hasWriteAccessToAction(__FUNCTION__) |
| 115 | ); |
| 116 | |
| 117 | $this->setData('data', $myForm->renderElements()); |
| 118 | $this->setView('blank.tpl', 'tao'); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Render the add property sub form. |
| 123 | * |
| 124 | * @requiresRight classUri WRITE |
| 125 | * @throws common_Exception |
| 126 | */ |
| 127 | public function removeClassProperty(RemoveClassPropertyService $removeClassPropertyService): void |
| 128 | { |
| 129 | if (!$this->isXmlHttpRequest()) { |
| 130 | throw new common_exception_BadRequest('wrong request mode'); |
| 131 | } |
| 132 | |
| 133 | $success = $removeClassPropertyService->remove($this->getPsrRequest()); |
| 134 | |
| 135 | if ($success) { |
| 136 | $this->returnJson(['success' => true]); |
| 137 | } else { |
| 138 | $this->returnError(__('Unable to remove the property.')); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * remove the index of the property. |
| 144 | * @throws Exception |
| 145 | * @throws common_exception_BadRequest |
| 146 | * @return void |
| 147 | */ |
| 148 | public function removePropertyIndex(): void |
| 149 | { |
| 150 | if (!$this->isXmlHttpRequest()) { |
| 151 | throw new common_exception_BadRequest('wrong request mode'); |
| 152 | } |
| 153 | if (!$this->hasRequestParameter('uri')) { |
| 154 | throw new common_exception_MissingParameter("Uri parameter is missing"); |
| 155 | } |
| 156 | |
| 157 | if (!$this->hasRequestParameter('indexProperty')) { |
| 158 | throw new common_exception_MissingParameter("indexProperty parameter is missing"); |
| 159 | } |
| 160 | |
| 161 | $indexPropertyUri = tao_helpers_Uri::decode($this->getRequestParameter('indexProperty')); |
| 162 | |
| 163 | //remove use of index property in property |
| 164 | $property = $this->getProperty(tao_helpers_Uri::decode($this->getRequestParameter('uri'))); |
| 165 | $property->removePropertyValue($this->getProperty(OntologyIndex::PROPERTY_INDEX), $indexPropertyUri); |
| 166 | |
| 167 | //remove index property |
| 168 | $indexProperty = new OntologyIndex($indexPropertyUri); |
| 169 | $indexProperty->delete(); |
| 170 | |
| 171 | $this->returnJson(['id' => $this->getRequestParameter('indexProperty')]); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Render the add index sub form. |
| 176 | * @throws Exception |
| 177 | * @throws common_exception_BadRequest |
| 178 | * @return void |
| 179 | */ |
| 180 | public function addPropertyIndex(): void |
| 181 | { |
| 182 | if (!$this->isXmlHttpRequest()) { |
| 183 | throw new common_exception_BadRequest('wrong request mode'); |
| 184 | } |
| 185 | if (!$this->hasRequestParameter('uri')) { |
| 186 | throw new Exception("wrong request Parameter"); |
| 187 | } |
| 188 | $uri = $this->getRequestParameter('uri'); |
| 189 | |
| 190 | $index = 1; |
| 191 | if ($this->hasRequestParameter('index')) { |
| 192 | $index = $this->getRequestParameter('index'); |
| 193 | } |
| 194 | |
| 195 | $propertyIndex = 1; |
| 196 | if ($this->hasRequestParameter('propertyIndex')) { |
| 197 | $propertyIndex = $this->getRequestParameter('propertyIndex'); |
| 198 | } |
| 199 | |
| 200 | //create and attach the new index property to the property |
| 201 | $property = $this->getProperty(tao_helpers_Uri::decode($uri)); |
| 202 | $class = $this->getClass("http://www.tao.lu/Ontologies/TAO.rdf#Index"); |
| 203 | |
| 204 | //get property range to select a default tokenizer |
| 205 | /** @var core_kernel_classes_Class $range */ |
| 206 | $range = $property->getRange(); |
| 207 | //range is empty select item content |
| 208 | $tokenizer = null; |
| 209 | if (is_null($range)) { |
| 210 | $tokenizer = $this->getResource('http://www.tao.lu/Ontologies/TAO.rdf#RawValueTokenizer'); |
| 211 | } else { |
| 212 | $tokenizer = $range->getUri() === OntologyRdfs::RDFS_LITERAL |
| 213 | ? $this->getResource('http://www.tao.lu/Ontologies/TAO.rdf#RawValueTokenizer') |
| 214 | : $this->getResource('http://www.tao.lu/Ontologies/TAO.rdf#LabelTokenizer'); |
| 215 | } |
| 216 | |
| 217 | $indexClass = $this->getClass('http://www.tao.lu/Ontologies/TAO.rdf#Index'); |
| 218 | $i = 0; |
| 219 | $indexIdentifierBackup = preg_replace('/[^a-z_0-9]/', '_', strtolower($property->getLabel())); |
| 220 | $indexIdentifierBackup = ltrim(trim($indexIdentifierBackup, '_'), '0..9'); |
| 221 | $indexIdentifier = $indexIdentifierBackup; |
| 222 | do { |
| 223 | if ($i !== 0) { |
| 224 | $indexIdentifier = $indexIdentifierBackup . '_' . $i; |
| 225 | } |
| 226 | $resources = $indexClass->searchInstances( |
| 227 | [OntologyIndex::PROPERTY_INDEX_IDENTIFIER => $indexIdentifier], |
| 228 | ['like' => false] |
| 229 | ); |
| 230 | $count = count($resources); |
| 231 | $i++; |
| 232 | } while ($count !== 0); |
| 233 | |
| 234 | $indexProperty = $class->createInstanceWithProperties([ |
| 235 | OntologyRdfs::RDFS_LABEL => preg_replace('/_/', ' ', ucfirst($indexIdentifier)), |
| 236 | OntologyIndex::PROPERTY_INDEX_IDENTIFIER => $indexIdentifier, |
| 237 | OntologyIndex::PROPERTY_INDEX_TOKENIZER => $tokenizer, |
| 238 | OntologyIndex::PROPERTY_INDEX_FUZZY_MATCHING => GenerisRdf::GENERIS_TRUE, |
| 239 | OntologyIndex::PROPERTY_DEFAULT_SEARCH => GenerisRdf::GENERIS_FALSE, |
| 240 | ]); |
| 241 | |
| 242 | $property->setPropertyValue($this->getProperty(OntologyIndex::PROPERTY_INDEX), $indexProperty); |
| 243 | |
| 244 | //generate form |
| 245 | $indexFormContainer = new tao_actions_form_IndexProperty( |
| 246 | new OntologyIndex($indexProperty), |
| 247 | $propertyIndex . $index |
| 248 | ); |
| 249 | $myForm = $indexFormContainer->getForm(); |
| 250 | $form = trim(preg_replace('/\s+/', ' ', $myForm->renderElements())); |
| 251 | $this->returnJson(['form' => $form]); |
| 252 | } |
| 253 | |
| 254 | protected function getCurrentClass(): core_kernel_classes_Class |
| 255 | { |
| 256 | $classUri = tao_helpers_Uri::decode($this->getRequestParameter('classUri')); |
| 257 | if (is_null($classUri) || empty($classUri)) { |
| 258 | $class = null; |
| 259 | $resource = $this->getCurrentInstance(); |
| 260 | foreach ($resource->getTypes() as $type) { |
| 261 | $class = $type; |
| 262 | break; |
| 263 | } |
| 264 | if (is_null($class)) { |
| 265 | throw new Exception("No valid class uri found"); |
| 266 | } |
| 267 | $returnValue = $class; |
| 268 | } else { |
| 269 | $returnValue = $this->getClass($classUri); |
| 270 | } |
| 271 | |
| 272 | return $returnValue; |
| 273 | } |
| 274 | |
| 275 | protected function getCurrentInstance(): core_kernel_classes_Resource |
| 276 | { |
| 277 | $uri = tao_helpers_Uri::decode($this->getRequestParameter('uri')); |
| 278 | if (is_null($uri) || empty($uri)) { |
| 279 | throw new tao_models_classes_MissingRequestParameterException("uri"); |
| 280 | } |
| 281 | return $this->getResource($uri); |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Create an edit form for a class and its property |
| 286 | * and handle the submitted data on save |
| 287 | * |
| 288 | * @param core_kernel_classes_Class $class |
| 289 | * @return tao_helpers_form_Form the generated form |
| 290 | * @throws Exception |
| 291 | */ |
| 292 | public function getClassForm(core_kernel_classes_Class $class): tao_helpers_form_Form |
| 293 | { |
| 294 | $data = $this->getRequestParameters(); |
| 295 | $classData = $this->extractClassData($data); |
| 296 | $propertyData = $this->extractPropertyData($data); |
| 297 | $formContainer = new tao_actions_form_Clazz( |
| 298 | $class, |
| 299 | $classData, |
| 300 | $propertyData, |
| 301 | $this->isElasticSearchEnabled(), |
| 302 | $this->getClassFormValidators() |
| 303 | ); |
| 304 | $myForm = $formContainer->getForm(); |
| 305 | |
| 306 | if ($myForm->isSubmited()) { |
| 307 | if ($myForm->isValid()) { |
| 308 | //get the data from parameters |
| 309 | |
| 310 | // get class data and save them |
| 311 | if (isset($data['class'])) { |
| 312 | $classValues = []; |
| 313 | foreach ($data['class'] as $key => $value) { |
| 314 | $classKey = tao_helpers_Uri::decode($key); |
| 315 | $classValues[$classKey] = tao_helpers_Uri::decode($value); |
| 316 | } |
| 317 | |
| 318 | $this->bindProperties($class, $classValues); |
| 319 | } |
| 320 | |
| 321 | //save all properties values |
| 322 | if (isset($data['properties'])) { |
| 323 | $this->saveProperties($data); |
| 324 | $this->populateSubmittedProperties($myForm, $data); |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | return $myForm; |
| 329 | } |
| 330 | |
| 331 | private function populateSubmittedProperties($myForm, $data): void |
| 332 | { |
| 333 | if (empty($data['properties'])) { |
| 334 | return; |
| 335 | } |
| 336 | $elementRangeArray = []; |
| 337 | $groups = $myForm->getGroups(); |
| 338 | |
| 339 | foreach ($data['properties'] as $prop) { |
| 340 | if (empty($prop['range']) || empty($prop['uri'])) { |
| 341 | continue; |
| 342 | } |
| 343 | |
| 344 | $elementUri = $groups['property_' . $prop['uri']]['elements'][0] ?? null; |
| 345 | |
| 346 | if (isset($elementUri)) { |
| 347 | $index = strstr($elementUri, '_', true); |
| 348 | $elementRangeArray[$index . '_range_list'] = $prop['range']; |
| 349 | |
| 350 | if (isset($prop['depends-on-property'])) { |
| 351 | $elementRangeArray[$index . '_depends-on-property'] = $prop['depends-on-property']; |
| 352 | $elementRangeArray[$index . '_uri'] = $prop['uri']; |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | $elements = []; |
| 358 | $dependsOnPropertyRepository = $this->getDependsOnPropertyRepository(); |
| 359 | |
| 360 | foreach ($myForm->getElements() as $element) { |
| 361 | if ( |
| 362 | $element instanceof tao_helpers_form_elements_xhtml_Combobox |
| 363 | && array_key_exists($element->getName(), $elementRangeArray) |
| 364 | ) { |
| 365 | if (strpos($element->getName(), 'depends-on-property') !== false) { |
| 366 | $options = $this->getDependsOnPropertyOptions( |
| 367 | $element, |
| 368 | $elementRangeArray, |
| 369 | $dependsOnPropertyRepository |
| 370 | ); |
| 371 | $element->setOptions($options); |
| 372 | } |
| 373 | |
| 374 | $element->setValue($elementRangeArray[$element->getName()]); |
| 375 | } |
| 376 | $elements[] = $element; |
| 377 | } |
| 378 | |
| 379 | $myForm->setElements($elements); |
| 380 | } |
| 381 | |
| 382 | private function getDependsOnPropertyOptions( |
| 383 | tao_helpers_form_FormElement $element, |
| 384 | array $elementRangeArray, |
| 385 | DependsOnPropertyRepositoryInterface $dependsOnPropertyRepository |
| 386 | ): array { |
| 387 | $index = substr($element->getName(), 0, strpos($element->getName(), '_')); |
| 388 | $options = $dependsOnPropertyRepository->findAll( |
| 389 | [ |
| 390 | 'property' => $this->getProperty(tao_helpers_Uri::decode($elementRangeArray[$index . '_uri'])), |
| 391 | 'listUri' => tao_helpers_Uri::decode($elementRangeArray[$index . '_range_list']), |
| 392 | ] |
| 393 | )->getOptionsList(); |
| 394 | return $options; |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Default property handling |
| 399 | * |
| 400 | * @param array $propertyValues |
| 401 | * @param core_kernel_classes_Resource $property |
| 402 | * @throws Exception |
| 403 | */ |
| 404 | protected function saveSimpleProperty(array $propertyValues, core_kernel_classes_Resource $property): void |
| 405 | { |
| 406 | $propertyMap = tao_helpers_form_GenerisFormFactory::getPropertyMap(); |
| 407 | |
| 408 | $type = $propertyValues['type']; |
| 409 | $range = $this->getDecodedPropertyValue($propertyValues, 'range'); |
| 410 | $dependsOnPropertyUri = $this->getDecodedPropertyValue($propertyValues, 'depends-on-property'); |
| 411 | |
| 412 | unset( |
| 413 | $propertyValues['uri'], |
| 414 | $propertyValues['type'], |
| 415 | $propertyValues['range'], |
| 416 | $propertyValues['depends-on-property'] |
| 417 | ); |
| 418 | |
| 419 | $rangeNotEmpty = false; |
| 420 | $values = [ |
| 421 | ValidationRuleRegistry::PROPERTY_VALIDATION_RULE => [], |
| 422 | ]; |
| 423 | |
| 424 | if (isset($propertyMap[$type])) { |
| 425 | $values[WidgetRdf::PROPERTY_WIDGET] = $propertyMap[$type]['widget']; |
| 426 | $rangeNotEmpty = $propertyMap[$type]['range'] === OntologyRdfs::RDFS_RESOURCE; |
| 427 | } |
| 428 | |
| 429 | foreach ($propertyValues as $key => $value) { |
| 430 | if (is_string($value)) { |
| 431 | $values[tao_helpers_Uri::decode($key)] = tao_helpers_Uri::decode($value); |
| 432 | } elseif (is_array($value)) { |
| 433 | $values[tao_helpers_Uri::decode($key)] = $value; |
| 434 | } else { |
| 435 | $this->logWarning('Unsuported value type ' . gettype($value)); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | $rangeValidator = new tao_helpers_form_validators_NotEmpty(['message' => __('Range field is required')]); |
| 440 | if ($rangeNotEmpty && !$rangeValidator->evaluate($range)) { |
| 441 | throw new Exception($rangeValidator->getMessage()); |
| 442 | } |
| 443 | |
| 444 | $this->bindProperties($property, $values); |
| 445 | |
| 446 | // set the range |
| 447 | $property->removePropertyValues($this->getProperty(OntologyRdfs::RDFS_RANGE)); |
| 448 | if (!empty($range)) { |
| 449 | $property->setRange($this->getClass($range)); |
| 450 | } elseif (isset($propertyMap[$type]) && !empty($propertyMap[$type]['range'])) { |
| 451 | $property->setRange($this->getClass($propertyMap[$type]['range'])); |
| 452 | } |
| 453 | |
| 454 | // set cardinality |
| 455 | if (isset($propertyMap[$type]['multiple'])) { |
| 456 | $property->setMultiple($propertyMap[$type]['multiple'] == GenerisRdf::GENERIS_TRUE); |
| 457 | } |
| 458 | |
| 459 | $this->setDependsOnProperty($property, $dependsOnPropertyUri); |
| 460 | } |
| 461 | |
| 462 | protected function savePropertyIndex(array $indexValues): void |
| 463 | { |
| 464 | $values = []; |
| 465 | foreach ($indexValues as $key => $value) { |
| 466 | $values[tao_helpers_Uri::decode($key)] = tao_helpers_Uri::decode($value); |
| 467 | } |
| 468 | |
| 469 | $validator = new tao_helpers_form_validators_IndexIdentifier(); |
| 470 | |
| 471 | // if the identifier is valid |
| 472 | $values[OntologyIndex::PROPERTY_INDEX_IDENTIFIER] = strtolower( |
| 473 | $values[OntologyIndex::PROPERTY_INDEX_IDENTIFIER] |
| 474 | ); |
| 475 | if (!$validator->evaluate($values[OntologyIndex::PROPERTY_INDEX_IDENTIFIER])) { |
| 476 | throw new Exception($validator->getMessage()); |
| 477 | } |
| 478 | |
| 479 | //if the property exists edit it, else create one |
| 480 | $existingIndex = OntologyIndexService::getIndexById($values[OntologyIndex::PROPERTY_INDEX_IDENTIFIER]); |
| 481 | $indexProperty = $this->getProperty($values['uri']); |
| 482 | if (!is_null($existingIndex) && !$existingIndex->equals($indexProperty)) { |
| 483 | throw new Exception("The index identifier should be unique"); |
| 484 | } |
| 485 | unset($values['uri']); |
| 486 | $this->bindProperties($indexProperty, $values); |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * Helper to save class and properties |
| 491 | * |
| 492 | * @param core_kernel_classes_Resource $resource |
| 493 | * @param array $values |
| 494 | */ |
| 495 | protected function bindProperties(core_kernel_classes_Resource $resource, array $values): void |
| 496 | { |
| 497 | $binder = new tao_models_classes_dataBinding_GenerisInstanceDataBinder($resource); |
| 498 | $binder->bind($values); |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * Extracts the data assoicuated with the class from the request |
| 503 | * |
| 504 | * @param array $data |
| 505 | * @return array |
| 506 | */ |
| 507 | protected function extractClassData(array $data): array |
| 508 | { |
| 509 | $classData = []; |
| 510 | if (isset($data['class'])) { |
| 511 | foreach ($data['class'] as $key => $value) { |
| 512 | $classData['class_' . $key] = $value; |
| 513 | } |
| 514 | } |
| 515 | return $classData; |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * Extracts the properties data from the request data, and formats |
| 520 | * it as an array with the keys being the property URI and the values |
| 521 | * being the associated data |
| 522 | * |
| 523 | * @param array $data |
| 524 | * @return array |
| 525 | */ |
| 526 | protected function extractPropertyData(array $data): array |
| 527 | { |
| 528 | $propertyData = []; |
| 529 | if (isset($data['properties'])) { |
| 530 | foreach ($data['properties'] as $key => $value) { |
| 531 | $propertyData[tao_helpers_Uri::decode($value['uri'])] = $value; |
| 532 | } |
| 533 | } |
| 534 | return $propertyData; |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * @param array $properties |
| 539 | * |
| 540 | * @throws core_kernel_persistence_Exception |
| 541 | */ |
| 542 | private function saveProperties(array $properties): void |
| 543 | { |
| 544 | $changedProperties = []; |
| 545 | |
| 546 | foreach ($properties['properties'] as $i => $propertyValues) { |
| 547 | //get index values |
| 548 | $indexes = null; |
| 549 | if (isset($propertyValues['indexes'])) { |
| 550 | $indexes = $propertyValues['indexes']; |
| 551 | unset($propertyValues['indexes']); |
| 552 | } |
| 553 | |
| 554 | $property = $this->getProperty(tao_helpers_Uri::decode($propertyValues['uri'])); |
| 555 | $oldProperty = new OldProperty( |
| 556 | $property->getLabel(), |
| 557 | $property->getOnePropertyValue($this->getProperty(WidgetRdf::PROPERTY_WIDGET)), |
| 558 | $property->getRange() ? $property->getRange()->getUri() : null, |
| 559 | $property->getPropertyValues( |
| 560 | $property->getProperty(ValidationRuleRegistry::PROPERTY_VALIDATION_RULE) |
| 561 | ), |
| 562 | $property->getDependsOnPropertyCollection(), |
| 563 | $property->getAlias() |
| 564 | ); |
| 565 | |
| 566 | $this->saveSimpleProperty($propertyValues, $property); |
| 567 | |
| 568 | $currentProperty = $this->getProperty(tao_helpers_Uri::decode($propertyValues['uri'])); |
| 569 | $validator = $this->getPropertyChangedValidator(); |
| 570 | |
| 571 | if ($validator->isPropertyChanged($currentProperty, $oldProperty)) { |
| 572 | $this->invalidatePropertyCache($validator, $currentProperty, $oldProperty); |
| 573 | |
| 574 | $changedProperties[] = [ |
| 575 | 'class' => $this->getCurrentClass(), |
| 576 | 'property' => $currentProperty, |
| 577 | 'oldProperty' => $oldProperty, |
| 578 | ]; |
| 579 | } |
| 580 | |
| 581 | //save index |
| 582 | if (!is_null($indexes)) { |
| 583 | foreach ($indexes as $indexValues) { |
| 584 | $this->savePropertyIndex($indexValues); |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | if (!empty($changedProperties)) { |
| 590 | $this->getEventManager()->trigger(new ClassPropertiesChangedEvent($changedProperties)); |
| 591 | |
| 592 | $this->getDependsOnPropertySynchronizer()->sync( |
| 593 | new DependsOnPropertySynchronizerContext([ |
| 594 | DependsOnPropertySynchronizerContext::PARAM_PROPERTIES => array_column( |
| 595 | $changedProperties, |
| 596 | 'property' |
| 597 | ), |
| 598 | ]) |
| 599 | ); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | private function getDecodedPropertyValue(array $propertyValues, string $propertyName): ?string |
| 604 | { |
| 605 | if (!isset($propertyValues[$propertyName])) { |
| 606 | return null; |
| 607 | } |
| 608 | |
| 609 | $propertyValue = trim($propertyValues[$propertyName]); |
| 610 | |
| 611 | if (empty($propertyValue)) { |
| 612 | return null; |
| 613 | } |
| 614 | |
| 615 | return tao_helpers_Uri::decode($propertyValue); |
| 616 | } |
| 617 | |
| 618 | private function setDependsOnProperty(core_kernel_classes_Resource $property, ?string $dependsOnPropertyUri): void |
| 619 | { |
| 620 | $isListsDependencyEnabled = $this->getFeatureFlagChecker()->isEnabled( |
| 621 | FeatureFlagChecker::FEATURE_FLAG_LISTS_DEPENDENCY_ENABLED |
| 622 | ); |
| 623 | |
| 624 | if (!$isListsDependencyEnabled) { |
| 625 | return; |
| 626 | } |
| 627 | |
| 628 | $property->removePropertyValues( |
| 629 | $this->getProperty(RemoteSourcedListOntology::PROPERTY_DEPENDS_ON_PROPERTY) |
| 630 | ); |
| 631 | |
| 632 | if ($dependsOnPropertyUri === null) { |
| 633 | return; |
| 634 | } |
| 635 | |
| 636 | $dependsOnPropertyCollection = new DependsOnPropertyCollection(); |
| 637 | $dependsOnPropertyCollection->append($this->getProperty($dependsOnPropertyUri)); |
| 638 | |
| 639 | $property->setDependsOnPropertyCollection($dependsOnPropertyCollection); |
| 640 | } |
| 641 | |
| 642 | private function invalidatePropertyCache( |
| 643 | PropertyChangedValidator $validator, |
| 644 | core_kernel_classes_Property $currentProperty, |
| 645 | OldProperty $oldProperty |
| 646 | ): void { |
| 647 | if ( |
| 648 | $oldProperty->getRangeUri() |
| 649 | && ($validator->isRangeChanged($currentProperty, $oldProperty) |
| 650 | || $validator->isPropertyTypeChanged($currentProperty, $oldProperty)) |
| 651 | ) { |
| 652 | $listUri = $oldProperty->getRangeUri(); |
| 653 | } |
| 654 | |
| 655 | if (empty($listUri) && $currentProperty->getRange() === null) { |
| 656 | return; |
| 657 | } |
| 658 | |
| 659 | $this->getParentPropertyListCachedRepository()->deleteCache( |
| 660 | [ |
| 661 | 'listUri' => $listUri ?? $currentProperty->getRange()->getUri() |
| 662 | ] |
| 663 | ); |
| 664 | } |
| 665 | |
| 666 | private function isElasticSearchEnabled(): bool |
| 667 | { |
| 668 | /** @var AdvancedSearchChecker $advancedSearchChecker */ |
| 669 | $advancedSearchChecker = $this->getServiceLocator()->get(AdvancedSearchChecker::class); |
| 670 | |
| 671 | return $advancedSearchChecker->isEnabled(); |
| 672 | } |
| 673 | |
| 674 | private function getFeatureFlagChecker(): FeatureFlagCheckerInterface |
| 675 | { |
| 676 | return $this->getServiceLocator()->get(FeatureFlagChecker::class); |
| 677 | } |
| 678 | |
| 679 | private function getParentPropertyListCachedRepository(): ParentPropertyListCachedRepository |
| 680 | { |
| 681 | return $this->getServiceLocator()->get(ParentPropertyListCachedRepository::class); |
| 682 | } |
| 683 | |
| 684 | private function getPropertyChangedValidator(): PropertyChangedValidator |
| 685 | { |
| 686 | return $this->getServiceLocator()->get(PropertyChangedValidator::class); |
| 687 | } |
| 688 | |
| 689 | private function getPropertyTypeValidator(): ValidatorInterface |
| 690 | { |
| 691 | return $this->getPsrContainer()->get(PropertyTypeValidator::class); |
| 692 | } |
| 693 | |
| 694 | private function getPropertyListValidator(): ValidatorInterface |
| 695 | { |
| 696 | return $this->getPsrContainer()->get(PropertyListValidator::class); |
| 697 | } |
| 698 | |
| 699 | private function getDependsOnPropertyRepository(): DependsOnPropertyRepositoryInterface |
| 700 | { |
| 701 | return $this->getPsrContainer()->get(DependsOnPropertyRepository::class); |
| 702 | } |
| 703 | |
| 704 | private function getDependsOnPropertySynchronizer(): DependsOnPropertySynchronizerInterface |
| 705 | { |
| 706 | return $this->getServiceLocator()->get(DependsOnPropertySynchronizer::class); |
| 707 | } |
| 708 | |
| 709 | private function getClassFormValidators(): array |
| 710 | { |
| 711 | return [ |
| 712 | tao_helpers_form_FormContainer::ATTRIBUTE_VALIDATORS => [ |
| 713 | ElementPropertyTypeFactory::PROPERTY_TYPE_ATTRIBUTE => [ |
| 714 | $this->getPropertyTypeValidator(), |
| 715 | ], |
| 716 | AbstractElementPropertyListValuesFactory::PROPERTY_LIST_ATTRIBUTE => [ |
| 717 | $this->getPropertyListValidator(), |
| 718 | ] |
| 719 | ] |
| 720 | ]; |
| 721 | } |
| 722 | } |