ILIAS  release_8 Revision v8.24
class.ilFileSystemTableGUI.php
Go to the documentation of this file.
1<?php
20use ILIAS\ResourceStorage\Preloader\SecureString;
21
26{
27 use SecureString; // This is just for those legacy classes which will be removed soon anyway.
28 protected bool $has_multi = false;
29 protected array $row_commands = [];
30 protected bool $label_enable = false;
31 protected string $label_header = "";
32 protected string $cur_dir = '';
33 protected string $cur_subdir = '';
34 protected string $relative_cur_dir;
35 protected ?bool $post_dir_path = null;
36 protected array $file_labels = [];
37 protected \ILIAS\Filesystem\Filesystem $filesystem;
42 public function __construct(
43 ilFileSystemGUI $a_parent_obj,
44 string $a_parent_cmd,
45 string $a_cur_dir,
46 string $a_cur_subdir,
47 bool $a_label_enable,
48 ?array $a_file_labels = [],
49 ?string $a_label_header = "",
50 ?array $a_commands = [],
51 ?bool $a_post_dir_path = false,
52 ?string $a_table_id = ""
53 ) {
54 global $DIC;
55 $this->setId($a_table_id);
56 $this->ctrl = $DIC->ctrl();
57 $this->lng = $DIC->language();
58 if ($a_cur_dir !== realpath($a_cur_dir)) {
59 throw new \InvalidArgumentException('$a_cur_dir must be a absolute path');
60 }
61 $this->filesystem = LegacyPathHelper::deriveFilesystemFrom($a_cur_dir);
62 $this->relative_cur_dir = LegacyPathHelper::createRelativePath($a_cur_dir);
63 $this->cur_dir = $a_cur_dir;
64 $this->cur_subdir = $a_cur_subdir;
65 $this->label_enable = $a_label_enable;
66 $this->label_header = $a_label_header;
67 $this->file_labels = $a_file_labels;
68 $this->post_dir_path = $a_post_dir_path;
69 $this->filesystem_gui = $a_parent_obj;
70
71 parent::__construct($a_parent_obj, $a_parent_cmd);
72 $this->setTitle($this->lng->txt("cont_files") . " " . $this->cur_subdir);
73
74 $this->has_multi = false;
75
76 foreach ((array) $a_commands as $i => $command) {
77 if (!($command["single"] ?? false)) {
78 // does also handle internal commands
79 $this->addMultiCommand("extCommand_" . $i, $command["name"]);
80 $this->has_multi = true;
81 } else {
82 $this->row_commands[] = array(
83 "cmd" => "extCommand_" . $i,
84 "caption" => $command["name"],
85 "allow_dir" => $command["allow_dir"] ?? "",
86 "id" => $command["id"] ?? "",
87 );
88 }
89 }
90 $this->addColumns();
91
92 $this->setDefaultOrderField("name");
93 $this->setDefaultOrderDirection("asc");
94
95 $this->setEnableHeader(true);
96 $this->setFormAction($this->ctrl->getFormAction($a_parent_obj));
97 $this->setRowTemplate(
98 "tpl.directory_row.html",
99 "Services/FileSystem"
100 );
101 $this->setEnableTitle(true);
102 }
103
104 public function numericOrdering(string $a_field): bool
105 {
106 if ($a_field == "size") {
107 return true;
108 }
109 return false;
110 }
111
112 protected function prepareOutput(): void
113 {
114 $this->determineOffsetAndOrder(true);
115 $this->setData($this->getEntries());
116 }
117
121 public function getEntries(): array
122 {
123 if ($this->filesystem->has($this->relative_cur_dir)) {
124 $entries = [];
125 if ($this->cur_dir!=='') {
126 $entries['..'] = [
127 'order_val' => -1,
128 'order_id' => -1,
129 'entry' => '..',
130 'type' => 'dir',
131 'subdir' => '',
132 'size' => 0
133 ];
134 }
135
136
137 foreach ($this->filesystem->listContents($this->relative_cur_dir) as $i => $content) {
138 $basename = basename($content->getPath());
139 $entries[$basename] = [
140 'order_val' => $i,
141 'order_id' => $i,
142 'entry' => $basename,
143 'type' => $content->isDir() ? 'dir' : 'file',
144 'subdir' => '',
145 'size' => $content->isFile() ? $this->filesystem->getSize($content->getPath(), 1)->inBytes() : 0
146 ];
147 }
148 } else {
149 $entries = array(array("type" => "dir", "entry" => ".."));
150 }
151 $items = array();
152
153 foreach ($entries as $e) {
154 if (($e["entry"] == ".") || ($e["entry"] == ".." && empty($this->cur_subdir))) {
155 continue;
156 }
157 $cfile = (!empty($this->cur_subdir))
158 ? $this->cur_subdir . "/" . $e["entry"]
159 : $e["entry"];
160
161 if ($this->label_enable) {
162 $label = (isset($this->file_labels[$cfile]) && is_array($this->file_labels[$cfile]))
163 ? implode(", ", $this->file_labels[$cfile])
164 : "";
165 }
166
167 $pref = ($e["type"] == "dir")
168 ? ($this->getOrderDirection() != "desc" ? "1_" : "9_")
169 : "5_";
170 $items[] = array("file" => $cfile,
171 "entry" => $e["entry"],
172 "type" => $e["type"],
173 "label" => $label ?? '',
174 "size" => $e["size"] ?? '',
175 "name" => $pref . $e["entry"]
176 );
177 }
178 return $items;
179 }
180
181 public function addColumns(): void
182 {
183 if ($this->has_multi) {
184 $this->setSelectAllCheckbox("file[]");
185 $this->addColumn("", "", "1", true);
186 }
187 $this->addColumn("", "", "1", true); // icon
188
189 $this->addColumn($this->lng->txt("cont_dir_file"), "name");
190 $this->addColumn($this->lng->txt("cont_size"), "size");
191
192 if ($this->label_enable) {
193 $this->addColumn($this->label_header, "label");
194 }
195
196 if (sizeof($this->row_commands)) {
197 $this->addColumn($this->lng->txt("actions"));
198 }
199 }
200
201 private function isDoubleDotDirectory(array $entry): bool
202 {
203 return $entry['entry'] === '..';
204 }
205
209 protected function fillRow(array $a_set): void
210 {
211 $hash = $this->post_dir_path
212 ? md5($a_set["file"])
213 : md5($a_set["entry"]);
214
215 if ($this->has_multi) {
216 if ($this->isDoubleDotDirectory($a_set)) {
217 $this->tpl->touchBlock('no_checkbox');
218 } else {
219 $this->tpl->setVariable("CHECKBOX_ID", $hash);
220 }
221 }
222
223 // label
224 if ($this->label_enable) {
225 $this->tpl->setCurrentBlock("Label");
226 $this->tpl->setVariable("TXT_LABEL", $a_set["label"]);
227 $this->tpl->parseCurrentBlock();
228 }
229
230 $this->ctrl->setParameter($this->parent_obj, "cdir", $this->cur_subdir);
231
232 if ($a_set["type"] == "dir") {
233 $this->tpl->setCurrentBlock("FileLink");
234 $this->ctrl->setParameter($this->parent_obj, "newdir", $a_set["entry"]);
235 $this->ctrl->setParameter($this->parent_obj, "resetoffset", 1);
236 $this->tpl->setVariable(
237 "LINK_FILENAME",
238 $this->ctrl->getLinkTarget($this->parent_obj, "listFiles")
239 );
240 $this->ctrl->setParameter($this->parent_obj, "newdir", "");
241 $this->tpl->setVariable("TXT_FILENAME", $a_set["entry"]);
242 $this->tpl->parseCurrentBlock();
243
244 $this->tpl->setVariable("ICON", "<img src=\"" .
245 ilUtil::getImagePath("icon_cat.svg") . "\">");
246 $this->ctrl->setParameter($this->parent_obj, "resetoffset", "");
247 } else {
248 $this->tpl->setCurrentBlock("File");
249 $this->tpl->setVariable("TXT_FILENAME2", $this->secure($a_set["entry"]));
250 $this->tpl->parseCurrentBlock();
251 }
252
253 if ($a_set["type"] != "dir") {
254 $this->tpl->setVariable("TXT_SIZE", ilUtil::formatSize($a_set["size"]));
255 }
256
257 // single item commands
258
259 $zip_mime_types = [
260 "application/zip",
261 "application/x-zip"
262 ];
263
264 if (count($this->row_commands) > 0
265 && !(($a_set["type"] ?? '') === "dir" && ($a_set["entry"] ?? '') === "..")) {
266 $advsel = new ilAdvancedSelectionListGUI();
267 $advsel->setListTitle('');
268 foreach ($this->row_commands as $rcom) {
269 if ($rcom["allow_dir"] || ($a_set["type"] ?? '') !== "dir") {
270 // see https://mantis.ilias.de/view.php?id=36305
271 // will be dropped soon anyway...
272 $path = $this->cur_dir . $a_set['entry'];
273 $mime_type = MimeType::getMimeType($path);
274 if (
275 $rcom["id"] !== "unzip_file"
276 || ($rcom["id"] === "unzip_file" && in_array($mime_type, $zip_mime_types))
277 ) {
278 $this->ctrl->setParameter($this->parent_obj, "fhsh", $hash);
279 $url = $this->ctrl->getLinkTarget($this->parent_obj, $rcom["cmd"]);
280 $this->ctrl->setParameter($this->parent_obj, "fhsh", "");
281
282 $advsel->addItem($rcom["caption"], "", $url);
283 }
284 }
285 }
286 $this->tpl->setVariable("ACTIONS", $advsel->getHTML());
287 }
288 }
289}
Mime type determination.
Definition: MimeType.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
File System Explorer GUI class.
__construct(ilFileSystemGUI $a_parent_obj, string $a_parent_cmd, string $a_cur_dir, string $a_cur_subdir, bool $a_label_enable, ?array $a_file_labels=[], ?string $a_label_header="", ?array $a_commands=[], ?bool $a_post_dir_path=false, ?string $a_table_id="")
Constructor.
numericOrdering(string $a_field)
Should this field be sorted numeric?
fillRow(array $a_set)
Fill table row.
ILIAS Filesystem Filesystem $filesystem
prepareOutput()
Anything that must be done before HTML is generated.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
determineOffsetAndOrder(bool $a_omit_offset=false)
setTitle(string $a_title, string $a_icon="", string $a_icon_alt="")
setEnableTitle(bool $a_enabletitle)
addMultiCommand(string $a_cmd, string $a_text)
setFormAction(string $a_form_action, bool $a_multipart=false)
addColumn(string $a_text, string $a_sort_field="", string $a_width="", bool $a_is_checkbox_action_column=false, string $a_class="", string $a_tooltip="", bool $a_tooltip_with_html=false)
setEnableHeader(bool $a_enableheader)
setDefaultOrderField(string $a_defaultorderfield)
setSelectAllCheckbox(string $a_select_all_checkbox, bool $a_select_all_on_top=false)
setRowTemplate(string $a_template, string $a_template_dir="")
Set row template.
setId(string $a_val)
setDefaultOrderDirection(string $a_defaultorderdirection)
setData(array $a_data)
Set table data.
static formatSize(int $size, string $a_mode='short', ?ilLanguage $a_lng=null)
Returns the specified file size value in a human friendly form.
static getImagePath(string $img, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
global $DIC
Definition: feed.php:28
$path
Definition: ltiservices.php:32
$i
Definition: metadata.php:41
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
$url