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