ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
ilDclContentImporter Class Reference

Hook-Class for exporting data-collections (used in SOAP-Class) This Class avoids duplicated code by routing the request to the right place. More...

+ Collaboration diagram for ilDclContentImporter:

Public Member Functions

 __construct ($ref_id, $table_id=null)
 
 import ($file, $simulate=false)
 

Data Fields

const EXPORT_EXCEL = 'xls'
 

Protected Member Functions

 checkImportType ($field)
 
 getImportFieldsFromTitles ($table, $titles)
 

Protected Attributes

 $max_imports = 100
 
 $supported_import_datatypes
 
 $warnings
 
 $ref_id
 
 $table_id
 
 $dcl
 
 $tables
 
 $lng
 

Detailed Description

Hook-Class for exporting data-collections (used in SOAP-Class) This Class avoids duplicated code by routing the request to the right place.

Author
Michael Herren mh@st.nosp@m.uder.nosp@m.-raim.nosp@m.ann..nosp@m.ch

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

Constructor & Destructor Documentation

◆ __construct()

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

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

70 {
71 global $DIC;
72 $lng = $DIC['lng'];
73
74 $this->ref_id = $ref_id;
75 $this->table_id = $table_id;
76
77 $this->lng = $lng;
78
79 $this->dcl = new ilObjDataCollection($ref_id);
80 $this->tables = ($table_id)? array($this->dcl->getTableById($table_id)) : $this->dcl->getTables();
81 }
Class ilObjDataCollection.
global $DIC

References $DIC, $lng, $ref_id, and $table_id.

Member Function Documentation

◆ checkImportType()

ilDclContentImporter::checkImportType (   $field)
protected
Parameters
ilDclBaseFieldModel$field
Returns
bool

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

185 {
186 if (in_array($field->getDatatypeId(), $this->supported_import_datatypes)) {
187 return true;
188 } else {
189 $this->warnings[] = $field->getTitle() . ": " . $this->lng->txt("dcl_not_supported_in_import");
190
191 return false;
192 }
193 }

Referenced by getImportFieldsFromTitles().

+ Here is the caller graph for this function:

◆ getImportFieldsFromTitles()

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

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

202 {
203 $fields = $table->getRecordFields();
204 $import_fields = array();
205 foreach ($fields as $field) {
206 if ($this->checkImportType($field)) {
207 // the fields will add themselves to $import_fields if their title is in $titles
208 $field->checkTitlesForImport($titles, $import_fields);
209 }
210 }
211
212 foreach ($titles as $key => $value) {
214 if (in_array($value, $std_field_titles)) {
215 $this->warnings[] = "(1, " . ilDataCollectionImporter::getExcelCharForInteger($key) . ") \"" . $value . "\" " . $this->lng->txt("dcl_std_field_not_importable");
216 } else if (!isset($import_fields[$key])) {
217 $this->warnings[] = "(1, " . ilDataCollectionImporter::getExcelCharForInteger($key+1) . ") \"" . $value . "\" " . $this->lng->txt("dcl_row_not_found");
218 }
219 }
220
221 return $import_fields;
222 }

References ilDclStandardField\_getAllStandardFieldTitles(), checkImportType(), and ilDataCollectionImporter\getExcelCharForInteger().

Referenced by import().

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

◆ import()

ilDclContentImporter::import (   $file,
  $simulate = false 
)

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

83 {
84 global $DIC;
85 $ilUser = $DIC['ilUser'];
86
87 $this->warnings = array();
88 try {
89 $excel = new ilExcel();
90 $excel->loadFromFile($file);
91 } catch (Exception $e) {
92 $this->warnings[] = $this->lng->txt("dcl_file_not_readable");
93 }
94
95 $sheet_count = $excel->getSheetCount();
96 $excel->setActiveSheet(0);
97
98 if($sheet_count != count($this->tables)) {
99 $this->warnings[] = $this->lng->txt('dcl_file_not_readable');
100 }
101
102 if (count($this->warnings)) {
103 return array('line'=>0, 'warnings'=>$this->warnings);
104 }
105
106 for($sheet = 0; $sheet < $sheet_count; $sheet++) {
107 $excel->setActiveSheet($sheet);
108 $table = $this->tables[$sheet];
109
110 // only 31 character-long table-titles are allowed
111 $sheet_title = substr($table->getTitle(), 0, 31);
112 if($excel->getSheetTitle() != $sheet_title) {
113 $this->warnings[] = $this->lng->txt('dcl_table_title_not_matching');
114 continue;
115 }
116
117 $field_names = array();
118 $sheet_data = $excel->getSheetAsArray();
119
120 foreach ($sheet_data[0] as $column) {
121 $field_names[] = $column;
122 }
123 $fields = $this->getImportFieldsFromTitles($table, $field_names);
124
125 $records_failed = 0;
126 for ($i = 2; $i <= count($sheet_data); $i ++) {
127 $record = new ilDclBaseRecordModel();
128 $record->setOwner($ilUser->getId());
129 $date_obj = new ilDateTime(time(), IL_CAL_UNIX);
130 $record->setCreateDate($date_obj->get(IL_CAL_DATETIME));
131 $record->setTableId($table->getId());
132 if (!$simulate) {
133 $record->doCreate();
134 }
135 $fields_failed = 0;
136 foreach ($fields as $col => $field) {
137 try {
138 $value = $record->getRecordFieldValueFromExcel($excel, $i, $col, $field);
139
140 if (is_array($value) && isset($value['warning'])) {
141 $this->warnings[] = $value['warning'];
142 $value = '';
143 }
144
145 $field->checkValidity($value, $record->getId());
146 if (!$simulate) {
147 $record->setRecordFieldValue($field->getId(), $value);
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
177 return array('line'=>($i-2 < 0 ? 0 : $i-2), 'warnings'=>$this->warnings);
178 }
$column
Definition: 39dropdown.php:62
const IL_CAL_UNIX
const IL_CAL_DATETIME
@classDescription Date and time handling
Class ilDclBaseRecordModel.
getImportFieldsFromTitles($table, $titles)
Class ilDclBaseFieldModel.
if(!file_exists("$old.txt")) if( $old===$new) if(file_exists("$new.txt")) $file
$ilUser
Definition: imgupload.php:18

References $column, $DIC, $file, $ilUser, ilDataCollectionImporter\getExcelCharForInteger(), getImportFieldsFromTitles(), IL_CAL_DATETIME, and IL_CAL_UNIX.

+ Here is the call graph for this function:

Field Documentation

◆ $dcl

ilDclContentImporter::$dcl
protected

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

◆ $lng

ilDclContentImporter::$lng
protected

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

Referenced by __construct().

◆ $max_imports

ilDclContentImporter::$max_imports = 100
protected

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

◆ $ref_id

ilDclContentImporter::$ref_id
protected

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

Referenced by __construct().

◆ $supported_import_datatypes

◆ $table_id

ilDclContentImporter::$table_id
protected

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

Referenced by __construct().

◆ $tables

ilDclContentImporter::$tables
protected

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

◆ $warnings

ilDclContentImporter::$warnings
protected

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

◆ EXPORT_EXCEL

const ilDclContentImporter::EXPORT_EXCEL = 'xls'

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


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