ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilFileSystemTableGUI.php
Go to the documentation of this file.
1 <?php
21 
26 {
27  use SecureString;
28 
29  // This is just for those legacy classes which will be removed soon anyway.
30  private \ILIAS\UI\Factory $ui_factory;
31  private \ILIAS\UI\Renderer $ui_renderer;
32  protected bool $has_multi = false;
33  protected array $row_commands = [];
34  protected bool $label_enable = false;
35  protected string $label_header = "";
36  protected string $cur_dir = '';
37  protected string $cur_subdir = '';
38  protected string $relative_cur_dir;
39  protected ?bool $post_dir_path = null;
40  protected array $file_labels = [];
41  protected \ILIAS\Filesystem\Filesystem $filesystem;
43 
47  public function __construct(
48  ilFileSystemGUI $a_parent_obj,
49  string $a_parent_cmd,
50  string $a_cur_dir,
51  string $a_cur_subdir,
52  bool $a_label_enable,
53  ?array $a_file_labels = [],
54  ?string $a_label_header = "",
55  ?array $a_commands = [],
56  ?bool $a_post_dir_path = false,
57  ?string $a_table_id = ""
58  ) {
59  global $DIC;
60  $this->setId($a_table_id);
61  $this->ctrl = $DIC->ctrl();
62  $this->lng = $DIC->language();
63  if ($a_cur_dir !== realpath($a_cur_dir)) {
64  throw new \InvalidArgumentException('$a_cur_dir must be a absolute path');
65  }
66  $this->filesystem = LegacyPathHelper::deriveFilesystemFrom($a_cur_dir);
67  $this->relative_cur_dir = LegacyPathHelper::createRelativePath($a_cur_dir);
68  $this->cur_dir = $a_cur_dir;
69  $this->cur_subdir = $a_cur_subdir;
70  $this->label_enable = $a_label_enable;
71  $this->label_header = $a_label_header;
72  $this->file_labels = $a_file_labels;
73  $this->post_dir_path = $a_post_dir_path;
74  $this->filesystem_gui = $a_parent_obj;
75  $this->ui_factory = $DIC->ui()->factory();
76  $this->ui_renderer = $DIC->ui()->renderer();
77 
78  parent::__construct($a_parent_obj, $a_parent_cmd);
79  $this->setTitle($this->lng->txt("cont_files") . " " . $this->cur_subdir);
80 
81  $this->has_multi = false;
82 
83  foreach ((array) $a_commands as $i => $command) {
84  if (!($command["single"] ?? false)) {
85  // does also handle internal commands
86  $this->addMultiCommand("extCommand_" . $i, $command["name"]);
87  $this->has_multi = true;
88  } else {
89  $this->row_commands[] = array(
90  "cmd" => "extCommand_" . $i,
91  "caption" => $command["name"],
92  "allow_dir" => $command["allow_dir"] ?? false,
93  "method" => $command["method"] ?? null,
94  );
95  }
96  }
97  $this->addColumns();
98 
99  $this->setDefaultOrderField("name");
100  $this->setDefaultOrderDirection("asc");
101 
102  $this->setEnableHeader(true);
103  $this->setFormAction($this->ctrl->getFormAction($a_parent_obj));
104  $this->setRowTemplate(
105  "tpl.directory_row.html",
106  "Services/FileSystem"
107  );
108  $this->setEnableTitle(true);
109  }
110 
111  public function numericOrdering(string $a_field): bool
112  {
113  if ($a_field == "size") {
114  return true;
115  }
116  return false;
117  }
118 
119  protected function prepareOutput(): void
120  {
121  $this->determineOffsetAndOrder(true);
122  $this->setData($this->getEntries());
123  }
124 
128  public function getEntries(): array
129  {
130  if ($this->filesystem->has($this->relative_cur_dir)) {
131  $entries = [];
132  if ($this->cur_dir !== '') {
133  $entries['..'] = [
134  'order_val' => -1,
135  'order_id' => -1,
136  'entry' => '..',
137  'type' => 'dir',
138  'subdir' => '',
139  'size' => 0
140  ];
141  }
142 
143  foreach ($this->filesystem->listContents($this->relative_cur_dir) as $i => $content) {
144  $basename = basename($content->getPath());
145  $entries[$basename] = [
146  'order_val' => $i,
147  'order_id' => $i,
148  'entry' => $basename,
149  'type' => $content->isDir() ? 'dir' : 'file',
150  'subdir' => '',
151  'size' => $content->isFile() ? $this->filesystem->getSize($content->getPath(), 1)->inBytes() : 0
152  ];
153  }
154  } else {
155  $entries = array(array("type" => "dir", "entry" => ".."));
156  }
157  $items = array();
158 
159  foreach ($entries as $e) {
160  if (($e["entry"] == ".") || ($e["entry"] == ".." && empty($this->cur_subdir))) {
161  continue;
162  }
163  $cfile = (!empty($this->cur_subdir))
164  ? $this->cur_subdir . "/" . $e["entry"]
165  : $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[] = array(
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 (sizeof($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  protected function fillRow(array $a_set): void
217  {
218  $hash = $this->post_dir_path
219  ? md5($a_set["file"])
220  : md5($a_set["entry"]);
221 
222  if ($this->has_multi) {
223  if ($this->isDoubleDotDirectory($a_set)) {
224  $this->tpl->touchBlock('no_checkbox');
225  } else {
226  $this->tpl->setVariable("CHECKBOX_ID", $hash);
227  }
228  }
229 
230  // label
231  if ($this->label_enable) {
232  $this->tpl->setCurrentBlock("Label");
233  $this->tpl->setVariable("TXT_LABEL", $a_set["label"]);
234  $this->tpl->parseCurrentBlock();
235  }
236 
237  $this->ctrl->setParameter($this->parent_obj, "cdir", $this->cur_subdir);
238 
239  if ($a_set["type"] == "dir") {
240  $this->tpl->setCurrentBlock("FileLink");
241  $this->ctrl->setParameter($this->parent_obj, "newdir", $a_set["entry"]);
242  $this->ctrl->setParameter($this->parent_obj, "resetoffset", 1);
243  $this->tpl->setVariable(
244  "LINK_FILENAME",
245  $this->ctrl->getLinkTarget($this->parent_obj, "listFiles")
246  );
247  $this->ctrl->setParameter($this->parent_obj, "newdir", "");
248  $this->tpl->setVariable("TXT_FILENAME", $a_set["entry"]);
249  $this->tpl->parseCurrentBlock();
250 
251  $this->tpl->setVariable(
252  "ICON",
253  "<img src=\"" .
254  ilUtil::getImagePath("standard/icon_cat.svg") . "\">"
255  );
256  $this->ctrl->setParameter($this->parent_obj, "resetoffset", "");
257  } else {
258  $this->tpl->setCurrentBlock("File");
259  $this->tpl->setVariable("TXT_FILENAME2", $this->secure($a_set["entry"]));
260  $this->tpl->parseCurrentBlock();
261  }
262 
263  if ($a_set["type"] != "dir") {
264  $this->tpl->setVariable("TXT_SIZE", ilUtil::formatSize($a_set["size"]));
265  }
266 
267  // single item commands
268  if (count($this->row_commands) > 0 && !($a_set["type"] === "dir" && $a_set["entry"] === "..")) {
269  $actions = [];
270 
271  foreach ($this->row_commands as $rcom) {
272  if ($rcom["allow_dir"] || $a_set["type"] !== "dir") {
273  $file_path = $this->cur_dir . $a_set['entry'];
274  if (
275  $rcom['method'] !== ilFileSystemGUI::CMD_UNZIP_FILE
276  || ($rcom['method'] === ilFileSystemGUI::CMD_UNZIP_FILE && MimeType::getMimeType($file_path) === "application/zip")
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  $actions[] = $this->ui_factory->link()->standard($rcom["caption"], $url);
283  }
284  }
285  }
286 
287  $dropdown = $this->ui_factory->dropdown()->standard($actions);
288  $this->tpl->setVariable("ACTIONS", $this->ui_renderer->render($dropdown));
289  }
290  }
291 }
setData(array $a_data)
setFormAction(string $a_form_action, bool $a_multipart=false)
setEnableTitle(bool $a_enabletitle)
setSelectAllCheckbox(string $a_select_all_checkbox, bool $a_select_all_on_top=false)
static getImagePath(string $img, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
setId(string $a_val)
global $DIC
Definition: feed.php:28
__construct(VocabulariesInterface $vocabularies)
setDefaultOrderField(string $a_defaultorderfield)
ILIAS Filesystem Filesystem $filesystem
static formatSize(int $size, string $a_mode='short', ?ilLanguage $a_lng=null)
Returns the specified file size value in a human friendly form.
setRowTemplate(string $a_template, string $a_template_dir="")
Set row template.
setDefaultOrderDirection(string $a_defaultorderdirection)
$url
Definition: ltiregstart.php:35
fillRow(array $a_set)
Fill table row.
setTitle(string $a_title, string $a_icon="", string $a_icon_alt="")
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)
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.
addMultiCommand(string $a_cmd, string $a_text)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
determineOffsetAndOrder(bool $a_omit_offset=false)
setEnableHeader(bool $a_enableheader)