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); $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', [ $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; global $db;
@ -85,12 +85,17 @@ class Settings implements ArrayAccess
} }
} }
$javascript = '';
ob_start(); ob_start();
?> ?>
<ul class="nav nav-tabs" id="myTab"> <ul class="nav nav-tabs" id="myTab">
<?php <?php
$i = 0; $i = 0;
foreach($settings as $setting) { foreach($settings as $setting) {
if (isset($setting['script'])) {
$javascript .= $setting['script'] . PHP_EOL;
}
if ($setting['type'] === 'category') { if ($setting['type'] === 'category') {
?> ?>
<li class="nav-item"> <li class="nav-item">
@ -105,7 +110,7 @@ class Settings implements ArrayAccess
<?php <?php
$checkbox = function ($key, $type, $value) { $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; $i = 0;
@ -126,7 +131,7 @@ class Settings implements ArrayAccess
echo '</tbody></table>'; 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"> <table class="table table-bordered table-striped">
<thead> <thead>
<tr> <tr>
@ -142,7 +147,7 @@ class Settings implements ArrayAccess
if (!isset($setting['hidden']) || !$setting['hidden']) { if (!isset($setting['hidden']) || !$setting['hidden']) {
?> ?>
<tr> <tr id="row_<?= $key ?>">
<td><label for="<?= $key ?>" class="control-label"><?= $setting['name'] ?></label></td> <td><label for="<?= $key ?>" class="control-label"><?= $setting['name'] ?></label></td>
<td> <td>
<?php <?php
@ -289,7 +294,7 @@ class Settings implements ArrayAccess
</div> </div>
<?php <?php
return ob_get_clean(); return ['content' => ob_get_clean(), 'script' => $javascript];
} }
#[\ReturnTypeWillChange] #[\ReturnTypeWillChange]

View File

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

View File

@ -11,10 +11,50 @@
<button name="save" type="submit" class="btn btn-primary">Save</button> <button name="save" type="submit" class="btn btn-primary">Save</button>
</div> </div>
<br/> <br/>
{{ settings|raw }} {{ settingsParsed|raw }}
</div> </div>
</div> </div>
</div> </div>
</form> </form>
</div> </div>
</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>