Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 77 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| tao_actions_form_CreateInstance | |
0.00% |
0 / 77 |
|
0.00% |
0 / 6 |
1056 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| initForm | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| initElements | |
0.00% |
0 / 51 |
|
0.00% |
0 / 1 |
650 | |||
| addSignature | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| getSignature | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getDataToSign | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| 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\OntologyRdfs; |
| 26 | use oat\oatbox\service\ServiceManager; |
| 27 | use oat\tao\helpers\form\validators\ResourceSignatureValidator; |
| 28 | use oat\tao\model\security\SignatureGenerator; |
| 29 | use oat\tao\model\security\SignatureValidator; |
| 30 | use oat\tao\model\TaoOntology; |
| 31 | use tao_helpers_form_elements_Treeview as TreeView; |
| 32 | |
| 33 | /** |
| 34 | * Short description of class tao_actions_form_CreateInstance |
| 35 | * |
| 36 | * @access public |
| 37 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 38 | * @package tao |
| 39 | |
| 40 | */ |
| 41 | class tao_actions_form_CreateInstance extends tao_helpers_form_FormContainer |
| 42 | { |
| 43 | public const EXCLUDED_PROPERTIES = 'excludedProperties'; |
| 44 | // --- ASSOCIATIONS --- |
| 45 | |
| 46 | |
| 47 | // --- ATTRIBUTES --- |
| 48 | |
| 49 | /** |
| 50 | * Short description of attribute classes |
| 51 | * |
| 52 | * @access private |
| 53 | * @var core_kernel_classes_Class[] |
| 54 | */ |
| 55 | private $classes; |
| 56 | |
| 57 | // --- OPERATIONS --- |
| 58 | |
| 59 | /** |
| 60 | * Short description of method __construct |
| 61 | * |
| 62 | * @access public |
| 63 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 64 | * @param array classes |
| 65 | * @param array options |
| 66 | */ |
| 67 | public function __construct(array $classes, $options) |
| 68 | { |
| 69 | $this->classes = $classes; |
| 70 | |
| 71 | parent::__construct([], $options); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Short description of method initForm |
| 76 | * |
| 77 | * @access public |
| 78 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 79 | */ |
| 80 | public function initForm() |
| 81 | { |
| 82 | |
| 83 | $name = isset($this->options['name']) ? $this->options['name'] : 'form_' . (count(self::$forms) + 1); |
| 84 | unset($this->options['name']); |
| 85 | |
| 86 | $this->form = tao_helpers_form_FormFactory::getForm($name, $this->options); |
| 87 | |
| 88 | //add create action in toolbar |
| 89 | $action = tao_helpers_form_FormFactory::getElement('save', 'Free'); |
| 90 | $value = '<a href="#" class="form-submitter btn-success small"><span class="icon-save"></span> ' |
| 91 | . __('Create') . '</a>'; |
| 92 | |
| 93 | $action->setValue($value); |
| 94 | |
| 95 | $this->form->setActions([$action], 'top'); |
| 96 | $this->form->setActions([$action], 'bottom'); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Short description of method initElements |
| 101 | * |
| 102 | * @access public |
| 103 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 104 | */ |
| 105 | public function initElements() |
| 106 | { |
| 107 | |
| 108 | $guiOrderProperty = new core_kernel_classes_Property(TaoOntology::PROPERTY_GUI_ORDER); |
| 109 | |
| 110 | //get the list of properties to set in the form |
| 111 | $defaultProperties = tao_helpers_form_GenerisFormFactory::getDefaultProperties(); |
| 112 | $editedProperties = $defaultProperties; |
| 113 | $excludedProperties = (isset($this->options[self::EXCLUDED_PROPERTIES]) |
| 114 | && is_array($this->options[self::EXCLUDED_PROPERTIES])) |
| 115 | ? $this->options[self::EXCLUDED_PROPERTIES] |
| 116 | : []; |
| 117 | $additionalProperties = (isset($this->options['additionalProperties']) |
| 118 | && is_array($this->options['additionalProperties'])) |
| 119 | ? $this->options['additionalProperties'] |
| 120 | : []; |
| 121 | $finalElements = []; |
| 122 | |
| 123 | $classProperties = []; |
| 124 | foreach ($this->classes as $class) { |
| 125 | $classProperties = array_merge(tao_helpers_form_GenerisFormFactory::getClassProperties($class)); |
| 126 | } |
| 127 | if (!empty($additionalProperties)) { |
| 128 | $classProperties = array_merge($classProperties, $additionalProperties); |
| 129 | } |
| 130 | |
| 131 | foreach ($classProperties as $property) { |
| 132 | if ( |
| 133 | !isset($editedProperties[$property->getUri()]) |
| 134 | && !in_array($property->getUri(), $excludedProperties, true) |
| 135 | ) { |
| 136 | $editedProperties[$property->getUri()] = $property; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | foreach ($editedProperties as $property) { |
| 141 | $property->feed(); |
| 142 | $widget = $property->getWidget(); |
| 143 | if ($widget === null || $widget instanceof core_kernel_classes_Literal) { |
| 144 | continue; |
| 145 | } |
| 146 | |
| 147 | if ($widget instanceof core_kernel_classes_Resource && $widget->getUri() === TreeView::WIDGET_ID) { |
| 148 | continue; |
| 149 | } |
| 150 | |
| 151 | //map properties widgets to form elements |
| 152 | $element = tao_helpers_form_GenerisFormFactory::elementMap($property); |
| 153 | |
| 154 | if (!is_null($element)) { |
| 155 | //set label validator |
| 156 | if ($property->getUri() === OntologyRdfs::RDFS_LABEL) { |
| 157 | $element->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty')); |
| 158 | } |
| 159 | |
| 160 | // don't show empty labels |
| 161 | if ($element instanceof tao_helpers_form_elements_Label && strlen($element->getRawValue()) == 0) { |
| 162 | continue; |
| 163 | } |
| 164 | |
| 165 | if ($property->getUri() == OntologyRdfs::RDFS_LABEL) { |
| 166 | // Label will not be a TAO Property. However, it should |
| 167 | // be always first. |
| 168 | array_splice($finalElements, 0, 0, [[$element, 1]]); |
| 169 | } elseif (count($guiOrderPropertyValues = $property->getPropertyValues($guiOrderProperty))) { |
| 170 | // get position of this property if it has one. |
| 171 | $position = intval($guiOrderPropertyValues[0]); |
| 172 | |
| 173 | // insert the element at the right place. |
| 174 | $i = 0; |
| 175 | while ( |
| 176 | $i < count($finalElements) |
| 177 | && ($position >= $finalElements[$i][1] && $finalElements[$i][1] !== null) |
| 178 | ) { |
| 179 | $i++; |
| 180 | } |
| 181 | |
| 182 | array_splice($finalElements, $i, 0, [[$element, $position]]); |
| 183 | } else { |
| 184 | // Unordered properties will go at the end of the form. |
| 185 | $finalElements[] = [$element, null]; |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // Add elements related to class properties to the form. |
| 191 | foreach ($finalElements as $element) { |
| 192 | $this->form->addElement($element[0]); |
| 193 | } |
| 194 | |
| 195 | // @todo currently tao cannot handle multiple classes |
| 196 | /* |
| 197 | $classUriElt = tao_helpers_form_FormFactory::getElement('classes', 'Hidden'); |
| 198 | $uris = array(); |
| 199 | foreach ($this->classes as $class) { |
| 200 | $uris[] = $class->getUri(); |
| 201 | } |
| 202 | $classUriElt->setValue($uris); |
| 203 | */ |
| 204 | |
| 205 | //add an hidden elt for the class uri |
| 206 | $classUriElt = tao_helpers_form_FormFactory::getElement('classUri', 'Hidden'); |
| 207 | $classUriElt->setValue(tao_helpers_Uri::encode($class->getUri())); |
| 208 | $this->form->addElement($classUriElt); |
| 209 | |
| 210 | $this->form->addElement($classUriElt); |
| 211 | |
| 212 | $this->addSignature(); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * @throws \common_Exception |
| 217 | */ |
| 218 | protected function addSignature() |
| 219 | { |
| 220 | $signature = tao_helpers_form_FormFactory::getElement('signature', 'Hidden'); |
| 221 | |
| 222 | $signature->setValue($this->getSignature()); |
| 223 | $signature->addValidator( |
| 224 | new ResourceSignatureValidator( |
| 225 | new SignatureValidator(), |
| 226 | $this->getDataToSign() |
| 227 | ) |
| 228 | ); |
| 229 | |
| 230 | $this->form->addElement($signature, true); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * @return string |
| 235 | * @throws \oat\tao\model\metadata\exception\InconsistencyConfigException |
| 236 | */ |
| 237 | protected function getSignature() |
| 238 | { |
| 239 | /** @var SignatureGenerator $signatureGenerator */ |
| 240 | $signatureGenerator = ServiceManager::getServiceManager()->get(SignatureGenerator::class); |
| 241 | |
| 242 | return $signatureGenerator->generate($this->getDataToSign()); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * @return string |
| 247 | */ |
| 248 | protected function getDataToSign() |
| 249 | { |
| 250 | $uris = []; |
| 251 | |
| 252 | foreach ($this->classes as $class) { |
| 253 | $uris[] = $class->getUri(); |
| 254 | } |
| 255 | |
| 256 | return implode('', $uris); |
| 257 | } |
| 258 | } |