2 namespace Shaarli\Config
;
5 * Plugin configuration helper functions.
7 * Note: no access to configuration files here.
11 * Process plugin administration form data and save it in an array.
13 * @param array $formData Data sent by the plugin admin form.
15 * @return array New list of enabled plugin, ordered.
17 * @throws PluginConfigOrderException Plugins can't be sorted because their order is invalid.
19 function save_plugin_config($formData)
21 // Make sure there are no duplicates in orders.
22 if (!validate_plugin_order($formData)) {
23 throw new PluginConfigOrderException();
27 $newEnabledPlugins = array();
28 foreach ($formData as $key => $data) {
29 if (startsWith($key, 'order')) {
33 // If there is no order, it means a disabled plugin has been enabled.
34 if (isset($formData['order_' . $key])) {
35 $plugins[(int) $formData['order_' . $key]] = $key;
38 $newEnabledPlugins[] = $key;
42 // New enabled plugins will be added at the end of order.
43 $plugins = array_merge($plugins, $newEnabledPlugins);
45 // Sort plugins by order.
46 if (!ksort($plugins)) {
47 throw new PluginConfigOrderException();
50 $finalPlugins = array();
51 // Make plugins order continuous.
52 foreach ($plugins as $plugin) {
53 $finalPlugins[] = $plugin;
60 * Validate plugin array submitted.
61 * Will fail if there is duplicate orders value.
63 * @param array $formData Data from submitted form.
65 * @return bool true if ok, false otherwise.
67 function validate_plugin_order($formData)
70 foreach ($formData as $key => $value) {
71 // No duplicate order allowed.
72 if (in_array($value, $orders)) {
76 if (startsWith($key, 'order')) {
85 * Affect plugin parameters values from the ConfigManager into plugins array.
87 * @param mixed $plugins Plugins array:
88 * $plugins[<plugin_name>]['parameters'][<param_name>] = [
90 * 'desc' => <description>
92 * @param mixed $conf Plugins configuration.
94 * @return mixed Updated $plugins array.
96 function load_plugin_parameter_values($plugins, $conf)
99 foreach ($plugins as $name => $plugin) {
100 if (empty($plugin['parameters'])) {
104 foreach ($plugin['parameters'] as $key => $param) {
105 if (!empty($conf[$key])) {
106 $out[$name]['parameters'][$key]['value'] = $conf[$key];
115 * Exception used if an error occur while saving plugin configuration.
117 class PluginConfigOrderException
extends \Exception
120 * Construct exception.
122 public function __construct()
124 $this->message
= 'An error occurred while trying to save plugins loading order.';