2 /* vim: set expandtab sw=4 ts=4 sts=4: */
8 require_once './libraries/Message.class.php';
11 * Handles the recently used tables.
13 * @TODO Change the release version in table pma_recent (#recent in Documentation.html)
20 * Defines the internal PMA table which contains recent tables.
28 * Reference to session variable containing recently used tables.
36 * PMA_RecentTable instance.
38 * @var PMA_RecentTable
40 private static $_instance;
42 public function __construct()
44 if (strlen($GLOBALS['cfg']['Server']['pmadb']) &&
45 strlen($GLOBALS['cfg']['Server']['recent'])) {
46 $this->pma_table
= PMA_backquote($GLOBALS['cfg']['Server']['pmadb']) .".".
47 PMA_backquote($GLOBALS['cfg']['Server']['recent']);
49 $server_id = $GLOBALS['server'];
50 if (! isset($_SESSION['tmp_user_values']['recent_tables'][$server_id])) {
51 $_SESSION['tmp_user_values']['recent_tables'][$server_id] =
52 isset($this->pma_table
) ?
$this->getFromDb() : array();
54 $this->tables
=& $_SESSION['tmp_user_values']['recent_tables'][$server_id];
58 * Returns class instance.
60 * @return PMA_RecentTable
62 public static function getInstance()
64 if (is_null(self
::$_instance)) {
65 self
::$_instance = new PMA_RecentTable();
67 return self
::$_instance;
71 * Returns recently used tables from phpMyAdmin database.
76 public function getFromDb()
78 // Read from phpMyAdmin database, if recent tables is not in session
80 " SELECT `tables` FROM " . $this->pma_table
.
81 " WHERE `username` = '" . $GLOBALS['cfg']['Server']['user'] . "'";
83 $row = PMA_DBI_fetch_array(PMA_query_as_controluser($sql_query));
85 return json_decode($row[0], true);
92 * Save recent tables into phpMyAdmin database.
95 * @return true|PMA_Message
97 public function saveToDb()
99 $username = $GLOBALS['cfg']['Server']['user'];
101 " REPLACE INTO " . $this->pma_table
. " (`username`, `tables`)" .
102 " VALUES ('" . $username . "', '" . PMA_sqlAddSlashes(json_encode($this->tables
)) . "')";
104 $success = PMA_DBI_try_query($sql_query, $GLOBALS['controllink']);
107 $message = PMA_Message
::error(__('Could not save recent table'));
108 $message->addMessage('<br /><br />');
109 $message->addMessage(PMA_Message
::rawError(PMA_DBI_getError($GLOBALS['controllink'])));
116 * Trim recent table according to the LeftRecentTable configuration.
118 * @return boolean True if trimming occurred
120 public function trim()
122 $max = max($GLOBALS['cfg']['LeftRecentTable'], 0);
123 $trimming_occured = count($this->tables
) > $max;
124 while (count($this->tables
) > $max) {
125 array_pop($this->tables
);
127 return $trimming_occured;
131 * Return options for HTML select.
135 public function getHtmlSelectOption()
137 // trim and save, in case where the configuration is changed
138 if ($this->trim() && isset($this->pma_table
)) {
142 $html = '<option value="">(' . __('Recent tables') . ') ...</option>';
143 if (count($this->tables
)) {
144 foreach ($this->tables
as $table) {
145 $html .= '<option value="' . htmlspecialchars(json_encode($table)) . '">' .
146 htmlspecialchars('`' . $table['db'] . '`.`' . $table['table'] . '`') . '</option>';
149 $html .= '<option value="">' . __('There are no recent tables') . '</option>';
155 * Return HTML select.
159 public function getHtmlSelect()
161 $html = '<input type="hidden" name="goto" id="LeftDefaultTabTable" value="' .
162 htmlspecialchars($GLOBALS['cfg']['LeftDefaultTabTable']) . '" />';
163 $html .= '<select name="selected_recent_table" id="recentTable">';
164 $html .= $this->getHtmlSelectOption();
165 $html .= '</select>';
171 * Add recently used tables.
173 * @param string $db Database name where the table is located
174 * @param string $table Table name
176 * @return true|PMA_Message True if success, PMA_Message if not
178 public function add($db, $table)
180 $table_arr = array();
181 $table_arr['db'] = $db;
182 $table_arr['table'] = $table;
184 // add only if this is new table
185 if (! isset($this->tables
[0]) ||
$this->tables
[0] != $table_arr) {
186 array_unshift($this->tables
, $table_arr);
187 $this->tables
= array_merge(array_unique($this->tables
, SORT_REGULAR
));
189 if (isset($this->pma_table
)) {
190 return $this->saveToDb();