PageSettings.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * Page-related settings
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Config;
  7. use PhpMyAdmin\Config\Forms\Page\PageFormList;
  8. use PhpMyAdmin\Core;
  9. use PhpMyAdmin\Message;
  10. use PhpMyAdmin\Response;
  11. use PhpMyAdmin\UserPreferences;
  12. /**
  13. * Page-related settings
  14. */
  15. class PageSettings
  16. {
  17. /**
  18. * Contains id of the form element
  19. *
  20. * @var string
  21. */
  22. private $elemId = 'page_settings_modal';
  23. /**
  24. * Name of the group to show
  25. *
  26. * @var string
  27. */
  28. private $groupName = '';
  29. /**
  30. * Contains HTML of errors
  31. *
  32. * @var string
  33. */
  34. private $errorHTML = '';
  35. /**
  36. * Contains HTML of settings
  37. *
  38. * @var string
  39. */
  40. private $HTML = '';
  41. /** @var UserPreferences */
  42. private $userPreferences;
  43. /**
  44. * @param string $formGroupName The name of config form group to display
  45. * @param string $elemId Id of the div containing settings
  46. */
  47. public function __construct($formGroupName, $elemId = null)
  48. {
  49. $this->userPreferences = new UserPreferences();
  50. $formClass = PageFormList::get($formGroupName);
  51. if ($formClass === null) {
  52. return;
  53. }
  54. if (isset($_REQUEST['printview']) && $_REQUEST['printview'] == '1') {
  55. return;
  56. }
  57. if (! empty($elemId)) {
  58. $this->elemId = $elemId;
  59. }
  60. $this->groupName = $formGroupName;
  61. $cf = new ConfigFile($GLOBALS['PMA_Config']->baseSettings);
  62. $this->userPreferences->pageInit($cf);
  63. $formDisplay = new $formClass($cf);
  64. // Process form
  65. $error = null;
  66. if (isset($_POST['submit_save'])
  67. && $_POST['submit_save'] == $formGroupName
  68. ) {
  69. $this->processPageSettings($formDisplay, $cf, $error);
  70. }
  71. // Display forms
  72. $this->HTML = $this->getPageSettingsDisplay($formDisplay, $error);
  73. }
  74. /**
  75. * Process response to form
  76. *
  77. * @param FormDisplay $formDisplay Form
  78. * @param ConfigFile $cf Configuration file
  79. * @param Message|null $error Error message
  80. *
  81. * @return void
  82. */
  83. private function processPageSettings(&$formDisplay, &$cf, &$error)
  84. {
  85. if (! $formDisplay->process(false) || $formDisplay->hasErrors()) {
  86. return;
  87. }
  88. // save settings
  89. $result = $this->userPreferences->save($cf->getConfigArray());
  90. if ($result === true) {
  91. // reload page
  92. $response = Response::getInstance();
  93. Core::sendHeaderLocation(
  94. $response->getFooter()->getSelfUrl()
  95. );
  96. exit;
  97. }
  98. $error = $result;
  99. }
  100. /**
  101. * Store errors in _errorHTML
  102. *
  103. * @param FormDisplay $formDisplay Form
  104. * @param Message|null $error Error message
  105. *
  106. * @return void
  107. */
  108. private function storeError(&$formDisplay, &$error)
  109. {
  110. $retval = '';
  111. if ($error) {
  112. $retval .= $error->getDisplay();
  113. }
  114. if ($formDisplay->hasErrors()) {
  115. // form has errors
  116. $retval .= '<div class="alert alert-danger config-form" role="alert">'
  117. . '<b>' . __(
  118. 'Cannot save settings, submitted configuration form contains '
  119. . 'errors!'
  120. ) . '</b>'
  121. . $formDisplay->displayErrors()
  122. . '</div>';
  123. }
  124. $this->errorHTML = $retval;
  125. }
  126. /**
  127. * Display page-related settings
  128. *
  129. * @param FormDisplay $formDisplay Form
  130. * @param Message $error Error message
  131. *
  132. * @return string
  133. */
  134. private function getPageSettingsDisplay(&$formDisplay, &$error)
  135. {
  136. $response = Response::getInstance();
  137. $retval = '';
  138. $this->storeError($formDisplay, $error);
  139. $retval .= '<div id="' . $this->elemId . '">';
  140. $retval .= '<div class="page_settings">';
  141. $retval .= $formDisplay->getDisplay(
  142. true,
  143. true,
  144. false,
  145. $response->getFooter()->getSelfUrl(),
  146. [
  147. 'submit_save' => $this->groupName,
  148. ]
  149. );
  150. $retval .= '</div>';
  151. $retval .= '</div>';
  152. return $retval;
  153. }
  154. /**
  155. * Get HTML output
  156. *
  157. * @return string
  158. */
  159. public function getHTML()
  160. {
  161. return $this->HTML;
  162. }
  163. /**
  164. * Get error HTML output
  165. *
  166. * @return string
  167. */
  168. public function getErrorHTML()
  169. {
  170. return $this->errorHTML;
  171. }
  172. }