Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
40.20% |
80 / 199 |
|
28.57% |
2 / 7 |
CRAP | |
0.00% |
0 / 1 |
| tao_helpers_form_GenerisFormFactory | |
40.20% |
80 / 199 |
|
28.57% |
2 / 7 |
419.21 | |
0.00% |
0 / 1 |
| elementMap | |
0.00% |
0 / 60 |
|
0.00% |
0 / 1 |
342 | |||
| getClassProperties | |
0.00% |
0 / 31 |
|
0.00% |
0 / 1 |
132 | |||
| getDefaultProperties | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getPropertyProperties | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
42 | |||
| getWidgetUriById | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPropertyMap | |
100.00% |
79 / 79 |
|
100.00% |
1 / 1 |
1 | |||
| extractTreeData | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| 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\generis\model\GenerisRdf; |
| 26 | use oat\generis\model\OntologyRdf; |
| 27 | use oat\generis\model\OntologyRdfs; |
| 28 | use oat\generis\model\WidgetRdf; |
| 29 | use oat\tao\helpers\form\elements\TreeAware; |
| 30 | use oat\tao\helpers\form\elements\xhtml\SearchDropdown; |
| 31 | use oat\tao\helpers\form\elements\xhtml\SearchTextBox; |
| 32 | use oat\tao\helpers\form\ValidationRuleRegistry; |
| 33 | use oat\tao\model\TaoOntology; |
| 34 | use tao_helpers_form_elements_AsyncFile as AsyncFile; |
| 35 | use tao_helpers_form_elements_Authoring as Authoring; |
| 36 | use tao_helpers_form_elements_Calendar as Calendar; |
| 37 | use tao_helpers_form_elements_Checkbox as Checkbox; |
| 38 | use tao_helpers_form_elements_Combobox as ComboBox; |
| 39 | use tao_helpers_form_elements_GenerisAsyncFile as GenerisAsyncFile; |
| 40 | use tao_helpers_form_elements_Hiddenbox as HiddenBox; |
| 41 | use tao_helpers_form_elements_Htmlarea as HtmlArea; |
| 42 | use tao_helpers_form_elements_Radiobox as RadioBox; |
| 43 | use tao_helpers_form_elements_Textarea as TextArea; |
| 44 | use tao_helpers_form_elements_Textbox as TextBox; |
| 45 | use tao_helpers_form_elements_Treebox as TreeBox; |
| 46 | |
| 47 | /** |
| 48 | * The GenerisFormFactory enables you to create Forms using rdf data and the |
| 49 | * api to provide it. You can give any node of your ontology and the factory |
| 50 | * create the appriopriate form. The Generis ontology (with the Widget Property) |
| 51 | * required to use it. |
| 52 | * Now only the xhtml rendering mode is implemented |
| 53 | * |
| 54 | * @access public |
| 55 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 56 | * @package tao |
| 57 | * @see core_kernel_classes_* packages |
| 58 | |
| 59 | */ |
| 60 | class tao_helpers_form_GenerisFormFactory |
| 61 | { |
| 62 | // --- ASSOCIATIONS --- |
| 63 | |
| 64 | |
| 65 | // --- ATTRIBUTES --- |
| 66 | |
| 67 | // --- OPERATIONS --- |
| 68 | |
| 69 | /** |
| 70 | * Enable you to map an rdf property to a form element using the Widget |
| 71 | * |
| 72 | * @access public |
| 73 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 74 | * |
| 75 | * @param core_kernel_classes_Property $property |
| 76 | * |
| 77 | * @return tao_helpers_form_FormElement |
| 78 | * |
| 79 | * @throws common_Exception |
| 80 | * @throws common_exception_Error |
| 81 | * @throws core_kernel_persistence_Exception |
| 82 | */ |
| 83 | public static function elementMap(core_kernel_classes_Property $property): ?tao_helpers_form_FormElement |
| 84 | { |
| 85 | //create the element from the right widget |
| 86 | $property->feed(); |
| 87 | |
| 88 | $widgetResource = $property->getWidget(); |
| 89 | |
| 90 | //authoring widget is not used in standalone mode |
| 91 | if ( |
| 92 | null === $widgetResource |
| 93 | || $widgetResource->getUri() === Authoring::WIDGET_ID |
| 94 | && tao_helpers_Context::check('STANDALONE_MODE') |
| 95 | ) { |
| 96 | return null; |
| 97 | } |
| 98 | |
| 99 | // horrible hack to fix file widget |
| 100 | if ($widgetResource->getUri() === AsyncFile::WIDGET_ID) { |
| 101 | $widgetResource = new core_kernel_classes_Resource(GenerisAsyncFile::WIDGET_ID); |
| 102 | } |
| 103 | |
| 104 | $element = tao_helpers_form_FormFactory::getElementByWidget( |
| 105 | tao_helpers_Uri::encode($property->getUri()), |
| 106 | $widgetResource |
| 107 | ); |
| 108 | |
| 109 | if (null === $element) { |
| 110 | return null; |
| 111 | } |
| 112 | |
| 113 | if ($element->getWidget() !== $widgetResource->getUri()) { |
| 114 | common_Logger::w( |
| 115 | 'Widget definition differs from implementation: ' . $element->getWidget() . ' != ' |
| 116 | . $widgetResource->getUri() |
| 117 | ); |
| 118 | return null; |
| 119 | } |
| 120 | |
| 121 | //use the property label as element description |
| 122 | $propDesc = trim($property->getLabel()) !== '' |
| 123 | ? $property->getLabel() |
| 124 | : str_replace(LOCAL_NAMESPACE, '', $property->getUri()); |
| 125 | $element->setDescription($propDesc); |
| 126 | |
| 127 | //multi elements use the property range as options |
| 128 | if (method_exists($element, 'setOptions')) { |
| 129 | $range = $property->getRange(); |
| 130 | |
| 131 | if ($range !== null) { |
| 132 | $options = []; |
| 133 | |
| 134 | if ($element instanceof TreeAware) { |
| 135 | $sortedOptions = $element->rangeToTree( |
| 136 | $property->getUri() === OntologyRdfs::RDFS_RANGE |
| 137 | ? new core_kernel_classes_Class(OntologyRdfs::RDFS_RESOURCE) |
| 138 | : $range |
| 139 | ); |
| 140 | } else { |
| 141 | foreach ($range->getInstances(true) as $rangeInstance) { |
| 142 | $level = $rangeInstance->getOnePropertyValue( |
| 143 | new core_kernel_classes_Property(TaoOntology::PROPERTY_LIST_LEVEL) |
| 144 | ); |
| 145 | if (is_null($level)) { |
| 146 | $options[tao_helpers_Uri::encode($rangeInstance->getUri())] = [ |
| 147 | tao_helpers_Uri::encode($rangeInstance->getUri()), |
| 148 | $rangeInstance->getLabel() |
| 149 | ]; |
| 150 | } else { |
| 151 | $level = ($level instanceof core_kernel_classes_Resource) |
| 152 | ? $level->getUri() |
| 153 | : (string)$level; |
| 154 | $options[$level] = [ |
| 155 | tao_helpers_Uri::encode($rangeInstance->getUri()), |
| 156 | $rangeInstance->getLabel() |
| 157 | ]; |
| 158 | } |
| 159 | } |
| 160 | ksort($options); |
| 161 | $sortedOptions = []; |
| 162 | foreach ($options as $id => $values) { |
| 163 | $sortedOptions[$values[0]] = $values[1]; |
| 164 | } |
| 165 | //set the default value to an empty space |
| 166 | if (method_exists($element, 'setEmptyOption')) { |
| 167 | $element->setEmptyOption(' '); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | //complete the options listing |
| 172 | $element->setOptions($sortedOptions); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | foreach (ValidationRuleRegistry::getRegistry()->getValidators($property) as $validator) { |
| 177 | $element->addValidator($validator); |
| 178 | } |
| 179 | |
| 180 | return $element; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Enable you to get the properties of a class. |
| 185 | * The advantage of this method is to limit the level of recusrivity in the |
| 186 | * It get the properties up to the defined top class |
| 187 | * |
| 188 | * @access public |
| 189 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 190 | * @param core_kernel_classes_Class $clazz |
| 191 | * @param core_kernel_classes_Class $topLevelClazz |
| 192 | * @return array |
| 193 | */ |
| 194 | public static function getClassProperties( |
| 195 | core_kernel_classes_Class $clazz, |
| 196 | core_kernel_classes_Class $topLevelClazz = null |
| 197 | ): array { |
| 198 | if (null === $topLevelClazz) { |
| 199 | $topLevelClazz = new core_kernel_classes_Class(TaoOntology::CLASS_URI_OBJECT); |
| 200 | } |
| 201 | |
| 202 | if ($clazz->getUri() === $topLevelClazz->getUri()) { |
| 203 | return $clazz->getProperties(false); |
| 204 | } |
| 205 | |
| 206 | //determine the parent path |
| 207 | $parents = []; |
| 208 | $top = false; |
| 209 | do { |
| 210 | if (!isset($lastLevelParents)) { |
| 211 | $parentClasses = $clazz->getParentClasses(false); |
| 212 | } else { |
| 213 | $parentClasses = []; |
| 214 | foreach ($lastLevelParents as $parent) { |
| 215 | $parentClasses = array_merge($parentClasses, $parent->getParentClasses(false)); |
| 216 | } |
| 217 | } |
| 218 | if (empty($parentClasses)) { |
| 219 | break; |
| 220 | } |
| 221 | $lastLevelParents = []; |
| 222 | foreach ($parentClasses as $parentClass) { |
| 223 | if ($parentClass->getUri() === OntologyRdfs::RDFS_CLASS) { |
| 224 | continue; |
| 225 | } |
| 226 | if ($parentClass->getUri() === $topLevelClazz->getUri()) { |
| 227 | $parents[$parentClass->getUri()] = $parentClass; |
| 228 | $top = true; |
| 229 | break; |
| 230 | } |
| 231 | |
| 232 | $allParentClasses = $parentClass->getParentClasses(true); |
| 233 | if (array_key_exists($topLevelClazz->getUri(), $allParentClasses)) { |
| 234 | $parents[$parentClass->getUri()] = $parentClass; |
| 235 | } |
| 236 | |
| 237 | $lastLevelParents[$parentClass->getUri()] = $parentClass; |
| 238 | } |
| 239 | } while (!$top); |
| 240 | |
| 241 | $propertyChunks = [[]]; |
| 242 | foreach ($parents as $parent) { |
| 243 | $propertyChunks[] = $parent->getProperties(false); |
| 244 | } |
| 245 | $propertyChunks[] = $clazz->getProperties(false); |
| 246 | |
| 247 | return array_merge(...$propertyChunks); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * get the default properties to add to every forms |
| 252 | * |
| 253 | * @access public |
| 254 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 255 | * @return array |
| 256 | */ |
| 257 | public static function getDefaultProperties(): array |
| 258 | { |
| 259 | return [ |
| 260 | new core_kernel_classes_Property(OntologyRdfs::RDFS_LABEL) |
| 261 | ]; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Get the properties of the rdfs Property class |
| 266 | * |
| 267 | * @access public |
| 268 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 269 | * @param string mode |
| 270 | * @return array |
| 271 | */ |
| 272 | public static function getPropertyProperties($mode = 'simple'): array |
| 273 | { |
| 274 | $returnValue = []; |
| 275 | |
| 276 | switch ($mode) { |
| 277 | case 'simple': |
| 278 | $defaultUris = [GenerisRdf::PROPERTY_IS_LG_DEPENDENT]; |
| 279 | break; |
| 280 | case 'advanced': |
| 281 | default: |
| 282 | $defaultUris = [ |
| 283 | OntologyRdfs::RDFS_LABEL, |
| 284 | WidgetRdf::PROPERTY_WIDGET, |
| 285 | OntologyRdfs::RDFS_RANGE, |
| 286 | GenerisRdf::PROPERTY_IS_LG_DEPENDENT |
| 287 | ]; |
| 288 | break; |
| 289 | } |
| 290 | $resourceClass = new core_kernel_classes_Class(OntologyRdf::RDF_PROPERTY); |
| 291 | foreach ($resourceClass->getProperties() as $property) { |
| 292 | if (in_array($property->getUri(), $defaultUris, true)) { |
| 293 | $returnValue[] = $property; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | return $returnValue; |
| 298 | } |
| 299 | |
| 300 | public static function getWidgetUriById(string $id): ?string |
| 301 | { |
| 302 | return self::getPropertyMap()[$id]['widget'] ?? null; |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Return the map between the Property properties: range, widget, etc. to |
| 307 | * shortcuts for the simplePropertyEditor |
| 308 | * |
| 309 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 310 | */ |
| 311 | public static function getPropertyMap(): array |
| 312 | { |
| 313 | return [ |
| 314 | 'text' => [ |
| 315 | 'title' => __('Text - Short - Field'), |
| 316 | 'widget' => TextBox::WIDGET_ID, |
| 317 | 'range' => OntologyRdfs::RDFS_LITERAL, |
| 318 | 'multiple' => GenerisRdf::GENERIS_FALSE, |
| 319 | ], |
| 320 | 'longtext' => [ |
| 321 | 'title' => __('Text - Long - Box'), |
| 322 | 'widget' => TextArea::WIDGET_ID, |
| 323 | 'range' => OntologyRdfs::RDFS_LITERAL, |
| 324 | 'multiple' => GenerisRdf::GENERIS_FALSE, |
| 325 | ], |
| 326 | 'html' => [ |
| 327 | 'title' => __('Text - Long - HTML editor'), |
| 328 | 'widget' => HtmlArea::WIDGET_ID, |
| 329 | 'range' => OntologyRdfs::RDFS_LITERAL, |
| 330 | 'multiple' => GenerisRdf::GENERIS_FALSE, |
| 331 | ], |
| 332 | 'list' => [ |
| 333 | 'title' => __('List - Single choice - Radio button'), |
| 334 | 'widget' => RadioBox::WIDGET_ID, |
| 335 | 'range' => OntologyRdfs::RDFS_RESOURCE, |
| 336 | 'multiple' => GenerisRdf::GENERIS_FALSE, |
| 337 | 'depends-on-property' => GenerisRdf::PROPERTY_DEPENDS_ON_PROPERTY, |
| 338 | ], |
| 339 | 'multiplenodetree' => [ |
| 340 | 'title' => __('Tree - Multiple node choice'), |
| 341 | 'widget' => TreeBox::WIDGET_ID, |
| 342 | 'range' => OntologyRdfs::RDFS_RESOURCE, |
| 343 | 'multiple' => GenerisRdf::GENERIS_TRUE, |
| 344 | ], |
| 345 | 'longlist' => [ |
| 346 | 'title' => __('List - Single choice - Drop down'), |
| 347 | 'widget' => ComboBox::WIDGET_ID, |
| 348 | 'range' => OntologyRdfs::RDFS_RESOURCE, |
| 349 | 'multiple' => GenerisRdf::GENERIS_FALSE, |
| 350 | 'depends-on-property' => GenerisRdf::PROPERTY_DEPENDS_ON_PROPERTY, |
| 351 | ], |
| 352 | 'multilist' => [ |
| 353 | 'title' => __('List - Multiple choice - Check box'), |
| 354 | 'widget' => Checkbox::WIDGET_ID, |
| 355 | 'range' => OntologyRdfs::RDFS_RESOURCE, |
| 356 | 'multiple' => GenerisRdf::GENERIS_TRUE, |
| 357 | 'depends-on-property' => GenerisRdf::PROPERTY_DEPENDS_ON_PROPERTY, |
| 358 | ], |
| 359 | 'multisearchlist' => [ |
| 360 | 'title' => __('List - Multiple choice - Search input'), |
| 361 | 'widget' => SearchTextBox::WIDGET_ID, |
| 362 | 'range' => OntologyRdfs::RDFS_RESOURCE, |
| 363 | 'multiple' => GenerisRdf::GENERIS_TRUE, |
| 364 | 'depends-on-property' => GenerisRdf::PROPERTY_DEPENDS_ON_PROPERTY, |
| 365 | ], |
| 366 | 'singlesearchlist' => [ |
| 367 | 'title' => __('List - Single choice - Search input'), |
| 368 | 'widget' => SearchDropdown::WIDGET_ID, |
| 369 | 'range' => OntologyRdfs::RDFS_RESOURCE, |
| 370 | 'multiple' => GenerisRdf::GENERIS_FALSE, |
| 371 | 'depends-on-property' => GenerisRdf::PROPERTY_DEPENDS_ON_PROPERTY, |
| 372 | ], |
| 373 | 'calendar' => [ |
| 374 | 'title' => __('Calendar'), |
| 375 | 'widget' => Calendar::WIDGET_ID, |
| 376 | 'range' => OntologyRdfs::RDFS_LITERAL, |
| 377 | 'multiple' => GenerisRdf::GENERIS_FALSE, |
| 378 | ], |
| 379 | 'password' => [ |
| 380 | 'title' => __('Password'), |
| 381 | 'widget' => HiddenBox::WIDGET_ID, |
| 382 | 'range' => OntologyRdfs::RDFS_LITERAL, |
| 383 | 'multiple' => GenerisRdf::GENERIS_FALSE, |
| 384 | ], |
| 385 | 'file' => [ |
| 386 | 'title' => __('File'), |
| 387 | 'widget' => AsyncFile::WIDGET_ID, |
| 388 | 'range' => GenerisRdf::CLASS_GENERIS_FILE, |
| 389 | 'multiple' => GenerisRdf::GENERIS_FALSE, |
| 390 | ], |
| 391 | ]; |
| 392 | } |
| 393 | |
| 394 | |
| 395 | /** |
| 396 | * Short description of method extractTreeData |
| 397 | * |
| 398 | * @access public |
| 399 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 400 | * @param array $data |
| 401 | * @return array |
| 402 | */ |
| 403 | public static function extractTreeData($data): array |
| 404 | { |
| 405 | $returnValue = []; |
| 406 | |
| 407 | if (isset($data['data'])) { |
| 408 | $data = [$data]; |
| 409 | } |
| 410 | foreach ($data as $node) { |
| 411 | $returnValue[$node['attributes']['id']] = $node['data']; |
| 412 | if (isset($node['children'])) { |
| 413 | $returnValue = array_merge($returnValue, self::extractTreeData($node['children'])); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | return $returnValue; |
| 418 | } |
| 419 | } |