ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilFileSystemTableGUI.php
Go to the documentation of this file.
1 <?php
2 
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 !== realpath($a_cur_dir)) {
62  throw new \InvalidArgumentException('$a_cur_dir must be a absolute path');
63  }
64  $this->filesystem = LegacyPathHelper::deriveFilesystemFrom($a_cur_dir);
65  $this->relative_cur_dir = LegacyPathHelper::createRelativePath($a_cur_dir);
66  $this->cur_dir = $a_cur_dir;
67  $this->ui_factory = $DIC->ui()->factory();
68  $this->ui_renderer = $DIC->ui()->renderer();
69 
70  parent::__construct($this->filesystem_gui, $a_parent_cmd);
71  $this->setTitle($this->lng->txt("cont_files") . " " . $this->cur_subdir);
72 
73  $this->has_multi = false;
74 
75  foreach ((array) $a_commands as $i => $command) {
76  if (!($command["single"] ?? false)) {
77  // does also handle internal commands
78  $this->addMultiCommand("extCommand_" . $i, $command["name"]);
79  $this->has_multi = true;
80  } else {
81  $this->row_commands[] = [
82  "cmd" => "extCommand_" . $i,
83  "caption" => $command["name"],
84  "allow_dir" => $command["allow_dir"] ?? false,
85  "method" => $command["method"] ?? null,
86  ];
87  }
88  }
89  $this->addColumns();
90 
91  $this->setDefaultOrderField("name");
92  $this->setDefaultOrderDirection("asc");
93 
94  $this->setEnableHeader(true);
95  $this->setFormAction($this->ctrl->getFormAction($this->filesystem_gui));
96  $this->setRowTemplate(
97  "tpl.directory_row.html",
98  "components/ILIAS/Filesystem"
99  );
100  $this->setEnableTitle(true);
101  }
102 
103  #[\Override]
104  public function numericOrdering(string $a_field): bool
105  {
106  return $a_field === "size";
107  }
108 
109  protected function prepareOutput(): void
110  {
111  $this->determineOffsetAndOrder(true);
112  $this->setData($this->getEntries());
113  }
114 
118  public function getEntries(): array
119  {
121  if ($this->filesystem->has($path)) {
122  $entries = [];
123  if ($this->cur_dir !== '') {
124  $entries['..'] = [
125  'order_val' => -1,
126  'order_id' => -1,
127  'entry' => '..',
128  'type' => 'dir',
129  'subdir' => '',
130  'size' => 0
131  ];
132  }
133 
134  foreach ($this->filesystem->listContents($path) as $i => $content) {
135  $basename = basename($content->getPath());
136  $entries[$basename] = [
137  'order_val' => $i,
138  'order_id' => $i,
139  'entry' => $basename,
140  'type' => $content->isDir() ? 'dir' : 'file',
141  'subdir' => '',
142  'size' => $content->isFile() ? $this->filesystem->getSize($content->getPath(), 1)->inBytes() : 0
143  ];
144  }
145  } else {
146  $entries = [["type" => "dir", "entry" => ".."]];
147  }
148  $items = [];
149 
150  foreach ($entries as $e) {
151  if ($e["entry"] === ".") {
152  continue;
153  }
154  if ($e["entry"] === ".." && empty($this->cur_subdir)) {
155  continue;
156  }
157  $cfile = (empty($this->cur_subdir))
158  ? $e["entry"]
159  : $this->cur_subdir . "/" . $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[] = [
171  "file" => $cfile,
172  "entry" => $e["entry"],
173  "type" => $e["type"],
174  "label" => $label ?? '',
175  "size" => $e["size"] ?? '',
176  "name" => $pref . $e["entry"]
177  ];
178  }
179  return $items;
180  }
181 
182  public function addColumns(): void
183  {
184  if ($this->has_multi) {
185  $this->setSelectAllCheckbox("file[]");
186  $this->addColumn("", "", "1", true);
187  }
188  $this->addColumn("", "", "1", true); // icon
189 
190  $this->addColumn($this->lng->txt("cont_dir_file"), "name");
191  $this->addColumn($this->lng->txt("cont_size"), "size");
192 
193  if ($this->label_enable) {
194  $this->addColumn($this->label_header, "label");
195  }
196 
197  if ($this->row_commands !== []) {
198  $this->addColumn($this->lng->txt("actions"));
199  }
200  }
201 
202  private function isDoubleDotDirectory(array $entry): bool
203  {
204  return $entry['entry'] === '..';
205  }
206 
210  #[\Override]
211  protected function fillRow(array $a_set): void
212  {
213  $hash = $this->post_dir_path
214  ? md5((string) $a_set["file"])
215  : md5((string) $a_set["entry"]);
216 
217  if ($this->has_multi) {
218  if ($this->isDoubleDotDirectory($a_set)) {
219  $this->tpl->touchBlock('no_checkbox');
220  } else {
221  $this->tpl->setVariable("CHECKBOX_ID", $hash);
222  }
223  }
224 
225  // label
226  if ($this->label_enable) {
227  $this->tpl->setCurrentBlock("Label");
228  $this->tpl->setVariable("TXT_LABEL", $a_set["label"]);
229  $this->tpl->parseCurrentBlock();
230  }
231 
232  $this->ctrl->setParameter($this->parent_obj, "cdir", $this->cur_subdir);
233 
234  if ($a_set["type"] == "dir") {
235  $this->tpl->setCurrentBlock("FileLink");
236  $this->ctrl->setParameter($this->parent_obj, "newdir", $a_set["entry"]);
237  $this->ctrl->setParameter($this->parent_obj, "resetoffset", 1);
238  $this->tpl->setVariable(
239  "LINK_FILENAME",
240  $this->ctrl->getLinkTarget($this->parent_obj, "listFiles")
241  );
242  $this->ctrl->setParameter($this->parent_obj, "newdir", "");
243  $this->tpl->setVariable("TXT_FILENAME", $a_set["entry"]);
244  $this->tpl->parseCurrentBlock();
245 
246  $this->tpl->setVariable(
247  "ICON",
248  "<img src=\"" .
249  ilUtil::getImagePath("standard/icon_cat.svg") . "\">"
250  );
251  $this->ctrl->setParameter($this->parent_obj, "resetoffset", "");
252  } else {
253  $this->tpl->setCurrentBlock("File");
254  $this->tpl->setVariable("TXT_FILENAME2", $this->secure($a_set["entry"]));
255  $this->tpl->parseCurrentBlock();
256  }
257 
258  if ($a_set["type"] != "dir") {
259  $this->tpl->setVariable("TXT_SIZE", ilUtil::formatSize($a_set["size"]));
260  }
261 
262  // single item commands
263  if ($this->row_commands !== [] && !($a_set["type"] === "dir" && $a_set["entry"] === "..")) {
264  $actions = [];
265 
266  foreach ($this->row_commands as $rcom) {
267  if ($rcom["allow_dir"] || $a_set["type"] !== "dir") {
268  $file_path = $this->cur_dir . $a_set['entry'];
269  if (
270  $rcom['method'] !== ilFileSystemGUI::CMD_UNZIP_FILE
271  || ($rcom['method'] === ilFileSystemGUI::CMD_UNZIP_FILE && MimeType::getMimeType($file_path) === "application/zip")
272  ) {
273  $this->ctrl->setParameter($this->parent_obj, "fhsh", $hash);
274  $url = $this->ctrl->getLinkTarget($this->parent_obj, $rcom["cmd"]);
275  $this->ctrl->setParameter($this->parent_obj, "fhsh", "");
276 
277  $actions[] = $this->ui_factory->link()->standard($rcom["caption"], $url);
278  }
279  }
280  }
281 
282  $dropdown = $this->ui_factory->dropdown()->standard($actions);
283  $this->tpl->setVariable("ACTIONS", $this->ui_renderer->render($dropdown));
284  }
285  }
286 }
setData(array $a_data)
setFormAction(string $a_form_action, bool $a_multipart=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setEnableTitle(bool $a_enabletitle)
setSelectAllCheckbox(string $a_select_all_checkbox, bool $a_select_all_on_top=false)
$url
Definition: shib_logout.php:68
setId(string $a_val)
$path
Definition: ltiservices.php:29
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
setDefaultOrderField(string $a_defaultorderfield)
This is how the factory for UI elements looks.
Definition: Factory.php:37
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:26
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.
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)
__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.