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