_pmaTable
= PMA_Util::backquote($GLOBALS['cfg']['Server']['pmadb']) . "."
. PMA_Util::backquote($GLOBALS['cfg']['Server']['recent']);
}
$server_id = $GLOBALS['server'];
if (! isset($_SESSION['tmp_user_values']['recent_tables'][$server_id])) {
$_SESSION['tmp_user_values']['recent_tables'][$server_id]
= isset($this->_pmaTable) ? $this->getFromDb() : array();
}
$this->tables =& $_SESSION['tmp_user_values']['recent_tables'][$server_id];
}
/**
* Returns class instance.
*
* @return PMA_RecentTable
*/
public static function getInstance()
{
if (is_null(self::$_instance)) {
self::$_instance = new PMA_RecentTable();
}
return self::$_instance;
}
/**
* Returns recently used tables from phpMyAdmin database.
*
* @return array
*/
public function getFromDb()
{
// Read from phpMyAdmin database, if recent tables is not in session
$sql_query
= " SELECT `tables` FROM " . $this->_pmaTable .
" WHERE `username` = '" . PMA_Util::sqlAddSlashes($GLOBALS['cfg']['Server']['user']) . "'";
$return = array();
$result = PMA_queryAsControlUser($sql_query, false);
if ($result) {
$row = PMA_DBI_fetch_array($result);
if (isset($row[0])) {
$return = json_decode($row[0], true);
}
}
return $return;
}
/**
* Save recent tables into phpMyAdmin database.
*
* @return true|PMA_Message
*/
public function saveToDb()
{
$username = $GLOBALS['cfg']['Server']['user'];
$sql_query
= " REPLACE INTO " . $this->_pmaTable . " (`username`, `tables`)" .
" VALUES ('" . $username . "', '"
. PMA_Util::sqlAddSlashes(
json_encode($this->tables)
) . "')";
$success = PMA_DBI_try_query($sql_query, $GLOBALS['controllink']);
if (! $success) {
$message = PMA_Message::error(__('Could not save recent table'));
$message->addMessage('
');
$message->addMessage(
PMA_Message::rawError(PMA_DBI_getError($GLOBALS['controllink']))
);
return $message;
}
return true;
}
/**
* Trim recent table according to the NumRecentTables configuration.
*
* @return boolean True if trimming occurred
*/
public function trim()
{
$max = max($GLOBALS['cfg']['NumRecentTables'], 0);
$trimming_occured = count($this->tables) > $max;
while (count($this->tables) > $max) {
array_pop($this->tables);
}
return $trimming_occured;
}
/**
* Return options for HTML select.
*
* @return string
*/
public function getHtmlSelectOption()
{
// trim and save, in case where the configuration is changed
if ($this->trim() && isset($this->_pmaTable)) {
$this->saveToDb();
}
$html = '';
if (count($this->tables)) {
foreach ($this->tables as $table) {
$html .= '';
}
} else {
$html .= '';
}
return $html;
}
/**
* Return HTML select.
*
* @return string
*/
public function getHtmlSelect()
{
$html = '';
return $html;
}
/**
* Add recently used tables.
*
* @param string $db database name where the table is located
* @param string $table table name
*
* @return true|PMA_Message True if success, PMA_Message if not
*/
public function add($db, $table)
{
$table_arr = array();
$table_arr['db'] = $db;
$table_arr['table'] = $table;
// add only if this is new table
if (! isset($this->tables[0]) || $this->tables[0] != $table_arr) {
array_unshift($this->tables, $table_arr);
$this->tables = array_merge(array_unique($this->tables, SORT_REGULAR));
$this->trim();
if (isset($this->_pmaTable)) {
return $this->saveToDb();
}
}
return true;
}
}
?>