2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Recent and Favorite table list handling
8 namespace PMA\libraries
;
10 use PMA\libraries\URL
;
13 * Handles the recently used and favorite tables.
15 * @TODO Change the release version in table pma_recent
16 * (#recent in documentation)
20 class RecentFavoriteTable
23 * Reference to session variable containing recently used or favorite tables.
31 * Defines type of action, Favorite or Recent table.
39 * RecentFavoriteTable instances.
44 private static $_instances = array();
47 * Creates a new instance of RecentFavoriteTable
49 * @param string $type the table type
53 private function __construct($type)
55 $this->_tableType
= $type;
56 $server_id = $GLOBALS['server'];
57 if (! isset($_SESSION['tmpval'][$this->_tableType
. '_tables'][$server_id])
59 $_SESSION['tmpval'][$this->_tableType
. '_tables'][$server_id]
60 = $this->_getPmaTable() ?
$this->getFromDb() : array();
63 =& $_SESSION['tmpval'][$this->_tableType
. '_tables'][$server_id];
67 * Returns class instance.
69 * @param string $type the table type
71 * @return RecentFavoriteTable
73 public static function getInstance($type)
75 if (! array_key_exists($type, self
::$_instances)) {
76 self
::$_instances[$type] = new RecentFavoriteTable($type);
78 return self
::$_instances[$type];
82 * Returns the recent/favorite tables array
86 public function getTables()
88 return $this->_tables
;
92 * Returns recently used tables or favorite from phpMyAdmin database.
96 public function getFromDb()
98 // Read from phpMyAdmin database, if recent tables is not in session
100 = " SELECT `tables` FROM " . $this->_getPmaTable() .
101 " WHERE `username` = '" . $GLOBALS['dbi']->escapeString($GLOBALS['cfg']['Server']['user']) . "'";
104 $result = PMA_queryAsControlUser($sql_query, false);
106 $row = $GLOBALS['dbi']->fetchArray($result);
107 if (isset($row[0])) {
108 $return = json_decode($row[0], true);
115 * Save recent/favorite tables into phpMyAdmin database.
117 * @return true|Message
119 public function saveToDb()
121 $username = $GLOBALS['cfg']['Server']['user'];
123 = " REPLACE INTO " . $this->_getPmaTable() . " (`username`, `tables`)" .
124 " VALUES ('" . $GLOBALS['dbi']->escapeString($username) . "', '"
125 . $GLOBALS['dbi']->escapeString(
126 json_encode($this->_tables
)
129 $success = $GLOBALS['dbi']->tryQuery($sql_query, $GLOBALS['controllink']);
133 switch ($this->_tableType
) {
135 $error_msg = __('Could not save recent table!');
139 $error_msg = __('Could not save favorite table!');
142 $message = Message
::error($error_msg);
143 $message->addMessage(
145 $GLOBALS['dbi']->getError($GLOBALS['controllink'])
155 * Trim recent.favorite table according to the
156 * NumRecentTables/NumFavoriteTables configuration.
158 * @return boolean True if trimming occurred
160 public function trim()
163 $GLOBALS['cfg']['Num' . ucfirst($this->_tableType
) . 'Tables'], 0
165 $trimming_occurred = count($this->_tables
) > $max;
166 while (count($this->_tables
) > $max) {
167 array_pop($this->_tables
);
169 return $trimming_occurred;
177 public function getHtmlList()
180 if (count($this->_tables
)) {
181 if ($this->_tableType
== 'recent') {
182 foreach ($this->_tables
as $table) {
183 $html .= '<li class="warp_link">';
184 $recent_params = array(
185 'db' => $table['db'],
186 'table' => $table['table']
188 $recent_url = 'tbl_recent_favorite.php'
189 . URL
::getCommon($recent_params);
190 $html .= '<a href="' . $recent_url . '">`'
191 . htmlspecialchars($table['db']) . '`.`'
192 . htmlspecialchars($table['table']) . '`</a>';
196 foreach ($this->_tables
as $table) {
197 $html .= '<li class="warp_link">';
199 $html .= '<a class="ajax favorite_table_anchor" ';
201 'db' => $table['db'],
202 'ajax_request' => true,
203 'favorite_table' => $table['table'],
204 'remove_favorite' => true
206 $fav_rm_url = 'db_structure.php'
207 . URL
::getCommon($fav_params);
208 $html .= 'href="' . $fav_rm_url
209 . '" title="' . __("Remove from Favorites")
210 . '" data-favtargetn="'
211 . md5($table['db'] . "." . $table['table'])
213 . Util
::getIcon('b_favorite.png')
217 'db' => $table['db'],
218 'table' => $table['table']
220 $table_url = 'tbl_recent_favorite.php'
221 . URL
::getCommon($fav_params);
222 $html .= '<a href="' . $table_url . '">`'
223 . htmlspecialchars($table['db']) . '`.`'
224 . htmlspecialchars($table['table']) . '`</a>';
229 $html .= '<li class="warp_link">'
230 . ($this->_tableType
== 'recent'
231 ?
__('There are no recent tables.')
232 :__('There are no favorite tables.'))
243 public function getHtml()
245 $html = '<div class="drop_list">';
246 if ($this->_tableType
== 'recent') {
247 $html .= '<span title="' . __('Recent tables')
248 . '" class="drop_button">'
249 . __('Recent') . '</span><ul id="pma_recent_list">';
251 $html .= '<span title="' . __('Favorite tables')
252 . '" class="drop_button">'
253 . __('Favorites') . '</span><ul id="pma_favorite_list">';
255 $html .= $this->getHtmlList();
256 $html .= '</ul></div>';
261 * Add recently used or favorite tables.
263 * @param string $db database name where the table is located
264 * @param string $table table name
266 * @return true|Message True if success, Message if not
268 public function add($db, $table)
270 // If table does not exist, do not add._getPmaTable()
271 if (! $GLOBALS['dbi']->getColumns($db, $table)) {
275 $table_arr = array();
276 $table_arr['db'] = $db;
277 $table_arr['table'] = $table;
279 // add only if this is new table
280 if (! isset($this->_tables
[0]) ||
$this->_tables
[0] != $table_arr) {
281 array_unshift($this->_tables
, $table_arr);
282 $this->_tables
= array_merge(array_unique($this->_tables
, SORT_REGULAR
));
284 if ($this->_getPmaTable()) {
285 return $this->saveToDb();
292 * Removes recent/favorite tables that don't exist.
294 * @param string $db database
295 * @param string $table table
297 * @return boolean|Message True if invalid and removed, False if not invalid,
298 * Message if error while removing
300 public function removeIfInvalid($db, $table)
302 foreach ($this->_tables
as $tbl) {
303 if ($tbl['db'] == $db && $tbl['table'] == $table) {
304 // TODO Figure out a better way to find the existence of a table
305 if (! $GLOBALS['dbi']->getColumns($tbl['db'], $tbl['table'])) {
306 return $this->remove($tbl['db'], $tbl['table']);
314 * Remove favorite tables.
316 * @param string $db database name where the table is located
317 * @param string $table table name
319 * @return true|Message True if success, Message if not
321 public function remove($db, $table)
323 $table_arr = array();
324 $table_arr['db'] = $db;
325 $table_arr['table'] = $table;
326 foreach ($this->_tables
as $key => $value) {
327 if ($value['db'] == $db && $value['table'] == $table) {
328 unset($this->_tables
[$key]);
331 if ($this->_getPmaTable()) {
332 return $this->saveToDb();
338 * Generate Html for sync Favorite tables anchor. (from localStorage to pmadb)
342 public function getHtmlSyncFavoriteTables()
345 $server_id = $GLOBALS['server'];
346 if ($server_id == 0) {
349 $cfgRelation = PMA_getRelationsParam();
350 // Not to show this once list is synchronized.
351 if ($cfgRelation['favoritework'] && ! isset($_SESSION['tmpval']['favorites_synced'][$server_id])) {
352 $params = array('ajax_request' => true, 'favorite_table' => true,
353 'sync_favorite_tables' => true);
354 $url = 'db_structure.php' . URL
::getCommon($params);
355 $retval = '<a class="hide" id="sync_favorite_tables"';
356 $retval .= ' href="' . $url . '"></a>';
362 * Generate Html to update recent tables.
364 * @return string html
366 public static function getHtmlUpdateRecentTables()
368 $params = array('ajax_request' => true, 'recent_table' => true);
369 $url = 'index.php' . URL
::getCommon($params);
370 $retval = '<a class="hide" id="update_recent_tables"';
371 $retval .= ' href="' . $url . '"></a>';
376 * Reutrn the name of the configuration storage table
378 * @return string pma table name
380 private function _getPmaTable()
382 $cfgRelation = PMA_getRelationsParam();
383 if (! empty($cfgRelation['db'])
384 && ! empty($cfgRelation[$this->_tableType
])
386 return Util
::backquote($cfgRelation['db']) . "."
387 . Util
::backquote($cfgRelation[$this->_tableType
]);