2 /* vim: set expandtab sw=4 ts=4 sts=4: */
8 if (! defined('PHPMYADMIN')) {
12 require_once './libraries/Message.class.php';
15 * Handles the recently used tables.
17 * @TODO Change the release version in table pma_recent
18 * (#recent in documentation)
25 * Defines the internal PMA table which contains recent tables.
33 * Reference to session variable containing recently used tables.
41 * PMA_RecentTable instance.
43 * @var PMA_RecentTable
45 private static $_instance;
47 public function __construct()
49 if (strlen($GLOBALS['cfg']['Server']['pmadb'])
50 && strlen($GLOBALS['cfg']['Server']['recent'])
53 = PMA_Util
::backquote($GLOBALS['cfg']['Server']['pmadb']) . "."
54 . PMA_Util
::backquote($GLOBALS['cfg']['Server']['recent']);
56 $server_id = $GLOBALS['server'];
57 if (! isset($_SESSION['tmp_user_values']['recent_tables'][$server_id])) {
58 $_SESSION['tmp_user_values']['recent_tables'][$server_id]
59 = isset($this->_pmaTable
) ?
$this->getFromDb() : array();
61 $this->tables
=& $_SESSION['tmp_user_values']['recent_tables'][$server_id];
65 * Returns class instance.
67 * @return PMA_RecentTable
69 public static function getInstance()
71 if (is_null(self
::$_instance)) {
72 self
::$_instance = new PMA_RecentTable();
74 return self
::$_instance;
78 * Returns recently used tables from phpMyAdmin database.
82 public function getFromDb()
84 // Read from phpMyAdmin database, if recent tables is not in session
86 = " SELECT `tables` FROM " . $this->_pmaTable
.
87 " WHERE `username` = '" . $GLOBALS['cfg']['Server']['user'] . "'";
90 $result = PMA_queryAsControlUser($sql_query, false);
92 $row = PMA_DBI_fetch_array($result);
94 $return = json_decode($row[0], true);
101 * Save recent tables into phpMyAdmin database.
103 * @return true|PMA_Message
105 public function saveToDb()
107 $username = $GLOBALS['cfg']['Server']['user'];
109 = " REPLACE INTO " . $this->_pmaTable
. " (`username`, `tables`)" .
110 " VALUES ('" . $username . "', '"
111 . PMA_Util
::sqlAddSlashes(
112 json_encode($this->tables
)
115 $success = PMA_DBI_try_query($sql_query, $GLOBALS['controllink']);
118 $message = PMA_Message
::error(__('Could not save recent table'));
119 $message->addMessage('<br /><br />');
120 $message->addMessage(
121 PMA_Message
::rawError(PMA_DBI_getError($GLOBALS['controllink']))
129 * Trim recent table according to the NumRecentTables configuration.
131 * @return boolean True if trimming occurred
133 public function trim()
135 $max = max($GLOBALS['cfg']['NumRecentTables'], 0);
136 $trimming_occured = count($this->tables
) > $max;
137 while (count($this->tables
) > $max) {
138 array_pop($this->tables
);
140 return $trimming_occured;
144 * Return options for HTML select.
148 public function getHtmlSelectOption()
150 // trim and save, in case where the configuration is changed
151 if ($this->trim() && isset($this->_pmaTable
)) {
155 $html = '<option value="">(' . __('Recent tables') . ') ...</option>';
156 if (count($this->tables
)) {
157 foreach ($this->tables
as $table) {
158 $html .= '<option value="'
159 . htmlspecialchars(json_encode($table)) . '">'
161 '`' . $table['db'] . '`.`' . $table['table'] . '`'
166 $html .= '<option value="">'
167 . __('There are no recent tables')
174 * Return HTML select.
178 public function getHtmlSelect()
180 $html = '<select name="selected_recent_table" id="recentTable">';
181 $html .= $this->getHtmlSelectOption();
182 $html .= '</select>';
188 * Add recently used tables.
190 * @param string $db database name where the table is located
191 * @param string $table table name
193 * @return true|PMA_Message True if success, PMA_Message if not
195 public function add($db, $table)
197 $table_arr = array();
198 $table_arr['db'] = $db;
199 $table_arr['table'] = $table;
201 // add only if this is new table
202 if (! isset($this->tables
[0]) ||
$this->tables
[0] != $table_arr) {
203 array_unshift($this->tables
, $table_arr);
204 $this->tables
= array_merge(array_unique($this->tables
, SORT_REGULAR
));
206 if (isset($this->_pmaTable
)) {
207 return $this->saveToDb();