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;
38 $newEnabledPlugins[] = $key;
42 // New enabled plugins will be added at the end of order.
43 $plugins = array_merge($plugins, $newEnabledPlugins);
45 // Sort plugins by order.
46 if (!ksort($plugins)) {
47 throw new PluginConfigOrderException();
50 $finalPlugins = array();
51 // Make plugins order continuous.
52 foreach ($plugins as $plugin) {
53 $finalPlugins[] = $plugin;
60 * Validate plugin array submitted.
61 * Will fail if there is duplicate orders value.
63 * @param array $formData Data from submitted form.
65 * @return bool true if ok, false otherwise.
67 function validate_plugin_order($formData)
70 foreach ($formData as $key => $value) {
71 // No duplicate order allowed.
72 if (in_array($value, $orders)) {
76 if (startsWith($key, 'order')) {
85 * Affect plugin parameters values from the ConfigManager into plugins array.
87 * @param mixed $plugins Plugins array:
88 * $plugins[<plugin_name>]['parameters'][<param_name>] = [
90 * 'desc' => <description>
92 * @param mixed $conf Plugins configuration.
94 * @return mixed Updated $plugins array.
96 function load_plugin_parameter_values($plugins, $conf)
99 foreach ($plugins as $name => $plugin) {
100 if (empty($plugin['parameters'])) {
104 foreach ($plugin['parameters'] as $key => $param) {
105 if (!empty($conf[$key])) {
106 $out[$name]['parameters'][$key]['value'] = $conf[$key];