ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.SettingsGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\User\RedirectOnMissingWrite;
24use ILIAS\UI\Factory as UIFactory;
25use ILIAS\UI\Renderer as UIRenderer;
30use Psr\Http\Message\ServerRequestInterface;
32
37{
38 use RedirectOnMissingWrite;
39
41 private readonly Stakeholder $stakeholder;
42
43 public function __construct(
44 private readonly \ilLanguage $lng,
45 private readonly \ilCtrl $ctrl,
46 private readonly \ilAccess $access,
47 private readonly \ilGlobalTemplateInterface $tpl,
48 private readonly \ilMustacheFactory $mail_mustache_factory,
49 private readonly UIFactory $ui_factory,
50 private readonly UIRenderer $ui_renderer,
51 private readonly Refinery $refinery,
52 private readonly ServerRequestInterface $request,
53 private readonly ResourceStorage $irss,
54 private readonly Repository $account_mail_repo
55 ) {
56 $this->stakeholder = new Stakeholder();
57 $this->upload_handler_gui = new UploadHandlerGUI(
58 $this->irss,
59 $this->stakeholder
60 );
61 }
62
63 public function executeCommand(): void
64 {
65 $this->redirectOnMissingWrite($this->access, $this->ctrl, $this->tpl, $this->lng);
66 if ($this->ctrl->getNextClass() === strtolower(UploadHandlerGUI::class)) {
67 $this->ctrl->forwardCommand($this->upload_handler_gui);
68 return;
69 }
70 $cmd = $this->ctrl->getCmd() . 'Cmd';
71 $this->$cmd();
72 }
73
74 private function showCmd(?StandardForm $form = null): void
75 {
76 $content = [
77 $form ?? $this->buildForm(),
78 $this->ui_factory->panel()->standard(
79 $this->lng->txt('mail_nacc_use_placeholder'),
80 $this->ui_factory->listing()->characteristicValue()->text([
81 '&lbrace;&lbrace;MAIL_SALUTATION&rbrace;&rbrace;' => $this->lng->txt('mail_nacc_salutation'),
82 '&lbrace;&lbrace;FIRST_NAME&rbrace;&rbrace;' => $this->lng->txt('firstname'),
83 '&lbrace;&lbrace;LAST_NAME&rbrace;&rbrace;' => $this->lng->txt('lastname'),
84 '&lbrace;&lbrace;EMAIL&rbrace;&rbrace;' => $this->lng->txt('email'),
85 '&lbrace;&lbrace;LOGIN&rbrace;&rbrace;' => $this->lng->txt('mail_nacc_login'),
86 '&lbrace;&lbrace;PASSWORD&rbrace;&rbrace;' => $this->lng->txt('password'),
87 '&lbrace;&lbrace;#IF_PASSWORD&rbrace;&rbrace;...&lbrace;&lbrace;/IF_PASSWORD&rbrace;&rbrace;' => $this->lng->txt('mail_nacc_pw_block'),
88 '&lbrace;&lbrace;#IF_NO_PASSWORD&rbrace;&rbrace;...&lbrace;&lbrace;/IF_NO_PASSWORD&rbrace;&rbrace;' => $this->lng->txt('mail_nacc_no_pw_block'),
89 '&lbrace;&lbrace;ADMIN_MAIL&rbrace;&rbrace;' => $this->lng->txt('mail_nacc_admin_mail'),
90 '&lbrace;&lbrace;ILIAS_URL&rbrace;&rbrace;' => $this->lng->txt('mail_nacc_ilias_url'),
91 '&lbrace;&lbrace;INSTALLATION_NAME&rbrace;&rbrace;' => $this->lng->txt('mail_nacc_installation_name'),
92 '&lbrace;&lbrace;TARGET&rbrace;&rbrace;' => $this->lng->txt('mail_nacc_target'),
93 '&lbrace;&lbrace;TARGET_TITLE&rbrace;&rbrace;' => $this->lng->txt('mail_nacc_target_title'),
94 '&lbrace;&lbrace;TARGET_TYPE&rbrace;&rbrace;' => $this->lng->txt('mail_nacc_target_type'),
95 '&lbrace;&lbrace;#IF_TARGET&rbrace;&rbrace;...&lbrace;&lbrace;/IF_TARGET&rbrace;&rbrace;' => $this->lng->txt('mail_nacc_target_block'),
96 '&lbrace;&lbrace;#IF_TIMELIMIT&rbrace;&rbrace;...&lbrace;&lbrace;/IF_TIMELIMIT&rbrace;&rbrace;' => $this->lng->txt('mail_nacc_if_timelimit'),
97 '&lbrace;&lbrace;TIMELIMIT&rbrace;&rbrace;' => $this->lng->txt('mail_nacc_timelimit')
98 ])
99 )
100 ];
101
102 $this->tpl->setContent($this->ui_renderer->render($content));
103 }
104
105 private function saveCmd(): void
106 {
107 $form = $this->buildForm()->withRequest($this->request);
108 $data = $form ->getData();
109 if ($data === null) {
110 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('form_input_not_valid'));
111 $this->showCmd($form);
112 return;
113 }
114
115 foreach ($data[0] as $lang_code => $new_account_mail) {
116 $old_account_mail = $this->account_mail_repo->getFor($lang_code);
117
118 $new_attachment_rid = $new_account_mail['attachment'] === []
119 ? null
120 : $new_account_mail['attachment'][0];
121
122 if ($old_account_mail->getAttachmentRid() !== null
123 && $old_account_mail->getAttachmentRid() !== $new_attachment_rid
124 && ($rid = $this->irss->manage()->find($old_account_mail->getAttachmentRid())) !== null) {
125 $this->irss->manage()->remove(
126 $rid,
127 $this->stakeholder
128 );
129 $old_account_mail->deleteAttachmentTempFile();
130 }
131
132 $this->account_mail_repo->store(
134 $lang_code,
135 trim($new_account_mail['subject']),
136 trim($new_account_mail['body']),
137 trim($new_account_mail['salutation_none_specific']),
138 trim($new_account_mail['salutation_male']),
139 trim($new_account_mail['salutation_female']),
140 $new_attachment_rid
141 )
142 );
143 }
144
145 $this->tpl->setOnScreenMessage('success', $this->lng->txt('msg_obj_modified'), true);
146 $this->showCmd();
147 }
148
149 private function buildForm(): StandardForm
150 {
151 $ff = $this->ui_factory->input()->field();
152 $inputs = [];
153 foreach ($this->lng->getInstalledLanguages() as $lang_code) {
154 $account_mail = $this->account_mail_repo->getFor($lang_code);
155
156 $title = $this->lng->txt('meta_l_' . $lang_code);
157 if ($lang_code === $this->lng->getDefaultLanguage()) {
158 $title .= ' (' . $this->lng->txt('default') . ')';
159 }
160
161 $inputs[$lang_code] = $ff->section(
162 [
163 'subject' => $ff->text($this->lng->txt('subject'))
164 ->withAdditionalTransformation(
165 $this->buildConvertCurlyBracesTrafo()
166 )->withAdditionalTransformation(
167 $this->buildValidateMailBodyConstraint()
168 )->withValue(
169 $this->convertCurlyBracesForFormOutput($account_mail->getSubject())
170 ),
171 'salutation_none_specific' => $ff->text($this->lng->txt('mail_salutation_general'))
172 ->withValue($account_mail->getSalutationNoneSpecific()),
173 'salutation_female' => $ff->text($this->lng->txt('mail_salutation_female'))
174 ->withValue($account_mail->getSalutationFemale()),
175 'salutation_male' => $ff->text($this->lng->txt('mail_salutation_male'))
176 ->withValue($account_mail->getSalutationMale()),
177 'body' => $ff->textarea($this->lng->txt('message_content'))
178 ->withAdditionalTransformation(
179 $this->buildConvertCurlyBracesTrafo()
180 )->withAdditionalTransformation(
181 $this->buildValidateMailBodyConstraint()
182 )->withValue(
183 $this->convertCurlyBracesForFormOutput($account_mail->getBody())
184 ),
185 'attachment' => $ff->file($this->upload_handler_gui, $this->lng->txt('attachment'))
186 ->withValue(
187 $account_mail->getAttachmentRid() === null
188 ? []
189 : [$account_mail->getAttachmentRid()]
190 )
191 ],
192 $title
193 );
194 }
195
196 return $this->ui_factory->input()->container()->form()->standard(
197 $this->ctrl->getFormActionByClass(self::class, 'save'),
198 [$ff->section(
199 $inputs,
200 $this->lng->txt('user_new_account_mail'),
201 $this->lng->txt('user_new_account_mail_desc')
202 )]
203 );
204 }
205
207 {
208 return $this->refinery->custom()->constraint(
209 function ($v): bool {
210 try {
211 $this->mail_mustache_factory->getBasicEngine()->render($v, []);
212 return true;
213 } catch (Exception) {
214 return false;
215 }
216 },
217 $this->lng->txt('mail_template_invalid_tpl_syntax')
218 );
219 }
220
222 {
223 return $this->refinery->custom()->transformation(
224 static fn(string $v) => str_replace(['&#123;&#123;', '&#125;&#125;'], ['{{', '}}'], $v)
225 );
226 }
227
228 private function convertCurlyBracesForFormOutput(string $value): string
229 {
230 return str_replace(['{{', '}}'], ['&#123;&#123;', '&#125;&#125;'], $value);
231 }
232}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Builds data types.
Definition: Factory.php:36
@ilCtrl_Calls ILIAS\User\Settings\NewAccountMail\SettingsGUI: ILIAS\User\Settings\NewAccountMail\Uplo...
__construct(private readonly \ilLanguage $lng, private readonly \ilCtrl $ctrl, private readonly \ilAccess $access, private readonly \ilGlobalTemplateInterface $tpl, private readonly \ilMustacheFactory $mail_mustache_factory, private readonly UIFactory $ui_factory, private readonly UIRenderer $ui_renderer, private readonly Refinery $refinery, private readonly ServerRequestInterface $request, private readonly ResourceStorage $irss, private readonly Repository $account_mail_repo)
Class ilAccessHandler Checks access for ILIAS objects.
Class ilCtrl provides processing control methods.
language handling
A constraint encodes some resrtictions on values.
Definition: Constraint.php:32
A transformation is a function from one datatype to another.
This describes a standard form.
Definition: Standard.php:29
An entity that renders components to a string output.
Definition: Renderer.php:31
global $lng
Definition: privfeed.php:31