Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 71 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| tao_models_classes_import_CsvUploadForm | |
0.00% |
0 / 71 |
|
0.00% |
0 / 3 |
56 | |
0.00% |
0 / 1 |
| initForm | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| initElements | |
0.00% |
0 / 54 |
|
0.00% |
0 / 1 |
30 | |||
| addFirstColumnElement | |
0.00% |
0 / 9 |
|
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\Helper\SystemHelper; |
| 26 | |
| 27 | /** |
| 28 | * Export form for QTI packages |
| 29 | * |
| 30 | * @access public |
| 31 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 32 | */ |
| 33 | class tao_models_classes_import_CsvUploadForm extends tao_helpers_form_FormContainer |
| 34 | { |
| 35 | public const IS_OPTION_FIRST_COLUMN_ENABLE = 'enable_option_first_columns'; |
| 36 | |
| 37 | // --- ASSOCIATIONS --- |
| 38 | |
| 39 | |
| 40 | // --- ATTRIBUTES --- |
| 41 | |
| 42 | // --- OPERATIONS --- |
| 43 | /** |
| 44 | * Short description of method initForm |
| 45 | * |
| 46 | * @access public |
| 47 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 48 | * @return mixed |
| 49 | */ |
| 50 | public function initForm() |
| 51 | { |
| 52 | $this->form = new tao_helpers_form_xhtml_Form('export'); |
| 53 | $submitElt = tao_helpers_form_FormFactory::getElement('import', 'Free'); |
| 54 | $submitElt->setValue( |
| 55 | "<input type='button' class='btn-success small form-refresher' value='" |
| 56 | . __('Next') . "' />" |
| 57 | ); |
| 58 | |
| 59 | $this->form->setActions([$submitElt], 'bottom'); |
| 60 | $this->form->setActions([], 'top'); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * overriden |
| 65 | * |
| 66 | * @access public |
| 67 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 68 | * @return mixed |
| 69 | */ |
| 70 | public function initElements() |
| 71 | { |
| 72 | //create file upload form box |
| 73 | $fileElt = tao_helpers_form_FormFactory::getElement('source', 'AsyncFile'); |
| 74 | $fileElt->setDescription(__("Add a CSV file")); |
| 75 | if (isset($_POST['import_sent_csv'])) { |
| 76 | $fileElt->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty')); |
| 77 | } else { |
| 78 | $fileElt->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty', ['message' => ''])); |
| 79 | } |
| 80 | $fileElt->addValidators([ |
| 81 | tao_helpers_form_FormFactory::getValidator( |
| 82 | 'FileMimeType', |
| 83 | [ |
| 84 | 'mimetype' => [ |
| 85 | 'text/plain', |
| 86 | 'text/csv', |
| 87 | 'text/comma-separated-values', |
| 88 | 'text/anytext', |
| 89 | 'application/csv', |
| 90 | 'application/txt', |
| 91 | 'application/csv-tab-delimited-table', |
| 92 | 'application/excel', |
| 93 | 'application/vnd.ms-excel', |
| 94 | 'application/vnd.msexcel', |
| 95 | ], |
| 96 | 'extension' => ['csv', 'txt'] |
| 97 | ] |
| 98 | ), |
| 99 | tao_helpers_form_FormFactory::getValidator( |
| 100 | 'FileSize', |
| 101 | ['max' => SystemHelper::getFileUploadLimit()] |
| 102 | ) |
| 103 | ]); |
| 104 | |
| 105 | $this->form->addElement($fileElt); |
| 106 | $this->form->createGroup('file', __('Import Metadata from CSV file'), ['source']); |
| 107 | |
| 108 | $csvSentElt = tao_helpers_form_FormFactory::getElement('import_sent_csv', 'Hidden'); |
| 109 | $csvSentElt->setValue(1); |
| 110 | $this->form->addElement($csvSentElt); |
| 111 | |
| 112 | // options |
| 113 | $optDelimiter = tao_helpers_form_FormFactory::getElement(tao_helpers_data_CsvFile::FIELD_DELIMITER, 'Textbox'); |
| 114 | $optDelimiter->setDescription(__("Field delimiter")); |
| 115 | $optDelimiter->setValue(';'); |
| 116 | $optDelimiter->addAttribute("size", 6); |
| 117 | $optDelimiter->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty')); |
| 118 | $this->form->addElement($optDelimiter); |
| 119 | |
| 120 | $optEncloser = tao_helpers_form_FormFactory::getElement(tao_helpers_data_CsvFile::FIELD_ENCLOSER, 'Textbox'); |
| 121 | $optEncloser->setDescription(__("Field encloser")); |
| 122 | $optEncloser->setValue('"'); |
| 123 | $optEncloser->addAttribute("size", 6); |
| 124 | $optEncloser->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty')); |
| 125 | $this->form->addElement($optEncloser); |
| 126 | |
| 127 | if (isset($this->options[static::IS_OPTION_FIRST_COLUMN_ENABLE])) { |
| 128 | if ($this->options[static::IS_OPTION_FIRST_COLUMN_ENABLE] === true) { |
| 129 | $optFirstColumn = $this->addFirstColumnElement(); |
| 130 | } |
| 131 | } else { |
| 132 | //backwards compatible |
| 133 | $optFirstColumn = $this->addFirstColumnElement(); |
| 134 | } |
| 135 | |
| 136 | $opts = [$optDelimiter, $optEncloser]; |
| 137 | |
| 138 | if (isset($optFirstColumn)) { |
| 139 | $opts[] = $optFirstColumn; |
| 140 | } |
| 141 | |
| 142 | $this->form->createGroup('options', __('CSV Options'), $opts); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @return tao_helpers_form_FormElement |
| 147 | * @throws Exception |
| 148 | * @throws common_Exception |
| 149 | */ |
| 150 | protected function addFirstColumnElement() |
| 151 | { |
| 152 | $optFirstColumn = tao_helpers_form_FormFactory::getElement( |
| 153 | tao_helpers_data_CsvFile::FIRST_ROW_COLUMN_NAMES, |
| 154 | 'Checkbox' |
| 155 | ); |
| 156 | $optFirstColumn->setDescription(__("First row column names")); |
| 157 | $optFirstColumn->setOptions([tao_helpers_data_CsvFile::FIRST_ROW_COLUMN_NAMES => '']); |
| 158 | $optFirstColumn->setValue(tao_helpers_data_CsvFile::FIRST_ROW_COLUMN_NAMES); |
| 159 | $this->form->addElement($optFirstColumn); |
| 160 | |
| 161 | return $optFirstColumn; |
| 162 | } |
| 163 | } |