add ability to get specific preset, defaulting to all

master
tinect 2019-11-14 22:07:49 +01:00
parent f41c2a688a
commit 9f754c95b9
4 changed files with 33 additions and 18 deletions

View File

@ -1,5 +1,5 @@
<?php <?php
function presets($_action, $_data = null) function presets($_action, $_kind, $_object)
{ {
if ($_SESSION['mailcow_cc_role'] !== 'admin') { if ($_SESSION['mailcow_cc_role'] !== 'admin') {
$_SESSION['return'][] = [ $_SESSION['return'][] = [
@ -13,8 +13,9 @@ function presets($_action, $_data = null)
global $lang; global $lang;
if ($_action === 'get') { if ($_action === 'get') {
$kind = strtolower(trim($_data)); $kind = strtolower(trim($_kind));
$langSection = 'admin'; $langSection = 'admin';
$presetsPath = __DIR__ . '/presets/' . $kind;
if (!in_array($kind, ['admin-rspamd', 'mailbox-sieve'], true)) { if (!in_array($kind, ['admin-rspamd', 'mailbox-sieve'], true)) {
return []; return [];
@ -24,19 +25,13 @@ function presets($_action, $_data = null)
$langSection = 'mailbox'; $langSection = 'mailbox';
} }
if ($_object !== 'all') {
return getPresetFromFilePath($presetsPath . '/' . $_object . '.yml', $langSection);
}
$presets = []; $presets = [];
foreach (glob(__DIR__ . '/presets/' . $kind . '/*.yml') as $filename) { foreach (glob($presetsPath . '/*.yml') as $filename) {
$preset = Spyc::YAMLLoad($filename); $presets[] = getPresetFromFilePath($filename, $langSection);
/* get translated headlines */
if (isset($preset['headline']) && strpos($preset['headline'], 'lang.') === 0) {
$langTextName = trim(substr($preset['headline'], 5));
if (isset($lang[$langSection][$langTextName])) {
$preset['headline'] = $lang[$langSection][$langTextName];
}
}
$presets[] = $preset;
} }
return $presets; return $presets;
@ -44,3 +39,19 @@ function presets($_action, $_data = null)
return []; return [];
} }
function getPresetFromFilePath($filePath, $langSection)
{
global $lang;
$preset = Spyc::YAMLLoad($filePath);
$preset = ['name' => basename($filePath, '.yml')] + $preset;
/* get translated headlines */
if (isset($preset['headline']) && strpos($preset['headline'], 'lang.') === 0) {
$langTextName = trim(substr($preset['headline'], 5));
if (isset($lang[$langSection][$langTextName])) {
$preset['headline'] = $lang[$langSection][$langTextName];
}
}
return $preset;
}

View File

@ -3,7 +3,7 @@ var rspamd_presetsElem = document.getElementById("rspamd_presets");
if (rspamd_presetsElem) { if (rspamd_presetsElem) {
$.ajax({ $.ajax({
dataType: 'json', dataType: 'json',
url: '/api/v1/get/presets/admin-rspamd', url: '/api/v1/get/presets-admin-rspamd/all',
jsonp: false, jsonp: false,
complete: function (data) { complete: function (data) {
if (data.responseText !== '{}') { if (data.responseText !== '{}') {

View File

@ -3,7 +3,7 @@ var sieve_presetsElem = document.getElementById("sieve_presets");
if (sieve_presetsElem) { if (sieve_presetsElem) {
$.ajax({ $.ajax({
dataType: 'json', dataType: 'json',
url: '/api/v1/get/presets/mailbox-sieve', url: '/api/v1/get/presets-mailbox-sieve/all',
jsonp: false, jsonp: false,
complete: function (data) { complete: function (data) {
if (data.responseText !== '{}') { if (data.responseText !== '{}') {

View File

@ -1071,8 +1071,12 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
break; break;
} }
break; break;
case "presets": case "presets-admin-rspamd":
process_get_return(presets('get', $object)); process_get_return(presets('get', 'admin-rspamd', $object));
break;
case "presets-mailbox-sieve":
process_get_return(presets('get', 'mailbox-sieve', $object));
break;
break; break;
// return no route found if no case is matched // return no route found if no case is matched
default: default: