ILIAS  release_8 Revision v8.24
class.ilMathJaxSettingsGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
28{
29 protected \ILIAS\DI\Container $dic;
30 protected \ilCtrl $ctrl;
31 protected \ilTabsGUI $tabs;
32 protected \ilLanguage $lng;
33 protected \ilGlobalTemplateInterface $tpl;
34 protected \ilToolbarGUI $toolbar;
35 protected \Psr\Http\Message\ServerRequestInterface $request;
37
39
43 public function __construct()
44 {
45 global $DIC;
46
47 // ILIAS dependencies
48 $this->dic = $DIC;
49 $this->ctrl = $this->dic->ctrl();
50 $this->tabs = $this->dic->tabs();
51 $this->toolbar = $this->dic->toolbar();
52 $this->lng = $this->dic->language();
53 $this->tpl = $this->dic->ui()->mainTemplate();
54 $this->request = $this->dic->http()->request();
55 $this->refinery = $this->dic->refinery();
56
57 $factory = new ilSettingsFactory($DIC->database());
58 $this->repository = new ilMathJaxConfigSettingsRepository($factory->settingsFor('MathJax'));
59 }
60
66 public function executeCommand(): void
67 {
68 $this->lng->loadLanguageModule('mathjax');
69
70 $cmd = $this->ctrl->getCmd('editSettings');
71 switch ($cmd) {
72 case 'editSettings':
73 case 'clearCache':
74 $this->$cmd();
75 break;
76
77 default:
78 $this->tpl->setContent('unknown command: ' . $cmd);
79 }
80 }
81
85 protected function editSettings(): void
86 {
87 $testcode = 'f(x)=\int_{-\infty}^x e^{-t^2}dt';
88
89 $config = $this->repository->getConfig();
90 $factory = $this->dic->ui()->factory()->input()->field();
91
92 // needed for the optional groups
93 $checkbox_transformation = $this->refinery->custom()->transformation(static function ($v) {
94 if (is_array($v) || is_bool($v)) {
95 return $v;
96 }
97 return ($v === 'checked');
98 });
99
100 // client-side rendering settings
101 $client_enabled = $factory->optionalGroup(
102 [
103
104 'client_polyfill_url' => $factory->url(
105 $this->lng->txt('mathjax_polyfill_url'),
106 implode('<br />', [
107 $this->lng->txt('mathjax_polyfill_url_desc_line1'),
108 $this->lng->txt('mathjax_polyfill_url_desc_line2')
109 ])
110 )
111 ->withValue($config->getClintPolyfillUrl()),
112
113 'client_script_url' => $factory->url(
114 $this->lng->txt('mathjax_script_url'),
115 implode('<br />', [
116 // Mantis #41564
117 sprintf($this->lng->txt('mathjax_script_url_desc_line1'), $config->getMathJax2DefaultUrl()),
118 sprintf($this->lng->txt('mathjax_script_url_desc_line2'), $config->getMathJax3DefaultUrl()),
119 ])
120 )->withRequired(true) // mantis #31645
121 ->withValue($config->getClientScriptUrl()),
122
123 'client_limiter' => $factory->select(
124 $this->lng->txt('mathjax_limiter'),
125 $config->getClientLimiterOptions(),
126 $this->lng->txt('mathjax_limiter_info')
127 )->withRequired(true)
128 ->withValue($config->getClientLimiter()),
129
130 'client_test' => $factory->text(
131 $this->lng->txt('mathjax_test_expression'),
132 $this->lng->txt('mathjax_test_expression_info_client')
134 $config->withClientEnabled(true)
135 ->withServerEnabled(false),
136 new ilMathJaxFactory()
137 )
139 ->insertLatexImages('<p>[tex]' . $testcode . '[/tex]</p>')
140 )->withDisabled(true)->withValue($testcode)
141
142 ],
143 $this->lng->txt('mathjax_enable_client'),
144 $this->lng->txt('mathjax_enable_client_info') . ' ' .
145 $this->renderLink('mathjax_home_link', 'https://www.mathjax.org')
146 )->withAdditionalTransformation($checkbox_transformation);
147
148 // server-side rendering settings
149 $server_enabled = $factory->optionalGroup(
150 [
151 'server_address' => $factory->url(
152 $this->lng->txt('mathjax_server_address'),
153 $this->lng->txt('mathjax_server_address_info')
154 )->withRequired(true)
155 ->withValue($config->getServerAddress()),
156
157 'server_timeout' => $factory->numeric(
158 $this->lng->txt('mathjax_server_timeout'),
159 $this->lng->txt('mathjax_server_timeout_info')
160 )//->withRequired(true) // mantis #31645
161 ->withValue($config->getServerTimeout()),
162
163 'server_for_browser' => $factory->checkbox(
164 $this->lng->txt('mathjax_server_for_browser'),
165 $this->lng->txt('mathjax_server_for_browser_info')
166 )->withValue($config->isServerForBrowser()),
167
168 'server_for_export' => $factory->checkbox(
169 $this->lng->txt('mathjax_server_for_export'),
170 $this->lng->txt('mathjax_server_for_export_info')
171 )->withValue($config->isServerForExport()),
172
173 'server_for_pdf' => $factory->checkbox(
174 $this->lng->txt('mathjax_server_for_pdf'),
175 $this->lng->txt('mathjax_server_for_pdf_info')
176 )->withValue($config->isServerForPdf()),
177
178 'cache_size' => $factory->text(
179 $this->lng->txt('mathjax_server_cache_size'),
180 $this->lng->txt('mathjax_server_cache_size_info') . ' ' .
181 $this->renderLink('mathjax_server_clear_cache', $this->ctrl->getLinkTarget($this, 'clearCache'), false)
182 )->withDisabled(true)->withValue(ilMathJax::getInstance()->getCacheSize()),
183
184 'server_test' => $factory->text(
185 $this->lng->txt('mathjax_test_expression'),
186 $this->lng->txt('mathjax_test_expression_info_server')
188 $config->withClientEnabled(false)
189 ->withServerEnabled(true)
190 ->withServerForBrowser(true),
191 new ilMathJaxFactory()
192 )
194 ->insertLatexImages('<p>[tex]' . $testcode . '[/tex]</p>')
195 )->withDisabled(true)->withValue($testcode)
196 ],
197 $this->lng->txt('mathjax_enable_server'),
198 $this->lng->txt('mathjax_enable_server_info') . ' ' .
199 $this->renderLink('mathjax_server_installation', './Services/MathJax/docs/install-server.md')
200 )->withAdditionalTransformation($checkbox_transformation);
201
202
203 // build the settings form
204 // uncheck optional groups, if not enabled, see https://mantis.ilias.de/view.php?id=26476
205 $form = $this->dic->ui()->factory()->input()->container()->form()->standard($this->ctrl->getFormAction($this), [
206 'mathjax' => $factory->section([], $this->lng->txt('mathjax_settings')),
207 'client_enabled' => $config->isClientEnabled() ? $client_enabled : $client_enabled->withValue(null),
208 'server_enabled' => $config->isServerEnabled() ? $server_enabled : $server_enabled->withValue(null)
209 ]);
210
211 // apply posted inputs if form is saved
212 if ($this->request->getMethod() === "POST") {
213 $form = $form->withRequest($this->request);
214 $data = $form->getData();
215 }
216
217 // posted inputs exist and are ok => save data
218 if (isset($data)) {
219 if (is_array($data['client_enabled'])) {
220 $client_data = $data['client_enabled'];
221 $config = $config->withClientEnabled(true)
222 ->withClientPolyfillUrl((string) $client_data['client_polyfill_url'])
223 ->withClientScriptUrl((string) $client_data['client_script_url'])
224 ->withClientLimiter((int) $client_data['client_limiter']);
225 } else {
226 $config = $config->withClientEnabled(false);
227 }
228
229 if (is_array($data['server_enabled'])) {
230 $server_data = $data['server_enabled'];
231 $config = $config->withServerEnabled(true)
232 ->withServerAddress((string) $server_data['server_address'])
233 ->withServerTimeout((int) $server_data['server_timeout'])
234 ->withServerForBrowser((bool) $server_data['server_for_browser'])
235 ->withServerForExport((bool) $server_data['server_for_export'])
236 ->withServerForPdf((bool) $server_data['server_for_pdf']);
237 } else {
238 $config = $config->withServerEnabled(false);
239 }
240 $this->repository->updateConfig($config);
241
242 $this->tpl->setOnScreenMessage('success', $this->lng->txt("settings_saved"), true);
243 $this->ctrl->redirect($this);
244 }
245
246 // form is not posted or has valisation errors
247 $this->tpl->setContent($this->dic->ui()->renderer()->render($form));
248 }
249
253 protected function clearCache(): void
254 {
255 ilMathJax::getInstance()->clearCache();
256 $this->tpl->setOnScreenMessage('success', $this->lng->txt('mathjax_server_cache_cleared'), true);
257 $this->ctrl->redirect($this);
258 }
259
263 protected function renderLink(string $langvar, string $url, bool $new_tab = true): string
264 {
265 $link = $this->dic->ui()->factory()->link()->standard(
266 $this->lng->txt($langvar),
267 $url
268 )->withOpenInNewViewport($new_tab);
269 return $this->dic->ui()->renderer()->render($link);
270 }
271}
Builds data types.
Definition: Factory.php:21
Repository for storing and loading the MathJax configuration.
Factory for objects used by ilMathJax.
GUI for MathJax Settings This GUI maintains the MathJax config stored in the ILIAS settings Since ILI...
ilGlobalTemplateInterface $tpl
Psr Http Message ServerRequestInterface $request
renderLink(string $langvar, string $url, bool $new_tab=true)
Render an html link.
ILIAS Refinery Factory $refinery
ilMathJaxConfigRespository $repository
executeCommand()
Execute a command This should be overridden in the child classes note: permissions are already checke...
editSettings()
Edit the MathJax settings.
clearCache()
Clear the directory with cached LaTeX graphics.
static getIndependent(ilMathJaxConfig $config, ilMathJaxFactory $factory)
Get an independent instance with a specific config for use in unit tests or on the mathjax settings p...
static getInstance()
Singleton: get instance for use in ILIAS requests with a config loaded from the settings.
const PURPOSE_BROWSER
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
$factory
Definition: metadata.php:75
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:85
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:59
$url