ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilFileVersionsGUI.php
Go to the documentation of this file.
1 <?php
2 
5 
11 {
12  const CMD_DEFAULT = 'index';
13  const CMD_DELETE_VERSIONS = "deleteVersions";
14  const CMD_ROLLBACK_VERSION = "rollbackVersion";
15  const CMD_DOWNLOAD_VERSION = "sendFile";
16  const HIST_ID = 'hist_id';
17  const CMD_CANCEL_DELETE = "cancelDeleteFile";
18  const CMD_CONFIRMED_DELETE_FILE = "confirmDeleteFile";
19  const CMD_CONFIRMED_DELETE_VERSIONS = 'confirmDeleteVersions';
20  const CMD_ADD_NEW_VERSION = 'addNewVersion';
21  const CMD_CREATE_NEW_VERSION = 'saveVersion';
22  const CMD_ADD_REPLACING_VERSION = 'addReplacingVersion';
23  const CMD_CREATE_REPLACING_VERSION = 'createReplacingVersion';
24  const CMD_MIGRATE = 'migrate';
28  private $toolbar;
32  private $access;
36  private $wsp_access;
40  private $ref_id;
44  private $lng;
48  private $http;
52  private $tabs;
56  private $ctrl;
60  private $tpl;
64  private $file;
68  protected $has_been_migrated = false;
69 
74  public function __construct(ilObjFile $file)
75  {
76  global $DIC;
77  $this->file = $file;
78  $this->ctrl = $DIC->ctrl();
79  $this->tpl = $DIC->ui()->mainTemplate();
80  $this->tabs = $DIC->tabs();
81  $this->http = $DIC->http();
82  $this->lng = $DIC->language();
83  $this->ref_id = (int) $this->http->request()->getQueryParams()['ref_id'];
84  $this->toolbar = $DIC->toolbar();
85  $this->access = $DIC->access();
86  $this->wsp_access = new ilWorkspaceAccessHandler();
87  $this->has_been_migrated = !empty($file->getResourceId());
88  }
89 
90  public function executeCommand()
91  {
92  // bugfix mantis 26007: use new function hasPermission to ensure that the check also works for workspace files
93  if (!$this->hasPermission('write')) {
94  ilUtil::sendFailure($this->lng->txt('permission_denied'), true);
95  $this->ctrl->returnToParent($this);
96  }
97  $cmd = $this->ctrl->getCmd(self::CMD_DEFAULT);
98 
99  switch ($cmd) {
100  case self::CMD_DEFAULT:
101  $this->index();
102  break;
103  case self::CMD_DOWNLOAD_VERSION:
104  $this->downloadVersion();
105  break;
106  case self::CMD_DELETE_VERSIONS:
107  $this->deleteVersions();
108  break;
109  case self::CMD_ROLLBACK_VERSION:
110  $this->rollbackVersion();
111  break;
112  case self::CMD_ADD_NEW_VERSION:
114  break;
115  case self::CMD_ADD_REPLACING_VERSION:
117  break;
118  case self::CMD_CREATE_NEW_VERSION:
120  // no break
121  case self::CMD_CREATE_REPLACING_VERSION:
123  break;
124  case self::CMD_CONFIRMED_DELETE_VERSIONS:
125  $this->confirmDeleteVersions();
126  break;
127  case self::CMD_CONFIRMED_DELETE_FILE:
128  $this->confirmDeleteFile();
129  break;
130  case self::CMD_MIGRATE:
131  $this->migrate();
132  break;
133  }
134  }
135 
136  private function index()
137  {
138  // Buttons
139  if ($this->has_been_migrated) {
140  $add_version = ilLinkButton::getInstance();
141  $add_version->setCaption('file_new_version');
142  $add_version->setUrl($this->ctrl->getLinkTarget($this, self::CMD_ADD_NEW_VERSION));
143  $this->toolbar->addButtonInstance($add_version);
144 
145  $replace_version = ilLinkButton::getInstance();
146  $replace_version->setCaption('replace_file');
147  $replace_version->setUrl($this->ctrl->getLinkTarget($this, self::CMD_ADD_REPLACING_VERSION));
148  $this->toolbar->addButtonInstance($replace_version);
149  } else {
150  $migrate = ilLinkButton::getInstance();
151  $migrate->setCaption('migrate');
152  $migrate->setUrl($this->ctrl->getLinkTarget($this, self::CMD_MIGRATE));
153 // $this->toolbar->addButtonInstance($migrate);
154  ilUtil::sendInfo($this->lng->txt('not_yet_migrated'));
155  }
156 
157  $table = new ilFileVersionsTableGUI($this, self::CMD_DEFAULT);
158  $this->tpl->setContent($table->getHTML());
159  }
160 
164  private function addVersion($mode = ilFileVersionFormGUI::MODE_ADD)
165  {
166  if (!$this->has_been_migrated) {
167  return;
168  }
169  $this->tabs->clearTargets();
170  $this->tabs->setBackTarget($this->lng->txt('back'), $this->ctrl->getLinkTarget($this, self::CMD_DEFAULT));
171 
172  $form = new ilFileVersionFormGUI($this, $mode);
173  $form->fillForm();
174  $this->tpl->setContent($form->getHTML());
175  }
176 
182  private function saveVersion($mode = ilFileVersionFormGUI::MODE_ADD)
183  {
184  if (!$this->has_been_migrated) {
185  return;
186  }
187  $form = new ilFileVersionFormGUI($this, $mode);
188  if ($form->saveObject()) {
189  ilUtil::sendSuccess($this->lng->txt('msg_obj_modified'), true);
190  $this->ctrl->redirect($this, self::CMD_DEFAULT);
191  }
192  $form->setValuesByPost();
193  $this->tpl->setContent($form->getHTML());
194  }
195 
196  private function downloadVersion()
197  {
198  $version = (int) $_GET[self::HIST_ID];
199  $this->file->sendFile($version);
200  try {
201  } catch (FileNotFoundException $e) {
202  }
203  }
204 
205  private function deleteVersions()
206  {
207  if (!$this->has_been_migrated) {
208  return;
209  }
210 
211  $version_ids = $this->getVersionIdsFromRequest();
212  $existing_versions = $this->file->getVersions();
213  $remaining_versions = array_udiff(
214  $existing_versions,
215  $version_ids,
216  static function ($a, $b) {
217  if ($a instanceof ilObjFileVersion) {
218  $a = $a->getHistEntryId();
219  }
220  if ($b instanceof ilObjFileVersion) {
221  $b = $b->getHistEntryId();
222  }
223  return $a - $b;
224  }
225  );
226 
227  if (count($version_ids) < 1) {
228  ilUtil::sendFailure($this->lng->txt("no_checkbox"), true);
229  $this->ctrl->redirect($this, self::CMD_DEFAULT);
230  } else {
231  $conf_gui = new ilConfirmationGUI();
232  $conf_gui->setFormAction($this->ctrl->getFormAction($this, self::CMD_DEFAULT));
233  $conf_gui->setCancel($this->lng->txt("cancel"), self::CMD_DEFAULT);
234 
235  $icon = ilObject::_getIcon($this->file->getId(), "small", $this->file->getType());
236  $alt = $this->lng->txt("icon") . " " . $this->lng->txt("obj_" . $this->file->getType());
237 
238  if (count($remaining_versions) < 1) {
239  // Ask
240  ilUtil::sendQuestion($this->lng->txt("file_confirm_delete_all_versions"));
241 
242  $conf_gui->setConfirm($this->lng->txt("confirm"), self::CMD_CONFIRMED_DELETE_FILE);
243  $conf_gui->addItem(
244  "id[]",
245  $this->ref_id,
246  $this->file->getTitle(),
247  $icon,
248  $alt
249  );
250  } else {
251  // Ask
252  ilUtil::sendQuestion($this->lng->txt("file_confirm_delete_versions"));
253 
254  $conf_gui->setConfirm($this->lng->txt("confirm"), self::CMD_CONFIRMED_DELETE_VERSIONS);
255 
256  foreach ($this->file->getVersions($version_ids) as $version) {
257  $a_text = $version['filename'] ?? $version->getFilename() ?? $this->file->getTitle();
258  $version_string = $version['hist_id'] ?? $version->getVersion();
259  $a_text .= " (v" . $version_string . ")";
260  $conf_gui->addItem(
261  "hist_id[]",
262  $version['hist_entry_id'],
263  $a_text,
264  $icon,
265  $alt
266  );
267  }
268  }
269 
270  $this->tpl->setContent($conf_gui->getHTML());
271  }
272  }
273 
274  private function rollbackVersion()
275  {
276  if (!$this->has_been_migrated) {
277  return;
278  }
279  $version_ids = $this->getVersionIdsFromRequest();
280 
281  // more than one entry selected?
282  if (count($version_ids) != 1) {
283  ilUtil::sendInfo($this->lng->txt("file_rollback_select_exact_one"), true);
284  $this->ctrl->redirect($this, self::CMD_DEFAULT);
285  }
286 
287  // rollback the version
288  $new_version = $this->file->rollback($version_ids[0]);
289 
290  ilUtil::sendSuccess(sprintf($this->lng->txt("file_rollback_done"), $new_version["rollback_version"]), true);
291  $this->ctrl->redirect($this, self::CMD_DEFAULT);
292  }
293 
294  private function confirmDeleteVersions()
295  {
296  if (!$this->has_been_migrated) {
297  return;
298  }
299  // delete versions after confirmation
300  $versions_to_delete = $this->getVersionIdsFromRequest();
301  if (is_array($versions_to_delete) && count($versions_to_delete) > 0) {
302  $this->file->deleteVersions($versions_to_delete);
303  ilUtil::sendSuccess($this->lng->txt("file_versions_deleted"), true);
304  }
305 
306  $this->ctrl->setParameter($this, self::HIST_ID, "");
307  $this->ctrl->redirect($this, self::CMD_DEFAULT);
308  }
309 
310  private function confirmDeleteFile()
311  {
312  if (!$this->has_been_migrated) {
313  return;
314  }
315  global $DIC;
316 
317  $parent_id = $DIC->repositoryTree()->getParentId($this->ref_id);
318 
319  $ru = new ilRepUtilGUI($this);
320  $ru->deleteObjects($parent_id, array($this->ref_id));
321 
322  // redirect to parent object
323  $this->ctrl->setParameterByClass(ilRepositoryGUI::class, "ref_id", $parent_id);
324  $this->ctrl->redirectByClass(ilRepositoryGUI::class);
325  }
326 
327  private function migrate() : void
328  {
329  global $DIC;
330  $migration = new ilFileObjectToStorageMigrationRunner(
331  $DIC->fileSystem()->storage(),
332  $DIC->database(),
333  rtrim(CLIENT_DATA_DIR, "/") . '/ilFile/migration_log.csv'
334  );
335  $migration->migrate(new ilFileObjectToStorageDirectory($this->file->getId(), $this->file->getDirectory()));
336  $this->ctrl->redirect($this, self::CMD_DEFAULT);
337  }
338 
342  public function getFile() : ilObjFile
343  {
344  return $this->file;
345  }
346 
350  private function getVersionIdsFromRequest() : array
351  {
352  // get ids either from GET (if single item was clicked) or
353  // from POST (if multiple items were selected)
354  $request = $this->http->request();
355 
356  $version_ids = [];
357  if (isset($request->getQueryParams()[self::HIST_ID])) {
358  $version_ids = [$request->getQueryParams()[self::HIST_ID]];
359  } elseif (isset($request->getParsedBody()[self::HIST_ID])) {
360  $version_ids = (array) $request->getParsedBody()[self::HIST_ID];
361  }
362 
363  array_walk($version_ids, static function (&$i) {
364  $i = (int) $i;
365  });
366 
367  return $version_ids;
368  }
369 
374  private function getVersionsToKeep(array $version_ids) : array
375  {
376  $versions_to_keep = $this->file->getVersions();
377  array_udiff($versions_to_keep, $version_ids, static function ($v1, $v2) {
378  if (is_array($v1) || $v1 instanceof ilObjFileVersion) {
379  $v1 = (int) $v1["hist_entry_id"];
380  } else {
381  if (!is_numeric($v1)) {
382  $v1 = (int) $v1;
383  }
384  }
385 
386  if (is_array($v2) || $v2 instanceof ilObjFileVersion) {
387  $v2 = (int) $v2["hist_entry_id"];
388  } else {
389  if (!is_numeric($v2)) {
390  $v2 = (int) $v2;
391  }
392  }
393 
394  return $v1 === $v2;
395  });
396 
397  return $versions_to_keep;
398  }
399 
407  private function hasPermission($a_permission)
408  {
409  // determine if the permission check concerns a workspace- or repository-object
410  if (isset($_GET['wsp_id'])) {
411  // permission-check concerning a workspace object
412  if ($this->wsp_access->checkAccess($a_permission, "", $this->ref_id)) {
413  return true;
414  }
415  } else {
416  // permission-check concerning a repository object
417  if ($this->access->checkAccess($a_permission, '', $this->ref_id)) {
418  return true;
419  }
420  }
421 
422  return false;
423  }
424 }
Class ilObjFileVersion.
static _getIcon( $a_obj_id="", $a_size="big", $a_type="", $a_offline=false)
Get icon for repository item.
Class ilFileObjectToStorageDirectory.
$_GET["client_id"]
Class ilFileVersionFormGUI.
Class ilFileVersionsGUI.
Access handler for personal workspace.
saveVersion($mode=ilFileVersionFormGUI::MODE_ADD)
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
const CLIENT_DATA_DIR
Definition: constants.php:44
static http()
Fetches the global http state from ILIAS.
Class ilFileVersionsTableGUI.
global $DIC
Definition: goto.php:24
Repository GUI Utilities.
static sendQuestion($a_info="", $a_keep=false)
Send Question to Screen.
addVersion($mode=ilFileVersionFormGUI::MODE_ADD)
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
__construct(ilObjFile $file)
ilFileVersionsGUI constructor.
getVersionsToKeep(array $version_ids)
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
Class FileNotFoundException Indicates that a file is missing or not found.
hasPermission($a_permission)
bugfix mantis 26007: this function was created to ensure that the access check not only works for rep...
$i
Definition: metadata.php:24
Confirmation screen class.