3 use Shaarli\Config\Exception\PluginConfigOrderException
;
4 use Shaarli\Plugin\PluginManager
;
7 * Plugin configuration helper functions.
9 * Note: no access to configuration files here.
13 * Process plugin administration form data and save it in an array.
15 * @param array $formData Data sent by the plugin admin form.
17 * @return array New list of enabled plugin, ordered.
19 * @throws PluginConfigOrderException Plugins can't be sorted because their order is invalid.
21 function save_plugin_config($formData)
23 // We can only save existing plugins
24 $directories = str_replace(
25 PluginManager
::$PLUGINS_PATH . '/',
27 glob(PluginManager
::$PLUGINS_PATH . '/*')
29 $formData = array_filter(
31 function ($value, string $key) use ($directories) {
32 return startsWith($key, 'order') || in_array($key, $directories);
37 // Make sure there are no duplicates in orders.
38 if (!validate_plugin_order($formData)) {
39 throw new PluginConfigOrderException();
43 $newEnabledPlugins = array();
44 foreach ($formData as $key => $data) {
45 if (startsWith($key, 'order')) {
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;
53 $newEnabledPlugins[] = $key;
57 // New enabled plugins will be added at the end of order.
58 $plugins = array_merge($plugins, $newEnabledPlugins);
60 // Sort plugins by order.
61 if (!ksort($plugins)) {
62 throw new PluginConfigOrderException();
65 $finalPlugins = array();
66 // Make plugins order continuous.
67 foreach ($plugins as $plugin) {
68 $finalPlugins[] = $plugin;
75 * Validate plugin array submitted.
76 * Will fail if there is duplicate orders value.
78 * @param array $formData Data from submitted form.
80 * @return bool true if ok, false otherwise.
82 function validate_plugin_order($formData)
85 foreach ($formData as $key => $value) {
86 // No duplicate order allowed.
87 if (in_array($value, $orders, true)) {
91 if (startsWith($key, 'order')) {
100 * Affect plugin parameters values from the ConfigManager into plugins array.
102 * @param mixed $plugins Plugins array:
103 * $plugins[<plugin_name>]['parameters'][<param_name>] = [
104 * 'value' => <value>,
105 * 'desc' => <description>
107 * @param mixed $conf Plugins configuration.
109 * @return mixed Updated $plugins array.
111 function load_plugin_parameter_values($plugins, $conf)
114 foreach ($plugins as $name => $plugin) {
115 if (empty($plugin['parameters'])) {
119 foreach ($plugin['parameters'] as $key => $param) {
120 if (!empty($conf[$key])) {
121 $out[$name]['parameters'][$key]['value'] = $conf[$key];