Select.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Code for displaying server selection
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. namespace PhpMyAdmin\Server;
  9. use PhpMyAdmin\Url;
  10. use PhpMyAdmin\Util;
  11. /**
  12. * PhpMyAdmin\Server\Select class
  13. *
  14. * @package PhpMyAdmin
  15. */
  16. class Select
  17. {
  18. /**
  19. * Renders the server selection in list or selectbox form, or option tags only
  20. *
  21. * @param boolean $not_only_options whether to include form tags or not
  22. * @param boolean $omit_fieldset whether to omit fieldset tag or not
  23. *
  24. * @return string
  25. */
  26. public static function render($not_only_options, $omit_fieldset)
  27. {
  28. $retval = '';
  29. // Show as list?
  30. if ($not_only_options) {
  31. $list = $GLOBALS['cfg']['DisplayServersList'];
  32. $not_only_options =! $list;
  33. } else {
  34. $list = false;
  35. }
  36. if ($not_only_options) {
  37. $retval .= '<form method="post" action="'
  38. . Util::getScriptNameForOption(
  39. $GLOBALS['cfg']['DefaultTabServer'], 'server'
  40. )
  41. . '" class="disableAjax">';
  42. if (! $omit_fieldset) {
  43. $retval .= '<fieldset>';
  44. }
  45. $retval .= Url::getHiddenFields(array());
  46. $retval .= '<label for="select_server">'
  47. . __('Current server:') . '</label> ';
  48. $retval .= '<select name="server" id="select_server" class="autosubmit">';
  49. $retval .= '<option value="">(' . __('Servers') . ') ...</option>' . "\n";
  50. } elseif ($list) {
  51. $retval .= __('Current server:') . '<br />';
  52. $retval .= '<ul id="list_server">';
  53. }
  54. foreach ($GLOBALS['cfg']['Servers'] as $key => $server) {
  55. if (empty($server['host'])) {
  56. continue;
  57. }
  58. if (!empty($GLOBALS['server']) && (int) $GLOBALS['server'] === (int) $key) {
  59. $selected = 1;
  60. } else {
  61. $selected = 0;
  62. }
  63. if (!empty($server['verbose'])) {
  64. $label = $server['verbose'];
  65. } else {
  66. $label = $server['host'];
  67. if (!empty($server['port'])) {
  68. $label .= ':' . $server['port'];
  69. }
  70. }
  71. if (! empty($server['only_db'])) {
  72. if (! is_array($server['only_db'])) {
  73. $label .= ' - ' . $server['only_db'];
  74. // try to avoid displaying a too wide selector
  75. } elseif (count($server['only_db']) < 4) {
  76. $label .= ' - ' . implode(', ', $server['only_db']);
  77. }
  78. }
  79. if (!empty($server['user']) && $server['auth_type'] == 'config') {
  80. $label .= ' (' . $server['user'] . ')';
  81. }
  82. if ($list) {
  83. $retval .= '<li>';
  84. if ($selected) {
  85. $retval .= '<strong>' . htmlspecialchars($label) . '</strong>';
  86. } else {
  87. $retval .= '<a class="disableAjax item" href="'
  88. . Util::getScriptNameForOption(
  89. $GLOBALS['cfg']['DefaultTabServer'], 'server'
  90. )
  91. . Url::getCommon(array('server' => $key))
  92. . '" >' . htmlspecialchars($label) . '</a>';
  93. }
  94. $retval .= '</li>';
  95. } else {
  96. $retval .= '<option value="' . $key . '" '
  97. . ($selected ? ' selected="selected"' : '') . '>'
  98. . htmlspecialchars($label) . '</option>' . "\n";
  99. }
  100. } // end while
  101. if ($not_only_options) {
  102. $retval .= '</select>';
  103. if (! $omit_fieldset) {
  104. $retval .= '</fieldset>';
  105. }
  106. $retval .= '</form>';
  107. } elseif ($list) {
  108. $retval .= '</ul>';
  109. }
  110. return $retval;
  111. }
  112. }