19 declare(strict_types=1);
64 $this->
lng = $DIC->language();
65 $this->
user = $DIC->user();
68 $this->tables = (
$table_id) ? [$this->dcl->getTableById($table_id)] : $this->dcl->getTables();
76 public function import(
string $file,
bool $simulate =
false): array
81 $excel->loadFromFile($file);
83 $this->warnings[] = $this->
lng->txt(
"dcl_file_not_readable");
86 $sheet_count = $excel->getSheetCount();
87 $excel->setActiveSheet(0);
89 if ($sheet_count != count($this->tables)) {
90 $this->warnings[] = $this->
lng->txt(
'dcl_file_not_readable');
93 if (count($this->warnings)) {
99 for ($sheet = 0; $sheet < $sheet_count; $sheet++) {
100 $excel->setActiveSheet($sheet);
101 $table = $this->tables[$sheet];
104 $sheet_title = substr($table->getTitle(), 0, 31);
105 if ($excel->getSheetTitle() != $sheet_title) {
106 $this->warnings[] = $this->
lng->txt(
'dcl_table_title_not_matching');
111 $sheet_data = $excel->getSheetAsArray();
113 foreach ($sheet_data[0] as $column) {
114 $field_names[] = $column;
119 for ($i = 2; $i <= count($sheet_data); $i++) {
121 $record->setOwner($this->
user->getId());
123 $record->setCreateDate($date_obj);
124 $record->setLastUpdate($date_obj);
125 $record->setLastEditBy($this->
user->getId());
126 $record->setTableId($table->getId());
131 foreach ($fields as $col => $field) {
133 if ($field->isStandardField()) {
134 $record->setStandardFieldValueFromExcel($excel, $i, $col, $field);
136 $value = $record->getRecordFieldValueFromExcel($excel, $i, $col, $field);
138 if (is_array($value) && isset($value[
'warning'])) {
139 $this->warnings[] = $value[
'warning'];
143 $field->checkValidity($value, $record->getId());
145 $record->setRecordFieldValue($field->getId(), $value);
154 if ($fields_failed < count($fields)) {
155 $record_imported =
true;
158 $record_imported =
false;
162 if (!$record_imported) {
163 $record->doDelete(
true);
168 if (($i - 1) - $records_failed > $this->max_imports) {
169 $this->warnings[] = $this->
lng->txt(
"dcl_max_import") . (count($sheet_data) - 1) .
" > " . $this->max_imports;
175 return [
'line' => (max($i - 2, 0)),
'warnings' => $this->warnings];
183 $this->warnings[] = $field->
getTitle() .
": " . $this->
lng->txt(
"dcl_not_supported_in_import");
197 foreach ($fields as $field) {
200 $field->checkTitlesForImport($titles, $import_fields);
204 foreach ($titles as $key => $value) {
207 foreach ($importable_titles as $identifier => $values) {
208 if (in_array($value, $values)) {
210 $std_field->setId(substr($identifier, 4));
211 $import_fields[$key] = $std_field;
215 if (in_array($value, $not_importable_titles)) {
218 if (!isset($import_fields[$key])) {
224 return $import_fields;
static _getNonImportableStandardFieldTitles()
const INPUTFORMAT_BOOLEAN
__construct(int $ref_id, ?int $table_id=null)
array $supported_import_datatypes
const INPUTFORMAT_TEXT_SELECTION
const INPUTFORMAT_DATE_SELECTION
int $ref_id
Ref-ID of DataCollection.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static _getImportableStandardFieldTitle()
getImportFieldsFromTitles(ilDclTable $table, array $titles)
getRecordFields()
Returns all fields of this table which are NOT standard fields.
const INPUTFORMAT_DATETIME
int $table_id
Table-Id for export.
const INPUTFORMAT_REFERENCE
static getExcelCharForInteger(int $int)
checkImportType(ilDclBaseFieldModel $field)
getDatatypeId()
Get datatype_id.