BaseFormList.php 2.7 KB

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