Fixes adding, editing and deleting a platform

main
Yasen Pramatarov 2024-08-26 21:19:21 +03:00
parent 1ba32d86f7
commit 666f3ca98b
8 changed files with 192 additions and 26 deletions

View File

@ -38,7 +38,6 @@ return [
'platforms' => [ 'platforms' => [
'0' => [ '0' => [
'name' => 'meet.example.com', 'name' => 'meet.example.com',
// database with logs from Jilo
'jilo_database' => '../../jilo/jilo.db', 'jilo_database' => '../../jilo/jilo.db',
], ],
], ],

View File

@ -0,0 +1,25 @@
<?php
// Function to format arrays with square brackets
function formatArray(array $array, $indentLevel = 2) {
$indent = str_repeat(' ', $indentLevel); // 4 spaces per indent level
$output = "[\n";
foreach ($array as $key => $value) {
$output .= $indent . "'" . $key . "'" . ' => ';
if (is_array($value)) {
$output .= formatArray($value, $indentLevel + 1);
} else {
$output .= var_export($value, true);
}
$output .= ",\n";
}
$output .= str_repeat(' ', $indentLevel - 1) . ']';
return $output;
}
?>

View File

@ -1,29 +1,36 @@
<?php <?php
// render config variables array // render config variables array
function renderConfig($config, $indent, $platform=false) { function renderConfig($configPart, $indent, $platform=false, $parent='') {
global $app_root; global $app_root;
if (isset($platform) && $platform === true) { global $config;
if ($parent === 'platforms') {
?> ?>
<div class="col-md-8 text-start">
<a class="btn btn-secondary" style="padding: 0px;" href="<?= $app_root ?>?page=config&action=add">add</a>
</div>
<div class="border bg-light" style="padding-left: <?= $indent ?>px; padding-bottom: 20px; padding-top: 20px;"> <div class="border bg-light" style="padding-left: <?= $indent ?>px; padding-bottom: 20px; padding-top: 20px;">
<?php } else { <?php } else {
?> ?>
<div style="padding-left: <?= $indent ?>px; padding-bottom: 20px;"> <div style="padding-left: <?= $indent ?>px; padding-bottom: 20px;">
<?php <?php
} }
foreach ($config as $config_item => $config_value) { foreach ($configPart as $config_item => $config_value) {
if ($parent === 'platforms') {
$indent = 0;
}
?> ?>
<div class="row mb-1" style="padding-left: <?= $indent ?>px;"> <div class="row mb-1" style="padding-left: <?= $indent ?>px;">
<div class="col-md-4 text-end"> <div class="col-md-4 text-end">
<?= htmlspecialchars($config_item) ?>: <?= htmlspecialchars($config_item) ?>:
</div> </div>
<?php <?php
if (isset($platform) && $platform === true) { ?> if ($parent === 'platforms') { ?>
<div class="col-md-8 text-start"> <div class="col-md-8 text-start">
<a class="btn btn-secondary" style="padding: 2px;" href="<?= $app_root ?>?platform=<?= htmlspecialchars($config_item) ?>&page=config&action=edit">edit</a> <a class="btn btn-secondary" style="padding: 2px;" href="<?= $app_root ?>?platform=<?= htmlspecialchars($config_item) ?>&page=config&action=edit">edit</a>
<?php <?php
// we don't delete the last platform // we don't delete the last platform
if (count($config) <= 1) { ?> if (count($configPart) <= 1) { ?>
<span class="btn btn-light" style="padding: 2px;" href="#" data-toggle="tooltip" data-placement="right" data-offset="30.0" title="can't delete the last platform">delete</span> <span class="btn btn-light" style="padding: 2px;" href="#" data-toggle="tooltip" data-placement="right" data-offset="30.0" title="can't delete the last platform">delete</span>
<?php } else { ?> <?php } else { ?>
<a class="btn btn-danger" style="padding: 2px;" href="<?= $app_root ?>?platform=<?= htmlspecialchars($config_item) ?>&page=config&action=delete">delete</a> <a class="btn btn-danger" style="padding: 2px;" href="<?= $app_root ?>?platform=<?= htmlspecialchars($config_item) ?>&page=config&action=delete">delete</a>
@ -32,14 +39,16 @@ function renderConfig($config, $indent, $platform=false) {
<?php } <?php }
if (is_array($config_value)) { if (is_array($config_value)) {
if ($config_item === 'platforms') {
$platform = true;
} else {
$platform = false;
}
// here we render recursively nested arrays // here we render recursively nested arrays
$indent = $indent + 50; $indent = $indent + 50;
if ($parent === 'platforms') {
$indent = 100;
}
if ($config_item === 'platforms') {
renderConfig($config_value, $indent, $platform, 'platforms');
} else {
renderConfig($config_value, $indent, $platform); renderConfig($config_value, $indent, $platform);
}
$indent = 0; $indent = 0;
} else { } else {
// if it's not array, just display it // if it's not array, just display it

View File

@ -2,6 +2,7 @@
$action = $_REQUEST['action'] ?? ''; $action = $_REQUEST['action'] ?? '';
require '../app/helpers/errors.php'; require '../app/helpers/errors.php';
require '../app/helpers/config.php';
// if a form is submitted, it's from the edit page // if a form is submitted, it's from the edit page
if ($_SERVER['REQUEST_METHOD'] == 'POST') { if ($_SERVER['REQUEST_METHOD'] == 'POST') {
@ -10,16 +11,66 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$content = file_get_contents($config_file); $content = file_get_contents($config_file);
$updatedContent = $content; $updatedContent = $content;
foreach ($_POST as $key => $value) { // new platform adding
// Create a regex pattern to match the key-value pair for the specified platform ID if (isset($_POST['new']) && $_POST['new'] === 'true') {
$pattern = "/((?:'[^']+'\s*=>\s*'[^']+'\s*,?\s*)*)('{$key}'\s*=>\s*)'[^']*'/s"; $newPlatform = [
// Replace using a callback to handle the match and replacement 'name' => $_POST['name'],
$updatedContent = preg_replace_callback($pattern, function($matches) use ($value) { 'jilo_database' => $_POST['jilo_database'],
return $matches[1] . $matches[2] . "'{$value}'"; ];
}, $updatedContent
// Determine the next available index for the new platform
$nextIndex = count($config['platforms']);
// Add the new platform to the platforms array
$config['platforms'][$nextIndex] = $newPlatform;
// Rebuild the PHP array syntax for the platforms
$platformsArray = formatArray($config['platforms']);
// Replace the platforms section in the config file
$updatedContent = preg_replace(
'/\'platforms\'\s*=>\s*\[[\s\S]+?\],/s',
"'platforms' => {$platformsArray}",
$content
); );
$updatedContent = preg_replace('/\s*\]\n/s', "\n", $updatedContent);
// deleting a platform
} elseif (isset($_POST['delete']) && $_POST['delete'] === 'true') {
$platform = $_POST['platform'];
$config['platforms'][$platform]['name'] = $_POST['name'];
$config['platforms'][$platform]['jilo_database'] = $_POST['jilo_database'];
$platformsArray = formatArray($config['platforms'][$platform], 3);
$updatedContent = preg_replace(
"/\s*'$platform'\s*=>\s*\[\s*'name'\s*=>\s*'[^']*',\s*'jilo_database'\s*=>\s*'[^']*',\s*\],/s",
"",
$content
);
// an update to an existing platform
} else {
$platform = $_POST['platform'];
$config['platforms'][$platform]['name'] = $_POST['name'];
$config['platforms'][$platform]['jilo_database'] = $_POST['jilo_database'];
$platformsArray = formatArray($config['platforms'][$platform], 3);
$updatedContent = preg_replace(
"/\s*'$platform'\s*=>\s*\[\s*'name'\s*=>\s*'[^']*',\s*'jilo_database'\s*=>\s*'[^']*',\s*\],/s",
"\n '{$platform}' => {$platformsArray},",
$content
);
} }
// check if file is writable // check if file is writable
if (!is_writable($config_file)) { if (!is_writable($config_file)) {
$_SESSION['error'] = getError('Configuration file is not writable.'); $_SESSION['error'] = getError('Configuration file is not writable.');
@ -45,9 +96,15 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// no form submitted, show the templates // no form submitted, show the templates
} else { } else {
switch ($action) { switch ($action) {
case 'add':
include('../app/templates/config-add-platform.php');
break;
case 'edit': case 'edit':
include('../app/templates/config-edit-platform.php'); include('../app/templates/config-edit-platform.php');
break; break;
case 'delete':
include('../app/templates/config-delete-platform.php');
break;
default: default:
include('../app/templates/config-list.php'); include('../app/templates/config-list.php');
} }

View File

@ -0,0 +1,39 @@
<!-- widget "config" -->
<div class="card text-center w-50 mx-auto">
<p class="h4 card-header">Add new Jitsi platform</p>
<div class="card-body">
<!--p class="card-text">add new platform:</p-->
<form method="POST" action="<?= $app_root ?>?platform=<?= htmlspecialchars($platform_id) ?>&page=config">
<div class="row mb-3">
<div class="col-md-4 text-end">
<label for="name" class="form-label">name</label>
<span class="text-danger" style="margin-right: -12px;">*</span>
</div>
<div class="col-md-8">
<input class="form-control" type="text" name="name" value="" required />
<p class="text-start"><small>descriptive name for the platform</small></p>
</div>
</div>
<div class="row mb-3">
<div class="col-md-4 text-end">
<label for="name" class="form-label">jilo_database</label>
<span class="text-danger" style="margin-right: -12px;">*</span>
</div>
<div class="col-md-8">
<input class="form-control" type="text" name="jilo_database" value="" required />
<p class="text-start"><small>path to the database file (relative to the app root)</small></p>
</div>
</div>
<input type="hidden" name="new" value="true" />
<br />
<a class="btn btn-secondary" href="<?= $app_root ?>?page=config" />Cancel</a>
<input type="submit" class="btn btn-primary" value="Save" />
</form>
</div>
</div>
<!-- /widget "config" -->

View File

@ -0,0 +1,29 @@
<!-- widget "config" -->
<div class="card text-center w-50 mx-auto">
<p class="h4 card-header">Jilo web configuration for Jitsi platform "<?= htmlspecialchars($platform_id) ?>"</p>
<div class="card-body">
<p class="card-text">delete a platform:</p>
<form method="POST" action="<?= $app_root ?>?platform=<?= htmlspecialchars($platform_id) ?>&page=config">
<?php foreach ($config['platforms'][$platform_id] as $config_item => $config_value) { ?>
<div class="row mb-3">
<div class="col-md-4 text-end">
<label for="<?= htmlspecialchars($config_item) ?>" class="form-label"><?= htmlspecialchars($config_item) ?>:</label>
</div>
<div class="col-md-8">
<div class="text-start"><?= htmlspecialchars($config_value ?? '')?></div>
<input type="hidden" name="<?= htmlspecialchars($config_item) ?>" value="<?= htmlspecialchars($config_value ?? '')?>" />
</div>
</div>
<?php } ?>
<br />
<input type="hidden" name="platform" value="<?= htmlspecialchars($platform_id) ?>" />
<input type="hidden" name="delete" value="true" />
<p class="h5 text-danger">Are you sure you want to delete this platform?</p>
<br />
<a class="btn btn-secondary" href="<?= $app_root ?>?page=config" />Cancel</a>
<input type="submit" class="btn btn-danger" value="Delete" />
</form>
</div>
</div>
<!-- /widget "config" -->

View File

@ -1,7 +1,7 @@
<!-- widget "config" --> <!-- widget "config" -->
<div class="card text-center w-50 mx-auto"> <div class="card text-center w-50 mx-auto">
<p class="h4 card-header">Jilo web configuration for Jitsi platform"<?= htmlspecialchars($platform_id) ?>"</p> <p class="h4 card-header">Jilo web configuration for Jitsi platform "<?= htmlspecialchars($platform_id) ?>"</p>
<div class="card-body"> <div class="card-body">
<p class="card-text">edit the platform details:</p> <p class="card-text">edit the platform details:</p>
<form method="POST" action="<?= $app_root ?>?platform=<?= htmlspecialchars($platform_id) ?>&page=config"> <form method="POST" action="<?= $app_root ?>?platform=<?= htmlspecialchars($platform_id) ?>&page=config">
@ -13,11 +13,17 @@
</div> </div>
<div class="col-md-8"> <div class="col-md-8">
<input class="form-control" type="text" name="<?= htmlspecialchars($config_item) ?>" value="<?= htmlspecialchars($config_value ?? '')?>" required /> <input class="form-control" type="text" name="<?= htmlspecialchars($config_item) ?>" value="<?= htmlspecialchars($config_value ?? '')?>" required />
<?php if ($config_item === 'name') { ?>
<p class="text-start"><small>descriptive name for the platform</small></p>
<?php } elseif ($config_item === 'jilo_database') { ?>
<p class="text-start"><small>path to the database file (relative to the app root)</small></p>
<?php } ?>
</div> </div>
</div> </div>
<?php } ?> <?php } ?>
<br />&nbsp;<br /> <br />
<a class="btn btn-secondary" href="<?= $app_root ?>?platform=<?= htmlspecialchars($platform_id) ?>&page=config" />Cancel</a> <input type="hidden" name="platform" value="<?= htmlspecialchars($platform_id) ?>" />
<a class="btn btn-secondary" href="<?= $app_root ?>?page=config" />Cancel</a>
<input type="submit" class="btn btn-primary" value="Save" /> <input type="submit" class="btn btn-primary" value="Save" />
</form> </form>
</div> </div>

View File

@ -12,11 +12,13 @@
<?php if ( isset($_SESSION['username']) ) { ?> <?php if ( isset($_SESSION['username']) ) { ?>
<?php foreach ($config['platforms'] as $index => $platform) { ?>
<li style="margin-right: 3px;"> <li style="margin-right: 3px;">
<a style="background-color: #111;" href="?platform=<?= htmlspecialchars(array_keys($config['platforms'])[$platform_id]) ?>&page=front"> <a style="background-color: #111;" href="?platform=<?= htmlspecialchars($index) ?>&page=front">
<?= htmlspecialchars($config['platforms'][$platform_id]['name']) ?> <?= htmlspecialchars($platform['name']) ?>
</a> </a>
</li> </li>
<?php } ?>
<?php } ?> <?php } ?>
</ul> </ul>