ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
class.ilFileSystemTableGUI.php
Go to the documentation of this file.
1<?php
2
24use ILIAS\ResourceStorage\Preloader\SecureString;
25
30{
31 use SecureString;
32
33 // This is just for those legacy classes which will be removed soon anyway.
36 protected bool $has_multi = false;
37 protected array $row_commands = [];
38 protected string $cur_dir = '';
39 protected string $relative_cur_dir;
41
45 public function __construct(
46 protected ilFileSystemGUI $filesystem_gui,
47 string $a_parent_cmd,
48 string $a_cur_dir,
49 protected string $cur_subdir,
50 protected bool $label_enable,
51 protected array $file_labels = [],
52 protected string $label_header = "",
53 ?array $a_commands = [],
54 protected ?bool $post_dir_path = false,
55 ?string $a_table_id = ""
56 ) {
57 global $DIC;
58 $this->setId($a_table_id);
59 $this->ctrl = $DIC->ctrl();
60 $this->lng = $DIC->language();
61 if ($a_cur_dir === '' || $a_cur_dir !== realpath($a_cur_dir)) {
62 // The requested directory is not a valid canonical absolute path,
63 // e.g. because its name contains non-UTF-8/special characters
64 // created by an older ILIAS version (Mantis 45045). Fall back to
65 // the managed base directory instead of throwing a fatal error
66 // that breaks the whole page.
67 $a_cur_dir = $this->filesystem_gui->getMainAbsoluteDir();
68 $this->cur_subdir = '';
69 }
70 $this->filesystem = LegacyPathHelper::deriveFilesystemFrom($a_cur_dir);
71 $this->relative_cur_dir = LegacyPathHelper::createRelativePath($a_cur_dir);
72 $this->cur_dir = $a_cur_dir;
73 $this->ui_factory = $DIC->ui()->factory();
74 $this->ui_renderer = $DIC->ui()->renderer();
75
76 parent::__construct($this->filesystem_gui, $a_parent_cmd);
77 $this->setTitle($this->lng->txt("cont_files") . " " . $this->cur_subdir);
78
79 $this->has_multi = false;
80
81 foreach ((array) $a_commands as $i => $command) {
82 if (!($command["single"] ?? false)) {
83 // does also handle internal commands
84 $this->addMultiCommand("extCommand_" . $i, $command["name"]);
85 $this->has_multi = true;
86 } else {
87 $this->row_commands[] = [
88 "cmd" => "extCommand_" . $i,
89 "caption" => $command["name"],
90 "allow_dir" => $command["allow_dir"] ?? false,
91 "method" => $command["method"] ?? null,
92 ];
93 }
94 }
95 $this->addColumns();
96
97 $this->setDefaultOrderField("name");
98 $this->setDefaultOrderDirection("asc");
99
100 $this->setEnableHeader(true);
101 $this->setFormAction($this->ctrl->getFormAction($this->filesystem_gui));
102 $this->setRowTemplate(
103 "tpl.directory_row.html",
104 "components/ILIAS/Filesystem"
105 );
106 $this->setEnableTitle(true);
107 }
108
109 #[\Override]
110 public function numericOrdering(string $a_field): bool
111 {
112 return $a_field === "size";
113 }
114
115 protected function prepareOutput(): void
116 {
117 $this->determineOffsetAndOrder(true);
118 $this->setData($this->getEntries());
119 }
120
124 public function getEntries(): array
125 {
127 if ($this->filesystem->has($path)) {
128 $entries = [];
129 if ($this->cur_dir !== '') {
130 $entries['..'] = [
131 'order_val' => -1,
132 'order_id' => -1,
133 'entry' => '..',
134 'type' => 'dir',
135 'subdir' => '',
136 'size' => 0
137 ];
138 }
139
140 foreach ($this->filesystem->listContents($path) as $i => $content) {
141 $basename = basename($content->getPath());
142 $entries[$basename] = [
143 'order_val' => $i,
144 'order_id' => $i,
145 'entry' => $basename,
146 'type' => $content->isDir() ? 'dir' : 'file',
147 'subdir' => '',
148 'size' => $content->isFile() ? $this->filesystem->getSize($content->getPath(), 1)->inBytes() : 0
149 ];
150 }
151 } else {
152 $entries = [["type" => "dir", "entry" => ".."]];
153 }
154 $items = [];
155
156 foreach ($entries as $e) {
157 if ($e["entry"] === ".") {
158 continue;
159 }
160 if ($e["entry"] === ".." && empty($this->cur_subdir)) {
161 continue;
162 }
163 $cfile = (empty($this->cur_subdir))
164 ? $e["entry"]
165 : $this->cur_subdir . "/" . $e["entry"];
166
167 if ($this->label_enable) {
168 $label = (isset($this->file_labels[$cfile]) && is_array($this->file_labels[$cfile]))
169 ? implode(", ", $this->file_labels[$cfile])
170 : "";
171 }
172
173 $pref = ($e["type"] === "dir")
174 ? ($this->getOrderDirection() !== "desc" ? "1_" : "9_")
175 : "5_";
176 $items[] = [
177 "file" => $cfile,
178 "entry" => $e["entry"],
179 "type" => $e["type"],
180 "label" => $label ?? '',
181 "size" => $e["size"] ?? '',
182 "name" => $pref . $e["entry"]
183 ];
184 }
185 return $items;
186 }
187
188 public function addColumns(): void
189 {
190 if ($this->has_multi) {
191 $this->setSelectAllCheckbox("file[]");
192 $this->addColumn("", "", "1", true);
193 }
194 $this->addColumn("", "", "1", true); // icon
195
196 $this->addColumn($this->lng->txt("cont_dir_file"), "name");
197 $this->addColumn($this->lng->txt("cont_size"), "size");
198
199 if ($this->label_enable) {
200 $this->addColumn($this->label_header, "label");
201 }
202
203 if ($this->row_commands !== []) {
204 $this->addColumn($this->lng->txt("actions"));
205 }
206 }
207
208 private function isDoubleDotDirectory(array $entry): bool
209 {
210 return $entry['entry'] === '..';
211 }
212
216 #[\Override]
217 protected function fillRow(array $a_set): void
218 {
219 $hash = $this->post_dir_path
220 ? md5((string) $a_set["file"])
221 : md5((string) $a_set["entry"]);
222
223 if ($this->has_multi) {
224 if ($this->isDoubleDotDirectory($a_set)) {
225 $this->tpl->touchBlock('no_checkbox');
226 } else {
227 $this->tpl->setVariable("CHECKBOX_ID", $hash);
228 }
229 }
230
231 // label
232 if ($this->label_enable) {
233 $this->tpl->setCurrentBlock("Label");
234 $this->tpl->setVariable("TXT_LABEL", $a_set["label"]);
235 $this->tpl->parseCurrentBlock();
236 }
237
238 $this->ctrl->setParameter($this->parent_obj, "cdir", $this->cur_subdir);
239
240 if ($a_set["type"] == "dir") {
241 $this->tpl->setCurrentBlock("FileLink");
242 $this->ctrl->setParameter($this->parent_obj, "newdir", $a_set["entry"]);
243 $this->ctrl->setParameter($this->parent_obj, "resetoffset", 1);
244 $this->tpl->setVariable(
245 "LINK_FILENAME",
246 $this->ctrl->getLinkTarget($this->parent_obj, "listFiles")
247 );
248 $this->ctrl->setParameter($this->parent_obj, "newdir", "");
249 $this->tpl->setVariable("TXT_FILENAME", $a_set["entry"]);
250 $this->tpl->parseCurrentBlock();
251
252 $this->tpl->setVariable(
253 "ICON",
254 "<img src=\"" .
255 ilUtil::getImagePath("standard/icon_cat.svg") . "\">"
256 );
257 $this->ctrl->setParameter($this->parent_obj, "resetoffset", "");
258 } else {
259 $this->tpl->setCurrentBlock("File");
260 $this->tpl->setVariable("TXT_FILENAME2", $this->secure($a_set["entry"]));
261 $this->tpl->parseCurrentBlock();
262 }
263
264 if ($a_set["type"] != "dir") {
265 $this->tpl->setVariable("TXT_SIZE", ilUtil::formatSize($a_set["size"]));
266 }
267
268 // single item commands
269 if ($this->row_commands !== [] && !($a_set["type"] === "dir" && $a_set["entry"] === "..")) {
270 $actions = [];
271
272 foreach ($this->row_commands as $rcom) {
273 if ($rcom["allow_dir"] || $a_set["type"] !== "dir") {
274 $file_path = $this->cur_dir . $a_set['entry'];
275 if (
276 $rcom['method'] !== ilFileSystemGUI::CMD_UNZIP_FILE
277 || ($rcom['method'] === ilFileSystemGUI::CMD_UNZIP_FILE && MimeType::getMimeType($file_path) === "application/zip")
278 ) {
279 $this->ctrl->setParameter($this->parent_obj, "fhsh", $hash);
280 $url = $this->ctrl->getLinkTarget($this->parent_obj, $rcom["cmd"]);
281 $this->ctrl->setParameter($this->parent_obj, "fhsh", "");
282
283 $actions[] = $this->ui_factory->link()->standard($rcom["caption"], $url);
284 }
285 }
286 }
287
288 $dropdown = $this->ui_factory->dropdown()->standard($actions);
289 $this->tpl->setVariable("ACTIONS", $this->ui_renderer->render($dropdown));
290 }
291 }
292}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Mime type determination.
Definition: MimeType.php:30
The legacy path helper provides convenient functions for the integration of the filesystem service wi...
File System Explorer GUI class.
__construct(protected ilFileSystemGUI $filesystem_gui, string $a_parent_cmd, string $a_cur_dir, protected string $cur_subdir, protected bool $label_enable, protected array $file_labels=[], protected string $label_header="", ?array $a_commands=[], protected ?bool $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.
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 getImagePath(string $image_name, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
static formatSize(int $size, string $a_mode='short', ?ilLanguage $a_lng=null)
Returns the specified file size value in a human friendly form.
The filesystem interface provides the public interface for the Filesystem service API consumer.
Definition: Filesystem.php:37
This is how the factory for UI elements looks.
Definition: Factory.php:38
An entity that renders components to a string output.
Definition: Renderer.php:31
$path
Definition: ltiservices.php:30
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $DIC
Definition: shib_login.php:26
$url
Definition: shib_logout.php:70