ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilContentStyleImageGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
24
29{
31 protected ilLanguage $lng;
37 protected ?string $current_image = null;
41 protected array $current_images = [];
42
43 public function __construct(
44 Content\InternalDomainService $domain_service,
45 Content\InternalGUIService $gui_service,
48 ) {
49 $this->access_manager = $access_manager;
50
51 $this->lng = $domain_service->lng();
52 $this->lng->loadLanguageModule("content");
53
54 $this->request = $gui_service
55 ->standardRequest();
56 $this->gui = $gui_service;
57
58 $images = $this->request->getFiles();
59 if (count($images) == 1 && $manager->filenameExists(current($images))) {
60 $this->current_image = current($images);
61 $this->current_images = $images;
62 } else {
63 $this->current_images = array_filter($images, function ($i) use ($manager) {
64 return $manager->filenameExists($i);
65 });
66 }
67
68 $this->manager = $manager;
69 $this->tpl = $gui_service->mainTemplate();
70 }
71
72 public function executeCommand(): void
73 {
74 $ctrl = $this->gui->ctrl();
75
76 $next_class = $ctrl->getNextClass($this);
77 $cmd = $ctrl->getCmd("listImages");
78
79 switch ($next_class) {
80
81 case strtolower(ilRepoStandardUploadHandlerGUI::class):
82 $form = $this->getImageForm();
83 $gui = $form->getRepoStandardUploadHandlerGUI("image");
84 $ctrl->forwardCommand($gui);
85 break;
86
87 default:
88 if (in_array($cmd, [
89 "listImages", "addImage", "cancelUpload", "uploadImage", "deleteImage",
90 "resizeImageForm", "resizeImage"
91 ])) {
92 $this->$cmd();
93 }
94 }
95 }
96
97 public function listImages(): void
98 {
99 $tpl = $this->gui->mainTemplate();
100 $ilToolbar = $this->gui->toolbar();
101 $ilCtrl = $this->gui->ctrl();
103
104 if ($this->access_manager->checkWrite()) {
105 $ilToolbar->addButton(
106 $lng->txt("sty_add_image"),
107 $ilCtrl->getLinkTarget($this, "addImage")
108 );
109 }
110
111 $table_gui = new ilStyleImageTableGUI(
112 $this,
113 "listImages",
114 $this->access_manager,
115 $this->manager
116 );
117 $tpl->setContent($table_gui->getHTML());
118 }
119
120 public function addImage(): void
121 {
122 $tpl = $this->gui->mainTemplate();
123
124 $form = $this->getImageForm();
125 $tpl->setContent($form->render());
126 }
127
128 public function cancelUpload(): void
129 {
130 $ilCtrl = $this->gui->ctrl();
131
132 $ilCtrl->redirect($this, "listImages");
133 }
134
135 public function uploadImage(): void
136 {
137 $tpl = $this->gui->mainTemplate();
138 $ilCtrl = $this->gui->ctrl();
139 $ilCtrl->redirect($this, "listImages");
140 }
141
142 protected function getImageForm(): FormAdapterGUI
143 {
144 $form = $this->gui->form(self::class, "uploadImage")
145 ->section("image_section", $this->lng->txt("sty_add_image"))
146 ->file(
147 "image",
148 $this->lng->txt("sty_image_file"),
149 $this->handleImageUpload(...), // Placeholder for upload handler
150 "image_id",
151 "",
152 1, // Limit to 1 file upload
153 ["image/jpeg", "image/png", "image/gif", "image/svg+xml"] // Accepted file types
154 );
155 return $form;
156 }
157
158 public function handleImageUpload(
159 \ILIAS\FileUpload\FileUpload $upload,
160 \ILIAS\FileUpload\DTO\UploadResult $result
161 ): \ILIAS\FileUpload\Handler\BasicHandlerResult {
162 $this->manager->importFromUploadResult(
163 $result
164 );
165 return new \ILIAS\FileUpload\Handler\BasicHandlerResult(
166 '',
167 \ILIAS\FileUpload\Handler\HandlerResult::STATUS_OK,
168 "dummy",
169 ''
170 );
171 }
172
173 public function deleteImage(): void
174 {
175 $ilCtrl = $this->gui->ctrl();
176 foreach ($this->current_images as $i) {
177 $this->manager->deleteByFilename($i);
178 }
179 $ilCtrl->redirect($this, "listImages");
180 }
181
182 protected function resizeImageForm(): void
183 {
184 $this->tpl->setContent($this->getResizeImageForm()->getHTML());
185 }
186
188 {
189 $ctrl = $this->gui->ctrl();
191
192 $form = new ilPropertyFormGUI();
193
194 $image = $this->manager->getByFilename($this->current_image);
195
196 // width height
197 $width_height = new ilWidthHeightInputGUI($lng->txt("cont_width") .
198 " / " . $lng->txt("cont_height"), "width_height");
199 $width_height->setConstrainProportions(true);
200 $width_height->setHeight($image->getHeight());
201 $width_height->setWidth($image->getWidth());
202 $form->addItem($width_height);
203
204 // file
205 $hi = new ilHiddenInputGUI("file");
206 $hi->setValue($this->current_image);
207 $form->addItem($hi);
208
209 $form->addCommandButton("resizeImage", $lng->txt("sty_resize"));
210
211 $form->setTitle($lng->txt("sty_resize_image"));
212 $form->setFormAction($ctrl->getFormAction($this));
213
214 return $form;
215 }
216
217 public function resizeImage(): void
218 {
219 $ctrl = $this->gui->ctrl();
221 $main_tpl = $this->gui->mainTemplate();
222
223 $form = $this->getResizeImageForm();
224 if ($form->checkInput()) {
225 $wh = $form->getInput("width_height");
226
227 $this->manager->resizeImage(
228 $this->current_image,
229 (int) ($wh["width"] ?? 0),
230 (int) ($wh["height"] ?? 0),
231 (bool) ($wh["const_prop"] ?? false)
232 );
233
234 $this->tpl->setOnScreenMessage('success', $lng->txt("msg_obj_modified"), true);
235 $ctrl->redirect($this, "listImages");
236 } else {
237 $form->setValuesByPost();
238 $main_tpl->setContent($form->getHTML());
239 }
240 }
241}
section(string $key, string $title, string $description="")
Manages access to content style editing.
Main business logic for content style images.
Content style internal ui factory.
@ilCtrl_Calls ilContentStyleImageGUI: ilRepoStandardUploadHandlerGUI
ilGlobalTemplateInterface $tpl
Content InternalGUIService $gui
__construct(Content\InternalDomainService $domain_service, Content\InternalGUIService $gui_service, Access\StyleAccessManager $access_manager, Content\ImageManager $manager)
handleImageUpload(\ILIAS\FileUpload\FileUpload $upload, \ILIAS\FileUpload\DTO\UploadResult $result)
Content StandardGUIRequest $request
Access StyleAccessManager $access_manager
This class represents a hidden form property in a property form.
language handling
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
Class ilObjStyleSheet.
This class represents a property form user interface.
TableGUI class for style editor (image list)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setContent(string $a_html)
Sets content for standard template.
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $lng
Definition: privfeed.php:31