ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 = 'xlsx'
 

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 12 of file class.ilDclContentImporter.php.

Constructor & Destructor Documentation

◆ __construct()

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

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

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

60  {
61  global $DIC;
62  $lng = $DIC['lng'];
63 
64  $this->ref_id = $ref_id;
65  $this->table_id = $table_id;
66 
67  $this->lng = $lng;
68 
69  $this->dcl = new ilObjDataCollection($ref_id);
70  $this->tables = ($table_id) ? array($this->dcl->getTableById($table_id)) : $this->dcl->getTables();
71  }
global $DIC
Definition: saml.php:7
Class ilObjDataCollection.

Member Function Documentation

◆ checkImportType()

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

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

References $supported_import_datatypes.

Referenced by getImportFieldsFromTitles().

182  {
183  if (in_array($field->getDatatypeId(), $this->supported_import_datatypes)) {
184  return true;
185  } else {
186  $this->warnings[] = $field->getTitle() . ": " . $this->lng->txt("dcl_not_supported_in_import");
187 
188  return false;
189  }
190  }
+ Here is the caller graph for this function:

◆ getImportFieldsFromTitles()

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

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

References $key, $table, $values, ilDclStandardField\_getImportableStandardFieldTitle(), ilDclStandardField\_getNonImportableStandardFieldTitles(), checkImportType(), and ilDataCollectionImporter\getExcelCharForInteger().

Referenced by import().

200  {
201  $fields = $table->getRecordFields();
202  $import_fields = array();
203  foreach ($fields as $field) {
204  if ($this->checkImportType($field)) {
205  // the fields will add themselves to $import_fields (at the correct position) if their title is in $titles
206  $field->checkTitlesForImport($titles, $import_fields);
207  }
208  }
209 
210  foreach ($titles as $key => $value) {
213  foreach ($importable_titles as $identifier => $values) {
214  if (in_array($value, $values)) {
215  $std_field = new ilDclStandardField();
216  $std_field->setId(substr($identifier, 4));
217  $import_fields[$key] = $std_field;
218  continue 2;
219  }
220  }
221  if (in_array($value, $not_importable_titles)) {
222  $this->warnings[] = "(1, " . ilDataCollectionImporter::getExcelCharForInteger($key) . ") \"" . $value . "\" " . $this->lng->txt("dcl_std_field_not_importable");
223  } else {
224  if (!isset($import_fields[$key])) {
225  $this->warnings[] = "(1, " . ilDataCollectionImporter::getExcelCharForInteger($key + 1) . ") \"" . $value . "\" " . $this->lng->txt("dcl_row_not_found");
226  }
227  }
228  }
229 
230  return $import_fields;
231  }
$values
Class ilDclBaseFieldModel.
if(empty($password)) $table
Definition: pwgen.php:24
$key
Definition: croninfo.php:18
+ 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 74 of file class.ilDclContentImporter.php.

References $DIC, $i, $ilUser, $table, ilDataCollectionImporter\getExcelCharForInteger(), getImportFieldsFromTitles(), IL_CAL_DATETIME, and IL_CAL_UNIX.

75  {
76  global $DIC;
77  $ilUser = $DIC['ilUser'];
78 
79  $this->warnings = array();
80  try {
81  $excel = new ilExcel();
82  $excel->loadFromFile($file);
83  } catch (Exception $e) {
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 array('line' => 0, 'warnings' => $this->warnings);
96  }
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 = array();
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($ilUser->getId());
121  $date_obj = new ilDateTime(time(), IL_CAL_UNIX);
122  $record->setCreateDate($date_obj->get(IL_CAL_DATETIME));
123  $record->setTableId($table->getId());
124  if (!$simulate) {
125  $record->doCreate();
126  }
127  $fields_failed = 0;
128  foreach ($fields as $col => $field) {
129  try {
130  if ($field->isStandardField()) {
131  $record->setStandardFieldValueFromExcel($excel, $i, $col, $field);
132  } else {
133  $value = $record->getRecordFieldValueFromExcel($excel, $i, $col, $field);
134 
135  if (is_array($value) && isset($value['warning'])) {
136  $this->warnings[] = $value['warning'];
137  $value = '';
138  }
139 
140  $field->checkValidity($value, $record->getId());
141  if (!$simulate) {
142  $record->setRecordFieldValue($field->getId(), $value);
143  }
144  }
145  } catch (ilDclInputException $e) {
146  $fields_failed++;
147  $this->warnings[] = "(" . $i . ", " . ilDataCollectionImporter::getExcelCharForInteger($col + 1) . ") " . $e;
148  }
149  }
150 
151  if ($fields_failed < count($fields)) {
152  $record_imported = true;
153  } else {
154  $records_failed++;
155  $record_imported = false;
156  }
157 
158  if (!$simulate) {
159  if (!$record_imported) { // if no fields have been filled, delete the record again
160  $record->doDelete(true); // omit notification
161  } else {
162  $record->doUpdate();
163  }
164  }
165  if (($i - 1) - $records_failed > $this->max_imports) {
166  $this->warnings[] = $this->lng->txt("dcl_max_import") . (count($sheet_data) - 1) . " > " . $this->max_imports;
167  break;
168  }
169  }
170  }
171 
172  return array('line' => ($i - 2 < 0 ? 0 : $i - 2), 'warnings' => $this->warnings);
173  }
const IL_CAL_DATETIME
global $DIC
Definition: saml.php:7
Class ilDclBaseFieldModel.
const IL_CAL_UNIX
Date and time handling
$ilUser
Definition: imgupload.php:18
Class ilDclBaseRecordModel.
$i
Definition: disco.tpl.php:19
if(empty($password)) $table
Definition: pwgen.php:24
getImportFieldsFromTitles($table, $titles)
+ Here is the call graph for this function:

Field Documentation

◆ $dcl

ilDclContentImporter::$dcl
protected

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

◆ $lng

ilDclContentImporter::$lng
protected

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

Referenced by __construct().

◆ $max_imports

ilDclContentImporter::$max_imports = 100
protected

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

◆ $ref_id

ilDclContentImporter::$ref_id
protected

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

Referenced by __construct().

◆ $supported_import_datatypes

◆ $table_id

ilDclContentImporter::$table_id
protected

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

Referenced by __construct().

◆ $tables

ilDclContentImporter::$tables
protected

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

◆ $warnings

ilDclContentImporter::$warnings
protected

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

◆ EXPORT_EXCEL

const ilDclContentImporter::EXPORT_EXCEL = 'xlsx'

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


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