3 use Shaarli\Config\Exception\PluginConfigOrderException
;
6 * Plugin configuration helper functions.
8 * Note: no access to configuration files here.
12 * Process plugin administration form data and save it in an array.
14 * @param array $formData Data sent by the plugin admin form.
16 * @return array New list of enabled plugin, ordered.
18 * @throws PluginConfigOrderException Plugins can't be sorted because their order is invalid.
20 function save_plugin_config($formData)
22 // Make sure there are no duplicates in orders.
23 if (!validate_plugin_order($formData)) {
24 throw new PluginConfigOrderException();
28 $newEnabledPlugins = array();
29 foreach ($formData as $key => $data) {
30 if (startsWith($key, 'order')) {
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;
39 $newEnabledPlugins[] = $key;
43 // New enabled plugins will be added at the end of order.
44 $plugins = array_merge($plugins, $newEnabledPlugins);
46 // Sort plugins by order.
47 if (!ksort($plugins)) {
48 throw new PluginConfigOrderException();
51 $finalPlugins = array();
52 // Make plugins order continuous.
53 foreach ($plugins as $plugin) {
54 $finalPlugins[] = $plugin;
61 * Validate plugin array submitted.
62 * Will fail if there is duplicate orders value.
64 * @param array $formData Data from submitted form.
66 * @return bool true if ok, false otherwise.
68 function validate_plugin_order($formData)
71 foreach ($formData as $key => $value) {
72 // No duplicate order allowed.
73 if (in_array($value, $orders)) {
77 if (startsWith($key, 'order')) {
86 * Affect plugin parameters values from the ConfigManager into plugins array.
88 * @param mixed $plugins Plugins array:
89 * $plugins[<plugin_name>]['parameters'][<param_name>] = [
91 * 'desc' => <description>
93 * @param mixed $conf Plugins configuration.
95 * @return mixed Updated $plugins array.
97 function load_plugin_parameter_values($plugins, $conf)
100 foreach ($plugins as $name => $plugin) {
101 if (empty($plugin['parameters'])) {
105 foreach ($plugin['parameters'] as $key => $param) {
106 if (!empty($conf[$key])) {
107 $out[$name]['parameters'][$key]['value'] = $conf[$key];