PageSettings.php 5.5 KB

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