3 * Functions related to configuration management.
7 * Process plugin administration form data and save it in an array.
9 * @param array $formData Data sent by the plugin admin form.
11 * @return array New list of enabled plugin, ordered.
13 * @throws PluginConfigOrderException Plugins can't be sorted because their order is invalid.
15 function save_plugin_config($formData)
17 // Make sure there are no duplicates in orders.
18 if (!validate_plugin_order($formData)) {
19 throw new PluginConfigOrderException();
23 $newEnabledPlugins = array();
24 foreach ($formData as $key => $data) {
25 if (startsWith($key, 'order')) {
29 // If there is no order, it means a disabled plugin has been enabled.
30 if (isset($formData['order_' . $key])) {
31 $plugins[(int) $formData['order_' . $key]] = $key;
34 $newEnabledPlugins[] = $key;
38 // New enabled plugins will be added at the end of order.
39 $plugins = array_merge($plugins, $newEnabledPlugins);
41 // Sort plugins by order.
42 if (!ksort($plugins)) {
43 throw new PluginConfigOrderException();
46 $finalPlugins = array();
47 // Make plugins order continuous.
48 foreach ($plugins as $plugin) {
49 $finalPlugins[] = $plugin;
56 * Validate plugin array submitted.
57 * Will fail if there is duplicate orders value.
59 * @param array $formData Data from submitted form.
61 * @return bool true if ok, false otherwise.
63 function validate_plugin_order($formData)
66 foreach ($formData as $key => $value) {
67 // No duplicate order allowed.
68 if (in_array($value, $orders)) {
72 if (startsWith($key, 'order')) {
81 * Affect plugin parameters values into plugins array.
83 * @param mixed $plugins Plugins array ($plugins[<plugin_name>]['parameters']['param_name'] = <value>.
84 * @param mixed $conf Plugins configuration.
86 * @return mixed Updated $plugins array.
88 function load_plugin_parameter_values($plugins, $conf)
91 foreach ($plugins as $name => $plugin) {
92 if (empty($plugin['parameters'])) {
96 foreach ($plugin['parameters'] as $key => $param) {
97 if (!empty($conf[$key])) {
98 $out[$name]['parameters'][$key] = $conf[$key];
107 * Exception used if an error occur while saving plugin configuration.
109 class PluginConfigOrderException
extends Exception
112 * Construct exception.
114 public function __construct()
116 $this->message
= 'An error occurred while trying to save plugins loading order.';