Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.59% |
50 / 54 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| MetadataImportForm | |
92.59% |
50 / 54 |
|
60.00% |
3 / 5 |
6.01 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| withRequestData | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| initForm | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| initElements | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| getFileUploadElement | |
93.55% |
29 / 31 |
|
0.00% |
0 / 1 |
2.00 | |||
| 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) 2021 (original work) Open Assessment Technologies SA. |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\import\Form; |
| 24 | |
| 25 | use oat\generis\Helper\SystemHelper; |
| 26 | use tao_helpers_form_FormElement; |
| 27 | use tao_helpers_form_FormFactory; |
| 28 | use tao_helpers_form_xhtml_Form; |
| 29 | use tao_models_classes_import_CsvUploadForm; |
| 30 | |
| 31 | class MetadataImportForm extends tao_models_classes_import_CsvUploadForm |
| 32 | { |
| 33 | /** @var array */ |
| 34 | private $requestData = []; |
| 35 | |
| 36 | /** @var tao_helpers_form_FormElement */ |
| 37 | private $fileUploadElement; |
| 38 | |
| 39 | /** @var tao_helpers_form_FormElement */ |
| 40 | private $hiddenImportElement; |
| 41 | |
| 42 | /** @var tao_helpers_form_FormElement */ |
| 43 | private $submitElement; |
| 44 | |
| 45 | public function __construct( |
| 46 | array $data = [], |
| 47 | array $options = [], |
| 48 | tao_helpers_form_FormElement $fileUploadElement = null, |
| 49 | tao_helpers_form_FormElement $hiddenImportElement = null, |
| 50 | tao_helpers_form_FormElement $submitElement = null |
| 51 | ) { |
| 52 | $this->fileUploadElement = $fileUploadElement; |
| 53 | $this->hiddenImportElement = $hiddenImportElement; |
| 54 | $this->submitElement = $submitElement; |
| 55 | $options[tao_models_classes_import_CsvUploadForm::IS_OPTION_FIRST_COLUMN_ENABLE] = false; |
| 56 | |
| 57 | parent::__construct($data, $options); |
| 58 | } |
| 59 | |
| 60 | public function withRequestData(array $requestData): self |
| 61 | { |
| 62 | $this->requestData = $requestData; |
| 63 | |
| 64 | return $this; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @inheritdoc |
| 69 | */ |
| 70 | public function initForm() |
| 71 | { |
| 72 | $this->form = new tao_helpers_form_xhtml_Form('export'); |
| 73 | $submitElt = $this->submitElement ?? tao_helpers_form_FormFactory::getElement('import', 'Free'); |
| 74 | $submitElt->setValue( |
| 75 | '<a href="#" class="form-submitter btn-success small"><span class="icon-import"></span> ' . |
| 76 | __('Import') . |
| 77 | '</a>' |
| 78 | ); |
| 79 | |
| 80 | $this->form->setActions([$submitElt], 'bottom'); |
| 81 | $this->form->setActions([], 'top'); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @inheritdoc |
| 86 | */ |
| 87 | public function initElements() |
| 88 | { |
| 89 | $this->form->addElement($this->getFileUploadElement()); |
| 90 | |
| 91 | $csvSentElt = $this->hiddenImportElement ?? tao_helpers_form_FormFactory::getElement( |
| 92 | 'import_sent_csv', |
| 93 | 'Hidden' |
| 94 | ); |
| 95 | $csvSentElt->setValue(1); |
| 96 | |
| 97 | $this->form->addElement($csvSentElt); |
| 98 | } |
| 99 | |
| 100 | private function getFileUploadElement(): tao_helpers_form_FormElement |
| 101 | { |
| 102 | $element = $this->fileUploadElement ?? tao_helpers_form_FormFactory::getElement('source', 'AsyncFile'); |
| 103 | $element->addValidators( |
| 104 | [ |
| 105 | tao_helpers_form_FormFactory::getValidator( |
| 106 | 'FileMimeType', |
| 107 | [ |
| 108 | 'mimetype' => [ |
| 109 | 'text/plain', |
| 110 | 'text/csv', |
| 111 | 'text/comma-separated-values', |
| 112 | 'application/csv', |
| 113 | 'application/csv-tab-delimited-table', |
| 114 | 'application/vnd.ms-excel', |
| 115 | 'application/vnd.msexcel', |
| 116 | ], |
| 117 | 'extension' => ['csv'] |
| 118 | ] |
| 119 | ), |
| 120 | tao_helpers_form_FormFactory::getValidator( |
| 121 | 'FileSize', |
| 122 | [ |
| 123 | 'max' => SystemHelper::getFileUploadLimit() |
| 124 | ] |
| 125 | ) |
| 126 | ] |
| 127 | ); |
| 128 | |
| 129 | if (isset($this->requestData['import_sent_csv'])) { |
| 130 | $element->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty')); |
| 131 | |
| 132 | return $element; |
| 133 | } |
| 134 | |
| 135 | $element->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty', ['message' => ''])); |
| 136 | |
| 137 | return $element; |
| 138 | } |
| 139 | } |