3 * Plugin configuration helper functions.
5 * Note: no access to configuration files here.
9 * Process plugin administration form data and save it in an array.
11 * @param array $formData Data sent by the plugin admin form.
13 * @return array New list of enabled plugin, ordered.
15 * @throws PluginConfigOrderException Plugins can't be sorted because their order is invalid.
17 function save_plugin_config($formData)
19 // Make sure there are no duplicates in orders.
20 if (!validate_plugin_order($formData)) {
21 throw new PluginConfigOrderException();
25 $newEnabledPlugins = array();
26 foreach ($formData as $key => $data) {
27 if (startsWith($key, 'order')) {
31 // If there is no order, it means a disabled plugin has been enabled.
32 if (isset($formData['order_' . $key])) {
33 $plugins[(int) $formData['order_' . $key]] = $key;
36 $newEnabledPlugins[] = $key;
40 // New enabled plugins will be added at the end of order.
41 $plugins = array_merge($plugins, $newEnabledPlugins);
43 // Sort plugins by order.
44 if (!ksort($plugins)) {
45 throw new PluginConfigOrderException();
48 $finalPlugins = array();
49 // Make plugins order continuous.
50 foreach ($plugins as $plugin) {
51 $finalPlugins[] = $plugin;
58 * Validate plugin array submitted.
59 * Will fail if there is duplicate orders value.
61 * @param array $formData Data from submitted form.
63 * @return bool true if ok, false otherwise.
65 function validate_plugin_order($formData)
68 foreach ($formData as $key => $value) {
69 // No duplicate order allowed.
70 if (in_array($value, $orders)) {
74 if (startsWith($key, 'order')) {
83 * Affect plugin parameters values from the ConfigManager into plugins array.
85 * @param mixed $plugins Plugins array:
86 * $plugins[<plugin_name>]['parameters'][<param_name>] = [
88 * 'desc' => <description>
90 * @param mixed $conf Plugins configuration.
92 * @return mixed Updated $plugins array.
94 function load_plugin_parameter_values($plugins, $conf)
97 foreach ($plugins as $name => $plugin) {
98 if (empty($plugin['parameters'])) {
102 foreach ($plugin['parameters'] as $key => $param) {
103 if (!empty($conf[$key])) {
104 $out[$name]['parameters'][$key]['value'] = $conf[$key];
113 * Exception used if an error occur while saving plugin configuration.
115 class PluginConfigOrderException
extends Exception
118 * Construct exception.
120 public function __construct()
122 $this->message
= 'An error occurred while trying to save plugins loading order.';