ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilFileSystemTableGUI.php
Go to the documentation of this file.
1 <?php
2 
22 
27 {
28  use SecureString;
29 
30  // This is just for those legacy classes which will be removed soon anyway.
31  private \ILIAS\UI\Factory $ui_factory;
32  private \ILIAS\UI\Renderer $ui_renderer;
33  protected bool $has_multi = false;
34  protected array $row_commands = [];
35  protected bool $label_enable = false;
36  protected string $label_header = "";
37  protected string $cur_dir = '';
38  protected string $cur_subdir = '';
39  protected string $relative_cur_dir;
40  protected ?bool $post_dir_path = null;
41  protected array $file_labels = [];
42  protected \ILIAS\Filesystem\Filesystem $filesystem;
44 
48  public function __construct(
49  ilFileSystemGUI $a_parent_obj,
50  string $a_parent_cmd,
51  string $a_cur_dir,
52  string $a_cur_subdir,
53  bool $a_label_enable,
54  ?array $a_file_labels = [],
55  ?string $a_label_header = "",
56  ?array $a_commands = [],
57  ?bool $a_post_dir_path = false,
58  ?string $a_table_id = ""
59  ) {
60  global $DIC;
61  $this->setId($a_table_id);
62  $this->ctrl = $DIC->ctrl();
63  $this->lng = $DIC->language();
64  if ($a_cur_dir !== realpath($a_cur_dir)) {
65  throw new \InvalidArgumentException('$a_cur_dir must be a absolute path');
66  }
67  $this->filesystem = LegacyPathHelper::deriveFilesystemFrom($a_cur_dir);
68  $this->relative_cur_dir = LegacyPathHelper::createRelativePath($a_cur_dir);
69  $this->cur_dir = $a_cur_dir;
70  $this->cur_subdir = $a_cur_subdir;
71  $this->label_enable = $a_label_enable;
72  $this->label_header = $a_label_header;
73  $this->file_labels = $a_file_labels;
74  $this->post_dir_path = $a_post_dir_path;
75  $this->filesystem_gui = $a_parent_obj;
76  $this->ui_factory = $DIC->ui()->factory();
77  $this->ui_renderer = $DIC->ui()->renderer();
78 
79  parent::__construct($a_parent_obj, $a_parent_cmd);
80  $this->setTitle($this->lng->txt("cont_files") . " " . $this->cur_subdir);
81 
82  $this->has_multi = false;
83 
84  foreach ((array) $a_commands as $i => $command) {
85  if (!($command["single"] ?? false)) {
86  // does also handle internal commands
87  $this->addMultiCommand("extCommand_" . $i, $command["name"]);
88  $this->has_multi = true;
89  } else {
90  $this->row_commands[] = array(
91  "cmd" => "extCommand_" . $i,
92  "caption" => $command["name"],
93  "allow_dir" => $command["allow_dir"] ?? false,
94  "method" => $command["method"] ?? null,
95  );
96  }
97  }
98  $this->addColumns();
99 
100  $this->setDefaultOrderField("name");
101  $this->setDefaultOrderDirection("asc");
102 
103  $this->setEnableHeader(true);
104  $this->setFormAction($this->ctrl->getFormAction($a_parent_obj));
105  $this->setRowTemplate(
106  "tpl.directory_row.html",
107  "components/ILIAS/Filesystem"
108  );
109  $this->setEnableTitle(true);
110  }
111 
112  public function numericOrdering(string $a_field): bool
113  {
114  if ($a_field == "size") {
115  return true;
116  }
117  return false;
118  }
119 
120  protected function prepareOutput(): void
121  {
122  $this->determineOffsetAndOrder(true);
123  $this->setData($this->getEntries());
124  }
125 
129  public function getEntries(): array
130  {
132  if ($this->filesystem->has($path)) {
133  $entries = [];
134  if ($this->cur_dir !== '') {
135  $entries['..'] = [
136  'order_val' => -1,
137  'order_id' => -1,
138  'entry' => '..',
139  'type' => 'dir',
140  'subdir' => '',
141  'size' => 0
142  ];
143  }
144 
145  foreach ($this->filesystem->listContents($path) as $i => $content) {
146  $basename = basename($content->getPath());
147  $entries[$basename] = [
148  'order_val' => $i,
149  'order_id' => $i,
150  'entry' => $basename,
151  'type' => $content->isDir() ? 'dir' : 'file',
152  'subdir' => '',
153  'size' => $content->isFile() ? $this->filesystem->getSize($content->getPath(), 1)->inBytes() : 0
154  ];
155  }
156  } else {
157  $entries = array(array("type" => "dir", "entry" => ".."));
158  }
159  $items = array();
160 
161  foreach ($entries as $e) {
162  if (($e["entry"] == ".") || ($e["entry"] == ".." && empty($this->cur_subdir))) {
163  continue;
164  }
165  $cfile = (!empty($this->cur_subdir))
166  ? $this->cur_subdir . "/" . $e["entry"]
167  : $e["entry"];
168 
169  if ($this->label_enable) {
170  $label = (isset($this->file_labels[$cfile]) && is_array($this->file_labels[$cfile]))
171  ? implode(", ", $this->file_labels[$cfile])
172  : "";
173  }
174 
175  $pref = ($e["type"] == "dir")
176  ? ($this->getOrderDirection() != "desc" ? "1_" : "9_")
177  : "5_";
178  $items[] = array(
179  "file" => $cfile,
180  "entry" => $e["entry"],
181  "type" => $e["type"],
182  "label" => $label ?? '',
183  "size" => $e["size"] ?? '',
184  "name" => $pref . $e["entry"]
185  );
186  }
187  return $items;
188  }
189 
190  public function addColumns(): void
191  {
192  if ($this->has_multi) {
193  $this->setSelectAllCheckbox("file[]");
194  $this->addColumn("", "", "1", true);
195  }
196  $this->addColumn("", "", "1", true); // icon
197 
198  $this->addColumn($this->lng->txt("cont_dir_file"), "name");
199  $this->addColumn($this->lng->txt("cont_size"), "size");
200 
201  if ($this->label_enable) {
202  $this->addColumn($this->label_header, "label");
203  }
204 
205  if (sizeof($this->row_commands)) {
206  $this->addColumn($this->lng->txt("actions"));
207  }
208  }
209 
210  private function isDoubleDotDirectory(array $entry): bool
211  {
212  return $entry['entry'] === '..';
213  }
214 
218  protected function fillRow(array $a_set): void
219  {
220  $hash = $this->post_dir_path
221  ? md5($a_set["file"])
222  : md5($a_set["entry"]);
223 
224  if ($this->has_multi) {
225  if ($this->isDoubleDotDirectory($a_set)) {
226  $this->tpl->touchBlock('no_checkbox');
227  } else {
228  $this->tpl->setVariable("CHECKBOX_ID", $hash);
229  }
230  }
231 
232  // label
233  if ($this->label_enable) {
234  $this->tpl->setCurrentBlock("Label");
235  $this->tpl->setVariable("TXT_LABEL", $a_set["label"]);
236  $this->tpl->parseCurrentBlock();
237  }
238 
239  $this->ctrl->setParameter($this->parent_obj, "cdir", $this->cur_subdir);
240 
241  if ($a_set["type"] == "dir") {
242  $this->tpl->setCurrentBlock("FileLink");
243  $this->ctrl->setParameter($this->parent_obj, "newdir", $a_set["entry"]);
244  $this->ctrl->setParameter($this->parent_obj, "resetoffset", 1);
245  $this->tpl->setVariable(
246  "LINK_FILENAME",
247  $this->ctrl->getLinkTarget($this->parent_obj, "listFiles")
248  );
249  $this->ctrl->setParameter($this->parent_obj, "newdir", "");
250  $this->tpl->setVariable("TXT_FILENAME", $a_set["entry"]);
251  $this->tpl->parseCurrentBlock();
252 
253  $this->tpl->setVariable(
254  "ICON",
255  "<img src=\"" .
256  ilUtil::getImagePath("standard/icon_cat.svg") . "\">"
257  );
258  $this->ctrl->setParameter($this->parent_obj, "resetoffset", "");
259  } else {
260  $this->tpl->setCurrentBlock("File");
261  $this->tpl->setVariable("TXT_FILENAME2", $this->secure($a_set["entry"]));
262  $this->tpl->parseCurrentBlock();
263  }
264 
265  if ($a_set["type"] != "dir") {
266  $this->tpl->setVariable("TXT_SIZE", ilUtil::formatSize($a_set["size"]));
267  }
268 
269  // single item commands
270  if (count($this->row_commands) > 0 && !($a_set["type"] === "dir" && $a_set["entry"] === "..")) {
271  $actions = [];
272 
273  foreach ($this->row_commands as $rcom) {
274  if ($rcom["allow_dir"] || $a_set["type"] !== "dir") {
275  $file_path = $this->cur_dir . $a_set['entry'];
276  if (
277  $rcom['method'] !== ilFileSystemGUI::CMD_UNZIP_FILE
278  || ($rcom['method'] === ilFileSystemGUI::CMD_UNZIP_FILE && MimeType::getMimeType($file_path) === "application/zip")
279  ) {
280  $this->ctrl->setParameter($this->parent_obj, "fhsh", $hash);
281  $url = $this->ctrl->getLinkTarget($this->parent_obj, $rcom["cmd"]);
282  $this->ctrl->setParameter($this->parent_obj, "fhsh", "");
283 
284  $actions[] = $this->ui_factory->link()->standard($rcom["caption"], $url);
285  }
286  }
287  }
288 
289  $dropdown = $this->ui_factory->dropdown()->standard($actions);
290  $this->tpl->setVariable("ACTIONS", $this->ui_renderer->render($dropdown));
291  }
292  }
293 }
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)
$url
Definition: shib_logout.php:63
setId(string $a_val)
$path
Definition: ltiservices.php:30
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.
global $DIC
Definition: shib_login.php:25
static getImagePath(string $image_name, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
setDefaultOrderDirection(string $a_defaultorderdirection)
fillRow(array $a_set)
Fill table row.
setTitle(string $a_title, string $a_icon="", string $a_icon_alt="")
__construct(Container $dic, ilPlugin $plugin)
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)