ILIAS  release_8 Revision v8.24
class.ilFileVersionFormGUI.php
Go to the documentation of this file.
1<?php
2
22use Psr\Http\Message\RequestInterface;
23
30{
32 public const MODE_ADD = 1;
33 public const MODE_REPLACE = 2;
34 public const F_TITLE = 'title';
35 public const F_DESCRIPTION = "description";
36 public const F_FILE = "file";
37 public const F_SAVE_MODE = 'save_mode';
38
39 private \ilObjFile $file;
41 private \ILIAS\UI\Factory $ui_factory;
42 private \ILIAS\UI\Renderer $ui_renderer;
43 private RequestInterface $request;
46 private \ILIAS\ResourceStorage\Services $resource_services;
47
49 private string $post_url;
50
54 public function __construct(ilFileVersionsGUI $file_version_gui, $mode = self::MODE_ADD)
55 {
56 global $DIC;
57 $this->file = $file_version_gui->getFile();
58 ;
59 $this->lng = $DIC->language();
60 $this->save_mode = $mode;
61 $this->ui_factory = $DIC->ui()->factory();
62 $this->ui_renderer = $DIC->ui()->renderer();
63 $this->request = $DIC->http()->request();
64 $this->global_tpl = $DIC->ui()->mainTemplate();
65 $this->resource_services = $DIC->resourceStorage();
66 $this->post_url = $DIC->ctrl()->getFormAction(
67 $file_version_gui,
68 $this->resolveParentCommand($mode)
69 );
70 $this->lng->loadLanguageModule('file');
71 $this->initForm();
72 }
73
74 private function initForm(): void
75 {
76 switch ($this->save_mode) {
78 $this->global_tpl->setOnScreenMessage('info', $this->lng->txt('replace_file_info'));
79 $group_title = $this->lng->txt('replace_file');
80 break;
81 case self::MODE_ADD:
82 default:
83 $this->global_tpl->setOnScreenMessage('info', $this->lng->txt('file_new_version_info'));
84 $group_title = $this->lng->txt('file_new_version');
85 break;
86 }
87
88 $inputs = [
89 self::F_TITLE => $this->ui_factory->input()->field()->text(
90 $this->lng->txt(self::F_TITLE),
91 $this->lng->txt("if_no_title_then_filename")
92 )
93 ->withValue($this->file->getTitle())
94 ->withRequired(false)
95 ->withMaxLength(ilObject::TITLE_LENGTH),
96 self::F_DESCRIPTION => $this->ui_factory->input()->field()->textarea(
97 $this->lng->txt(self::F_DESCRIPTION)
98 ),
99 self::F_FILE => $this->ui_factory->input()->field()->file(
101 $this->file,
102 $this->save_mode === self::MODE_REPLACE
105 ),
106 $this->lng->txt(self::F_FILE)
107 )->withMaxFileSize(ilFileUtils::getUploadSizeLimitBytes())
108 ];
109
110 $group = $this->ui_factory->input()->field()->group($inputs, $group_title);
111
112 $this->form = $this->ui_factory->input()->container()->form()->standard($this->post_url, [$group]);
113 }
114
115 public function saveObject(): bool
116 {
117 $this->form = $this->form->withRequest($this->request);
118 $data = $this->form->getData()[0];
119
120 $title = !empty($data[self::F_TITLE]) ? $data[self::F_TITLE] : $this->file->getTitle();
121 $description = $data[self::F_DESCRIPTION] !== '' ? $data[self::F_DESCRIPTION] : $this->file->getDescription();
122 $revision_number = $data[self::F_FILE][0] ?? null;
123 if ($revision_number === null) {
124 return false;
125 }
126 $revision_number = (int) $revision_number;
127
128 // Title differs, update Revision and Object
129 $rid = $this->resource_services->manage()->find($this->file->getResourceId());
130 $resource = $this->resource_services->manage()->getResource($rid);
131 $new_revision = $resource->getSpecificRevision($revision_number);
132
133 $new_revision->setTitle(
134 $this->ensureSuffix($title, $new_revision->getInformation()->getSuffix())
135 );
136 $this->resource_services->manage()->updateRevision($new_revision);
137
138 $this->file->setDescription($description);
139 $this->file->updateObjectFromCurrentRevision();
140
141 return true;
142 }
143
144 public function getHTML(): string
145 {
146 return $this->ui_renderer->render($this->form);
147 }
148
149 private function resolveParentCommand(int $mode): string
150 {
151 switch ($mode) {
152 case self::MODE_ADD:
153 default:
157 }
158 }
159}
static getUploadSizeLimitBytes()
Class ilFileVersionFormGUI.
__construct(ilFileVersionsGUI $file_version_gui, $mode=self::MODE_ADD)
ilFileVersionFormGUI constructor.
ILIAS UI Component Input Container Form Standard $form
ilGlobalTemplateInterface $global_tpl
ILIAS ResourceStorage Services $resource_services
Class ilFileVersionsGUI.
Class ilFileVersionsUploadHandlerGUI.
language handling
const TITLE_LENGTH
global $DIC
Definition: feed.php:28
This describes a standard form.
Definition: Standard.php:27
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
form( $class_path, string $cmd)
trait ilObjFileSecureString
Trait ilObjFileSecureString.
ensureSuffix(string $title, ?string $suffix=null)