Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
| CsvAbstractImporter | |
0.00% |
0 / 26 |
|
0.00% |
0 / 9 |
272 | |
0.00% |
0 / 1 |
| getExludedProperties | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getStaticData | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getAdditionAdapterOptions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getClassProperties | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getColumnMapping | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
20 | |||
| getValidators | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setValidators | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getStaticMap | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| importFile | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
30 | |||
| 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) 2015 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\tao\model\import; |
| 23 | |
| 24 | use oat\generis\model\GenerisRdf; |
| 25 | |
| 26 | /** |
| 27 | * Abstract class the describe a csv import |
| 28 | * |
| 29 | * @access public |
| 30 | * @author Antoine Robin <antoine.robin@vesperiagroup.com |
| 31 | * @package tao |
| 32 | */ |
| 33 | abstract class CsvAbstractImporter |
| 34 | { |
| 35 | protected $validators = []; |
| 36 | |
| 37 | |
| 38 | /** |
| 39 | * Returns an array of the Uris of the properties |
| 40 | * that should not be importable via CVS |
| 41 | * |
| 42 | * Can be overriden by CsvImporters that are adapted to |
| 43 | * import resources of a specific class |
| 44 | * |
| 45 | * @return array |
| 46 | */ |
| 47 | protected function getExludedProperties() |
| 48 | { |
| 49 | return []; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Returns an key => value array of properties |
| 54 | * to be set on the new resources |
| 55 | * |
| 56 | * Can be overriden by CsvImporters that are adapted to |
| 57 | * import resources of a specific class |
| 58 | * |
| 59 | * @return array |
| 60 | */ |
| 61 | protected function getStaticData() |
| 62 | { |
| 63 | return []; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns aditional options to be set to the |
| 68 | * GenericAdapterCsv |
| 69 | * |
| 70 | * Can be overriden by CsvImporters that are adapted to |
| 71 | * import resources of a specific class |
| 72 | * |
| 73 | * @see tao_helpers_data_GenerisAdapterCsv |
| 74 | * @return array |
| 75 | */ |
| 76 | protected function getAdditionAdapterOptions() |
| 77 | { |
| 78 | return []; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @return array |
| 83 | */ |
| 84 | protected function getClassProperties($clazz) |
| 85 | { |
| 86 | $topLevelClass = new \core_kernel_classes_Class(GenerisRdf::CLASS_GENERIS_RESOURCE); |
| 87 | $classProperties = \tao_models_classes_TaoService::singleton()->getClazzProperties($clazz, $topLevelClass); |
| 88 | |
| 89 | return $classProperties; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @param \tao_helpers_data_CsvFile $csv_data |
| 94 | * @param boolean $firstRowAsColumnName |
| 95 | * @return array |
| 96 | */ |
| 97 | protected function getColumnMapping($csv_data, $firstRowAsColumnName = true) |
| 98 | { |
| 99 | //build the mapping form |
| 100 | if (!$csv_data->count()) { |
| 101 | return []; |
| 102 | } |
| 103 | |
| 104 | // 'class properties' contains an associative array(str:'propertyUri' => 'str:propertyLabel') describing |
| 105 | // properties belonging to the target class. |
| 106 | // 'ranged properties' contains an associative array(str:'propertyUri' => 'str:propertyLabel') describing |
| 107 | // properties belonging to the target class and that have a range. |
| 108 | // 'csv_column' contains an array(int:columnIndex => 'str:columnLabel') that will be used to create the |
| 109 | // selection of possible CSV column to map in views. |
| 110 | // 'csv_column' might have NULL values for 'str:columnLabel' meaning that there was no header row with column |
| 111 | // names in the CSV file. |
| 112 | |
| 113 | // Format the column mapping option for the form. |
| 114 | if ($firstRowAsColumnName && null != $csv_data->getColumnMapping()) { |
| 115 | // set the column label for each entry. |
| 116 | // $csvColMapping = array('label', 'comment', ...) |
| 117 | |
| 118 | return $csv_data->getColumnMapping(); |
| 119 | } else { |
| 120 | // set an empty value for each entry of the array |
| 121 | // to describe that column names are unknown. |
| 122 | // $csvColMapping = array(null, null, ...) |
| 123 | |
| 124 | return array_fill(0, $csv_data->getColumnCount(), null); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @return array |
| 130 | */ |
| 131 | public function getValidators() |
| 132 | { |
| 133 | return $this->validators; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @param array $validators |
| 138 | */ |
| 139 | public function setValidators($validators) |
| 140 | { |
| 141 | $this->validators = $validators; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | /** |
| 146 | * Additional mapping values but that comes from another source than the CSV file. |
| 147 | * It enables you to define a mapping that will to work along with the CSV mapping. |
| 148 | * |
| 149 | * @return the mapping in the same form than the staticData (uri -> val/uri) |
| 150 | */ |
| 151 | public function getStaticMap() |
| 152 | { |
| 153 | return []; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | /** |
| 158 | * @param \core_kernel_classes_Class $class where data will be imported |
| 159 | * @param array $options contains parameters under key => value format |
| 160 | * file => required |
| 161 | * map => required |
| 162 | * callbacks => optional |
| 163 | * field_delimiter => optional |
| 164 | * field_encloser => optional |
| 165 | * first_row_column_names => optional |
| 166 | * multi_values_delimiter => optional |
| 167 | * onResourceImported => optional |
| 168 | * staticMap => optional |
| 169 | * @return \common_report_Report |
| 170 | */ |
| 171 | public function importFile($class, $options) |
| 172 | { |
| 173 | if (!isset($options['file'])) { |
| 174 | throw new \BadFunctionCallException("Import file is missing"); |
| 175 | } |
| 176 | |
| 177 | if (!isset($options['staticMap']) || !is_array($options['staticMap'])) { |
| 178 | $options['staticMap'] = $this->getStaticData(); |
| 179 | } else { |
| 180 | $options['staticMap'] = array_merge($options['staticMap'], $this->getStaticData()); |
| 181 | } |
| 182 | $options = array_merge($options, $this->getAdditionAdapterOptions()); |
| 183 | |
| 184 | //import the file |
| 185 | $adapter = new \tao_helpers_data_GenerisAdapterCsv($options); |
| 186 | $adapter->setValidators($this->getValidators()); |
| 187 | $report = $adapter->import($options['file'], $class); |
| 188 | |
| 189 | |
| 190 | if ($report->getType() == \common_report_Report::TYPE_SUCCESS) { |
| 191 | $report->setData($adapter->getOptions()); |
| 192 | } |
| 193 | return $report; |
| 194 | } |
| 195 | } |