add ability to get specific preset, defaulting to all

This commit is contained in:
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
function presets($_action, $_data = null)
function presets($_action, $_kind, $_object)
{
if ($_SESSION['mailcow_cc_role'] !== 'admin') {
$_SESSION['return'][] = [
@@ -13,8 +13,9 @@ function presets($_action, $_data = null)
global $lang;
if ($_action === 'get') {
$kind = strtolower(trim($_data));
$kind = strtolower(trim($_kind));
$langSection = 'admin';
$presetsPath = __DIR__ . '/presets/' . $kind;
if (!in_array($kind, ['admin-rspamd', 'mailbox-sieve'], true)) {
return [];
@@ -24,19 +25,13 @@ function presets($_action, $_data = null)
$langSection = 'mailbox';
}
if ($_object !== 'all') {
return getPresetFromFilePath($presetsPath . '/' . $_object . '.yml', $langSection);
}
$presets = [];
foreach (glob(__DIR__ . '/presets/' . $kind . '/*.yml') as $filename) {
$preset = Spyc::YAMLLoad($filename);
/* 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;
foreach (glob($presetsPath . '/*.yml') as $filename) {
$presets[] = getPresetFromFilePath($filename, $langSection);
}
return $presets;
@@ -44,3 +39,19 @@ function presets($_action, $_data = null)
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;
}