BaseFormList.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * User preferences form
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Config\Forms;
  7. use PhpMyAdmin\Config\ConfigFile;
  8. use function array_merge;
  9. use function class_exists;
  10. use function in_array;
  11. class BaseFormList
  12. {
  13. /**
  14. * List of all forms
  15. *
  16. * @var string[]
  17. */
  18. protected static $all = [];
  19. /** @var string */
  20. protected static $ns = 'PhpMyAdmin\\Config\\Forms\\';
  21. /** @var array */
  22. private $forms;
  23. /**
  24. * @return string[]
  25. */
  26. public static function getAll()
  27. {
  28. return static::$all;
  29. }
  30. /**
  31. * @param string $name Name
  32. */
  33. public static function isValid($name): bool
  34. {
  35. return in_array($name, static::$all);
  36. }
  37. /**
  38. * @param string $name Name
  39. *
  40. * @return string|null
  41. * @psalm-return class-string<BaseForm>|null
  42. */
  43. public static function get($name)
  44. {
  45. if (static::isValid($name)) {
  46. /** @var class-string<BaseForm> $class */
  47. $class = static::$ns . $name . 'Form';
  48. return $class;
  49. }
  50. return null;
  51. }
  52. /**
  53. * @param ConfigFile $cf Config file instance
  54. */
  55. final public function __construct(ConfigFile $cf)
  56. {
  57. $this->forms = [];
  58. foreach (static::$all as $form) {
  59. $class = (string) static::get($form);
  60. if (! class_exists($class)) {
  61. continue;
  62. }
  63. $this->forms[] = new $class($cf);
  64. }
  65. }
  66. /**
  67. * Processes forms, returns true on successful save
  68. *
  69. * @param bool $allowPartialSave allows for partial form saving
  70. * on failed validation
  71. * @param bool $checkFormSubmit whether check for $_POST['submit_save']
  72. */
  73. public function process($allowPartialSave = true, $checkFormSubmit = true): bool
  74. {
  75. $ret = true;
  76. foreach ($this->forms as $form) {
  77. $ret = $ret && $form->process($allowPartialSave, $checkFormSubmit);
  78. }
  79. return $ret;
  80. }
  81. /**
  82. * Displays errors
  83. *
  84. * @return string HTML for errors
  85. */
  86. public function displayErrors()
  87. {
  88. $ret = '';
  89. foreach ($this->forms as $form) {
  90. $ret .= $form->displayErrors();
  91. }
  92. return $ret;
  93. }
  94. /**
  95. * Reverts erroneous fields to their default values
  96. */
  97. public function fixErrors(): void
  98. {
  99. foreach ($this->forms as $form) {
  100. $form->fixErrors();
  101. }
  102. }
  103. /**
  104. * Tells whether form validation failed
  105. */
  106. public function hasErrors(): bool
  107. {
  108. $ret = false;
  109. foreach ($this->forms as $form) {
  110. $ret = $ret || $form->hasErrors();
  111. }
  112. return $ret;
  113. }
  114. /**
  115. * Returns list of fields used in the form.
  116. *
  117. * @return string[]
  118. */
  119. public static function getFields()
  120. {
  121. $names = [];
  122. foreach (static::$all as $form) {
  123. $class = (string) static::get($form);
  124. if (! class_exists($class)) {
  125. continue;
  126. }
  127. $names = array_merge($names, $class::getFields());
  128. }
  129. return $names;
  130. }
  131. }