Console.php 3.6 KB

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