ILIAS  release_8 Revision v8.23
class.ilDclContentExporter.php
Go to the documentation of this file.
1 <?php
2 
20 {
21  public const SOAP_FUNCTION_NAME = 'exportDataCollectionContent';
22  public const EXPORT_EXCEL = 'xlsx';
23  public const IN_PROGRESS_POSTFIX = '.prog';
27  protected int $ref_id;
31  protected ?int $table_id;
35  protected array $filter;
36 
38 
39  protected ilLanguage $lng;
40 
41  protected ilDclTable $table;
43  protected array $tables;
44 
45  public function __construct(int $ref_id, ?int $table_id, array $filter = [])
46  {
47  global $DIC;
48  $this->main_tpl = $DIC->ui()->mainTemplate();
49  $lng = $DIC['lng'];
50 
51  $this->ref_id = $ref_id;
52  $this->table_id = $table_id;
53  $this->filter = $filter;
54 
55  $this->dcl = new ilObjDataCollection($ref_id);
56  $this->tables = ($table_id) ? [$this->dcl->getTableById($table_id)] : $this->dcl->getTables();
57 
58  $lng->loadLanguageModule('dcl');
59  $this->lng = $lng;
60  }
61 
66  public function sanitizeFilename(string $filename): string
67  {
68  $dangerous_filename_characters = [" ", '"', "'", "&", "/", "\\", "?", "#", "`"];
69 
70  return str_replace($dangerous_filename_characters, "_", iconv("utf-8", "ascii//TRANSLIT", $filename));
71  }
72 
76  public function getExportContentPath(string $format): string
77  {
78  return ilExport::_getExportDirectory($this->dcl->getId(), $format, 'dcl') . '/';
79  }
80 
84  protected function fillRowExcel(
85  ilDclTable $table,
86  ilExcel $worksheet,
87  ilDclBaseRecordModel $record,
88  int $row
89  ): void {
90  $col = 0;
91  foreach ($table->getFields() as $field) {
92  if ($field->getExportable()) {
93  $record->fillRecordFieldExcelExport($worksheet, $row, $col, $field->getId());
94  }
95  }
96  }
97 
101  protected function fillHeaderExcel(ilDclTable $table, ilExcel $worksheet, int $row): void
102  {
103  $col = 0;
104 
105  foreach ($table->getFields() as $field) {
106  if ($field->getExportable()) {
107  $field->fillHeaderExcel($worksheet, $row, $col);
108  }
109  }
110  }
111 
115  protected function fillMetaExcel(ilDclTable $table, ilExcel $worksheet, int $row): void
116  {
117  }
118 
124  public function export(string $format = self::EXPORT_EXCEL, string $filepath = null, bool $send = false)
125  {
126  if (count($this->tables) == 0) {
127  return;
128  }
129 
130  if (empty($filepath)) {
131  $filepath = $this->getExportContentPath($format);
132  ilFileUtils::makeDirParents($filepath);
133 
134  $basename = (isset($this->table_id)) ? $this->tables[0]->getTitle() : 'complete';
135  $filename = time() . '__' . $basename . "_" . date("Y-m-d_H-i");
136 
137  $filepath .= $this->sanitizeFilename($filename);
138  } else {
139  $filename = pathinfo($filepath, PATHINFO_FILENAME);
140  }
141 
142  $in_progress_file = $filepath . self::IN_PROGRESS_POSTFIX;
143  file_put_contents($in_progress_file, "");
144 
145  $data_available = false;
146  $fields_available = false;
147  if ($format == self::EXPORT_EXCEL) {
148  $adapter = new ilExcel();
149  foreach ($this->tables as $table) {
151 
152  $list = $table->getPartialRecords($this->dcl->getRefId(), 'id', 'asc', null, 0, $this->filter);
153  $data_available = $data_available || ($list['total'] > 0);
154  $fields_available = $fields_available || (count($table->getExportableFields()) > 0);
155  if ($list['total'] > 0 && count($table->getExportableFields()) > 0) {
156  // only 31 character-long table-titles are allowed
157  $title = substr($table->getTitle(), 0, 31);
158  $adapter->addSheet($title);
159  $row = 1;
160 
161  $this->fillMetaExcel($table, $adapter, $row);
162 
163  // #14813
164  $pre = $row;
165  $this->fillHeaderExcel($table, $adapter, $row);
166  if ($pre == $row) {
167  $row++;
168  }
169 
170  foreach ($list['records'] as $set) {
171  $this->fillRowExcel($table, $adapter, $set, $row);
172  $row++; // #14760
173  }
174 
175  $data_available = true;
176  }
177  }
178  }
179 
180  if (file_exists($in_progress_file)) {
181  unlink($in_progress_file);
182  }
183 
184  if (!$data_available) {
185  $this->main_tpl->setOnScreenMessage('info', $this->lng->txt('dcl_no_export_content_available'));
186 
187  return false;
188  }
189 
190  if (!$fields_available) {
191  global $ilCtrl;
192  $this->main_tpl->setOnScreenMessage('info', sprintf(
193  $this->lng->txt('dcl_no_export_fields_available'),
194  $ilCtrl->getLinkTargetByClass(
195  ['ilDclTableListGUI', 'ilDclFieldListGUI'],
196  'listFields'
197  )
198  ));
199  return false;
200  }
201 
202  if ($send) {
203  $adapter->sendToClient($filename);
204  } else {
205  $adapter->writeToFile($filepath);
206  }
207  return true;
208  }
209 
215  public function exportAsync(string $format = self::EXPORT_EXCEL, string $filepath = null)
216  {
217  global $DIC;
218  $ilLog = $DIC['ilLog'];
219 
220  $method = self::SOAP_FUNCTION_NAME;
221 
222  $soap_params = [$this->dcl->getRefId()];
223  array_push($soap_params, $this->table_id, $format, $filepath);
224 
225  $new_session_id = ilSession::_duplicate($_COOKIE[session_name()]);
226  $client_id = $_COOKIE['ilClientId'];
227 
228  // Start cloning process using soap call
229  $soap_client = new ilSoapClient();
230  $soap_client->setResponseTimeout(5);
231  $soap_client->enableWSDL(true);
232 
233  $ilLog->write(__METHOD__ . ': Trying to call Soap client...');
234 
235  array_unshift($soap_params, $new_session_id . '::' . $client_id);
236 
237  if ($soap_client->init()) {
238  $ilLog->info('Calling soap ' . $method . ' method with params ' . print_r($soap_params, true));
239  $res = $soap_client->call($method, $soap_params);
240  } else {
241  $ilLog->warning('SOAP clone call failed. Calling clone method manually');
242  if (method_exists('ilSoapFunctions', $method)) {
243  $res = ilSoapFunctions::$method(
244  $new_session_id . '::' . $client_id,
245  $this->dcl->getRefId(),
247  $format,
248  $filepath
249  );
250  } else {
251  throw new ilDclException("SOAP call " . $method . " does not exists!");
252  }
253  }
254 
255  return $res;
256  }
257 }
$res
Definition: ltiservices.php:69
static _duplicate(string $a_session_id)
Duplicate session.
getExportContentPath(string $format)
Return export path.
ilGlobalTemplateInterface $main_tpl
getFields()
Returns all fields of this table including the standard fields.
loadLanguageModule(string $a_module)
Load language module.
array $filter
Array with filters.
export(string $format=self::EXPORT_EXCEL, string $filepath=null, bool $send=false)
Creates an export of a specific data collection table.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
static _getExportDirectory(int $a_obj_id, string $a_type="xml", string $a_obj_type="", string $a_entity="")
Get export directory for an repository object.
fillMetaExcel(ilDclTable $table, ilExcel $worksheet, int $row)
Fill Excel meta-data.
__construct(int $ref_id, ?int $table_id, array $filter=[])
static resetCache()
Resets all the cache fields.
int $table_id
Table-Id for export.
global $DIC
Definition: feed.php:28
fillRowExcel(ilDclTable $table, ilExcel $worksheet, ilDclBaseRecordModel $record, int $row)
Fill a excel row.
fillHeaderExcel(ilDclTable $table, ilExcel $worksheet, int $row)
Fill Excel header.
$format
Definition: metadata.php:235
sanitizeFilename(string $filename)
Sanitize the given filename The ilUtil::_sanitizeFilename() does not clean enough.
$filename
Definition: buildRTE.php:78
fillRecordFieldExcelExport(ilExcel $worksheet, int &$row, int &$col, $field_id)
int $ref_id
Ref-ID of DataCollection.
exportAsync(string $format=self::EXPORT_EXCEL, string $filepath=null)
Start Export async.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$client_id
Definition: ltiauth.php:68
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$_COOKIE[session_name()]
Definition: xapitoken.php:54
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...