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