ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilDclContentExporter.php
Go to the documentation of this file.
1 <?php
2 
20 declare(strict_types=1);
21 
27 {
28  public const SOAP_FUNCTION_NAME = 'exportDataCollectionContent';
29  public const EXPORT_EXCEL = 'xlsx';
30  public const IN_PROGRESS_POSTFIX = '.prog';
34  protected int $ref_id;
38  protected ?int $table_id;
42  protected array $filter;
43 
45 
46  protected ilLanguage $lng;
47 
48  protected ilDclTable $table;
50  protected array $tables;
51 
52  public function __construct(int $ref_id, ?int $table_id, array $filter = [])
53  {
54  global $DIC;
55  $this->main_tpl = $DIC->ui()->mainTemplate();
56  $lng = $DIC['lng'];
57 
58  $this->ref_id = $ref_id;
59  $this->table_id = $table_id;
60  $this->filter = $filter;
61 
62  $this->dcl = new ilObjDataCollection($ref_id);
63  $this->tables = ($table_id) ? [$this->dcl->getTableById($table_id)] : $this->dcl->getTables();
64 
65  $lng->loadLanguageModule('dcl');
66  $this->lng = $lng;
67  }
68 
73  public function sanitizeFilename(string $filename): string
74  {
75  $dangerous_filename_characters = [" ", '"', "'", "&", "/", "\\", "?", "#", "`"];
76 
77  return str_replace($dangerous_filename_characters, "_", iconv("utf-8", "ascii//TRANSLIT", $filename));
78  }
79 
83  public function getExportContentPath(string $format): string
84  {
85  return ilExport::_getExportDirectory($this->dcl->getId(), $format, 'dcl') . '/';
86  }
87 
91  protected function fillRowExcel(
92  ilDclTable $table,
93  ilExcel $worksheet,
94  ilDclBaseRecordModel $record,
95  int $row
96  ): void {
97  $col = 0;
98  foreach ($table->getFields() as $field) {
99  if ($field->getExportable()) {
100  $record->fillRecordFieldExcelExport($worksheet, $row, $col, $field->getId());
101  }
102  }
103  }
104 
108  protected function fillHeaderExcel(ilDclTable $table, ilExcel $worksheet, int $row): void
109  {
110  $col = 0;
111 
112  foreach ($table->getFields() as $field) {
113  if ($field->getExportable()) {
114  $field->fillHeaderExcel($worksheet, $row, $col);
115  }
116  }
117  }
118 
122  protected function fillMetaExcel(ilDclTable $table, ilExcel $worksheet, int $row): void
123  {
124  }
125 
131  public function export(string $format = self::EXPORT_EXCEL, string $filepath = null, bool $send = false)
132  {
133  if (count($this->tables) == 0) {
134  return;
135  }
136 
137  if (empty($filepath)) {
138  $filepath = $this->getExportContentPath($format);
139  ilFileUtils::makeDirParents($filepath);
140 
141  $basename = (isset($this->table_id)) ? $this->tables[0]->getTitle() : 'complete';
142  $filename = time() . '__' . $basename . "_" . date("Y-m-d_H-i");
143 
144  $filepath .= $this->sanitizeFilename($filename);
145  } else {
146  $filename = pathinfo($filepath, PATHINFO_FILENAME);
147  }
148 
149  $in_progress_file = $filepath . self::IN_PROGRESS_POSTFIX;
150  file_put_contents($in_progress_file, "");
151 
152  $data_available = false;
153  $fields_available = false;
154  $adapter = new ilExcel();
155  if ($format == self::EXPORT_EXCEL) {
156  foreach ($this->tables as $table) {
158 
159  $list = $table->getPartialRecords((string) $this->dcl->getRefId(), 'id', 'asc', null, 0, $this->filter);
160  $data_available = $data_available || ($list['total'] > 0);
161  $fields_available = $fields_available || (count($table->getExportableFields()) > 0);
162  if ($list['total'] > 0 && count($table->getExportableFields()) > 0) {
163  // only 31 character-long table-titles are allowed
164  $title = substr($table->getTitle(), 0, 31);
165  $adapter->addSheet($title);
166  $row = 1;
167 
168  $this->fillMetaExcel($table, $adapter, $row);
169 
170  // #14813
171  $this->fillHeaderExcel($table, $adapter, $row);
172  $row++;
173 
174  foreach ($list['records'] as $set) {
175  $this->fillRowExcel($table, $adapter, $set, $row);
176  $row++; // #14760
177  }
178 
179  $data_available = true;
180  }
181  }
182  }
183 
184  if (file_exists($in_progress_file)) {
185  unlink($in_progress_file);
186  }
187 
188  if (!$data_available || !$fields_available) {
189  $this->main_tpl->setOnScreenMessage('failure', $this->lng->txt('dcl_no_export_data_available'));
190  return false;
191  }
192 
193  $this->main_tpl->setOnScreenMessage($this->main_tpl::MESSAGE_TYPE_SUCCESS, $this->lng->txt('exp_file_created'), true);
194  if ($send) {
195  $adapter->sendToClient($filename);
196  } else {
197  $adapter->writeToFile($filepath);
198  }
199  return true;
200  }
201 
202  public function exportAsync(string $format = self::EXPORT_EXCEL, string $filepath = null): mixed
203  {
204  global $DIC;
205  $ilLog = $DIC['ilLog'];
206 
207  $method = self::SOAP_FUNCTION_NAME;
208 
209  $soap_params = [$this->dcl->getRefId()];
210  array_push($soap_params, $this->table_id, $format, $filepath);
211 
212  $new_session_id = ilSession::_duplicate($_COOKIE[session_name()]);
213  $client_id = $_COOKIE['ilClientId'];
214 
215  // Start cloning process using soap call
216  $soap_client = new ilSoapClient();
217  $soap_client->setResponseTimeout(5);
218  $soap_client->enableWSDL(true);
219 
220  $ilLog->write(__METHOD__ . ': Trying to call Soap client...');
221 
222  array_unshift($soap_params, $new_session_id . '::' . $client_id);
223 
224  if ($soap_client->init()) {
225  $ilLog->info('Calling soap ' . $method . ' method with params ' . print_r($soap_params, true));
226  $res = $soap_client->call($method, $soap_params);
227  } else {
228  $ilLog->warning('SOAP clone call failed. Calling clone method manually');
229  if (method_exists('ilSoapFunctions', $method)) {
230  $res = ilSoapFunctions::$method(
231  $new_session_id . '::' . $client_id,
232  $this->dcl->getRefId(),
234  $format,
235  $filepath
236  );
237  } else {
238  throw new ilDclException("SOAP call " . $method . " does not exists!");
239  }
240  }
241 
242  return $res;
243  }
244 }
$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.
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.
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)
$client_id
Definition: ltiauth.php:68
Hook-Class for exporting data-collections (used in SOAP-Class) This Class avoids duplicated code by r...
filter(string $filter_id, $class_path, string $cmd, bool $activated=true, bool $expanded=true)
$_COOKIE[session_name()]
Definition: xapitoken.php:54