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