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 into plugins array.
85 * @param mixed $plugins Plugins array ($plugins[<plugin_name>]['parameters']['param_name'] = <value>.
86 * @param mixed $conf Plugins configuration.
88 * @return mixed Updated $plugins array.
90 function load_plugin_parameter_values($plugins, $conf)
93 foreach ($plugins as $name => $plugin) {
94 if (empty($plugin['parameters'])) {
98 foreach ($plugin['parameters'] as $key => $param) {
99 if (!empty($conf[$key])) {
100 $out[$name]['parameters'][$key] = $conf[$key];
109 * Exception used if an error occur while saving plugin configuration.
111 class PluginConfigOrderException
extends Exception
114 * Construct exception.
116 public function __construct()
118 $this->message
= 'An error occurred while trying to save plugins loading order.';