ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilFileVersionsGUI.php
Go to the documentation of this file.
1 <?php
2 
5 
12 {
13  const CMD_DEFAULT = 'index';
14  const CMD_DELETE_VERSIONS = "deleteVersions";
15  const CMD_ROLLBACK_VERSION = "rollbackVersion";
16  const CMD_DOWNLOAD_VERSION = "sendFile";
17  const HIST_ID = 'hist_id';
18  const CMD_CANCEL_DELETE = "cancelDeleteFile";
19  const CMD_CONFIRMED_DELETE_FILE = "confirmDeleteFile";
20  const CMD_CONFIRMED_DELETE_VERSIONS = 'confirmDeleteVersions';
21  const CMD_ADD_NEW_VERSION = 'addNewVersion';
22  const CMD_CREATE_NEW_VERSION = 'saveVersion';
23  const CMD_ADD_REPLACING_VERSION = 'addReplacingVersion';
24  const CMD_CREATE_REPLACING_VERSION = 'createReplacingVersion';
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;
65 
66 
72  public function __construct(ilObjFile $file)
73  {
74  global $DIC;
75  $this->file = $file;
76  $this->ctrl = $DIC->ctrl();
77  $this->tpl = $DIC->ui()->mainTemplate();
78  $this->tabs = $DIC->tabs();
79  $this->http = $DIC->http();
80  $this->lng = $DIC->language();
81  $this->ref_id = (int) $this->http->request()->getQueryParams()['ref_id'];
82  $this->toolbar = $DIC->toolbar();
83  $this->access = $DIC->access();
84  $this->wsp_access = new ilWorkspaceAccessHandler();
85  }
86 
87 
88  public function executeCommand()
89  {
90  // bugfix mantis 26007: use new function hasPermission to ensure that the check also works for workspace files
91  if (!$this->hasPermission('write')) {
92  ilUtil::sendFailure($this->lng->txt('permission_denied'), true);
93  $this->ctrl->returnToParent($this);
94  }
95  $cmd = $this->ctrl->getCmd(self::CMD_DEFAULT);
96 
97  switch ($cmd) {
98  case self::CMD_DEFAULT:
99  $this->index();
100  break;
101  case self::CMD_DOWNLOAD_VERSION:
102  $this->downloadVersion();
103  break;
104  case self::CMD_DELETE_VERSIONS:
105  $this->deleteVersions();
106  break;
107  case self::CMD_ROLLBACK_VERSION:
108  $this->rollbackVersion();
109  break;
110  case self::CMD_ADD_NEW_VERSION:
112  break;
113  case self::CMD_ADD_REPLACING_VERSION:
115  break;
116  case self::CMD_CREATE_NEW_VERSION:
118  // no break
119  case self::CMD_CREATE_REPLACING_VERSION:
121  break;
122  case self::CMD_CONFIRMED_DELETE_VERSIONS:
123  $this->confirmDeleteVersions();
124  break;
125  case self::CMD_CONFIRMED_DELETE_FILE:
126  $this->confirmDeleteFile();
127  break;
128  }
129  }
130 
131 
132  private function index()
133  {
134  // Buttons
135  $add_version = ilLinkButton::getInstance();
136  $add_version->setCaption('file_new_version');
137  $add_version->setUrl($this->ctrl->getLinkTarget($this, self::CMD_ADD_NEW_VERSION));
138  $this->toolbar->addButtonInstance($add_version);
139 
140  $replace_version = ilLinkButton::getInstance();
141  $replace_version->setCaption('replace_file');
142  $replace_version->setUrl($this->ctrl->getLinkTarget($this, self::CMD_ADD_REPLACING_VERSION));
143  $this->toolbar->addButtonInstance($replace_version);
144 
145  $table = new ilFileVersionsTableGUI($this, self::CMD_DEFAULT);
146  $this->tpl->setContent($table->getHTML());
147  }
148 
149 
153  private function addVersion($mode = ilFileVersionFormGUI::MODE_ADD)
154  {
155  $this->tabs->clearTargets();
156  $this->tabs->setBackTarget($this->lng->txt('back'), $this->ctrl->getLinkTarget($this, self::CMD_DEFAULT));
157 
158  $form = new ilFileVersionFormGUI($this, $mode);
159  $form->fillForm();
160  $this->tpl->setContent($form->getHTML());
161  }
162 
163 
170  private function saveVersion($mode = ilFileVersionFormGUI::MODE_ADD)
171  {
172  $form = new ilFileVersionFormGUI($this, $mode);
173  if ($form->saveObject()) {
174  ilUtil::sendSuccess($this->lng->txt('msg_obj_modified'), true);
175  $this->ctrl->redirect($this, self::CMD_DEFAULT);
176  }
177  $form->setValuesByPost();
178  $this->tpl->setContent($form->getHTML());
179  }
180 
181 
182  private function downloadVersion()
183  {
184  $version = (int) $_GET[self::HIST_ID];
185  $this->file->sendFile($version);
186  try {
187  } catch (FileNotFoundException $e) {
188  }
189  }
190 
191 
192  private function deleteVersions()
193  {
194  $version_ids = $this->getVersionIdsFromRequest();
195 
196  if (count($version_ids) < 1) {
197  ilUtil::sendFailure($this->lng->txt("no_checkbox"), true);
198  $this->ctrl->redirect($this, self::CMD_DEFAULT);
199  } else {
200  // check if all versions are selected
201  $versions_to_keep = $this->getVersionsToKeep($version_ids);
202 
203  $conf_gui = new ilConfirmationGUI();
204  $conf_gui->setFormAction($this->ctrl->getFormAction($this, self::CMD_DEFAULT));
205  $conf_gui->setCancel($this->lng->txt("cancel"), self::CMD_CANCEL_DELETE);
206 
207  $icon = ilObject::_getIcon($this->file->getId(), "small", $this->file->getType());
208  $alt = $this->lng->txt("icon") . " " . $this->lng->txt("obj_" . $this->file->getType());
209 
210  if (count($versions_to_keep) < 1) {
211  // Ask
212  ilUtil::sendQuestion($this->lng->txt("file_confirm_delete_all_versions"));
213 
214  $conf_gui->setConfirm($this->lng->txt("confirm"), self::CMD_CONFIRMED_DELETE_FILE);
215  $conf_gui->addItem(
216  "id[]",
217  $this->ref_id,
218  $this->file->getTitle(),
219  $icon,
220  $alt
221  );
222  } else {
223  // Ask
224  ilUtil::sendQuestion($this->lng->txt("file_confirm_delete_versions"));
225 
226  $conf_gui->setConfirm($this->lng->txt("confirm"), self::CMD_CONFIRMED_DELETE_VERSIONS);
227 
241  foreach ($this->file->getVersions($version_ids) as $version) {
242  $conf_gui->addItem(
243  "hist_id[]",
244  $version['hist_entry_id'],
245  $version['filename'] ?? $this->file->getTitle(),
246  $icon,
247  $alt
248  );
249  }
250  }
251 
252  $this->tpl->setContent($conf_gui->getHTML());
253  }
254  }
255 
256 
257  private function rollbackVersion()
258  {
259  $version_ids = $this->getVersionIdsFromRequest();
260 
261  // more than one entry selected?
262  if (count($version_ids) != 1) {
263  ilUtil::sendInfo($this->lng->txt("file_rollback_select_exact_one"), true);
264  $this->ctrl->redirect($this, self::CMD_DEFAULT);
265  }
266 
267  // rollback the version
268  $new_version = $this->file->rollback($version_ids[0]);
269 
270  ilUtil::sendSuccess(sprintf($this->lng->txt("file_rollback_done"), $new_version["rollback_version"]), true);
271  $this->ctrl->redirect($this, self::CMD_DEFAULT);
272  }
273 
274 
275  private function confirmDeleteVersions()
276  {
277  // delete versions after confirmation
278  if (is_array($_POST[self::HIST_ID]) && count($_POST[self::HIST_ID]) > 0) {
279  $this->file->deleteVersions($_POST[self::HIST_ID]);
280  ilUtil::sendSuccess($this->lng->txt("file_versions_deleted"), true);
281  }
282 
283  $this->ctrl->setParameter($this, self::HIST_ID, "");
284  $this->ctrl->redirect($this, self::CMD_DEFAULT);
285  }
286 
287 
288  private function confirmDeleteFile()
289  {
290  global $DIC;
291 
292  $parent_id = $DIC->repositoryTree()->getParentId($this->ref_id);
293 
294  $ru = new ilRepUtilGUI($this);
295  $ru->deleteObjects($parent_id, array($this->ref_id));
296 
297  // redirect to parent object
298  $this->ctrl->setParameterByClass(ilRepositoryGUI::class, "ref_id", $parent_id);
299  $this->ctrl->redirectByClass(ilRepositoryGUI::class);
300  }
301 
302 
306  public function getFile() : ilObjFile
307  {
308  return $this->file;
309  }
310 
311 
315  private function getVersionIdsFromRequest() : array
316  {
317  // get ids either from GET (if single item was clicked) or
318  // from POST (if multiple items were selected)
319  $request = $this->http->request();
320 
321  $version_ids = [];
322  if (isset($request->getQueryParams()[self::HIST_ID])) {
323  $version_ids = [$request->getQueryParams()[self::HIST_ID]];
324  } elseif (isset($request->getParsedBody()[self::HIST_ID])) {
325  $version_ids = (array) $request->getParsedBody()[self::HIST_ID];
326  }
327 
328  return $version_ids;
329  }
330 
331 
337  private function getVersionsToKeep(array $version_ids) : array
338  {
339  $versions_to_keep = array_udiff($this->file->getVersions(), $version_ids, function ($v1, $v2) {
340  if (is_array($v1)) {
341  $v1 = (int) $v1["hist_entry_id"];
342  } else {
343  if (!is_int($v1)) {
344  $v1 = (int) $v1;
345  }
346  }
347 
348  if (is_array($v2)) {
349  $v2 = (int) $v2["hist_entry_id"];
350  } else {
351  if (!is_int($v2)) {
352  $v2 = (int) $v2;
353  }
354  }
355 
356  return $v1 - $v2;
357  });
358 
359  return $versions_to_keep;
360  }
361 
362 
372  private function hasPermission($a_permission)
373  {
374  // determine if the permission check concerns a workspace- or repository-object
375  if (isset($_GET['wsp_id'])) {
376  // permission-check concerning a workspace object
377  if ($this->wsp_access->checkAccess($a_permission, "", $this->ref_id)) {
378  return true;
379  }
380  } else {
381  // permission-check concerning a repository object
382  if ($this->access->checkAccess($a_permission, '', $this->ref_id)) {
383  return true;
384  }
385  }
386 
387  return false;
388  }
389 }
foreach($paths as $path) $request
Definition: asyncclient.php:32
global $DIC
Definition: saml.php:7
$_GET["client_id"]
Class ilFileVersionFormGUI.
Class ilFileVersionsGUI.
Access handler for personal workspace.
saveVersion($mode=ilFileVersionFormGUI::MODE_ADD)
$version
Definition: build.php:27
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
static http()
Fetches the global http state from ILIAS.
if(isset($_POST['submit'])) $form
Class ilFileVersionsTableGUI.
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)
hasPermission($a_permission)
bugfix mantis 26007: this function was created to ensure that the access check not only works for rep...
if(empty($password)) $table
Definition: pwgen.php:24
$_POST["username"]
Confirmation screen class.