ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
class.ilDclContentExporter.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22{
23 public const SOAP_FUNCTION_NAME = 'exportDataCollectionContent';
24 public const EXPORT_EXCEL = 'xlsx';
25 public const IN_PROGRESS_POSTFIX = '.prog';
26 protected int $ref_id;
27 protected ?int $table_id;
28 protected array $filter;
30 protected ilLanguage $lng;
31 protected ilDclTable $table;
33 protected array $tables;
34
35 public function __construct(int $ref_id, ?int $table_id, array $filter = [])
36 {
37 global $DIC;
38 $this->main_tpl = $DIC->ui()->mainTemplate();
39 $lng = $DIC->language();
40
41 $this->ref_id = $ref_id;
42 $this->table_id = $table_id;
43 $this->filter = $filter;
44
45 $this->dcl = new ilObjDataCollection($ref_id);
46 $this->tables = ($table_id) ? [$this->dcl->getTableById($table_id)] : $this->dcl->getTables();
47
49 $this->lng = $lng;
50 }
51
56 public function sanitizeFilename(string $filename): string
57 {
58 $dangerous_filename_characters = [" ", '"', "'", "&", "/", "\\", "?", "#", "`"];
59
60 return str_replace($dangerous_filename_characters, "_", iconv("utf-8", "ascii//TRANSLIT", $filename));
61 }
62
63 public function getExportContentPath(string $format): string
64 {
65 return ilExport::_getExportDirectory($this->dcl->getId(), $format, 'dcl') . '/';
66 }
67
68 protected function fillRowExcel(
70 ilExcel $worksheet,
72 int $row
73 ): void {
74 $col = 0;
75 foreach ($table->getFields() as $field) {
76 if ($field->getExportable()) {
77 $record->fillRecordFieldExcelExport($worksheet, $row, $col, $field->getId());
78 }
79 }
80 }
81
82 protected function fillHeaderExcel(ilDclTable $table, ilExcel $worksheet, int $row): void
83 {
84 $col = 0;
85
86 foreach ($table->getFields() as $field) {
87 if ($field->getExportable()) {
88 $field->fillHeaderExcel($worksheet, $row, $col);
89 }
90 }
91 }
92
93 protected function fillMetaExcel(ilDclTable $table, ilExcel $worksheet, int $row): void
94 {
95 }
96
97 public function export(string $format = self::EXPORT_EXCEL, ?string $filepath = null, bool $send = false): bool
98 {
99 if (empty($filepath)) {
100 $filepath = $this->getExportContentPath($format);
102
103 $basename = (isset($this->table_id)) ? $this->tables[0]->getTitle() : 'complete';
104 $filename = time() . '__' . $basename . "_" . date("Y-m-d_H-i");
105
106 $filepath .= $this->sanitizeFilename($filename);
107 } else {
108 $filename = pathinfo($filepath, PATHINFO_FILENAME);
109 }
110
111 $in_progress_file = $filepath . self::IN_PROGRESS_POSTFIX;
112 file_put_contents($in_progress_file, "");
113
114 $data_available = false;
115 $fields_available = false;
116 $adapter = new ilExcel();
117 if ($format == self::EXPORT_EXCEL) {
118 foreach ($this->tables as $table) {
120
121 $list = $table->getPartialRecords((string) $this->dcl->getRefId(), 'id', 'asc', null, 0, $this->filter);
122 $data_available = $data_available || ($list['total'] > 0);
123 $fields_available = $fields_available || (count($table->getExportableFields()) > 0);
124 if ($list['total'] > 0 && count($table->getExportableFields()) > 0) {
125 $adapter->addSheet($table->getTitle());
126 $row = 1;
127 $this->fillMetaExcel($table, $adapter, $row);
128 $this->fillHeaderExcel($table, $adapter, $row);
129 $row++;
130 foreach ($list['records'] as $set) {
131 $this->fillRowExcel($table, $adapter, $set, $row);
132 $row++;
133 }
134
135 $data_available = true;
136 }
137 }
138 }
139
140 if (file_exists($in_progress_file)) {
141 unlink($in_progress_file);
142 }
143
144 if (!$data_available || !$fields_available) {
145 $this->main_tpl->setOnScreenMessage('failure', $this->lng->txt('dcl_no_export_data_available'));
146 return false;
147 }
148
149 $this->main_tpl->setOnScreenMessage($this->main_tpl::MESSAGE_TYPE_SUCCESS, $this->lng->txt('exp_file_created'), true);
150 if ($send) {
151 $adapter->sendToClient($filename);
152 } else {
153 $adapter->writeToFile($filepath);
154 }
155 return true;
156 }
157
158 public function exportAsync(string $format = self::EXPORT_EXCEL, ?string $filepath = null): mixed
159 {
160 global $DIC;
161
162 $method = self::SOAP_FUNCTION_NAME;
163
164 $soap_params = [$this->dcl->getRefId()];
165 array_push($soap_params, $this->table_id, $format, $filepath);
166
167 $new_session_id = ilSession::_duplicate(
168 $DIC->http()->wrapper()->cookie()->retrieve(session_name(), $DIC->refinery()->kindlyTo()->string())
169 );
170 $client_id = $DIC->http()->wrapper()->cookie()->retrieve('ilClientId', $DIC->refinery()->kindlyTo()->string());
171
172 $soap_client = new ilSoapClient();
173 $soap_client->setResponseTimeout(5);
174 $soap_client->enableWSDL(true);
175
176 $DIC->logger()->root()->write(__METHOD__ . ': Trying to call Soap client...');
177
178 array_unshift($soap_params, $new_session_id . '::' . $client_id);
179
180 if ($soap_client->init()) {
181 $DIC->logger()->root()->info('Calling soap ' . $method . ' method with params ' . print_r($soap_params, true));
182 $res = $soap_client->call($method, $soap_params);
183 } else {
184 $DIC->logger()->root()->warning('SOAP clone call failed. Calling clone method manually');
185 if (method_exists('ilSoapFunctions', $method)) {
186 $res = ilSoapFunctions::$method(
187 $new_session_id . '::' . $client_id,
188 $this->dcl->getRefId(),
189 $this->table_id,
190 $format,
191 $filepath
192 );
193 } else {
194 throw new ilDclException("SOAP call " . $method . " does not exists!");
195 }
196 }
197
198 return $res;
199 }
200}
$filename
Definition: buildRTE.php:78
fillRecordFieldExcelExport(ilExcel $worksheet, int &$row, int &$col, $field_id)
static resetCache()
Resets all the cache fields.
__construct(int $ref_id, ?int $table_id, array $filter=[])
fillMetaExcel(ilDclTable $table, ilExcel $worksheet, int $row)
export(string $format=self::EXPORT_EXCEL, ?string $filepath=null, bool $send=false)
sanitizeFilename(string $filename)
Sanitize the given filename The ilUtil::_sanitizeFilename() does not clean enough.
fillRowExcel(ilDclTable $table, ilExcel $worksheet, ilDclBaseRecordModel $record, int $row)
ilGlobalTemplateInterface $main_tpl
exportAsync(string $format=self::EXPORT_EXCEL, ?string $filepath=null)
fillHeaderExcel(ilDclTable $table, ilExcel $worksheet, int $row)
getFields()
Returns all fields of this table including the standard fields.
static _getExportDirectory(int $a_obj_id, string $a_type="xml", string $a_obj_type="", string $a_entity="")
@depricated Get export directory for an repository object
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
language handling
loadLanguageModule(string $a_module)
Load language module.
static _duplicate(string $a_session_id)
Duplicate session.
$client_id
Definition: ltiauth.php:67
$res
Definition: ltiservices.php:69
filter(string $filter_id, array $class_path, string $cmd, bool $activated=true, bool $expanded=true)
global $DIC
Definition: shib_login.php:26