fix: update NotificationAgents.page and CSS for improved layout and functionality

- Changed PHP opening tag from `<?PHP` to `<?php` for consistency.
- Refactored XML loading and agent handling logic for better readability and maintainability.
- Enhanced the form structure and button group styling in NotificationAgents.page.
- Adjusted button spacing in default-base.css from `1rem` to `1.2rem` for improved layout consistency.
This commit is contained in:
Zack Spear
2025-05-27 14:27:26 -07:00
parent 9a5c9a03e2
commit 2831266e39
2 changed files with 125 additions and 61 deletions

View File

@@ -2,7 +2,7 @@ Menu="Notifications:3"
Title="Notification Agents"
Tag="rss-square"
---
<?PHP
<?php
/* Copyright 2005-2023, Lime Technology
* Copyright 2012-2023, Bergware International.
*
@@ -88,81 +88,144 @@ function initDropdown() {
<input type="hidden" name="#command" value="/webGui/scripts/agent">
<input type="hidden" name="#arg[1]" value="">
<input type="hidden" name="#arg[2]" value="">
</form>
<?
<?php
$fields = ['Event','Subject','Timestamp','Description','Importance','Content','Link'];
$xml_files = glob("/usr/local/emhttp/plugins/dynamix/agents/*.xml");
$i = 1;
foreach ($xml_files as $xml_file) {
$xml = @simplexml_load_file($xml_file);
if ( ! $xml ) continue;
if ( isset($xml->Language) ) {
$guiLanguage = ($locale == "" ) ? "en_US" : $locale;
$acceptedLanguages = explode(" ",$xml->Language);
if ( ! in_array($guiLanguage,$acceptedLanguages) )
$xml = @simplexml_load_file($xml_file);
if (! $xml) {
continue;
}
$name = str_replace(' ','_',$xml->Name);
if (isset($xml->Language)) {
$guiLanguage = ($locale == "") ? "en_US" : $locale;
$acceptedLanguages = explode(" ", $xml->Language);
if (! in_array($guiLanguage, $acceptedLanguages)) {
continue;
}
}
$name = str_replace(' ', '_', $xml->Name);
$titleId = 'agent-' . strtolower($name);
$enabledAgent = agent_fullname("$name.sh", "enabled");
$disabledAgent = agent_fullname("$name.sh", "disabled");
if (is_file($disabledAgent)) {
$file = $disabledAgent;
if (is_file($enabledAgent)) unlink($enabledAgent);
$file = $disabledAgent;
if (is_file($enabledAgent)) {
unlink($enabledAgent);
}
} else {
$file = $enabledAgent;
$file = $enabledAgent;
}
$values = [];
$script = "";
if (is_file($file)) {
preg_match("/[#]{6,100}([^#]*?)[#]{6,100}/si", file_get_contents($file), $match);
if (isset($match[1])) {
foreach (explode(PHP_EOL, $match[1]) as $line) {
if (strpos($line, "=")) {
[$k, $v] = my_explode("=",str_replace("\"", "", $line),2);
$values[$k] = $v;
}
preg_match("/[#]{6,100}([^#]*?)[#]{6,100}/si", file_get_contents($file), $match);
if (isset($match[1])) {
foreach (explode(PHP_EOL, $match[1]) as $line) {
if (strpos($line, "=")) {
[$k, $v] = my_explode("=", str_replace("\"", "", $line), 2);
$values[$k] = $v;
}
}
}
}
}
foreach (explode(PHP_EOL,(String) $xml->Script) as $line) if (trim($line)) $script .= trim($line)."{1}";
echo '<div class="title"><span class="left"><img src="/plugins/dynamix/icons/'.strtolower(str_replace('_','',$name)).'.png" class="icon" style="height:16px;width:16px;">'.str_replace('_',' ',$name).'</span><span class="status vhshift">'.(is_file($enabledAgent) ? '<span class="green">'._("Enabled").'</span>' : '<span class="red">'._("Disabled").'</span>').'</span></div>';
echo '<form method="POST" name="'.$name.'" action="/update.php" target="progressFrame">';
echo '<input type="hidden" name="#include" value="/webGui/include/update.file.php">';
echo '<input type="hidden" name="#file" value="'.$file.'">';
echo '<input type="hidden" name="#command" value="/webGui/scripts/agent">';
echo '<input type="hidden" name="#arg[1]" value="">';
echo '<input type="hidden" name="#arg[2]" value="">';
echo '<input type="hidden" name="text" value="">';
echo '<dl><dt>'._("Agent function").':</dt><dd><select name="Enabled">';
echo mk_option(is_file($disabledAgent), 'no', _('Disabled'));
echo mk_option(is_file($enabledAgent), 'yes', _('Enabled'));
echo '</select></dd></dl>';
echo '<script>scripts["'.$name.'"]='.json_encode($script).';enabledAgents["'.$name.'"]="'.$enabledAgent.'";disabledAgents["'.$name.'"]="'.$disabledAgent.'";</script>';
foreach ($xml->Variables->children() as $v) {
$vName = preg_replace('#\[([^\]]*)\]#', '<$1>', (string) $v);
$vDesc = ucfirst(strtolower(preg_replace('#\[([^\]]*)\]#', '<$1>', $v->attributes()->Desc)));
$vDefault = preg_replace('#\[([^\]]*)\]#', '<$1>', $v->attributes()->Default);
$vHelp = preg_replace('#\[([^\]]*)\]#', '<$1>', $v->attributes()->Help);
echo "<dl><dt>$vDesc:</dt><dd>";
if (preg_match('/title|message/', $vDesc)) {
echo '<select id="slot_'.$i++.'" name="'.$vName.'" multiple style="display:none">';
$value = str_replace('\n',',',isset($values[$vName]) ? $values[$vName] : $vDefault);
foreach ($fields as $field) echo mk_option_check($value,'$'.strtoupper($field),_($field));
echo '</select>';
} else {
echo '<input type="text" name="'.$vName.'" class="variable" required value="'.( isset($values[$vName]) ? $values[$vName] : $vDefault ).'">';
}
echo '</dd></dl>';
if ($vHelp) echo '<blockquote class="inline_help">'.$vHelp.'</blockquote>';
foreach (explode(PHP_EOL, (string) $xml->Script) as $line) {
if (trim($line)) {
$script .= trim($line)."{1}";
}
}
echo '<dl><dt>&nbsp;</dt><dd><input type="submit" value='._("Apply").' onclick="prepareService(this, \''.$name.'\')" disabled>';
echo '<input type="button" value='._("Done").' onclick="done()">';
if (is_file($file)) {
echo '<input type="button" value='._("Delete").' onclick="execCmd(\'delete\',\''.$name.'\')">';
echo '<input type="button" value='._("Test").' onclick="testService(\''.$name.'\')"'.($file==$enabledAgent ? '>' : ' disabled>');
}
echo '</dd></dl></form><div style="min-height:50px;"></div>';
$displayName = str_replace('_', ' ', $name);
$iconPath = "/plugins/dynamix/icons/" . strtolower(str_replace('_', '', $name)) . ".png";
$isEnabled = is_file($enabledAgent);
$statusClass = $isEnabled ? 'green' : 'red';
$statusText = $isEnabled ? _("Enabled") : _("Disabled");
?>
<div id="<?= $titleId ?>" class="title">
<span class="left">
<img src="<?= $iconPath ?>" class="icon" style="height:16px;width:16px;">
<?= $displayName ?>
</span>
<span class="right status">
<span class="<?= $statusClass ?>"><?= $statusText ?></span>
</span>
</div>
<form method="POST" name="<?= $name ?>" action="/update.php" target="progressFrame">
<input type="hidden" name="#include" value="/webGui/include/update.file.php">
<input type="hidden" name="#file" value="<?= $file ?>">
<input type="hidden" name="#command" value="/webGui/scripts/agent">
<input type="hidden" name="#arg[1]" value="">
<input type="hidden" name="#arg[2]" value="">
<input type="hidden" name="text" value="">
<dl>
<dt><?= _("Agent function") ?>:</dt>
<dd>
<select name="Enabled">
<?= mk_option(is_file($disabledAgent), 'no', _('Disabled')) ?>
<?= mk_option(is_file($enabledAgent), 'yes', _('Enabled')) ?>
</select>
</dd>
</dl>
<script>
scripts["<?= $name ?>"] = <?= json_encode($script) ?>;
enabledAgents["<?= $name ?>"] = "<?= $enabledAgent ?>";
disabledAgents["<?= $name ?>"] = "<?= $disabledAgent ?>";
</script>
<?php foreach ($xml->Variables->children() as $v):
$vName = preg_replace('#\[([^\]]*)\]#', '<$1>', (string) $v);
$vDesc = ucfirst(strtolower(preg_replace('#\[([^\]]*)\]#', '<$1>', $v->attributes()->Desc)));
$vDefault = preg_replace('#\[([^\]]*)\]#', '<$1>', $v->attributes()->Default);
$vHelp = preg_replace('#\[([^\]]*)\]#', '<$1>', $v->attributes()->Help);
$currentValue = isset($values[$vName]) ? $values[$vName] : $vDefault;
$isTitleOrMessage = preg_match('/title|message/', $vDesc);
?>
<dl>
<dt><?= $vDesc ?>:</dt>
<dd>
<?php if ($isTitleOrMessage):
$value = str_replace('\n', ',', $currentValue);
?>
<select id="slot_<?= $i++ ?>" name="<?= $vName ?>" multiple style="display:none">
<?php foreach ($fields as $field): ?>
<?= mk_option_check($value, '$'.strtoupper($field), _($field)) ?>
<?php endforeach; ?>
</select>
<?php else: ?>
<input type="text" name="<?= $vName ?>" class="variable" required value="<?= $currentValue ?>">
<?php endif; ?>
</dd>
</dl>
<?php if ($vHelp): ?>
<blockquote class="inline_help"><?= $vHelp ?></blockquote>
<?php endif; ?>
<?php endforeach; ?>
<dl>
<dt>&nbsp;</dt>
<dd>
<div class="button-group">
<input type="submit" value="<?= _("Apply") ?>" onclick="prepareService(this, '<?= $name ?>')" disabled>
<input type="button" value="<?= _("Done") ?>" onclick="done()">
<?php if (is_file($file)): ?>
<input type="button" value="<?= _("Delete") ?>" onclick="execCmd('delete','<?= $name ?>')">
<input type="button" value="<?= _("Test") ?>" onclick="testService('<?= $name ?>')"<?= $file == $enabledAgent ? '>' : ' disabled>' ?>
<?php endif; ?>
</div>
</dd>
</dl>
</form>
<?php
}
?>

View File

@@ -197,10 +197,11 @@ a.button,
}
/* necessary evil until we re-write button styles */
.buttons-no-margin {
.buttons-no-margin,
.button-group {
display: flex;
flex-wrap: wrap;
gap: 1rem;
gap: 1.2rem;
input[type="button"],
input[type="reset"],