]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/config/ConfigPlugin.php
lint: apply phpcbf to application/
[github/shaarli/Shaarli.git] / application / config / ConfigPlugin.php
1 <?php
2
3 use Shaarli\Config\Exception\PluginConfigOrderException;
4
5 /**
6 * Plugin configuration helper functions.
7 *
8 * Note: no access to configuration files here.
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 } else {
38 $newEnabledPlugins[] = $key;
39 }
40 }
41
42 // New enabled plugins will be added at the end of order.
43 $plugins = array_merge($plugins, $newEnabledPlugins);
44
45 // Sort plugins by order.
46 if (!ksort($plugins)) {
47 throw new PluginConfigOrderException();
48 }
49
50 $finalPlugins = array();
51 // Make plugins order continuous.
52 foreach ($plugins as $plugin) {
53 $finalPlugins[] = $plugin;
54 }
55
56 return $finalPlugins;
57 }
58
59 /**
60 * Validate plugin array submitted.
61 * Will fail if there is duplicate orders value.
62 *
63 * @param array $formData Data from submitted form.
64 *
65 * @return bool true if ok, false otherwise.
66 */
67 function validate_plugin_order($formData)
68 {
69 $orders = array();
70 foreach ($formData as $key => $value) {
71 // No duplicate order allowed.
72 if (in_array($value, $orders)) {
73 return false;
74 }
75
76 if (startsWith($key, 'order')) {
77 $orders[] = $value;
78 }
79 }
80
81 return true;
82 }
83
84 /**
85 * Affect plugin parameters values from the ConfigManager into plugins array.
86 *
87 * @param mixed $plugins Plugins array:
88 * $plugins[<plugin_name>]['parameters'][<param_name>] = [
89 * 'value' => <value>,
90 * 'desc' => <description>
91 * ]
92 * @param mixed $conf Plugins configuration.
93 *
94 * @return mixed Updated $plugins array.
95 */
96 function load_plugin_parameter_values($plugins, $conf)
97 {
98 $out = $plugins;
99 foreach ($plugins as $name => $plugin) {
100 if (empty($plugin['parameters'])) {
101 continue;
102 }
103
104 foreach ($plugin['parameters'] as $key => $param) {
105 if (!empty($conf[$key])) {
106 $out[$name]['parameters'][$key]['value'] = $conf[$key];
107 }
108 }
109 }
110
111 return $out;
112 }