ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilDclContentImporter.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22{
23 //const SOAP_FUNCTION_NAME = 'exportDataCollectionContent';
24
25 public const EXPORT_EXCEL = 'xlsx';
26 protected int $max_imports = 100;
28 = [
38 ];
39 protected array $warnings;
43 protected int $ref_id;
47 protected int $table_id;
48
53 protected array $tables;
54
55 protected ilLanguage $lng;
56 protected ilObjUser $user;
57
58 public function __construct(int $ref_id, ?int $table_id = null)
59 {
60 global $DIC;
61
62 $this->ref_id = $ref_id;
63 $this->table_id = $table_id;
64
65 $this->lng = $DIC->language();
66 $this->user = $DIC->user();
67
68 $this->dcl = new ilObjDataCollection($ref_id);
69 $this->tables = ($table_id) ? [$this->dcl->getTableById($table_id)] : $this->dcl->getTables();
70 }
71
77 public function import(string $file, bool $simulate = false): array
78 {
79 $this->warnings = [];
80 $excel = new ilExcel();
81 try {
82 $excel->loadFromFile($file);
83 } catch (Exception) {
84 $this->warnings[] = $this->lng->txt("dcl_file_not_readable");
85 }
86
87 $sheet_count = $excel->getSheetCount();
88 $excel->setActiveSheet(0);
89
90 if ($sheet_count != count($this->tables)) {
91 $this->warnings[] = $this->lng->txt('dcl_file_not_readable');
92 }
93
94 if (count($this->warnings)) {
95 return ['line' => 0, 'warnings' => $this->warnings];
96 }
97
98 $i = 0;
99
100 for ($sheet = 0; $sheet < $sheet_count; $sheet++) {
101 $excel->setActiveSheet($sheet);
102 $table = $this->tables[$sheet];
103
104 // only 31 character-long table-titles are allowed
105 $sheet_title = substr($table->getTitle(), 0, 31);
106 if ($excel->getSheetTitle() != $sheet_title) {
107 $this->warnings[] = $this->lng->txt('dcl_table_title_not_matching');
108 continue;
109 }
110
111 $field_names = [];
112 $sheet_data = $excel->getSheetAsArray();
113
114 foreach ($sheet_data[0] as $column) {
115 $field_names[] = $column;
116 }
117 $fields = $this->getImportFieldsFromTitles($table, $field_names);
118
119 $records_failed = 0;
120 for ($i = 2; $i <= count($sheet_data); $i++) {
121 $record = new ilDclBaseRecordModel();
122 $record->setOwner($this->user->getId());
123 $date_obj = new ilDateTime(time(), IL_CAL_UNIX);
124 $record->setCreateDate($date_obj);
125 $record->setLastUpdate($date_obj);
126 $record->setLastEditBy($this->user->getId());
127 $record->setTableId($table->getId());
128 if (!$simulate) {
129 $record->doCreate();
130 }
131 $fields_failed = 0;
132 foreach ($fields as $col => $field) {
133 try {
134 if ($field->isStandardField()) {
135 $record->setStandardFieldValueFromExcel($excel, $i, $col, $field);
136 } else {
137 $value = $record->getRecordFieldValueFromExcel($excel, $i, $col, $field);
138
139 if (is_array($value) && isset($value['warning'])) {
140 $this->warnings[] = $value['warning'];
141 $value = '';
142 }
143
144 $field->checkValidity($value, $record->getId());
145 if (!$simulate) {
146 $record->setRecordFieldValue($field->getId(), $value);
147 }
148 }
149 } catch (ilDclInputException $e) {
150 $fields_failed++;
151 $this->warnings[] = "(" . $i . ", " . ilDataCollectionImporter::getExcelCharForInteger($col + 1) . ") " . $e;
152 }
153 }
154
155 if ($fields_failed < count($fields)) {
156 $record_imported = true;
157 } else {
158 $records_failed++;
159 $record_imported = false;
160 }
161
162 if (!$simulate) {
163 if (!$record_imported) { // if no fields have been filled, delete the record again
164 $record->doDelete(true); // omit notification
165 } else {
166 $record->doUpdate();
167 }
168 }
169 if (($i - 1) - $records_failed > $this->max_imports) {
170 $this->warnings[] = $this->lng->txt("dcl_max_import") . (count($sheet_data) - 1) . " > " . $this->max_imports;
171 break;
172 }
173 }
174 }
175
176 return ['line' => (max($i - 2, 0)), 'warnings' => $this->warnings];
177 }
178
179 protected function checkImportType(ilDclBaseFieldModel $field): bool
180 {
181 if (in_array($field->getDatatypeId(), $this->supported_import_datatypes)) {
182 return true;
183 } else {
184 $this->warnings[] = $field->getTitle() . ": " . $this->lng->txt("dcl_not_supported_in_import");
185
186 return false;
187 }
188 }
189
194 protected function getImportFieldsFromTitles(ilDclTable $table, array $titles): array
195 {
196 $fields = $table->getRecordFields();
197 $import_fields = [];
198 foreach ($fields as $field) {
199 if ($this->checkImportType($field)) {
200 // the fields will add themselves to $import_fields (at the correct position) if their title is in $titles
201 $field->checkTitlesForImport($titles, $import_fields);
202 }
203 }
204
205 foreach ($titles as $key => $value) {
208 foreach ($importable_titles as $identifier => $values) {
209 if (in_array($value, $values)) {
210 $std_field = new ilDclStandardField();
211 $std_field->setId(substr($identifier, 4));
212 $import_fields[$key] = $std_field;
213 continue 2;
214 }
215 }
216 if (in_array($value, $not_importable_titles)) {
217 $this->warnings[] = "(1, " . ilDataCollectionImporter::getExcelCharForInteger($key) . ") \"" . $value . "\" " . $this->lng->txt("dcl_std_field_not_importable");
218 } else {
219 if (!isset($import_fields[$key])) {
220 $this->warnings[] = "(1, " . ilDataCollectionImporter::getExcelCharForInteger($key + 1) . ") \"" . $value . "\" " . $this->lng->txt("dcl_row_not_found");
221 }
222 }
223 }
224
225 return $import_fields;
226 }
227}
const IL_CAL_UNIX
@classDescription Date and time handling
__construct(int $ref_id, ?int $table_id=null)
int $ref_id
Ref-ID of DataCollection.
getImportFieldsFromTitles(ilDclTable $table, array $titles)
int $table_id
Table-Id for export.
checkImportType(ilDclBaseFieldModel $field)
const INPUTFORMAT_TEXT_SELECTION
const INPUTFORMAT_DATETIME_SELECTION
const INPUTFORMAT_DATE_SELECTION
getRecordFields()
Returns all fields of this table which are NOT standard fields.
language handling
User class.
global $DIC
Definition: shib_login.php:26