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