*/
private static array $instances = [];
private Relation $relation;
/**
* Creates a new instance of RecentFavoriteTable
*
* @param string $type the table type
*/
private function __construct(public Template $template, string $type)
{
$this->relation = new Relation($GLOBALS['dbi']);
$this->tableType = $type;
$serverId = $GLOBALS['server'];
if (! isset($_SESSION['tmpval'][$this->tableType . 'Tables'][$serverId])) {
$_SESSION['tmpval'][$this->tableType . 'Tables'][$serverId] = $this->getPmaTable()
? $this->getFromDb()
: [];
}
$this->tables =& $_SESSION['tmpval'][$this->tableType . 'Tables'][$serverId];
}
/**
* 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(): array
{
return $this->tables;
}
/**
* Returns recently used tables or favorite from phpMyAdmin database.
*
* @return array
*/
public function getFromDb(): array
{
// Read from phpMyAdmin database, if recent tables is not in session
$sqlQuery = ' SELECT `tables` FROM ' . $this->getPmaTable()
. ' WHERE `username` = '
. $GLOBALS['dbi']->quoteString($GLOBALS['cfg']['Server']['user'], Connection::TYPE_CONTROL);
$result = $GLOBALS['dbi']->tryQueryAsControlUser($sqlQuery);
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(): bool|Message
{
$username = $GLOBALS['cfg']['Server']['user'];
$sqlQuery = ' REPLACE INTO ' . $this->getPmaTable() . ' (`username`, `tables`)'
. ' VALUES (' . $GLOBALS['dbi']->quoteString($username) . ', '
. $GLOBALS['dbi']->quoteString(json_encode($this->tables)) . ')';
$success = $GLOBALS['dbi']->tryQuery($sqlQuery, Connection::TYPE_CONTROL);
if (! $success) {
$errorMsg = '';
switch ($this->tableType) {
case 'recent':
$errorMsg = __('Could not save recent table!');
break;
case 'favorite':
$errorMsg = __('Could not save favorite table!');
break;
}
$message = Message::error($errorMsg);
$message->addMessage(
Message::rawError($GLOBALS['dbi']->getError(Connection::TYPE_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 = '