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