]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/config/ConfigPlugin.php
047d2b03113320e1d3b0fba190376152cef5b75d
[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 into plugins array.
84 *
85 * @param mixed $plugins Plugins array ($plugins[<plugin_name>]['parameters']['param_name'] = <value>.
86 * @param mixed $conf Plugins configuration.
87 *
88 * @return mixed Updated $plugins array.
89 */
90 function load_plugin_parameter_values($plugins, $conf)
91 {
92 $out = $plugins;
93 foreach ($plugins as $name => $plugin) {
94 if (empty($plugin['parameters'])) {
95 continue;
96 }
97
98 foreach ($plugin['parameters'] as $key => $param) {
99 if (!empty($conf[$key])) {
100 $out[$name]['parameters'][$key] = $conf[$key];
101 }
102 }
103 }
104
105 return $out;
106 }
107
108 /**
109 * Exception used if an error occur while saving plugin configuration.
110 */
111 class PluginConfigOrderException extends Exception
112 {
113 /**
114 * Construct exception.
115 */
116 public function __construct()
117 {
118 $this->message = 'An error occurred while trying to save plugins loading order.';
119 }
120 }