mirror of
https://github.com/slawkens/myaac.git
synced 2025-05-01 11:49:20 +02:00
Add Settings menu, including all plugins with settings
One change included in previous commit, due to missclick
This commit is contained in:
parent
939508e799
commit
24ab125d28
@ -2,7 +2,9 @@
|
|||||||
|
|
||||||
return [
|
return [
|
||||||
['name' => 'Dashboard', 'icon' => 'tachometer-alt', 'order' => 10, 'link' => 'dashboard'],
|
['name' => 'Dashboard', 'icon' => 'tachometer-alt', 'order' => 10, 'link' => 'dashboard'],
|
||||||
['name' => 'Settings', 'icon' => 'edit', 'order' => 19, 'link' => 'settings&plugin=core'],
|
['name' => 'Settings', 'icon' => 'edit', 'order' => 19, 'link' =>
|
||||||
|
require ADMIN . 'includes/settings_menus.php'
|
||||||
|
],
|
||||||
['name' => 'News', 'icon' => 'newspaper', 'order' => 20, 'link' =>
|
['name' => 'News', 'icon' => 'newspaper', 'order' => 20, 'link' =>
|
||||||
[
|
[
|
||||||
['name' => 'View', 'link' => 'news', 'icon' => 'list', 'order' => 10],
|
['name' => 'View', 'link' => 'news', 'icon' => 'list', 'order' => 10],
|
||||||
|
@ -301,18 +301,18 @@ class Settings implements ArrayAccess
|
|||||||
|
|
||||||
$this->loadPlugin($offset);
|
$this->loadPlugin($offset);
|
||||||
|
|
||||||
$pluginName = $this->valuesAsked['pluginName'];
|
$pluginKeyName = $this->valuesAsked['pluginKeyName'];
|
||||||
$key = $this->valuesAsked['key'];
|
$key = $this->valuesAsked['key'];
|
||||||
|
|
||||||
// remove whole plugin settings
|
// remove whole plugin settings
|
||||||
if (!isset($value)) {
|
if (!isset($value)) {
|
||||||
$this->offsetUnset($offset);
|
$this->offsetUnset($offset);
|
||||||
$this->deleteFromDatabase($pluginName, $key);
|
$this->deleteFromDatabase($pluginKeyName, $key);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->settings[$pluginName][$key] = $value;
|
$this->settings[$pluginKeyName][$key] = $value;
|
||||||
$this->updateInDatabase($pluginName, $key, $value);
|
$this->updateInDatabase($pluginKeyName, $key, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[\ReturnTypeWillChange]
|
#[\ReturnTypeWillChange]
|
||||||
@ -320,7 +320,7 @@ class Settings implements ArrayAccess
|
|||||||
{
|
{
|
||||||
$this->loadPlugin($offset);
|
$this->loadPlugin($offset);
|
||||||
|
|
||||||
$pluginName = $this->valuesAsked['pluginName'];
|
$pluginKeyName = $this->valuesAsked['pluginKeyName'];
|
||||||
$key = $this->valuesAsked['key'];
|
$key = $this->valuesAsked['key'];
|
||||||
|
|
||||||
// remove specified plugin settings (all)
|
// remove specified plugin settings (all)
|
||||||
@ -328,7 +328,7 @@ class Settings implements ArrayAccess
|
|||||||
return isset($this->settings[$offset]);
|
return isset($this->settings[$offset]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return isset($this->settings[$pluginName][$key]);
|
return isset($this->settings[$pluginKeyName][$key]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[\ReturnTypeWillChange]
|
#[\ReturnTypeWillChange]
|
||||||
@ -336,7 +336,7 @@ class Settings implements ArrayAccess
|
|||||||
{
|
{
|
||||||
$this->loadPlugin($offset);
|
$this->loadPlugin($offset);
|
||||||
|
|
||||||
$pluginName = $this->valuesAsked['pluginName'];
|
$pluginKeyName = $this->valuesAsked['pluginKeyName'];
|
||||||
$key = $this->valuesAsked['key'];
|
$key = $this->valuesAsked['key'];
|
||||||
|
|
||||||
if (isset($this->cache[$offset])) {
|
if (isset($this->cache[$offset])) {
|
||||||
@ -345,15 +345,15 @@ class Settings implements ArrayAccess
|
|||||||
|
|
||||||
// remove specified plugin settings (all)
|
// remove specified plugin settings (all)
|
||||||
if(!isset($key)) {
|
if(!isset($key)) {
|
||||||
unset($this->settingsArray[$pluginName]);
|
unset($this->settingsArray[$pluginKeyName]);
|
||||||
unset($this->settings[$pluginName]);
|
unset($this->settings[$pluginKeyName]);
|
||||||
$this->deleteFromDatabase($pluginName);
|
$this->deleteFromDatabase($pluginKeyName);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
unset($this->settingsArray[$pluginName][$key]);
|
unset($this->settingsArray[$pluginKeyName][$key]);
|
||||||
unset($this->settings[$pluginName][$key]);
|
unset($this->settings[$pluginKeyName][$key]);
|
||||||
$this->deleteFromDatabase($pluginName, $key);
|
$this->deleteFromDatabase($pluginKeyName, $key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -374,26 +374,26 @@ class Settings implements ArrayAccess
|
|||||||
|
|
||||||
$this->loadPlugin($offset);
|
$this->loadPlugin($offset);
|
||||||
|
|
||||||
$pluginName = $this->valuesAsked['pluginName'];
|
$pluginKeyName = $this->valuesAsked['pluginKeyName'];
|
||||||
$key = $this->valuesAsked['key'];
|
$key = $this->valuesAsked['key'];
|
||||||
|
|
||||||
// return specified plugin settings (all)
|
// return specified plugin settings (all)
|
||||||
if(!isset($key)) {
|
if(!isset($key)) {
|
||||||
return $this->settingsArray[$pluginName];
|
return $this->settingsArray[$pluginKeyName];
|
||||||
}
|
}
|
||||||
|
|
||||||
$ret = [];
|
$ret = [];
|
||||||
if(isset($this->settingsArray[$pluginName][$key])) {
|
if(isset($this->settingsArray[$pluginKeyName][$key])) {
|
||||||
$ret = $this->settingsArray[$pluginName][$key];
|
$ret = $this->settingsArray[$pluginKeyName][$key];
|
||||||
}
|
}
|
||||||
|
|
||||||
if(isset($this->settings[$pluginName][$key])) {
|
if(isset($this->settings[$pluginKeyName][$key])) {
|
||||||
$value = $this->settings[$pluginName][$key];
|
$value = $this->settings[$pluginKeyName][$key];
|
||||||
|
|
||||||
$ret['value'] = $value;
|
$ret['value'] = $value;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$ret['value'] = $this->settingsArray[$pluginName][$key]['default'];
|
$ret['value'] = $this->settingsArray[$pluginKeyName][$key]['default'];
|
||||||
}
|
}
|
||||||
|
|
||||||
if(isset($ret['type'])) {
|
if(isset($ret['type'])) {
|
||||||
@ -417,17 +417,17 @@ class Settings implements ArrayAccess
|
|||||||
|
|
||||||
private function updateValuesAsked($offset)
|
private function updateValuesAsked($offset)
|
||||||
{
|
{
|
||||||
$pluginName = $offset;
|
$pluginKeyName = $offset;
|
||||||
if (strpos($offset, '.')) {
|
if (strpos($offset, '.')) {
|
||||||
$explode = explode('.', $offset, 2);
|
$explode = explode('.', $offset, 2);
|
||||||
|
|
||||||
$pluginName = $explode[0];
|
$pluginKeyName = $explode[0];
|
||||||
$key = $explode[1];
|
$key = $explode[1];
|
||||||
|
|
||||||
$this->valuesAsked = ['pluginName' => $pluginName, 'key' => $key];
|
$this->valuesAsked = ['pluginKeyName' => $pluginKeyName, 'key' => $key];
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$this->valuesAsked = ['pluginName' => $pluginName, 'key' => null];
|
$this->valuesAsked = ['pluginKeyName' => $pluginKeyName, 'key' => null];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -435,28 +435,29 @@ class Settings implements ArrayAccess
|
|||||||
{
|
{
|
||||||
$this->updateValuesAsked($offset);
|
$this->updateValuesAsked($offset);
|
||||||
|
|
||||||
$pluginName = $this->valuesAsked['pluginName'];
|
$pluginKeyName = $this->valuesAsked['pluginKeyName'];
|
||||||
$key = $this->valuesAsked['key'];
|
$key = $this->valuesAsked['key'];
|
||||||
|
|
||||||
if (!isset($this->settingsArray[$pluginName])) {
|
if (!isset($this->settingsArray[$pluginKeyName])) {
|
||||||
if ($pluginName === 'core') {
|
if ($pluginKeyName === 'core') {
|
||||||
$settingsFilePath = SYSTEM . 'settings.php';
|
$settingsFilePath = SYSTEM . 'settings.php';
|
||||||
} else {
|
} else {
|
||||||
$pluginSettings = Plugins::getPluginSettings($pluginName);
|
//$pluginSettings = Plugins::getPluginSettings($pluginKeyName);
|
||||||
if (!$pluginSettings) {
|
$settings = Plugins::getAllPluginsSettings();
|
||||||
warning("Setting $pluginName does not exist or does not have settings defined.");
|
if (!isset($settings[$pluginKeyName])) {
|
||||||
|
warning("Setting $pluginKeyName does not exist or does not have settings defined.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$settingsFilePath = BASE . $pluginSettings;
|
$settingsFilePath = BASE . $settings[$pluginKeyName]['settingsFilename'];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!file_exists($settingsFilePath)) {
|
if (!file_exists($settingsFilePath)) {
|
||||||
throw new \RuntimeException('Failed to load settings file for plugin: ' . $pluginName);
|
throw new \RuntimeException('Failed to load settings file for plugin: ' . $pluginKeyName);
|
||||||
}
|
}
|
||||||
|
|
||||||
$tmp = require $settingsFilePath;
|
$tmp = require $settingsFilePath;
|
||||||
$this->settingsArray[$pluginName] = $tmp['settings'];
|
$this->settingsArray[$pluginKeyName] = $tmp['settings'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -168,6 +168,36 @@ class Plugins {
|
|||||||
return $hooks;
|
return $hooks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function getAllPluginsSettings()
|
||||||
|
{
|
||||||
|
$cache = Cache::getInstance();
|
||||||
|
if ($cache->enabled()) {
|
||||||
|
$tmp = '';
|
||||||
|
if ($cache->fetch('plugins_settings', $tmp)) {
|
||||||
|
return unserialize($tmp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$settings = [];
|
||||||
|
foreach (self::getAllPluginsJson() as $plugin) {
|
||||||
|
if (isset($plugin['settings'])) {
|
||||||
|
$settingsFile = require BASE . $plugin['settings'];
|
||||||
|
if (!isset($settingsFile['key'])) {
|
||||||
|
warning("Settings file for plugin - {$plugin['name']} does not contain 'key' field");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$settings[$settingsFile['key']] = ['pluginFilename' => $plugin['filename'], 'settingsFilename' => $plugin['settings']];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($cache->enabled()) {
|
||||||
|
$cache->set('plugins_settings', serialize($settings), 600); // cache for 10 minutes
|
||||||
|
}
|
||||||
|
|
||||||
|
return $settings;
|
||||||
|
}
|
||||||
|
|
||||||
public static function getAllPluginsJson($disabled = false)
|
public static function getAllPluginsJson($disabled = false)
|
||||||
{
|
{
|
||||||
$cache = Cache::getInstance();
|
$cache = Cache::getInstance();
|
||||||
@ -180,32 +210,26 @@ class Plugins {
|
|||||||
|
|
||||||
$plugins = [];
|
$plugins = [];
|
||||||
foreach (get_plugins($disabled) as $filename) {
|
foreach (get_plugins($disabled) as $filename) {
|
||||||
$string = file_get_contents(PLUGINS . $filename . '.json');
|
$plugin = self::getPluginJson($filename);
|
||||||
$plugin = json_decode($string, true);
|
|
||||||
self::$plugin_json = $plugin;
|
if (!$plugin) {
|
||||||
if ($plugin == null) {
|
|
||||||
self::$warnings[] = 'Cannot load ' . $filename . '.json. File might be not a valid json code.';
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($plugin['enabled']) && !getBoolean($plugin['enabled'])) {
|
|
||||||
self::$warnings[] = 'Skipping ' . $filename . '... The plugin is disabled.';
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$plugin['filename'] = $filename;
|
||||||
$plugins[] = $plugin;
|
$plugins[] = $plugin;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($cache->enabled()) {
|
if ($cache->enabled()) {
|
||||||
$cache->set('plugins', serialize($plugins), 600);
|
$cache->set('plugins', serialize($plugins), 600); // cache for 10 minutes
|
||||||
}
|
}
|
||||||
|
|
||||||
return $plugins;
|
return $plugins;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getPluginSettings($pluginName)
|
public static function getPluginSettings($filename)
|
||||||
{
|
{
|
||||||
$plugin_json = self::getPluginJson($pluginName);
|
$plugin_json = self::getPluginJson($filename);
|
||||||
if (!$plugin_json) {
|
if (!$plugin_json) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -217,27 +241,27 @@ class Plugins {
|
|||||||
return $plugin_json['settings'];
|
return $plugin_json['settings'];
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getPluginJson($name = null)
|
public static function getPluginJson($filename = null)
|
||||||
{
|
{
|
||||||
if(!isset($name)) {
|
if(!isset($filename)) {
|
||||||
return self::$plugin_json;
|
return self::$plugin_json;
|
||||||
}
|
}
|
||||||
|
|
||||||
$pathToPlugin = PLUGINS . $name . '.json';
|
$pathToPlugin = PLUGINS . $filename . '.json';
|
||||||
if (!file_exists($pathToPlugin)) {
|
if (!file_exists($pathToPlugin)) {
|
||||||
self::$warnings[] = "Cannot load " . $name . ".json. File doesn't exist.";
|
self::$warnings[] = "Cannot load $filename.json. File doesn't exist.";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$string = file_get_contents($pathToPlugin);
|
$string = file_get_contents($pathToPlugin);
|
||||||
$plugin_json = json_decode($string, true);
|
$plugin_json = json_decode($string, true);
|
||||||
if ($plugin_json == null) {
|
if ($plugin_json == null) {
|
||||||
self::$warnings[] = 'Cannot load ' . $name . '.json. File might be not a valid json code.';
|
self::$warnings[] = "Cannot load $filename.json. File might be not a valid json code.";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(isset($plugin_json['enabled']) && $plugin_json['enabled'] === 0) {
|
if (isset($plugin_json['enabled']) && !getBoolean($plugin_json['enabled'])) {
|
||||||
self::$warnings[] = 'Skipping ' . $name . '... The plugin is disabled.';
|
self::$warnings[] = 'Skipping ' . $filename . '... The plugin is disabled.';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user