ConfigPlugin.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. }
  33. else {
  34. $newEnabledPlugins[] = $key;
  35. }
  36. }
  37. // New enabled plugins will be added at the end of order.
  38. $plugins = array_merge($plugins, $newEnabledPlugins);
  39. // Sort plugins by order.
  40. if (!ksort($plugins)) {
  41. throw new PluginConfigOrderException();
  42. }
  43. $finalPlugins = array();
  44. // Make plugins order continuous.
  45. foreach ($plugins as $plugin) {
  46. $finalPlugins[] = $plugin;
  47. }
  48. return $finalPlugins;
  49. }
  50. /**
  51. * Validate plugin array submitted.
  52. * Will fail if there is duplicate orders value.
  53. *
  54. * @param array $formData Data from submitted form.
  55. *
  56. * @return bool true if ok, false otherwise.
  57. */
  58. function validate_plugin_order($formData)
  59. {
  60. $orders = array();
  61. foreach ($formData as $key => $value) {
  62. // No duplicate order allowed.
  63. if (in_array($value, $orders)) {
  64. return false;
  65. }
  66. if (startsWith($key, 'order')) {
  67. $orders[] = $value;
  68. }
  69. }
  70. return true;
  71. }
  72. /**
  73. * Affect plugin parameters values from the ConfigManager into plugins array.
  74. *
  75. * @param mixed $plugins Plugins array:
  76. * $plugins[<plugin_name>]['parameters'][<param_name>] = [
  77. * 'value' => <value>,
  78. * 'desc' => <description>
  79. * ]
  80. * @param mixed $conf Plugins configuration.
  81. *
  82. * @return mixed Updated $plugins array.
  83. */
  84. function load_plugin_parameter_values($plugins, $conf)
  85. {
  86. $out = $plugins;
  87. foreach ($plugins as $name => $plugin) {
  88. if (empty($plugin['parameters'])) {
  89. continue;
  90. }
  91. foreach ($plugin['parameters'] as $key => $param) {
  92. if (!empty($conf[$key])) {
  93. $out[$name]['parameters'][$key]['value'] = $conf[$key];
  94. }
  95. }
  96. }
  97. return $out;
  98. }