ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilFileVersionFormGUI.php
Go to the documentation of this file.
1 <?php
2 
22 
29 {
31  public const MODE_ADD = 1;
32  public const MODE_REPLACE = 2;
33  public const F_TITLE = 'title';
34  public const F_DESCRIPTION = "description";
35  public const F_FILE = "file";
36  public const F_SAVE_MODE = 'save_mode';
37 
38  private \ilObjFile $file;
40  private \ILIAS\UI\Factory $ui_factory;
41  private \ILIAS\UI\Renderer $ui_renderer;
44  private ilLanguage $lng;
45  private \ILIAS\ResourceStorage\Services $resource_services;
46  private string $post_url;
48 
52  public function __construct(ilFileVersionsGUI $file_version_gui, private int $save_mode = self::MODE_ADD)
53  {
54  global $DIC;
55  $this->file = $file_version_gui->getFile();
56  ;
57  $this->lng = $DIC->language();
58  $this->ui_factory = $DIC->ui()->factory();
59  $this->ui_renderer = $DIC->ui()->renderer();
60  $this->request = $DIC->http()->request();
61  $this->global_tpl = $DIC->ui()->mainTemplate();
62  $this->resource_services = $DIC->resourceStorage();
63  $this->post_url = $DIC->ctrl()->getFormAction(
64  $file_version_gui,
65  $this->resolveParentCommand($save_mode)
66  );
67  $this->lng->loadLanguageModule('file');
68  $this->upload_limit = $DIC['ui.upload_limit_resolver'];
69  $this->initForm();
70  }
71 
72  private function initForm(): void
73  {
74  switch ($this->save_mode) {
75  case self::MODE_REPLACE:
76  $this->global_tpl->setOnScreenMessage('info', $this->lng->txt('replace_file_info'));
77  $group_title = $this->lng->txt('replace_file');
78  break;
79  case self::MODE_ADD:
80  default:
81  $this->global_tpl->setOnScreenMessage('info', $this->lng->txt('file_new_version_info'));
82  $group_title = $this->lng->txt('file_new_version');
83  break;
84  }
85 
86  $upload_handler = new ilFileVersionsUploadHandlerGUI(
87  $this->file,
88  $this->save_mode === self::MODE_REPLACE
91  );
92 
93  $size = new DataSize(
94  $this->upload_limit->getBestPossibleUploadLimitInBytes($upload_handler),
95  DataSize::MB
96  );
97 
98  $inputs = [
99  self::F_TITLE => $this->ui_factory->input()->field()->text(
100  $this->lng->txt(self::F_TITLE),
101  $this->lng->txt("if_no_title_then_filename")
102  )->withRequired(false)
103  ->withMaxLength(ilObject::TITLE_LENGTH)
104  ->withValue($this->file->getTitle()),
105 
106  self::F_DESCRIPTION => $this->ui_factory->input()->field()->textarea(
107  $this->lng->txt(self::F_DESCRIPTION)
108  ),
109  self::F_FILE => $this->ui_factory->input()->field()->file(
110  $upload_handler,
111  $this->lng->txt(self::F_FILE),
112  sprintf(
113  $this->lng->txt('upload_files_limit'),
114  (string) $size
115  ),
116  )->withMaxFiles(1)
117  ->withRequired(true),
118  ];
119 
120  $group = $this->ui_factory->input()->field()->group($inputs, $group_title);
121 
122  $this->form = $this->ui_factory->input()->container()->form()->standard($this->post_url, [$group]);
123  }
124 
125  public function saveObject(): bool
126  {
127  $this->form = $this->form->withRequest($this->request);
128  $data = $this->form->getData()[0] ?? null;
129  if ($data === null) {
130  return false;
131  }
132 
133  $title = !empty($data[self::F_TITLE]) ? $data[self::F_TITLE] : $this->file->getTitle();
134  $description = $data[self::F_DESCRIPTION] !== '' ? $data[self::F_DESCRIPTION] : $this->file->getDescription();
135  $revision_number = $data[self::F_FILE][0] ?? null;
136  if ($revision_number === null) {
137  return false;
138  }
139  $revision_number = (int) $revision_number;
140 
141  // Title differs, update Revision and Object
142  $rid = $this->resource_services->manage()->find($this->file->getResourceId());
143  $resource = $this->resource_services->manage()->getResource($rid);
144  $new_revision = $resource->getSpecificRevision($revision_number);
145 
146  $new_revision->setTitle(
147  $this->ensureSuffix($title, $new_revision->getInformation()->getSuffix())
148  );
149  $this->resource_services->manage()->updateRevision($new_revision);
150 
151  $this->file->setDescription($description);
152  $this->file->updateObjectFromCurrentRevision();
153 
154  return true;
155  }
156 
157  public function getHTML(): string
158  {
159  return $this->ui_renderer->render($this->form);
160  }
161 
162  private function resolveParentCommand(int $mode): string
163  {
164  return match ($mode) {
167  };
168  }
169 }
ILIAS UI Component Input Container Form Standard $form
const TITLE_LENGTH
Class ilFileVersionFormGUI.
UploadLimitResolver $upload_limit
This class provides the data size with additional information to remove the work to calculate the siz...
Definition: DataSize.php:30
ilGlobalTemplateInterface $global_tpl
global $DIC
Definition: feed.php:28
ILIAS ResourceStorage Services $resource_services
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ilFileVersionsUploadHandlerGUI.
This describes a standard form.
Definition: Standard.php:26
trait ilObjFileSecureString
Trait ilObjFileSecureString.
form( $class_path, string $cmd, string $submit_caption="")
__construct(ilFileVersionsGUI $file_version_gui, private int $save_mode=self::MODE_ADD)
ilFileVersionFormGUI constructor.
ensureSuffix(string $title, ?string $suffix=null)