Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 98 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| tao_models_classes_import_CSVMappingForm | |
0.00% |
0 / 98 |
|
0.00% |
0 / 3 |
506 | |
0.00% |
0 / 1 |
| initForm | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| initElements | |
0.00% |
0 / 89 |
|
0.00% |
0 / 1 |
420 | |||
| prepareString | |
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\generis\model\OntologyRdfs; |
| 26 | |
| 27 | /** |
| 28 | * This container initialize the form used to map class properties to data to be |
| 29 | * |
| 30 | * @access public |
| 31 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 32 | * @package tao |
| 33 | |
| 34 | */ |
| 35 | class tao_models_classes_import_CSVMappingForm extends tao_helpers_form_FormContainer |
| 36 | { |
| 37 | // --- ASSOCIATIONS --- |
| 38 | |
| 39 | |
| 40 | // --- ATTRIBUTES --- |
| 41 | |
| 42 | // --- OPERATIONS --- |
| 43 | /** |
| 44 | * Suffix to append to the default values of the properties |
| 45 | * @var string |
| 46 | */ |
| 47 | public const DEFAULT_VALUES_SUFFIX = '-taocsvdef'; |
| 48 | |
| 49 | |
| 50 | /** |
| 51 | * Short description of method initForm |
| 52 | * |
| 53 | * @access public |
| 54 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 55 | * @return mixed |
| 56 | */ |
| 57 | public function initForm() |
| 58 | { |
| 59 | |
| 60 | |
| 61 | $this->form = tao_helpers_form_FormFactory::getForm('mapping'); |
| 62 | |
| 63 | $importElt = tao_helpers_form_FormFactory::getElement('import', 'Free'); |
| 64 | $importElt->setValue( |
| 65 | '<a href="#" class="form-submitter btn-success small"><span class="icon-import"></span> ' |
| 66 | . __('Import') . '</a>' |
| 67 | ); |
| 68 | $this->form->setActions([$importElt], 'bottom'); |
| 69 | $this->form->setActions([], 'top'); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Short description of method initElements |
| 74 | * |
| 75 | * @access public |
| 76 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 77 | * @throws Exception |
| 78 | * @throws common_Exception |
| 79 | * @return mixed |
| 80 | */ |
| 81 | public function initElements() |
| 82 | { |
| 83 | |
| 84 | if (!isset($this->options['class_properties'])) { |
| 85 | throw new Exception('No class properties found'); |
| 86 | } |
| 87 | if (!isset($this->options['csv_column'])) { |
| 88 | throw new Exception('No csv columns found'); |
| 89 | } |
| 90 | |
| 91 | $columnsOptions = []; |
| 92 | $columnsOptionsLabels = []; |
| 93 | $columnsOptionsLiteral = []; |
| 94 | $columnsOptionsLiteral['csv_select'] = ' --- ' . __('Select') . ' --- '; |
| 95 | $columnsOptionsLiteral['csv_null'] = ' --- ' . __("Don't set"); |
| 96 | |
| 97 | $columnsOptionsRanged = []; |
| 98 | $columnsOptionsRanged['csv_select'] = ' --- ' . __('Select') . ' --- '; |
| 99 | $columnsOptionsRanged['csv_null'] = ' --- ' . __('Use default value'); |
| 100 | |
| 101 | // We build the list of CSV columns that can be mapped to |
| 102 | // the target class properties. |
| 103 | if ( |
| 104 | isset($this->options[tao_helpers_data_CsvFile::FIRST_ROW_COLUMN_NAMES]) |
| 105 | && $this->options[tao_helpers_data_CsvFile::FIRST_ROW_COLUMN_NAMES] |
| 106 | ) { |
| 107 | foreach ($this->options['csv_column'] as $i => $column) { |
| 108 | $columnsOptions[$i . tao_models_classes_import_CsvImporter::OPTION_POSTFIX] = __('Column') . ' ' |
| 109 | . ($i + 1) . ' : ' . $column; |
| 110 | $columnsOptionsLabels[$i . tao_models_classes_import_CsvImporter::OPTION_POSTFIX] = $this |
| 111 | ->prepareString($column); |
| 112 | } |
| 113 | } else { |
| 114 | // We do not know column so we display more neutral information |
| 115 | // about columns to the end user. |
| 116 | for ($i = 0; $i < count($this->options['csv_column']); $i++) { |
| 117 | $columnsOptions[$i . tao_models_classes_import_CsvImporter::OPTION_POSTFIX] = __('Column') . ' ' |
| 118 | . ($i + 1); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | foreach ($this->options['class_properties'] as $propertyUri => $propertyLabel) { |
| 123 | $propElt = tao_helpers_form_FormFactory::getElement($propertyUri, 'Combobox'); |
| 124 | $propElt->setDescription($propertyLabel); |
| 125 | |
| 126 | // literal or ranged? |
| 127 | $options = array_key_exists($propertyUri, $this->options['ranged_properties']) |
| 128 | ? array_merge($columnsOptionsRanged, $columnsOptions) |
| 129 | : array_merge($columnsOptionsLiteral, $columnsOptions); |
| 130 | |
| 131 | $value = 'csv_select'; |
| 132 | if (isset($_POST[$propertyUri]) && isset($options[$_POST[$propertyUri]])) { |
| 133 | $value = $_POST[$propertyUri]; |
| 134 | } |
| 135 | |
| 136 | // We trying compare current label with option labels from file and set most suitable |
| 137 | $label = $this->prepareString($propertyLabel); |
| 138 | $value = key(preg_grep("/^$label$/", $columnsOptionsLabels)) ?: $value; |
| 139 | |
| 140 | $propElt->setOptions($options); |
| 141 | $propElt->setValue($value); |
| 142 | |
| 143 | /** Add mandatory label */ |
| 144 | if (tao_helpers_Uri::decode($propertyUri) == OntologyRdfs::RDFS_LABEL) { |
| 145 | $elementEqualsToCsvSelect = new tao_helpers_form_elements_xhtml_Hidden(); |
| 146 | $elementEqualsToCsvSelect->setValue('csv_select'); |
| 147 | $elementEqualsToCsvSelect->setDescription(' to null'); |
| 148 | $propElt->addValidator(tao_helpers_form_FormFactory::getValidator( |
| 149 | 'Equals', |
| 150 | [ |
| 151 | 'reference' => $elementEqualsToCsvSelect, |
| 152 | 'invert' => true |
| 153 | ] |
| 154 | )); |
| 155 | } |
| 156 | |
| 157 | $this->form->addElement($propElt); |
| 158 | } |
| 159 | if (count($this->options['class_properties']) > 0) { |
| 160 | $this->form->createGroup( |
| 161 | 'property_mapping', |
| 162 | __('Map the properties to the CSV columns'), |
| 163 | array_keys($this->options['class_properties']) |
| 164 | ); |
| 165 | } |
| 166 | $ranged = []; |
| 167 | foreach ($this->options['ranged_properties'] as $propertyUri => $propertyLabel) { |
| 168 | $property = new core_kernel_classes_Property(tao_helpers_Uri::decode($propertyUri)); |
| 169 | $propElt = tao_helpers_form_GenerisFormFactory::elementMap($property); |
| 170 | if (!is_null($propElt)) { |
| 171 | $defName = tao_helpers_Uri::encode($property->getUri()) . self::DEFAULT_VALUES_SUFFIX; |
| 172 | $propElt->setName($defName); |
| 173 | if ($this->form->getElement($propertyUri)->getRawValue() == 'csv_null') { |
| 174 | $propElt->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty')); |
| 175 | } |
| 176 | $this->form->addElement($propElt); |
| 177 | $ranged[$defName] = $propElt; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | if (count($this->options['ranged_properties']) > 0) { |
| 182 | $this->form->createGroup('ranged_property', __('Define the default values'), array_keys($ranged)); |
| 183 | } |
| 184 | |
| 185 | $importFileEle = tao_helpers_form_FormFactory::getElement('importFile', 'Hidden'); |
| 186 | $this->form->addElement($importFileEle); |
| 187 | |
| 188 | $optDelimiter = tao_helpers_form_FormFactory::getElement(tao_helpers_data_CsvFile::FIELD_DELIMITER, 'Hidden'); |
| 189 | $this->form->addElement($optDelimiter); |
| 190 | |
| 191 | $optEncloser = tao_helpers_form_FormFactory::getElement(tao_helpers_data_CsvFile::FIELD_ENCLOSER, 'Hidden'); |
| 192 | $this->form->addElement($optEncloser); |
| 193 | |
| 194 | $optMulti = tao_helpers_form_FormFactory::getElement( |
| 195 | tao_helpers_data_CsvFile::MULTI_VALUES_DELIMITER, |
| 196 | 'Hidden' |
| 197 | ); |
| 198 | $this->form->addElement($optMulti); |
| 199 | |
| 200 | if (isset($this->options[tao_models_classes_import_CsvUploadForm::IS_OPTION_FIRST_COLUMN_ENABLE])) { |
| 201 | if ($this->options[tao_models_classes_import_CsvUploadForm::IS_OPTION_FIRST_COLUMN_ENABLE] === true) { |
| 202 | $optFirstColumn = tao_helpers_form_FormFactory::getElement( |
| 203 | tao_helpers_data_CsvFile::FIRST_ROW_COLUMN_NAMES, |
| 204 | 'Hidden' |
| 205 | ); |
| 206 | $this->form->addElement($optFirstColumn); |
| 207 | } |
| 208 | } else { |
| 209 | $optFirstColumn = tao_helpers_form_FormFactory::getElement( |
| 210 | tao_helpers_data_CsvFile::FIRST_ROW_COLUMN_NAMES, |
| 211 | 'Hidden' |
| 212 | ); |
| 213 | $this->form->addElement($optFirstColumn); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | protected function prepareString($value) |
| 218 | { |
| 219 | return mb_strtolower(preg_replace('/[\s\t\r\n-]/', '', $value)); |
| 220 | } |
| 221 | } |