]>
Commit | Line | Data |
---|---|---|
59404d79 | 1 | <?php |
e6cd773f A |
2 | |
3 | use Shaarli\Config\Exception\PluginConfigOrderException; | |
3c66e564 | 4 | |
59404d79 | 5 | /** |
684e662a A |
6 | * Plugin configuration helper functions. |
7 | * | |
8 | * Note: no access to configuration files here. | |
59404d79 A |
9 | */ |
10 | ||
11 | /** | |
12 | * Process plugin administration form data and save it in an array. | |
13 | * | |
14 | * @param array $formData Data sent by the plugin admin form. | |
15 | * | |
16 | * @return array New list of enabled plugin, ordered. | |
17 | * | |
18 | * @throws PluginConfigOrderException Plugins can't be sorted because their order is invalid. | |
19 | */ | |
20 | function save_plugin_config($formData) | |
21 | { | |
22 | // Make sure there are no duplicates in orders. | |
23 | if (!validate_plugin_order($formData)) { | |
24 | throw new PluginConfigOrderException(); | |
25 | } | |
26 | ||
27 | $plugins = array(); | |
28 | $newEnabledPlugins = array(); | |
29 | foreach ($formData as $key => $data) { | |
30 | if (startsWith($key, 'order')) { | |
31 | continue; | |
32 | } | |
33 | ||
34 | // If there is no order, it means a disabled plugin has been enabled. | |
35 | if (isset($formData['order_' . $key])) { | |
36 | $plugins[(int) $formData['order_' . $key]] = $key; | |
37 | } | |
38 | else { | |
39 | $newEnabledPlugins[] = $key; | |
40 | } | |
41 | } | |
42 | ||
43 | // New enabled plugins will be added at the end of order. | |
44 | $plugins = array_merge($plugins, $newEnabledPlugins); | |
45 | ||
46 | // Sort plugins by order. | |
47 | if (!ksort($plugins)) { | |
48 | throw new PluginConfigOrderException(); | |
49 | } | |
50 | ||
51 | $finalPlugins = array(); | |
52 | // Make plugins order continuous. | |
53 | foreach ($plugins as $plugin) { | |
54 | $finalPlugins[] = $plugin; | |
55 | } | |
56 | ||
57 | return $finalPlugins; | |
58 | } | |
59 | ||
60 | /** | |
61 | * Validate plugin array submitted. | |
62 | * Will fail if there is duplicate orders value. | |
63 | * | |
64 | * @param array $formData Data from submitted form. | |
65 | * | |
66 | * @return bool true if ok, false otherwise. | |
67 | */ | |
68 | function validate_plugin_order($formData) | |
69 | { | |
70 | $orders = array(); | |
71 | foreach ($formData as $key => $value) { | |
72 | // No duplicate order allowed. | |
73 | if (in_array($value, $orders)) { | |
74 | return false; | |
75 | } | |
76 | ||
77 | if (startsWith($key, 'order')) { | |
78 | $orders[] = $value; | |
79 | } | |
80 | } | |
81 | ||
82 | return true; | |
83 | } | |
84 | ||
85 | /** | |
15170b51 | 86 | * Affect plugin parameters values from the ConfigManager into plugins array. |
59404d79 | 87 | * |
15170b51 A |
88 | * @param mixed $plugins Plugins array: |
89 | * $plugins[<plugin_name>]['parameters'][<param_name>] = [ | |
90 | * 'value' => <value>, | |
91 | * 'desc' => <description> | |
92 | * ] | |
59404d79 A |
93 | * @param mixed $conf Plugins configuration. |
94 | * | |
95 | * @return mixed Updated $plugins array. | |
96 | */ | |
97 | function load_plugin_parameter_values($plugins, $conf) | |
98 | { | |
99 | $out = $plugins; | |
100 | foreach ($plugins as $name => $plugin) { | |
101 | if (empty($plugin['parameters'])) { | |
102 | continue; | |
103 | } | |
104 | ||
105 | foreach ($plugin['parameters'] as $key => $param) { | |
106 | if (!empty($conf[$key])) { | |
15170b51 | 107 | $out[$name]['parameters'][$key]['value'] = $conf[$key]; |
59404d79 A |
108 | } |
109 | } | |
110 | } | |
111 | ||
112 | return $out; | |
113 | } |