Add new function: only_if, to hide fields when they are not enabled [WIP]

Not fully finished yet
This commit is contained in:
slawkens 2023-05-17 06:07:52 +02:00
parent 7aff4557a6
commit 2f8ee7a7eb
4 changed files with 121 additions and 11 deletions

View File

@ -64,8 +64,10 @@ if (isset($_POST['save'])) {
$title = ($plugin == 'core' ? 'Settings' : 'Plugin Settings - ' . $plugin);
$settings = Settings::display($settingsKeyName, $settingsFile['settings']);
$settingsParsed = Settings::display($settingsKeyName, $settingsFile['settings']);
$twig->display('admin.settings.html.twig', [
'settings' => $settings,
'settingsParsed' => $settingsParsed['content'],
'settings' => $settingsFile['settings'],
//'script' => $settingsParsed['script'],
]);

View File

@ -71,7 +71,7 @@ class Settings implements ArrayAccess
}
}
public static function display($plugin, $settings): string
public static function display($plugin, $settings): array
{
global $db;
@ -85,12 +85,17 @@ class Settings implements ArrayAccess
}
}
$javascript = '';
ob_start();
?>
<ul class="nav nav-tabs" id="myTab">
<?php
$i = 0;
foreach($settings as $setting) {
if (isset($setting['script'])) {
$javascript .= $setting['script'] . PHP_EOL;
}
if ($setting['type'] === 'category') {
?>
<li class="nav-item">
@ -105,7 +110,7 @@ class Settings implements ArrayAccess
<?php
$checkbox = function ($key, $type, $value) {
echo '<label><input type="radio" id="' . $key . '" name="settings[' . $key . ']" value="' . ($type ? 'true' : 'false') . '" ' . ($value === $type ? 'checked' : '') . '/>' . ($type ? 'Yes' : 'No') . '</label> ';
echo '<label><input type="radio" id="' . $key . '_' . ($type ? 'yes' : 'no') . '" name="settings[' . $key . ']" value="' . ($type ? 'true' : 'false') . '" ' . ($value === $type ? 'checked' : '') . '/>' . ($type ? 'Yes' : 'No') . '</label> ';
};
$i = 0;
@ -126,7 +131,7 @@ class Settings implements ArrayAccess
echo '</tbody></table>';
}
?>
<h2 style="text-align: center"><strong><?= $setting['title']; ?></strong></h2>
<h3 style="text-align: center"><strong><?= $setting['title']; ?></strong></h3>
<table class="table table-bordered table-striped">
<thead>
<tr>
@ -142,7 +147,7 @@ class Settings implements ArrayAccess
if (!isset($setting['hidden']) || !$setting['hidden']) {
?>
<tr>
<tr id="row_<?= $key ?>">
<td><label for="<?= $key ?>" class="control-label"><?= $setting['name'] ?></label></td>
<td>
<?php
@ -289,7 +294,7 @@ class Settings implements ArrayAccess
</div>
<?php
return ob_get_clean();
return ['content' => ob_get_clean(), 'script' => $javascript];
}
#[\ReturnTypeWillChange]

View File

@ -116,7 +116,10 @@ return [
'name' => 'Visitors Counter TTL',
'type' => 'number',
'desc' => 'Time To Live for Visitors Counter. In other words - how long user will be marked as online. In Minutes',
'default' => 10
'default' => 10,
'only_if' => [
'visitors_counter'=> '=', 'true'
]
],
'views_counter' => [
'name' => 'Views Counter',
@ -190,12 +193,39 @@ return [
'type' => 'boolean',
'desc' => 'Is AAC configured to send e-mails?',
'default' => false,
/*'script' => <<<'SCRIPT'
<script>
$(function () {
$('#mail_enabled_yes, #mail_enabled_no').on('change', function() {
let elements = ['mail_address', 'mail_signature_plain', 'mail_signature_html', 'mail_option',
'smtp_host', 'smtp_port', 'smtp_auth', 'smtp_user', 'smtp_pass', 'smtp_security', 'smtp_debug'];
let show = ($(this).val() === 'true');
let heading = $("h3:contains('SMTP (Mail Server)'):last");
elements.forEach(function(el) {
if (show) {
$('#row_' + el).show(500);
heading.show(500);
}
else {
$('#row_' + el).hide(500);
heading.hide(500);
}
});
});
});
</script>
SCRIPT,*/
],
'mail_address' => [
'name' => 'Mail Address',
'type' => 'email',
'desc' => 'Server e-mail address (from:)',
'default' => 'no-reply@your-server.org',
'show_if' => [
'mail_enabled', '=', 'true'
],
],
/*'mail_admin' => [
'name' => 'Mail Admin Address',
@ -210,6 +240,9 @@ return [
'default' => '--
Sent by MyAAC,
https://my-aac.org',
'show_if' => [
'mail_enabled', '=', 'true'
]
],
'mail_signature_html' => [
'name' => 'Mail Signature (HTML)',
@ -218,10 +251,16 @@ https://my-aac.org',
'default' => escapeHtml('<br/>
Sent by MyAAC,<br/>
<a href="https://my-aac.org">my-aac.org</a>'),
'show_if' => [
'mail_enabled', '=', 'true'
]
],
[
'section_smtp' => [
'type' => 'section',
'title' => 'SMTP (Mail Server)'
'title' => 'SMTP (Mail Server)',
'show_if' => [
'mail_enabled', '=', 'true'
]
],
'mail_option' => [
'name' => 'Mail Option',
@ -229,36 +268,54 @@ Sent by MyAAC,<br/>
'options' => [0 => 'Mail (PHP Built-in)', 1 => 'SMTP (Gmail or Microsoft Outlook)'],
'desc' => 'Mail sender. Set to SMTP if using Gmail or Microsoft Outlook, or any other provider',
'default' => 0,
'show_if' => [
'mail_enabled', '=', 'true'
]
],
'smtp_host' => [
'name' => 'SMTP Host',
'type' => 'text',
'desc' => 'SMTP mail host. smtp.gmail.com for GMail / smtp-mail.outlook.com for Microsoft Outlook',
'default' => '',
'show_if' => [
'mail_enabled', '=', 'true'
]
],
'smtp_port' => [
'name' => 'SMTP Host',
'type' => 'number',
'desc' => '25 (default) / 465 (ssl, GMail) / 587 (tls, Microsoft Outlook)',
'default' => 25,
'show_if' => [
'mail_enabled', '=', 'true'
]
],
'smtp_auth' => [
'name' => 'SMTP Auth',
'type' => 'boolean',
'desc' => 'Need authorization for Server? In normal situation, almost always Yes.',
'default' => true,
'show_if' => [
'mail_enabled', '=', 'true'
]
],
'smtp_user' => [
'name' => 'SMTP Username',
'type' => 'text',
'desc' => 'Here your email username to authenticate with SMTP',
'default' => 'admin@example.org',
'show_if' => [
'mail_enabled', '=', 'true'
]
],
'smtp_pass' => [
'name' => 'SMTP Password',
'type' => 'password',
'desc' => 'Here your email password to authenticate with SMTP',
'default' => '',
'show_if' => [
'mail_enabled', '=', 'true'
]
],
'smtp_security' => [
'name' => 'SMTP Security',
@ -266,12 +323,18 @@ Sent by MyAAC,<br/>
'options' => ['None', 'SSL', 'TLS'],
'desc' => 'What kind of encryption to use on the SMTP connection',
'default' => '',
'show_if' => [
'mail_enabled', '=', 'true'
]
],
'smtp_debug' => [
'name' => 'SMTP Debug',
'type' => 'boolean',
'desc' => 'Activate to see more logs about mailing errors in error.log',
'default' => false,
'show_if' => [
'mail_enabled', '=', 'true'
]
],
[
'type' => 'category',

View File

@ -11,10 +11,50 @@
<button name="save" type="submit" class="btn btn-primary">Save</button>
</div>
<br/>
{{ settings|raw }}
{{ settingsParsed|raw }}
</div>
</div>
</div>
</form>
</div>
</div>
<script>
function doShowHide(el, show)
{
if (show) {
$(el).show()
}
else {
$(el).hide()
}
}
{% for key, value in settings %}
{% if value.show_if is defined %}
$(function () {
$('input[name="settings[{{ value.show_if[0] }}]"]').change(function () {
performChecks_{{ key }}(this);
});
});
function performChecks_{{ key }}(el)
{
let success = false;
let thisVal = $(el).val();
let operator = '{{ value.show_if[1]|raw }}';
if (operator === '>') {
success = thisVal > Number('{{ value.show_if[2] }}');
}
else if (operator === '<') {
success = thisVal < Number('{{ value.show_if[2] }}');
}
else if (operator === '==' || operator === '=') {
success = thisVal == '{{ value.show_if[2] }}';
}
doShowHide('#row_{{ key }}', success);
}
{% endif %}
{% endfor %}
</script>