Skip to content

Commit

Permalink
Add notification platform field settings (#2)
Browse files Browse the repository at this point in the history
Adjust container list fields
  • Loading branch information
austinwbest committed Nov 19, 2023
1 parent a59fdbf commit 06a54ea
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 38 deletions.
6 changes: 2 additions & 4 deletions root/app/www/public/ajax/containers.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
<th scope="col">Update</th>
<th scope="col">State</th>
<th scope="col">Added</th>
<th scope="col">Running</th>
<th scope="col">CPU</th>
<th scope="col">Memory</th>
<th scope="col">
Expand Down Expand Up @@ -82,12 +81,11 @@
<tr id="<?= $nameHash ?>">
<th scope="row"><input id="massTrigger-<?= $nameHash ?>" type="checkbox" class="form-check-input containers-check"></th>
<td><?= ($logo ? '<img src="' . $logo . '" height="32" width="32">' : '') ?></td>
<td><?= $process['Names'] ?></td>
<td><?= $process['Names'] ?><br><span class="text-muted small-text"><?= truncateMiddle($process['inspect'][0]['Config']['Image'], 35) ?></span></td>
<td id="<?= $nameHash ?>-control"><?= $control ?></td>
<td id="<?= $nameHash ?>-update" title="Last pulled: <?= date('Y-m-d H:i:s', $pullData['checked']) ?>"><?= $updateStatus ?></td>
<td id="<?= $nameHash ?>-state"><?= $process['State'] ?></td>
<td id="<?= $nameHash ?>-running"><?= $process['RunningFor'] ?></td>
<td id="<?= $nameHash ?>-status"><?= $process['Status'] ?></td>
<td><span id="<?= $nameHash ?>-running"><?= $process['RunningFor'] ?></span><br><span id="<?= $nameHash ?>-status"><?= $process['Status'] ?></span></td>
<td id="<?= $nameHash ?>-cpu"><?= $process['stats']['CPUPerc'] ?></td>
<td id="<?= $nameHash ?>-mem"><?= $process['stats']['MemPerc'] ?></td>
<td>
Expand Down
56 changes: 46 additions & 10 deletions root/app/www/public/ajax/notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
?>
<div class="container-fluid pt-4 px-4">
<div class="bg-secondary rounded h-100 p-4">
<h4 class="mt-3">Triggers</h4>
<div class="table-responsive">
<table class="table">
<thead>
Expand All @@ -58,7 +59,7 @@
<tbody>
<?php
foreach ($triggers as $trigger) {
$notificationSetting = $settings['notifications'][$trigger['name']];
$notificationSetting = $settings['notifications']['triggers'][$trigger['name']];
?>
<tr>
<th scope="row"><input type="checkbox" <?= ($notificationSetting['active'] ? 'checked' : '') ?> class="form-check-input notification-check" id="notifications-name-<?= $trigger['name'] ?>"></th>
Expand All @@ -67,8 +68,8 @@
<td>
<select class="form-control" id="notifications-platform-<?= $trigger['name'] ?>">
<?php
foreach ($platforms as $platform) {
?><option <?= ($notificationSetting['platform'] == $platform['id'] ? 'selected' : '') ?> value="<?= $platform['id'] ?>"><?= $platform['name'] ?></option><?php
foreach ($platforms as $platformId => $platform) {
?><option <?= ($notificationSetting['platform'] == $platformId ? 'selected' : '') ?> value="<?= $platformId ?>"><?= $platform['name'] ?></option><?php
}
?>
</select>
Expand All @@ -78,23 +79,45 @@
}
?>
</tbody>
<tfoot>
</table>
</div>
</div>
<div class="bg-secondary rounded h-100 p-4 mt-3">
<h4 class="mt-3">Platforms</h4>
<?php foreach ($platforms as $platformId => $platform) { ?>
<h6><?= $platform['name'] ?></h6>
<div class="table-responsive mt-2">
<table class="table">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Setting</th>
<th scope="col">Description</th>
</tr>
</thead>
<tbody>
<?php foreach ($platform['fields'] as $field) { ?>
<tr>
<td colspan="9" align="center">
<button type="button" class="btn btn-info" onclick="saveNotificationSettings()">Save Changes</button>
</td>
<th scope="row"><?= $field['label'] ?><?= ($field['required'] ? '<span class="text-danger">*</span>' : '') ?></th>
<td><?= notificationPlatformField($platformId, $field) ?></td>
<td><?= $field['description'] ?></td>
</tr>
</tfoot>
<?php } ?>
</tbody>
</table>
</div>
<?php } ?>
</div>
<div class="bg-secondary rounded h-100 p-4 mt-3 text-center">
<button type="button" class="btn btn-info" onclick="saveNotificationSettings()">Save Changes</button>
</div>
</div>
<?php
}

if ($_POST['m'] == 'saveNotificationSettings') {
//-- TRIGGER SETTINGS
$newSettings = [];

foreach ($_POST as $key => $val) {
if (strpos($key, '-name-') === false) {
continue;
Expand All @@ -106,7 +129,20 @@
'platform' => $_POST['notifications-platform-' . $type]
];
}
$settings['notifications']['triggers'] = $newSettings;

//-- PLATFORM SETTINGS
$newSettings = [];
foreach ($_POST as $key => $val) {
if (strpos($key, '-platform-') === false) {
continue;
}

$strip = str_replace('notifications-platform-', '', $key);
list($platformId, $platformField) = explode('-', $strip);
$newSettings[$platformId][$platformField] = $val;
}
$settings['notifications']['platforms'] = $newSettings;

$settings['notifications'] = $newSettings;
setFile(SETTINGS_FILE, $settings);
}
4 changes: 4 additions & 0 deletions root/app/www/public/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,8 @@ pre {
background-color: #000;
color: #FFF;
padding: 1em;
}

.small-text {
font-size: small;
}
34 changes: 33 additions & 1 deletion root/app/www/public/functions/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,36 @@ function byteConversion($bytes, $measurement = false, $dec = 2)
case 'TiB':
return round($bytes / pow(1024, 4), $dec);
}
}
}

