ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilFileVersionFormGUI.php
Go to the documentation of this file.
1<?php
2
23use Psr\Http\Message\RequestInterface;
24
31{
33 public const MODE_ADD = 1;
34 public const MODE_REPLACE = 2;
35 public const F_TITLE = 'title';
36 public const F_DESCRIPTION = "description";
37 public const F_FILE = "file";
38 public const F_SAVE_MODE = 'save_mode';
39
40 private \ilObjFile $file;
41 private ?Standard $form;
44 private RequestInterface $request;
48 private string $post_url;
49
53 public function __construct(ilFileVersionsGUI $file_version_gui, private int $save_mode = self::MODE_ADD)
54 {
55 global $DIC;
56 $this->file = $file_version_gui->getFile();
57 ;
58 $this->lng = $DIC->language();
59 $this->ui_factory = $DIC->ui()->factory();
60 $this->ui_renderer = $DIC->ui()->renderer();
61 $this->request = $DIC->http()->request();
62 $this->global_tpl = $DIC->ui()->mainTemplate();
63 $this->resource_services = $DIC->resourceStorage();
64 $this->post_url = $DIC->ctrl()->getFormAction(
65 $file_version_gui,
66 $this->resolveParentCommand($save_mode)
67 );
68 $this->lng->loadLanguageModule('file');
69 $this->initForm();
70 }
71
72 private function initForm(): void
73 {
74 switch ($this->save_mode) {
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 $inputs = [
94 self::F_TITLE => $this->ui_factory->input()->field()->text(
95 $this->lng->txt(self::F_TITLE),
96 $this->lng->txt("if_no_title_then_filename")
97 )->withRequired(false)
98 ->withMaxLength(ilObject::TITLE_LENGTH)
99 ->withValue($this->file->getTitle()),
100
101 self::F_DESCRIPTION => $this->ui_factory->input()->field()->textarea(
102 $this->lng->txt(self::F_DESCRIPTION)
103 ),
104 self::F_FILE => $this->ui_factory->input()->field()->file(
105 $upload_handler,
106 $this->lng->txt(self::F_FILE)
107 )->withMaxFiles(1)
108 ->withRequired(true),
109 ];
110
111 $group = $this->ui_factory->input()->field()->group($inputs, $group_title);
112
113 $this->form = $this->ui_factory->input()->container()->form()->standard($this->post_url, [$group]);
114 }
115
116 public function saveObject(): bool
117 {
118 $this->form = $this->form->withRequest($this->request);
119 $data = $this->form->getData()[0] ?? null;
120 if ($data === null) {
121 return false;
122 }
123
124 $title = empty($data[self::F_TITLE]) ? $this->file->getTitle() : $data[self::F_TITLE];
125 $description = $data[self::F_DESCRIPTION] !== '' ? $data[self::F_DESCRIPTION] : $this->file->getDescription();
126 $revision_number = $data[self::F_FILE][0] ?? null;
127 if ($revision_number === null) {
128 return false;
129 }
130 $revision_number = (int) $revision_number;
131
132 // Title differs, update Revision and Object
133 $rid = $this->resource_services->manage()->find($this->file->getResourceId());
134 $resource = $this->resource_services->manage()->getResource($rid);
135 $new_revision = $resource->getSpecificRevision($revision_number);
136
137 $new_revision->setTitle(
138 $this->ensureSuffix($title, $new_revision->getInformation()->getSuffix())
139 );
140 $this->resource_services->manage()->updateRevision($new_revision);
141
142 $this->file->setDescription($description);
143 $this->file->updateObjectFromCurrentRevision();
144
145 return true;
146 }
147
148 public function getHTML(): string
149 {
150 return $this->ui_renderer->render($this->form);
151 }
152
153 private function resolveParentCommand(int $mode): string
154 {
155 return match ($mode) {
158 };
159 }
160}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Class ilFileVersionFormGUI.
__construct(ilFileVersionsGUI $file_version_gui, private int $save_mode=self::MODE_ADD)
ilFileVersionFormGUI constructor.
ilGlobalTemplateInterface $global_tpl
Class ilFileVersionsUploadHandlerGUI.
language handling
const TITLE_LENGTH
This describes a standard form.
Definition: Standard.php:29
This is how the factory for UI elements looks.
Definition: Factory.php:38
An entity that renders components to a string output.
Definition: Renderer.php:31
form( $class_path, string $cmd, string $submit_caption="")
global $DIC
Definition: shib_login.php:26
trait ilObjFileSecureString
Trait ilObjFileSecureString.
ensureSuffix(string $title, ?string $suffix=null)