mytinytodo/includes/api/ExtSettingsController.php

106 lines
2.9 KiB
PHP
Raw Normal View History

2022-09-08 08:55:05 +00:00
<?php declare(strict_types=1);
class ExtSettingsController extends ApiController {
/**
* Get extension settings page
* @return void
* @throws Exception
*/
2022-09-19 12:51:23 +00:00
function get(string $ext)
2022-09-08 08:55:05 +00:00
{
checkWriteAccess();
2022-09-08 08:55:05 +00:00
/** @var MTTExtension|MTTExtensionSettingsInterface $instance */
$instance = $this->extInstance($ext);
if (!$instance) {
return;
}
2022-09-23 09:02:50 +00:00
$meta = MTTExtension::extMetaInfo($ext);
if (!$meta || !isset($meta['name'])) {
return;
}
2022-09-08 08:55:05 +00:00
$data = $instance->settingsPage();
2022-09-16 09:08:55 +00:00
$lang = Lang::instance();
$nameKey = 'ext.'. $ext. '.name';
if ($lang->hasKey($nameKey)) {
$name = htmlspecialchars($lang->get($nameKey));
}
else {
2022-09-23 09:02:50 +00:00
$name = htmlspecialchars($meta['name']);
2022-09-16 09:08:55 +00:00
}
2022-09-08 08:55:05 +00:00
$escapedExt = htmlspecialchars($ext);
2022-09-16 09:08:55 +00:00
$e = function($s) use($lang) { return htmlspecialchars($lang->get($s)); };
2023-08-28 11:06:26 +00:00
$formStart = '';
$formEnd = '';
$formButtons = '';
if ($instance->settingsPageType() == 0) {
2023-08-28 11:06:26 +00:00
$formStart = "<form id='ext_settings_form' data-ext='$escapedExt'>";
$formEnd = "</form>";
$formButtons =
<<<EOD
<div class="tr form-bottom-buttons">
<button type="submit">{$e('set_submit')}</button>
<button type="button" class="mtt-back-button">{$e('set_cancel')}</button>
</div>
EOD;
}
2022-09-08 08:55:05 +00:00
$data =
<<<EOD
2022-09-16 09:08:55 +00:00
<h3 class="page-title"><a class="mtt-back-button"></a> $name </h3>
2022-09-08 08:55:05 +00:00
<div id="settings_msg" style="display:none"></div>
2023-08-28 11:06:26 +00:00
$formStart
2022-09-08 08:55:05 +00:00
<div class="mtt-settings-table">
$data
$formButtons
2022-09-08 08:55:05 +00:00
</div>
2023-08-28 11:06:26 +00:00
$formEnd
2022-09-08 08:55:05 +00:00
EOD;
$this->response->htmlContent($data);
}
/**
* Save extension settings
* @return void
* @throws Exception
*/
2022-09-19 12:51:23 +00:00
function put(string $ext)
2022-09-08 08:55:05 +00:00
{
checkWriteAccess();
2022-09-08 08:55:05 +00:00
/** @var MTTExtension|MTTExtensionSettingsInterface $instance */
$instance = $this->extInstance($ext);
if (!$instance) {
return;
}
//$userError = '';
$saved = $instance->saveSettings($this->req->jsonBody ?? [], $userError);
$a = [ 'saved' => (int)$saved ];
if ($userError) {
$a['msg'] = $userError;
}
$this->response->data = $a;
}
2022-09-19 12:51:23 +00:00
private function extInstance(string $ext): ?MTTExtensionSettingsInterface
2022-09-08 08:55:05 +00:00
{
$instance = MTTExtensionLoader::extensionInstance($ext);
if (!$instance) {
$this->response->data = [ 'msg' => "Unknown extension" ];
$this->response->code = 404;
return null;
}
if (! ($instance instanceof MTTExtensionSettingsInterface) ) {
$this->response->data = [ 'msg' => "No settings page for extension" ];
$this->response->code = 500;
return null;
}
return $instance;
}
}