ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilDclContentImporter Class Reference
+ Collaboration diagram for ilDclContentImporter:

Public Member Functions

 __construct (int $ref_id, ?int $table_id=null)
 
 import (string $file, bool $simulate=false)
 

Data Fields

const EXPORT_EXCEL = 'xlsx'
 

Protected Member Functions

 checkImportType (ilDclBaseFieldModel $field)
 
 getImportFieldsFromTitles (ilDclTable $table, array $titles)
 

Protected Attributes

int $max_imports = 100
 
array $supported_import_datatypes
 
array $warnings
 
int $ref_id
 Ref-ID of DataCollection. More...
 
int $table_id
 Table-Id for export. More...
 
ilObjDataCollection $dcl
 
array $tables
 
ilLanguage $lng
 
ilObjUser $user
 

Detailed Description

Definition at line 21 of file class.ilDclContentImporter.php.

Constructor & Destructor Documentation

◆ __construct()

ilDclContentImporter::__construct ( int  $ref_id,
?int  $table_id = null 
)

Definition at line 58 of file class.ilDclContentImporter.php.

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 }
int $ref_id
Ref-ID of DataCollection.
int $table_id
Table-Id for export.
global $DIC
Definition: shib_login.php:26

References $DIC, $ref_id, $table_id, ILIAS\Repository\lng(), and ILIAS\Repository\user().

+ Here is the call graph for this function:

Member Function Documentation

◆ checkImportType()

ilDclContentImporter::checkImportType ( ilDclBaseFieldModel  $field)
protected

Definition at line 179 of file class.ilDclContentImporter.php.

179 : 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 }

References ilDclBaseFieldModel\getDatatypeId(), ilDclBaseFieldModel\getTitle(), and ILIAS\Repository\lng().

Referenced by getImportFieldsFromTitles().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getImportFieldsFromTitles()

ilDclContentImporter::getImportFieldsFromTitles ( ilDclTable  $table,
array  $titles 
)
protected
Parameters
string[]$titles
Returns
ilDclBaseFieldModel[]

Definition at line 194 of file class.ilDclContentImporter.php.

194 : 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 }
checkImportType(ilDclBaseFieldModel $field)
getRecordFields()
Returns all fields of this table which are NOT standard fields.

References ilDclStandardField\_getImportableStandardFieldTitle(), ilDclStandardField\_getNonImportableStandardFieldTitles(), checkImportType(), ilDataCollectionImporter\getExcelCharForInteger(), ilDclTable\getRecordFields(), and ILIAS\Repository\lng().

Referenced by import().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ import()

ilDclContentImporter::import ( string  $file,
bool  $simulate = false 
)
Exceptions
ilException

PhpOffice\PhpSpreadsheet\Exception

Exceptions
ilDateTimeException

Definition at line 77 of file class.ilDclContentImporter.php.

77 : 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 }
const IL_CAL_UNIX
@classDescription Date and time handling
getImportFieldsFromTitles(ilDclTable $table, array $titles)

References Vendor\Package\$e, $warnings, ilDataCollectionImporter\getExcelCharForInteger(), getImportFieldsFromTitles(), IL_CAL_UNIX, ILIAS\Repository\lng(), and ILIAS\Repository\user().

+ Here is the call graph for this function:

Field Documentation

◆ $dcl

ilObjDataCollection ilDclContentImporter::$dcl
protected

Definition at line 49 of file class.ilDclContentImporter.php.

◆ $lng

ilLanguage ilDclContentImporter::$lng
protected

Definition at line 55 of file class.ilDclContentImporter.php.

◆ $max_imports

int ilDclContentImporter::$max_imports = 100
protected

Definition at line 26 of file class.ilDclContentImporter.php.

◆ $ref_id

int ilDclContentImporter::$ref_id
protected

Ref-ID of DataCollection.

Definition at line 43 of file class.ilDclContentImporter.php.

Referenced by __construct().

◆ $supported_import_datatypes

◆ $table_id

int ilDclContentImporter::$table_id
protected

Table-Id for export.

Definition at line 47 of file class.ilDclContentImporter.php.

Referenced by __construct().

◆ $tables

array ilDclContentImporter::$tables
protected

Definition at line 53 of file class.ilDclContentImporter.php.

◆ $user

ilObjUser ilDclContentImporter::$user
protected

Definition at line 56 of file class.ilDclContentImporter.php.

◆ $warnings

array ilDclContentImporter::$warnings
protected

Definition at line 39 of file class.ilDclContentImporter.php.

Referenced by import().

◆ EXPORT_EXCEL

const ilDclContentImporter::EXPORT_EXCEL = 'xlsx'

Definition at line 25 of file class.ilDclContentImporter.php.


The documentation for this class was generated from the following file: