Settings [WIP]

New Settings class
New Plugins::load() method
Move config.php to settings.php
MyAAC Settings will have plugin_name = 'core'
Add compat_config.php
This commit is contained in:
slawkens
2020-06-09 01:39:55 +02:00
parent 909bfffb51
commit fa0de1c413
11 changed files with 480 additions and 462 deletions

View File

@@ -8,71 +8,76 @@
* @link https://my-aac.org
*/
defined('MYAAC') or die('Direct access not allowed!');
$title = 'Options';
$title = 'Settings';
$plugin = $_GET['plugin'];
if (!isset($plugin) || empty($plugin)) {
require_once SYSTEM . 'clients.conf.php';
if (!isset($_GET['plugin']) || empty($_GET['plugin'])) {
error('Please select plugin name from left Panel.');
return;
}
$pluginOptions = Plugins::getPluginOptions($plugin);
if (!$pluginOptions) {
error('This plugin does not exist or does not have options defined.');
return;
}
$plugin = $_GET['plugin'];
$message = '';
$optionsFile = require BASE . $pluginOptions;
if (!is_array($optionsFile)) {
return;
}
$name = $optionsFile['name'];
$options = $optionsFile['options'];
if (isset($_POST['save'])) {
foreach ($options as $key => $_config) {
// TODO:
// Save functionality
// Check if exist, then INSERT or UPDATE
/*$query = $db->query(
sprintf('SELECT `value` FROM `%s` WHERE `name` = %s AND `key` = %s',
TABLE_PREFIX . 'options_' . $table,
$name,
$key)
);*/
if($plugin != 'core') {
$pluginSettings = Plugins::getPluginSettings($plugin);
if (!$pluginSettings) {
error('This plugin does not exist or does not have options defined.');
return;
}
}
$optionsValues = [];
$optionsTypes = ['bool', 'double', 'int', 'text', 'varchar'];
foreach($optionsTypes as $type) {
$query = 'SELECT `key`, `value` FROM `' . TABLE_PREFIX . 'options_' . $type . '` WHERE `name` = ' . $db->quote($name) . ';';
$query = $db->query($query);
$optionsValues = $optionsValues + $query->fetchAll();
if($plugin === 'core') {
$settingsFile = require SYSTEM . 'settings.php';
}
else {
$settingsFile = require BASE . $pluginSettings;
}
if (!is_array($settingsFile)) {
return;
}
if (isset($_POST['save'])) {
$db->query('DELETE FROM `' . TABLE_PREFIX . 'settings` WHERE `plugin_name` = ' . $db->quote($plugin) . ';');
foreach ($_POST['settings'] as $key => $value) {
try {
$db->insert(TABLE_PREFIX . 'settings', ['plugin_name' => $plugin, 'key' => $key, 'value' => $value]);
} catch (PDOException $error) {
warning('Error while saving setting (' . $plugin . ' - ' . $key . '): ' . $error->getMessage());
}
}
$cache = Cache::getInstance();
if ($cache->enabled()) {
$cache->delete('settings');
}
success('Saved at ' . date('H:i'));
}
$title = ($plugin == 'core' ? 'MyAAC Settings' : 'Plugin Settings - ' . $plugin);
$query = 'SELECT `key`, `value` FROM `' . TABLE_PREFIX . 'settings` WHERE `plugin_name` = ' . $db->quote($plugin) . ';';
$query = $db->query($query);
$settingsDb = [];
if($query->rowCount() > 0) {
foreach($query->fetchAll(PDO::FETCH_ASSOC) as $value) {
$settingsDb[$value['key']] = $value['value'];
}
}
//var_dump($optionsValues);
?>
<form method="post">
<div class="row">
<div class="col-md-12">
<div class="box">
<div class="box-header">
<h3 class="box-title">Plugin Options - <?= $plugin ?></h3>
</div>
<div class="box-body">
<?= $message ?>
<button name="save" type="submit" class="btn btn-primary">Save</button>
</div>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th style="width: 10%">Key</th>
<th style="width: 10%">Name</th>
<th style="width: 30%">Value</th>
<th>Description</th>
@@ -81,37 +86,76 @@ foreach($optionsTypes as $type) {
<tbody>
<?php
foreach($options as $key => $_config) {
$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> ';
};
foreach($settingsFile as $key => $setting) {
?>
<tr>
<td><label for="<?= $key ?>" class="control-label"><?= $key ?></label></td>
<td><label for="<?= $key ?>" class="control-label"><?= $_config['name'] ?></label></td>
<td><label for="<?= $key ?>" class="control-label"><?= $setting['name'] ?></label></td>
<td>
<?php
if ($_config['type'] === 'boolean') {
$_config['type'] = 'options';
$_config['options'] = ['true' => 'Yes', 'false' => 'No'];
}
else if (in_array($_config['type'], ['varchar', 'number'])) {
echo '<input class="form-control" type="' . $_config['type'] . '" name="' . $key . '" value="' . (config($key) === null ? $_config['default'] : config($key)) . '" id="' . $key . '"/>';
}
else if($_config['type'] === 'textarea') {
echo '<textarea class="form-control" name="' . $key . '" id="' . $key . '">' . config($key) . '</textarea>';
}
if ($_config['type'] === 'options') {
echo '<select class="form-control" name="' . $key . '" id="' . $key . '">';
foreach ($_config['options'] as $value => $option) {
if($value === 'true') {
$selected = config($key) === true;
}
else if($value === 'false') {
$selected = config($key) === false;
if ($setting['type'] === 'boolean') {
if(isset($settingsDb[$key])) {
if($settingsDb[$key] === 'true') {
$value = true;
}
else {
$selected = config($key) == $value;
$value = false;
}
}
else {
$value = (isset($setting['default']) ? $setting['default'] : false);
}
$checkbox($key, true, $value);
$checkbox($key, false, $value);
}
else if (in_array($setting['type'], ['text', 'number'])) {
echo '<input class="form-control" type="' . $setting['type'] . '" name="settings[' . $key . ']" value="' . (isset($settingsDb[$key]) ? $settingsDb[$key] : (!empty($setting['default']) ? $setting['default'] : '')) . '" id="' . $key . '"/>';
}
else if($setting['type'] === 'textarea') {
echo '<textarea class="form-control" name="settings[' . $key . ']" id="' . $key . '">' . (isset($settingsDb[$key]) ? $settingsDb[$key] : (!empty($setting['default']) ? $setting['default'] : '')) . '</textarea>';
}
if ($setting['type'] === 'options') {
if ($setting['options'] === '$templates') {
$templates = array();
foreach (get_templates() as $value) {
$templates[$value] = $value;
}
$setting['options'] = $templates;
}
else if($setting['options'] === '$clients') {
$clients = array();
foreach((array)config('clients') as $client) {
$client_version = (string)($client / 100);
if(strpos($client_version, '.') === false)
$client_version .= '.0';
$clients[$client] = $client_version;
}
$setting['options'] = $clients;
}
echo '<select class="form-control" name="settings[' . $key . ']" id="' . $key . '">';
foreach ($setting['options'] as $value => $option) {
$compareTo = (isset($settingsDb[$key]) ? $settingsDb[$key] : (isset($setting['default']) ? $setting['default'] : ''));
if($value === 'true') {
$selected = $compareTo === true;
}
else if($value === 'false') {
$selected = $compareTo === false;
}
else {
$selected = $compareTo == $value;
}
echo '<option value="' . $value . '" ' . ($selected ? 'selected' : '') . '>' . $option . '</option>';
@@ -122,7 +166,7 @@ foreach($optionsTypes as $type) {
</td>
<td>
<div class="well">
<?= $_config['desc'] ?>
<?= $setting['desc'] ?>
</div>
</td>
</tr>