Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 100 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| tao_actions_form_Clazz | |
0.00% |
0 / 100 |
|
0.00% |
0 / 8 |
600 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| getClassInstance | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getTopClazz | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getPropertyForm | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
2 | |||
| initForm | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
12 | |||
| initElements | |
0.00% |
0 / 57 |
|
0.00% |
0 / 1 |
240 | |||
| getSystemProperties | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| getNamespaceHelper | |
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 | declare(strict_types=1); |
| 26 | |
| 27 | use oat\generis\model\GenerisRdf; |
| 28 | use oat\generis\model\OntologyRdfs; |
| 29 | use oat\oatbox\service\ServiceManager; |
| 30 | use oat\tao\helpers\NamespaceHelper; |
| 31 | |
| 32 | /** |
| 33 | * Short description of class tao_actions_form_Clazz |
| 34 | * |
| 35 | * @access public |
| 36 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 37 | * @package tao |
| 38 | */ |
| 39 | class tao_actions_form_Clazz extends tao_helpers_form_FormContainer |
| 40 | { |
| 41 | /** |
| 42 | * @var core_kernel_classes_Class |
| 43 | */ |
| 44 | protected $clazz; |
| 45 | |
| 46 | /** |
| 47 | * Property values that are currently being treated |
| 48 | * |
| 49 | * @var array |
| 50 | */ |
| 51 | protected $propertyData; |
| 52 | |
| 53 | /** |
| 54 | * @var bool |
| 55 | */ |
| 56 | private $disableIndexChanges; |
| 57 | |
| 58 | /** |
| 59 | * @throws common_Exception |
| 60 | */ |
| 61 | public function __construct( |
| 62 | core_kernel_classes_Class $clazz, |
| 63 | array $classData, |
| 64 | array $propertyData, |
| 65 | bool $disableIndexChanges = false, |
| 66 | array $options = [] |
| 67 | ) { |
| 68 | $this->clazz = $clazz; |
| 69 | $this->propertyData = $propertyData; |
| 70 | $this->disableIndexChanges = $disableIndexChanges; |
| 71 | |
| 72 | parent::__construct($classData, $options); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Class instance being authored |
| 77 | * |
| 78 | * @return core_kernel_classes_Class |
| 79 | */ |
| 80 | protected function getClassInstance(): core_kernel_classes_Class |
| 81 | { |
| 82 | return $this->clazz; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Top level class until which all properties |
| 87 | * should be displayed |
| 88 | * |
| 89 | * @return core_kernel_classes_Class |
| 90 | */ |
| 91 | protected function getTopClazz(): core_kernel_classes_Class |
| 92 | { |
| 93 | return new core_kernel_classes_Class(GenerisRdf::CLASS_GENERIS_RESOURCE); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Returns the form for the property, based on the mode |
| 98 | * |
| 99 | * @param core_kernel_classes_Property $property |
| 100 | * @param integer $index |
| 101 | * @param boolean $isParentProp |
| 102 | * @param array $propertyData |
| 103 | * @return tao_helpers_form_Form|null |
| 104 | * @throws common_Exception |
| 105 | */ |
| 106 | protected function getPropertyForm( |
| 107 | core_kernel_classes_Property $property, |
| 108 | int $index, |
| 109 | bool $isParentProp, |
| 110 | array $propertyData |
| 111 | ): ?tao_helpers_form_Form { |
| 112 | $options = [ |
| 113 | 'index' => $index, |
| 114 | 'isParentProperty' => $isParentProp, |
| 115 | 'disableIndexChanges' => $this->disableIndexChanges |
| 116 | ]; |
| 117 | $propFormContainer = new tao_actions_form_SimpleProperty( |
| 118 | $this->getClassInstance(), |
| 119 | $property, |
| 120 | $options, |
| 121 | $propertyData |
| 122 | ); |
| 123 | return $propFormContainer->getForm(); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Initialize the form |
| 128 | * |
| 129 | * @access protected |
| 130 | * @return void |
| 131 | * @throws common_Exception |
| 132 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 133 | */ |
| 134 | protected function initForm(): void |
| 135 | { |
| 136 | (isset($this->options['name'])) ? $name = $this->options['name'] : $name = ''; |
| 137 | if (empty($name)) { |
| 138 | $name = 'form_' . (count(self::$forms) + 1); |
| 139 | } |
| 140 | unset($this->options['name']); |
| 141 | |
| 142 | $this->form = tao_helpers_form_FormFactory::getForm($name, $this->options); |
| 143 | |
| 144 | //add property action in toolbar |
| 145 | $actions = tao_helpers_form_FormFactory::getCommonActions(); |
| 146 | $propertyElt = tao_helpers_form_FormFactory::getElement('property', 'Free'); |
| 147 | $propertyElt->setValue( |
| 148 | "<a href='#' class='btn-info property-adder small'><span class='icon-property-add'></span> " |
| 149 | . __('Add property') . "</a>" |
| 150 | ); |
| 151 | $actions[] = $propertyElt; |
| 152 | |
| 153 | //add a hidden field that states it is a class edition form. |
| 154 | $classElt = tao_helpers_form_FormFactory::getElement('tao.forms.class', 'Hidden'); |
| 155 | $classElt->setValue('1'); |
| 156 | $classElt->addClass('global'); |
| 157 | $this->form->addElement($classElt); |
| 158 | |
| 159 | $this->form->setActions($actions, 'top'); |
| 160 | $this->form->setActions($actions, 'bottom'); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Initialize the form elements |
| 165 | * |
| 166 | * @access protected |
| 167 | * @return void |
| 168 | * @throws common_Exception |
| 169 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 170 | */ |
| 171 | protected function initElements(): void |
| 172 | { |
| 173 | $clazz = $this->getClassInstance(); |
| 174 | |
| 175 | //add a group form for the class edition |
| 176 | $elementNames = []; |
| 177 | foreach (tao_helpers_form_GenerisFormFactory::getDefaultProperties() as $property) { |
| 178 | //map properties widgets to form elements |
| 179 | $element = tao_helpers_form_GenerisFormFactory::elementMap($property); |
| 180 | if (!is_null($element)) { |
| 181 | //take property values to populate the form |
| 182 | $values = $clazz->getPropertyValues($property); |
| 183 | if (!$property->isMultiple()) { |
| 184 | if (count($values) > 1) { |
| 185 | $values = array_slice($values, 0, 1); |
| 186 | } |
| 187 | } |
| 188 | foreach ($values as $value) { |
| 189 | if (!is_null($value)) { |
| 190 | $element->setValue($value); |
| 191 | } |
| 192 | } |
| 193 | $element->setName('class_' . $element->getName()); |
| 194 | |
| 195 | //set label validator, read only |
| 196 | if ($property->getUri() == OntologyRdfs::RDFS_LABEL) { |
| 197 | $readonly = tao_helpers_form_FormFactory::getElement('', 'Readonly'); |
| 198 | $readonly->setDescription($element->getDescription()); |
| 199 | $readonly->setValue($element->getRawValue()); |
| 200 | $element = $readonly; |
| 201 | } |
| 202 | $element->addClass('global'); |
| 203 | $this->form->addElement($element); |
| 204 | |
| 205 | $elementNames[] = $element->getName(); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | //add an hidden elt for the class uri |
| 210 | $classUriElt = tao_helpers_form_FormFactory::getElement('classUri', 'Hidden'); |
| 211 | $classUriElt->setValue(tao_helpers_Uri::encode($clazz->getUri())); |
| 212 | $classUriElt->addClass('global'); |
| 213 | $this->form->addElement($classUriElt); |
| 214 | |
| 215 | $hiddenId = tao_helpers_form_FormFactory::getElement('id', 'Hidden'); |
| 216 | $hiddenId->setValue($clazz->getUri()); |
| 217 | $hiddenId->addClass('global'); |
| 218 | $this->form->addElement($hiddenId); |
| 219 | |
| 220 | //class properties edition: add a group form for each property |
| 221 | |
| 222 | $classProperties = tao_helpers_form_GenerisFormFactory::getClassProperties($clazz, $this->getTopClazz()); |
| 223 | |
| 224 | $i = 0; |
| 225 | $systemProperties = $this->getSystemProperties(); |
| 226 | |
| 227 | foreach ($classProperties as $classProperty) { |
| 228 | $i++; |
| 229 | $namespace = substr($classProperty->getUri(), 0, strpos($classProperty->getUri(), '#')); |
| 230 | $useEditor = $this->getNamespaceHelper()->isNamespaceSupported($namespace); |
| 231 | |
| 232 | $parentProp = true; |
| 233 | $domains = $classProperty->getDomain(); |
| 234 | foreach ($domains->getIterator() as $domain) { |
| 235 | if ( |
| 236 | array_search($classProperty->getUri(), $systemProperties) !== false |
| 237 | || $domain->getUri() == $clazz->getUri() |
| 238 | ) { |
| 239 | $parentProp = false; |
| 240 | //@todo use the getPrivileges method once implemented |
| 241 | break; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | if ($useEditor) { |
| 246 | $propData = []; |
| 247 | if (isset($this->propertyData[$classProperty->getUri()])) { |
| 248 | foreach ($this->propertyData[$classProperty->getUri()] as $key => $value) { |
| 249 | $propData[$i . '_' . $key] = $value; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | $propForm = $this->getPropertyForm($classProperty, $i, $parentProp, $propData); |
| 254 | |
| 255 | //and get its elements and groups |
| 256 | $this->form->setElements(array_merge($this->form->getElements(), $propForm->getElements())); |
| 257 | $this->form->setGroups(array_merge($this->form->getGroups(), $propForm->getGroups())); |
| 258 | |
| 259 | unset($propForm); |
| 260 | } else { // read only properties |
| 261 | $roElement = tao_helpers_form_FormFactory::getElement('roProperty' . $i, 'Free'); |
| 262 | $roElement->setValue(__('Cannot be edited')); |
| 263 | $this->form->addElement($roElement); |
| 264 | |
| 265 | $groupTitle = '<span class="property-heading-label">' . _dh($classProperty->getLabel()) . '</span>'; |
| 266 | $this->form->createGroup("ro_property_{$i}", $groupTitle, ['roProperty' . $i]); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Returns list of all system property classes |
| 273 | * @return array |
| 274 | */ |
| 275 | protected function getSystemProperties(): array |
| 276 | { |
| 277 | $constants = get_defined_constants(true); |
| 278 | |
| 279 | $keys = array_filter(array_keys($constants['user']), function ($key) { |
| 280 | return strstr($key, 'PROPERTY') !== false; |
| 281 | }); |
| 282 | |
| 283 | return array_intersect_key($constants['user'], array_flip($keys)); |
| 284 | } |
| 285 | |
| 286 | private function getNamespaceHelper(): NamespaceHelper |
| 287 | { |
| 288 | return ServiceManager::getServiceManager()->get(NamespaceHelper::SERVICE_ID); |
| 289 | } |
| 290 | } |