Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 146 |
|
0.00% |
0 / 12 |
CRAP | |
0.00% |
0 / 1 |
| tao_actions_form_SimpleProperty | |
0.00% |
0 / 146 |
|
0.00% |
0 / 12 |
1260 | |
0.00% |
0 / 1 |
| initElements | |
0.00% |
0 / 84 |
|
0.00% |
0 / 1 |
342 | |||
| getTreeElement | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
| addDependsOnProperty | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| getListElement | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| disableValues | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
| getSchemaProperties | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
6 | |||
| getDependsOnPropertyFormFieldFactory | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getElementPropertyTypeFactory | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getElementPropertyListValuesFactory | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getElementPropertyEmptyListValuesFactory | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getFeatureFlagChecker | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getContainer | |
0.00% |
0 / 1 |
|
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) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
| 19 | * (under the project TAO-TRANSFER); |
| 20 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 21 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | use oat\tao\model\TaoOntology; |
| 26 | use oat\generis\model\GenerisRdf; |
| 27 | use oat\generis\model\OntologyRdfs; |
| 28 | use Psr\Container\ContainerInterface; |
| 29 | use oat\oatbox\service\ServiceManager; |
| 30 | use oat\taoBackOffice\model\tree\TreeService; |
| 31 | use oat\tao\model\search\index\OntologyIndex; |
| 32 | use oat\tao\helpers\form\ValidationRuleRegistry; |
| 33 | use oat\tao\model\featureFlag\FeatureFlagChecker; |
| 34 | use oat\tao\helpers\form\elements\xhtml\Validators; |
| 35 | use oat\tao\helpers\form\Factory\ElementFactoryContext; |
| 36 | use oat\tao\helpers\form\Factory\ElementFactoryInterface; |
| 37 | use oat\tao\model\featureFlag\FeatureFlagCheckerInterface; |
| 38 | use oat\tao\helpers\form\Factory\ElementPropertyTypeFactory; |
| 39 | use oat\tao\helpers\form\Factory\ElementPropertyListValuesFactory; |
| 40 | use oat\tao\helpers\form\Factory\ElementPropertyEmptyListValuesFactory; |
| 41 | use oat\tao\model\Lists\Presentation\Web\Factory\DependsOnPropertyFormFieldFactory; |
| 42 | |
| 43 | /** |
| 44 | * Enable you to edit a property |
| 45 | * |
| 46 | * @access public |
| 47 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 48 | * @package tao |
| 49 | |
| 50 | */ |
| 51 | class tao_actions_form_SimpleProperty extends tao_actions_form_AbstractProperty |
| 52 | { |
| 53 | /** |
| 54 | * Initialize the form elements |
| 55 | * |
| 56 | * @access protected |
| 57 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 58 | */ |
| 59 | protected function initElements() |
| 60 | { |
| 61 | |
| 62 | $property = $this->getPropertyInstance(); |
| 63 | |
| 64 | $index = $this->getIndex(); |
| 65 | |
| 66 | $propertyProperties = $this->getSchemaProperties(); |
| 67 | $values = $property->getPropertiesValues($propertyProperties); |
| 68 | |
| 69 | $elementNames = []; |
| 70 | foreach ($propertyProperties as $propertyProperty) { |
| 71 | //map properties widgets to form elements |
| 72 | $element = tao_helpers_form_GenerisFormFactory::elementMap($propertyProperty); |
| 73 | |
| 74 | if (!is_null($element)) { |
| 75 | //take property values to populate the form |
| 76 | if (isset($values[$propertyProperty->getUri()])) { |
| 77 | if ($element instanceof Validators) { |
| 78 | $this->disableValues($property, $element); |
| 79 | } |
| 80 | |
| 81 | $propertyValues = $values[$propertyProperty->getUri()]; |
| 82 | foreach ($propertyValues as $value) { |
| 83 | if (!is_null($value)) { |
| 84 | if ($value instanceof core_kernel_classes_Resource) { |
| 85 | $element->setValue($value->getUri()); |
| 86 | } |
| 87 | if ($value instanceof core_kernel_classes_Literal) { |
| 88 | $element->setValue((string)$value); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | $element->setName("{$index}_{$element->getName()}"); |
| 94 | $element->addClass('property'); |
| 95 | |
| 96 | if ($propertyProperty->getUri() == TaoOntology::PROPERTY_GUI_ORDER) { |
| 97 | $element->addValidator(tao_helpers_form_FormFactory::getValidator('Integer')); |
| 98 | } |
| 99 | if ($propertyProperty->getUri() == OntologyRdfs::RDFS_LABEL) { |
| 100 | $element->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty')); |
| 101 | } |
| 102 | if ( |
| 103 | $propertyProperty->getUri() === GenerisRdf::PROPERTY_IS_STATISTICAL && |
| 104 | empty($values[$propertyProperty->getUri()]) |
| 105 | ) { |
| 106 | $element->setValue(tao_helpers_Uri::encode(GenerisRdf::GENERIS_FALSE)); |
| 107 | } |
| 108 | |
| 109 | $this->form->addElement($element); |
| 110 | $elementNames[] = $element->getName(); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | $elementContext = new ElementFactoryContext( |
| 115 | [ |
| 116 | ElementFactoryContext::PARAM_PROPERTY => $property, |
| 117 | ElementFactoryContext::PARAM_INDEX => $index, |
| 118 | ElementFactoryContext::PARAM_DATA => $this->data, |
| 119 | ] |
| 120 | ); |
| 121 | |
| 122 | //build the type list from the "widget/range to type" map |
| 123 | $typeElt = $this->getElementPropertyTypeFactory()->create($elementContext); |
| 124 | |
| 125 | $this->form->addElement($typeElt); |
| 126 | $elementNames[] = $typeElt->getName(); |
| 127 | |
| 128 | $range = $property->getRange(); |
| 129 | |
| 130 | $rangeSelect = $this->getElementPropertyEmptyListValuesFactory()->create($elementContext); |
| 131 | |
| 132 | $this->form->addElement($rangeSelect); |
| 133 | $elementNames[] = $rangeSelect->getName(); |
| 134 | |
| 135 | //list drop down |
| 136 | $listElt = $this->getListElement($range); |
| 137 | $this->form->addElement($listElt); |
| 138 | $elementNames[] = $listElt->getName(); |
| 139 | |
| 140 | //trees dropdown |
| 141 | $treeElt = $this->getTreeElement($range); |
| 142 | $this->form->addElement($treeElt); |
| 143 | $elementNames[] = $treeElt->getName(); |
| 144 | |
| 145 | //index part |
| 146 | $indexes = $property->getPropertyValues(new core_kernel_classes_Property(OntologyIndex::PROPERTY_INDEX)); |
| 147 | foreach ($indexes as $i => $indexUri) { |
| 148 | $indexProperty = new OntologyIndex($indexUri); |
| 149 | $indexFormContainer = new tao_actions_form_IndexProperty($indexProperty, $index . $i); |
| 150 | /** @var tao_helpers_form_Form $indexForm */ |
| 151 | $indexForm = $indexFormContainer->getForm(); |
| 152 | foreach ($indexForm->getElements() as $element) { |
| 153 | $this->form->addElement($element); |
| 154 | $elementNames[] = $element->getName(); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | //add this element only when the property is defined (type) |
| 159 | if (!is_null($property->getRange())) { |
| 160 | $addIndexElt = tao_helpers_form_FormFactory::getElement("index_{$index}_add", 'Free'); |
| 161 | $addIndexElt->setValue( |
| 162 | "<a href='#' class='btn-info index-adder small index'><span class='icon-add'></span> " |
| 163 | . __('Add index') . "</a><div class='clearfix'></div>" |
| 164 | ); |
| 165 | $this->form->addElement($addIndexElt); |
| 166 | $elementNames[] = $addIndexElt; |
| 167 | } else { |
| 168 | $addIndexElt = tao_helpers_form_FormFactory::getElement("index_{$index}_p", 'Free'); |
| 169 | $addIndexElt->setValue( |
| 170 | "<p class='index' >" . __('Choose a type for your property first') . "</p>" |
| 171 | ); |
| 172 | $this->form->addElement($addIndexElt); |
| 173 | $elementNames[] = $addIndexElt; |
| 174 | } |
| 175 | |
| 176 | //add an hidden elt for the property uri |
| 177 | $encodedUri = tao_helpers_Uri::encode($property->getUri()); |
| 178 | $propUriElt = tao_helpers_form_FormFactory::getElement("{$index}_uri", 'Hidden'); |
| 179 | $propUriElt->addAttribute('class', 'property-uri property'); |
| 180 | $propUriElt->setValue($encodedUri); |
| 181 | $this->form->addElement($propUriElt); |
| 182 | $elementNames[] = $propUriElt; |
| 183 | |
| 184 | $element = $this->addDependsOnProperty($index, $property); |
| 185 | |
| 186 | if ($element) { |
| 187 | $this->form->addElement($element); |
| 188 | |
| 189 | $elementNames[] = $element->getName(); |
| 190 | } |
| 191 | |
| 192 | if (!empty($elementNames)) { |
| 193 | $groupTitle = $this->getGroupTitle($property); |
| 194 | $this->form->createGroup("property_{$encodedUri}", $groupTitle, $elementNames); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * @param $range |
| 200 | * |
| 201 | * @return tao_helpers_form_elements_xhtml_Combobox |
| 202 | * @throws common_Exception |
| 203 | */ |
| 204 | protected function getTreeElement($range) |
| 205 | { |
| 206 | |
| 207 | $dataService = TreeService::singleton(); |
| 208 | /** |
| 209 | * @var tao_helpers_form_elements_xhtml_Combobox $element |
| 210 | */ |
| 211 | $element = tao_helpers_form_FormFactory::getElement("{$this->getIndex()}_range_tree", 'Combobox'); |
| 212 | $element->setDescription(__('Tree values')); |
| 213 | $element->addAttribute('class', 'property-template tree-template'); |
| 214 | $element->addAttribute('disabled', 'disabled'); |
| 215 | $element->setEmptyOption(' --- ' . __('select') . ' --- '); |
| 216 | $treeOptions = []; |
| 217 | foreach ($dataService->getTrees() as $tree) { |
| 218 | $treeOptions[tao_helpers_Uri::encode($tree->getUri())] = $tree->getLabel(); |
| 219 | if (null !== $range && $range->getUri() === $tree->getUri()) { |
| 220 | $element->setValue($tree->getUri()); |
| 221 | } |
| 222 | } |
| 223 | $element->setOptions($treeOptions); |
| 224 | |
| 225 | return $element; |
| 226 | } |
| 227 | |
| 228 | private function addDependsOnProperty( |
| 229 | int $index, |
| 230 | core_kernel_classes_Property $property |
| 231 | ): ?tao_helpers_form_FormElement { |
| 232 | return $this->getDependsOnPropertyFormFieldFactory()->create( |
| 233 | [ |
| 234 | 'index' => $index, |
| 235 | 'property' => $property, |
| 236 | ] |
| 237 | ); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * @param $range |
| 242 | * |
| 243 | * @return tao_helpers_form_elements_xhtml_Combobox |
| 244 | * @throws common_Exception |
| 245 | */ |
| 246 | protected function getListElement($range) |
| 247 | { |
| 248 | return $this->getElementPropertyListValuesFactory()->create( |
| 249 | new ElementFactoryContext( |
| 250 | [ |
| 251 | ElementFactoryContext::PARAM_INDEX => $this->getIndex(), |
| 252 | ElementFactoryContext::PARAM_RANGE => $range, |
| 253 | ] |
| 254 | ) |
| 255 | ); |
| 256 | } |
| 257 | |
| 258 | private function disableValues(core_kernel_classes_Property $property, Validators $element): void |
| 259 | { |
| 260 | $isListsDependencyEnabled = $this->getFeatureFlagChecker()->isEnabled( |
| 261 | FeatureFlagChecker::FEATURE_FLAG_LISTS_DEPENDENCY_ENABLED |
| 262 | ); |
| 263 | |
| 264 | if (!$isListsDependencyEnabled) { |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | $requiredParentValues = ['notEmpty']; |
| 269 | $disabledValues = []; |
| 270 | |
| 271 | foreach ($property->getDependsOnPropertyCollection() as $parentProperty) { |
| 272 | $validationRuleProperty = $this->getProperty(ValidationRuleRegistry::PROPERTY_VALIDATION_RULE); |
| 273 | $validationRules = $parentProperty->getPropertyValues($validationRuleProperty); |
| 274 | |
| 275 | $disabledValues = array_merge( |
| 276 | $disabledValues, |
| 277 | array_diff($requiredParentValues, $validationRules) |
| 278 | ); |
| 279 | } |
| 280 | |
| 281 | $element->setDisabledValues(array_unique($disabledValues)); |
| 282 | } |
| 283 | |
| 284 | private function getSchemaProperties(): array |
| 285 | { |
| 286 | return array_merge( |
| 287 | tao_helpers_form_GenerisFormFactory::getDefaultProperties(), |
| 288 | array_filter( |
| 289 | [ |
| 290 | $this->getProperty(GenerisRdf::PROPERTY_ALIAS), |
| 291 | $this->getProperty(GenerisRdf::PROPERTY_IS_LG_DEPENDENT), |
| 292 | $this->getFeatureFlagChecker()->isEnabled('FEATURE_FLAG_STATISTIC_METADATA_IMPORT') |
| 293 | ? $this->getProperty(GenerisRdf::PROPERTY_IS_STATISTICAL) |
| 294 | : null, |
| 295 | $this->getProperty(TaoOntology::PROPERTY_GUI_ORDER), |
| 296 | $this->getProperty(ValidationRuleRegistry::PROPERTY_VALIDATION_RULE) |
| 297 | ] |
| 298 | ) |
| 299 | ); |
| 300 | } |
| 301 | |
| 302 | private function getDependsOnPropertyFormFieldFactory(): DependsOnPropertyFormFieldFactory |
| 303 | { |
| 304 | return $this->getContainer()->get(DependsOnPropertyFormFieldFactory::class); |
| 305 | } |
| 306 | |
| 307 | private function getElementPropertyTypeFactory(): ElementFactoryInterface |
| 308 | { |
| 309 | return $this->getContainer()->get(ElementPropertyTypeFactory::class); |
| 310 | } |
| 311 | |
| 312 | private function getElementPropertyListValuesFactory(): ElementFactoryInterface |
| 313 | { |
| 314 | return $this->getContainer()->get(ElementPropertyListValuesFactory::class); |
| 315 | } |
| 316 | |
| 317 | private function getElementPropertyEmptyListValuesFactory(): ElementFactoryInterface |
| 318 | { |
| 319 | return $this->getContainer()->get(ElementPropertyEmptyListValuesFactory::class); |
| 320 | } |
| 321 | |
| 322 | private function getFeatureFlagChecker(): FeatureFlagCheckerInterface |
| 323 | { |
| 324 | return $this->getContainer()->get(FeatureFlagChecker::class); |
| 325 | } |
| 326 | |
| 327 | private function getContainer(): ContainerInterface |
| 328 | { |
| 329 | return ServiceManager::getServiceManager()->getContainer(); |
| 330 | } |
| 331 | } |