*/
private static $instances = [];
/** @var Relation */
private $relation;
/**
* Creates a new instance of RecentFavoriteTable
*
* @param Template $template Template object
* @param string $type the table type
*/
private function __construct(Template $template, string $type)
{
$this->template = $template;
global $dbi;
$this->relation = new Relation($dbi);
$this->tableType = $type;
$server_id = $GLOBALS['server'];
if (! isset($_SESSION['tmpval'][$this->tableType . 'Tables'][$server_id])) {
$_SESSION['tmpval'][$this->tableType . 'Tables'][$server_id] = $this->getPmaTable()
? $this->getFromDb()
: [];
}
$this->tables =& $_SESSION['tmpval'][$this->tableType . 'Tables'][$server_id];
}
/**
* Returns class instance.
*
* @param string $type the table type
* @psalm-param 'favorite'|'recent' $type
*/
public static function getInstance(string $type): RecentFavoriteTable
{
if (! array_key_exists($type, self::$instances)) {
$template = new Template();
self::$instances[$type] = new RecentFavoriteTable($template, $type);
}
return self::$instances[$type];
}
/**
* Returns the recent/favorite tables array
*
* @return array
*/
public function getTables()
{
return $this->tables;
}
/**
* Returns recently used tables or favorite from phpMyAdmin database.
*
* @return array
*/
public function getFromDb(): array
{
global $dbi;
// Read from phpMyAdmin database, if recent tables is not in session
$sql_query = ' SELECT `tables` FROM ' . $this->getPmaTable() .
" WHERE `username` = '" . $dbi->escapeString($GLOBALS['cfg']['Server']['user']) . "'";
$result = $dbi->tryQueryAsControlUser($sql_query);
if ($result) {
$value = $result->fetchValue();
if (is_string($value)) {
return json_decode($value, true);
}
}
return [];
}
/**
* Save recent/favorite tables into phpMyAdmin database.
*
* @return true|Message
*/
public function saveToDb()
{
global $dbi;
$username = $GLOBALS['cfg']['Server']['user'];
$sql_query = ' REPLACE INTO ' . $this->getPmaTable() . ' (`username`, `tables`)' .
" VALUES ('" . $dbi->escapeString($username) . "', '"
. $dbi->escapeString(
json_encode($this->tables)
) . "')";
$success = $dbi->tryQuery($sql_query, DatabaseInterface::CONNECT_CONTROL);
if (! $success) {
$error_msg = '';
switch ($this->tableType) {
case 'recent':
$error_msg = __('Could not save recent table!');
break;
case 'favorite':
$error_msg = __('Could not save favorite table!');
break;
}
$message = Message::error($error_msg);
$message->addMessage(
Message::rawError($dbi->getError(DatabaseInterface::CONNECT_CONTROL)),
'
'
);
return $message;
}
return true;
}
/**
* Trim recent.favorite table according to the
* NumRecentTables/NumFavoriteTables configuration.
*/
public function trim(): bool
{
$max = max(
$GLOBALS['cfg']['Num' . ucfirst($this->tableType) . 'Tables'],
0
);
$trimmingOccurred = count($this->tables) > $max;
while (count($this->tables) > $max) {
array_pop($this->tables);
}
return $trimmingOccurred;
}
/**
* Return HTML ul.
*/
public function getHtmlList(): string
{
if (count($this->tables)) {
if ($this->tableType === 'recent') {
$tables = [];
foreach ($this->tables as $table) {
$tables[] = [
'db' => $table['db'],
'table' => $table['table'],
];
}
return $this->template->render('recent_favorite_table_recent', ['tables' => $tables]);
}
$tables = [];
foreach ($this->tables as $table) {
$removeParameters = [
'db' => $table['db'],
'ajax_request' => true,
'favorite_table' => $table['table'],
'remove_favorite' => true,
];
$tableParameters = [
'db' => $table['db'],
'table' => $table['table'],
'md5' => md5($table['db'] . '.' . $table['table']),
];
$tables[] = [
'remove_parameters' => $removeParameters,
'table_parameters' => $tableParameters,
];
}
return $this->template->render('recent_favorite_table_favorite', ['tables' => $tables]);
}
return $this->template->render('recent_favorite_table_no_tables', [
'is_recent' => $this->tableType === 'recent',
]);
}
public function getHtml(): string
{
$html = '