Console.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Used to render the console of PMA's pages
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. declare(strict_types=1);
  9. namespace PhpMyAdmin;
  10. use PhpMyAdmin\Bookmark;
  11. use PhpMyAdmin\Relation;
  12. use PhpMyAdmin\Template;
  13. use PhpMyAdmin\Util;
  14. /**
  15. * Class used to output the console
  16. *
  17. * @package PhpMyAdmin
  18. */
  19. class Console
  20. {
  21. /**
  22. * Whether to display anything
  23. *
  24. * @access private
  25. * @var bool
  26. */
  27. private $_isEnabled;
  28. /**
  29. * Whether we are servicing an ajax request.
  30. *
  31. * @access private
  32. * @var bool
  33. */
  34. private $_isAjax;
  35. /**
  36. * @var Relation
  37. */
  38. private $relation;
  39. /**
  40. * @var Template
  41. */
  42. public $template;
  43. /**
  44. * Creates a new class instance
  45. */
  46. public function __construct()
  47. {
  48. $this->_isEnabled = true;
  49. $this->relation = new Relation($GLOBALS['dbi']);
  50. $this->template = new Template();
  51. }
  52. /**
  53. * Set the ajax flag to indicate whether
  54. * we are servicing an ajax request
  55. *
  56. * @param bool $isAjax Whether we are servicing an ajax request
  57. *
  58. * @return void
  59. */
  60. public function setAjax(bool $isAjax): void
  61. {
  62. $this->_isAjax = $isAjax;
  63. }
  64. /**
  65. * Disables the rendering of the footer
  66. *
  67. * @return void
  68. */
  69. public function disable(): void
  70. {
  71. $this->_isEnabled = false;
  72. }
  73. /**
  74. * Renders the bookmark content
  75. *
  76. * @access public
  77. * @return string
  78. */
  79. public static function getBookmarkContent(): string
  80. {
  81. $template = new Template();
  82. $cfgBookmark = Bookmark::getParams($GLOBALS['cfg']['Server']['user']);
  83. if ($cfgBookmark) {
  84. $bookmarks = Bookmark::getList(
  85. $GLOBALS['dbi'],
  86. $GLOBALS['cfg']['Server']['user']
  87. );
  88. $count_bookmarks = count($bookmarks);
  89. if ($count_bookmarks > 0) {
  90. $welcomeMessage = sprintf(
  91. _ngettext(
  92. 'Showing %1$d bookmark (both private and shared)',
  93. 'Showing %1$d bookmarks (both private and shared)',
  94. $count_bookmarks
  95. ),
  96. $count_bookmarks
  97. );
  98. } else {
  99. $welcomeMessage = __('No bookmarks');
  100. }
  101. unset($count_bookmarks, $private_message, $shared_message);
  102. return $template->render('console/bookmark_content', [
  103. 'welcome_message' => $welcomeMessage,
  104. 'bookmarks' => $bookmarks,
  105. ]);
  106. }
  107. return '';
  108. }
  109. /**
  110. * Returns the list of JS scripts required by console
  111. *
  112. * @return array list of scripts
  113. */
  114. public function getScripts(): array
  115. {
  116. return ['console.js'];
  117. }
  118. /**
  119. * Renders the console
  120. *
  121. * @access public
  122. * @return string
  123. */
  124. public function getDisplay(): string
  125. {
  126. if ((! $this->_isAjax) && $this->_isEnabled) {
  127. $cfgBookmark = Bookmark::getParams(
  128. $GLOBALS['cfg']['Server']['user']
  129. );
  130. $image = Util::getImage('console', __('SQL Query Console'));
  131. $_sql_history = $this->relation->getHistory(
  132. $GLOBALS['cfg']['Server']['user']
  133. );
  134. $bookmarkContent = static::getBookmarkContent();
  135. return $this->template->render('console/display', [
  136. 'cfg_bookmark' => $cfgBookmark,
  137. 'image' => $image,
  138. 'sql_history' => $_sql_history,
  139. 'bookmark_content' => $bookmarkContent,
  140. ]);
  141. }
  142. return '';
  143. }
  144. }