aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/config/ConfigPlugin.php
diff options
context:
space:
mode:
Diffstat (limited to 'application/config/ConfigPlugin.php')
-rw-r--r--application/config/ConfigPlugin.php118
1 files changed, 118 insertions, 0 deletions
diff --git a/application/config/ConfigPlugin.php b/application/config/ConfigPlugin.php
new file mode 100644
index 00000000..8af89d04
--- /dev/null
+++ b/application/config/ConfigPlugin.php
@@ -0,0 +1,118 @@
1<?php
2/**
3 * Functions related to configuration management.
4 */
5
6/**
7 * Process plugin administration form data and save it in an array.
8 *
9 * @param array $formData Data sent by the plugin admin form.
10 *
11 * @return array New list of enabled plugin, ordered.
12 *
13 * @throws PluginConfigOrderException Plugins can't be sorted because their order is invalid.
14 */
15function save_plugin_config($formData)
16{
17 // Make sure there are no duplicates in orders.
18 if (!validate_plugin_order($formData)) {
19 throw new PluginConfigOrderException();
20 }
21
22 $plugins = array();
23 $newEnabledPlugins = array();
24 foreach ($formData as $key => $data) {
25 if (startsWith($key, 'order')) {
26 continue;
27 }
28
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;
32 }
33 else {
34 $newEnabledPlugins[] = $key;
35 }
36 }
37
38 // New enabled plugins will be added at the end of order.
39 $plugins = array_merge($plugins, $newEnabledPlugins);
40
41 // Sort plugins by order.
42 if (!ksort($plugins)) {
43 throw new PluginConfigOrderException();
44 }
45
46 $finalPlugins = array();
47 // Make plugins order continuous.
48 foreach ($plugins as $plugin) {
49 $finalPlugins[] = $plugin;
50 }
51
52 return $finalPlugins;
53}
54
55/**
56 * Validate plugin array submitted.
57 * Will fail if there is duplicate orders value.
58 *
59 * @param array $formData Data from submitted form.
60 *
61 * @return bool true if ok, false otherwise.
62 */
63function validate_plugin_order($formData)
64{
65 $orders = array();
66 foreach ($formData as $key => $value) {
67 // No duplicate order allowed.
68 if (in_array($value, $orders)) {
69 return false;
70 }
71
72 if (startsWith($key, 'order')) {
73 $orders[] = $value;
74 }
75 }
76
77 return true;
78}
79
80/**
81 * Affect plugin parameters values into plugins array.
82 *
83 * @param mixed $plugins Plugins array ($plugins[<plugin_name>]['parameters']['param_name'] = <value>.
84 * @param mixed $conf Plugins configuration.
85 *
86 * @return mixed Updated $plugins array.
87 */
88function load_plugin_parameter_values($plugins, $conf)
89{
90 $out = $plugins;
91 foreach ($plugins as $name => $plugin) {
92 if (empty($plugin['parameters'])) {
93 continue;
94 }
95
96 foreach ($plugin['parameters'] as $key => $param) {
97 if (!empty($conf[$key])) {
98 $out[$name]['parameters'][$key] = $conf[$key];
99 }
100 }
101 }
102
103 return $out;
104}
105
106/**
107 * Exception used if an error occur while saving plugin configuration.
108 */
109class PluginConfigOrderException extends Exception
110{
111 /**
112 * Construct exception.
113 */
114 public function __construct()
115 {
116 $this->message = 'An error occurred while trying to save plugins loading order.';
117 }
118}