Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 82 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
tao_models_classes_import_CsvImporter | |
0.00% |
0 / 82 |
|
0.00% |
0 / 6 |
552 | |
0.00% |
0 / 1 |
getLabel | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getForm | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
createImportFormContainer | |
0.00% |
0 / 42 |
|
0.00% |
0 / 1 |
72 | |||
import | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
90 | |||
getTaskParameters | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
getServiceManager | |
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) 2013-2018 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
19 | * |
20 | */ |
21 | |
22 | use oat\generis\model\OntologyRdfs; |
23 | use oat\oatbox\event\EventManagerAwareTrait; |
24 | use oat\oatbox\service\ServiceManager; |
25 | use oat\tao\model\event\CsvImportEvent; |
26 | use oat\tao\model\import\ImportHandlerHelperTrait; |
27 | use oat\tao\model\import\TaskParameterProviderInterface; |
28 | use oat\tao\model\upload\UploadService; |
29 | use oat\tao\model\import\CsvAbstractImporter; |
30 | use Zend\ServiceManager\ServiceLocatorAwareInterface; |
31 | |
32 | /** |
33 | * Basic import of csv files |
34 | * |
35 | * @access public |
36 | * @author Joel Bout, <joel@taotesting.com> |
37 | * @package tao |
38 | */ |
39 | class tao_models_classes_import_CsvImporter extends CsvAbstractImporter implements |
40 | tao_models_classes_import_ImportHandler, |
41 | ServiceLocatorAwareInterface, |
42 | TaskParameterProviderInterface |
43 | { |
44 | use EventManagerAwareTrait; |
45 | use ImportHandlerHelperTrait { |
46 | getTaskParameters as getDefaultTaskParameters; |
47 | } |
48 | |
49 | public const OPTION_POSTFIX = '_O'; |
50 | |
51 | /** |
52 | * (non-PHPdoc) |
53 | * @see tao_models_classes_import_ImportHandler::getLabel() |
54 | */ |
55 | public function getLabel() |
56 | { |
57 | return __('CSV'); |
58 | } |
59 | |
60 | /** |
61 | * (non-PHPdoc) |
62 | * @see tao_models_classes_import_ImportHandler::getForm() |
63 | */ |
64 | public function getForm() |
65 | { |
66 | $form = empty($_POST['source']) && empty($_POST['importFile']) |
67 | ? new tao_models_classes_import_CsvUploadForm() |
68 | : $this->createImportFormContainer(); |
69 | return $form->getForm(); |
70 | } |
71 | |
72 | /** |
73 | * Constructs the Import form container |
74 | * In need of a major refactoring, which will |
75 | * probably involve refactoring the Form engine as well |
76 | * |
77 | * @return tao_models_classes_import_CSVMappingForm |
78 | * @throws \oat\generis\model\fileReference\FileSerializerException |
79 | * @throws common_exception_NotAcceptable |
80 | */ |
81 | private function createImportFormContainer() |
82 | { |
83 | $sourceContainer = new tao_models_classes_import_CsvUploadForm(); |
84 | $sourceForm = $sourceContainer->getForm(); |
85 | /** @var tao_helpers_form_FormElement $element */ |
86 | foreach ($sourceForm->getElements() as $element) { |
87 | $element->feed(); |
88 | } |
89 | |
90 | $sourceForm->getElement('source')->feed(); |
91 | $fileInfo = $sourceForm->getValue('source'); |
92 | |
93 | if (isset($_POST['importFile'])) { |
94 | $serial = $_POST['importFile']; |
95 | } else { |
96 | $serial = $fileInfo['uploaded_file']; |
97 | } |
98 | |
99 | if (!is_string($serial)) { |
100 | throw new InvalidArgumentException('Import file has to be a valid file serial.'); |
101 | } |
102 | |
103 | /** @var UploadService $uploadService */ |
104 | $uploadService = $this->getServiceManager()->get(UploadService::SERVICE_ID); |
105 | $file = $uploadService->getUploadedFlyFile($serial); |
106 | |
107 | $properties = [tao_helpers_Uri::encode(OntologyRdfs::RDFS_LABEL) => __('Label')]; |
108 | $rangedProperties = []; |
109 | |
110 | $classUri = \tao_helpers_Uri::decode($_POST['classUri']); |
111 | $class = new core_kernel_classes_Class($classUri); |
112 | $classProperties = $this->getClassProperties($class); |
113 | |
114 | foreach ($classProperties as $property) { |
115 | if (!in_array($property->getUri(), $this->getExludedProperties())) { |
116 | //@todo manage the properties with range |
117 | $range = $property->getRange(); |
118 | $properties[tao_helpers_Uri::encode($property->getUri())] = $property->getLabel(); |
119 | |
120 | if ($range instanceof core_kernel_classes_Resource && $range->getUri() != OntologyRdfs::RDFS_LITERAL) { |
121 | $rangedProperties[tao_helpers_Uri::encode($property->getUri())] = $property->getLabel(); |
122 | } |
123 | } |
124 | } |
125 | |
126 | //load the csv data from the file (uploaded in the upload form) to get the columns |
127 | $csv_data = new tao_helpers_data_CsvFile($sourceForm->getValues()); |
128 | $csv_data->load($file); |
129 | |
130 | $values = $sourceForm->getValues(); |
131 | $values[tao_helpers_data_CsvFile::FIRST_ROW_COLUMN_NAMES] = !empty( |
132 | $values[tao_helpers_data_CsvFile::FIRST_ROW_COLUMN_NAMES] |
133 | ); |
134 | $values['importFile'] = $serial; |
135 | $myFormContainer = new tao_models_classes_import_CSVMappingForm($values, [ |
136 | 'class_properties' => $properties, |
137 | 'ranged_properties' => $rangedProperties, |
138 | 'csv_column' => $this->getColumnMapping($csv_data, $sourceForm->getValue( |
139 | tao_helpers_data_CsvFile::FIRST_ROW_COLUMN_NAMES |
140 | )), |
141 | tao_helpers_data_CsvFile::FIRST_ROW_COLUMN_NAMES => $sourceForm->getValue( |
142 | tao_helpers_data_CsvFile::FIRST_ROW_COLUMN_NAMES |
143 | ), |
144 | ]); |
145 | |
146 | return $myFormContainer; |
147 | } |
148 | |
149 | /** |
150 | * (non-PHPdoc) |
151 | * @see tao_models_classes_import_ImportHandler::import() |
152 | * @param core_kernel_classes_Class $class |
153 | * @param tao_helpers_form_Form|array $form |
154 | * @param string|null $userId owner of the resource |
155 | * @return common_report_Report |
156 | * @throws \oat\oatbox\service\ServiceNotFoundException |
157 | * @throws \common_Exception |
158 | */ |
159 | public function import($class, $form, $userId = null) |
160 | { |
161 | // for backward compatibility |
162 | $options = $form instanceof \tao_helpers_form_Form ? $form->getValues() : $form; |
163 | |
164 | $options['file'] = $this->fetchUploadedFile($form); |
165 | |
166 | // Clean "csv_select" values from form view. |
167 | // Transform any "csv_select" in "csv_null" in order to |
168 | // have the same importation behaviour for both because |
169 | // semantics are the same. |
170 | |
171 | // for backward compatibility |
172 | $map = $form instanceof \tao_helpers_form_Form |
173 | ? $form->getValues('property_mapping') |
174 | : $form['property_mapping']; |
175 | $newMap = []; |
176 | |
177 | foreach ($map as $k => $m) { |
178 | if ($m !== 'csv_select') { |
179 | $newMap[$k] = $map[$k]; |
180 | } else { |
181 | $newMap[$k] = 'csv_null'; |
182 | } |
183 | $newMap[$k] = str_replace(self::OPTION_POSTFIX, '', $newMap[$k]); |
184 | common_Logger::d('map: ' . $k . ' => ' . $newMap[$k]); |
185 | } |
186 | $options['map'] = $newMap; |
187 | |
188 | $staticMap = []; |
189 | |
190 | // for backward compatibility |
191 | $rangedProperties = $form instanceof \tao_helpers_form_Form |
192 | ? $form->getValues('ranged_property') |
193 | : $form['ranged_property']; |
194 | |
195 | foreach ($rangedProperties as $propUri => $value) { |
196 | if (strpos($propUri, tao_models_classes_import_CSVMappingForm::DEFAULT_VALUES_SUFFIX) !== false) { |
197 | $cleanUri = str_replace(tao_models_classes_import_CSVMappingForm::DEFAULT_VALUES_SUFFIX, '', $propUri); |
198 | $staticMap[$cleanUri] = $value; |
199 | } |
200 | } |
201 | $options['staticMap'] = array_merge($staticMap, $this->getStaticData()); |
202 | |
203 | $report = parent::importFile($class, $options, $userId); |
204 | |
205 | if ($report->getType() == common_report_Report::TYPE_SUCCESS) { |
206 | $this->getEventManager()->trigger(new CsvImportEvent($report)); |
207 | } |
208 | |
209 | return $report; |
210 | } |
211 | |
212 | /** |
213 | * Defines the task parameters to be stored for later use. |
214 | * |
215 | * @param tao_helpers_form_Form $form |
216 | * @return array |
217 | */ |
218 | public function getTaskParameters(tao_helpers_form_Form $form) |
219 | { |
220 | return array_merge( |
221 | $form->getValues(), |
222 | [ |
223 | 'property_mapping' => $form->getValues('property_mapping'), |
224 | 'ranged_property' => $form->getValues('ranged_property') |
225 | ], |
226 | $this->getDefaultTaskParameters($form) |
227 | ); |
228 | } |
229 | |
230 | /** |
231 | * @return ServiceManager |
232 | */ |
233 | private function getServiceManager() |
234 | { |
235 | return ServiceManager::getServiceManager(); |
236 | } |
237 | } |