function truncateEnd($str, $max, $minLength = false)
{
if (!is_string($str)) {
return $str;
}

if (!$minLength && strlen($str) <= $max) {
return $str;
}

if (strlen($str) > $max) {
$str = substr($str, 0, $max - 3) . '...';
}

if ($minLength && strlen($str) < $max) {
$str = str_pad($str, $max, ' ', STR_PAD_RIGHT);
}

return $str;
}

function truncateMiddle($str, $max)
{
if (strlen($str) <= $max) {
return $str;
}

$padMax = $max - 5;

return substr($str, 0, floor($padMax / 2)) . '.....' . substr($str, (floor($padMax / 2) * -1));
}
21 changes: 21 additions & 0 deletions root/app/www/public/functions/notifications.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
----------------------------------
------ Created: 111923 ------
------ Austin Best ------
----------------------------------
*/

function notificationPlatformField($platform, $field)
{
$settings = getFile(SETTINGS_FILE);
$settings = $settings['notifications']['platforms'][$platform];

switch ($field['type']) {
case 'password':
return '<input class="form-control" type="password" id="notifications-platform-' . $platform . '-' . $field['name'] . '" value="' . $settings[$field['name']] . '" '. ($field['required'] ? 'data-required="true"' : '') .'>';
case 'text':
return '<input class="form-control" type="text" id="notifications-platform-' . $platform . '-' . $field['name'] . '" value="' . $settings[$field['name']] . '" '. ($field['required'] ? 'data-required="true"' : '') .'>';
}
}
13 changes: 7 additions & 6 deletions root/app/www/public/includes/platforms.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
*/

$platforms = [
[
'id' => 1,
1 => [
'name' => 'Notifiarr',
'fields' => [
[
'name' => 'apikey',
'type' => 'password',
'required' => true
'name' => 'apikey',
'type' => 'text',
'required' => false,
'label' => 'API Key',
'description' => 'Your apikey from notifiarr.com (integration specific or global)'
]
]
]
]
];
18 changes: 3 additions & 15 deletions root/app/www/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
<a id="menu-logs" onclick="initPage('logs')" style="cursor: pointer;" class="nav-item nav-link"><i class="fas fa-file-code me-2"></i>Logs</a>
</div>
</nav>
<div class="w-100 text-center" style="position: absolute; bottom: 0;">
Theme By <a href="https://htmlcodex.com">HTML Codex</a>
</div>
</div>
<!-- Sidebar End -->

Expand All @@ -93,21 +96,6 @@
</div>
</div>
<!-- App End -->

<!-- Footer Start -->
<div class="container-fluid pt-4 px-4">
<div class="bg-secondary rounded-top p-4" style="position: absolute; bottom: 0; width: 80%;">
<div class="row">
<div class="col-12 col-sm-6 text-center text-sm-start">
&copy; DockWatch <?= date('Y') ?>, All Right Reserved.
</div>
<div class="col-12 col-sm-6 text-center text-sm-end">
Theme By <a href="https://htmlcodex.com">HTML Codex</a>
</div>
</div>
</div>
</div>
<!-- Footer End -->
</div>
<!-- Content End -->

Expand Down
15 changes: 13 additions & 2 deletions root/app/www/public/js/notification.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
function saveNotificationSettings()
{
loadingStart();

let requiredError = false;
let params = '';
$.each($('[id^=notifications-]'), function () {
const required = $(this).attr('data-required');
let val = '';
if ($(this).is(':checkbox') || $(this).is(':radio')) {
val = $(this).prop('checked') ? 1 : 0;
} else {
val = $(this).val();
}

if (required && val == '') {
requiredError = true;
}

params += '&' + $(this).attr('id') + '=' + val;
});

if (requiredError) {
toast('Notifications', 'Required fields can not be empty', 'error');
return;
}

loadingStart();

$.ajax({
type: 'POST',
url: '../ajax/notification.php',
Expand Down

0 comments on commit 06a54ea

Please sign in to comment.