ConfigPlugin.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. use Shaarli\Config\Exception\PluginConfigOrderException;
  3. /**
  4. * Plugin configuration helper functions.
  5. *
  6. * Note: no access to configuration files here.
  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. $plugins = array();
  24. $newEnabledPlugins = array();
  25. foreach ($formData as $key => $data) {
  26. if (startsWith($key, 'order')) {
  27. continue;
  28. }
  29. // If there is no order, it means a disabled plugin has been enabled.
  30. if (isset($formData['order_' . $key])) {
  31. $plugins[(int) $formData['order_' . $key]] = $key;
  32. } else {
  33. $newEnabledPlugins[] = $key;
  34. }
  35. }
  36. // New enabled plugins will be added at the end of order.
  37. $plugins = array_merge($plugins, $newEnabledPlugins);
  38. // Sort plugins by order.
  39. if (!ksort($plugins)) {
  40. throw new PluginConfigOrderException();
  41. }
  42. $finalPlugins = array();
  43. // Make plugins order continuous.
  44. foreach ($plugins as $plugin) {
  45. $finalPlugins[] = $plugin;
  46. }
  47. return $finalPlugins;
  48. }
  49. /**
  50. * Validate plugin array submitted.
  51. * Will fail if there is duplicate orders value.
  52. *
  53. * @param array $formData Data from submitted form.
  54. *
  55. * @return bool true if ok, false otherwise.
  56. */
  57. function validate_plugin_order($formData)
  58. {
  59. $orders = array();
  60. foreach ($formData as $key => $value) {
  61. // No duplicate order allowed.
  62. if (in_array($value, $orders)) {
  63. return false;
  64. }
  65. if (startsWith($key, 'order')) {
  66. $orders[] = $value;
  67. }
  68. }
  69. return true;
  70. }
  71. /**
  72. * Affect plugin parameters values from the ConfigManager into plugins array.
  73. *
  74. * @param mixed $plugins Plugins array:
  75. * $plugins[<plugin_name>]['parameters'][<param_name>] = [
  76. * 'value' => <value>,
  77. * 'desc' => <description>
  78. * ]
  79. * @param mixed $conf Plugins configuration.
  80. *
  81. * @return mixed Updated $plugins array.
  82. */
  83. function load_plugin_parameter_values($plugins, $conf)
  84. {
  85. $out = $plugins;
  86. foreach ($plugins as $name => $plugin) {
  87. if (empty($plugin['parameters'])) {
  88. continue;
  89. }
  90. foreach ($plugin['parameters'] as $key => $param) {
  91. if (!empty($conf[$key])) {
  92. $out[$name]['parameters'][$key]['value'] = $conf[$key];
  93. }
  94. }
  95. }
  96. return $out;
  97. }