2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Recent and Favorite table list handling
9 if (! defined('PHPMYADMIN')) {
13 require_once './libraries/Message.class.php';
16 * Handles the recently used and favorite tables.
18 * @TODO Change the release version in table pma_recent
19 * (#recent in documentation)
23 class PMA_RecentFavoriteTable
26 * Reference to session variable containing recently used or favorite tables.
34 * Defines type of action, Favorite or Recent table.
42 * PMA_RecentFavoriteTable instances.
47 private static $_instances = array();
50 * Creates a new instance of PMA_RecentFavoriteTable
52 * @param string $type the table type
56 private function __construct($type)
58 $this->_tableType
= $type;
59 $server_id = $GLOBALS['server'];
60 if (! isset($_SESSION['tmpval'][$this->_tableType
. '_tables'][$server_id])
62 $_SESSION['tmpval'][$this->_tableType
. '_tables'][$server_id]
63 = $this->_getPmaTable() ?
$this->getFromDb() : array();
66 =& $_SESSION['tmpval'][$this->_tableType
. '_tables'][$server_id];
70 * Returns class instance.
72 * @param string $type the table type
74 * @return PMA_RecentFavoriteTable
76 public static function getInstance($type)
78 if (! array_key_exists($type, self
::$_instances)) {
79 self
::$_instances[$type] = new PMA_RecentFavoriteTable($type);
81 return self
::$_instances[$type];
85 * Returns the recent/favorite tables array
89 public function getTables()
91 return $this->_tables
;
95 * Returns recently used tables or favorite from phpMyAdmin database.
99 public function getFromDb()
101 // Read from phpMyAdmin database, if recent tables is not in session
103 = " SELECT `tables` FROM " . $this->_getPmaTable() .
104 " WHERE `username` = '" . $GLOBALS['cfg']['Server']['user'] . "'";
107 $result = PMA_queryAsControlUser($sql_query, false);
109 $row = $GLOBALS['dbi']->fetchArray($result);
110 if (isset($row[0])) {
111 $return = json_decode($row[0], true);
118 * Save recent/favorite tables into phpMyAdmin database.
120 * @return true|PMA_Message
122 public function saveToDb()
124 $username = $GLOBALS['cfg']['Server']['user'];
126 = " REPLACE INTO " . $this->_getPmaTable() . " (`username`, `tables`)" .
127 " VALUES ('" . $username . "', '"
128 . PMA_Util
::sqlAddSlashes(
129 json_encode($this->_tables
)
132 $success = $GLOBALS['dbi']->tryQuery($sql_query, $GLOBALS['controllink']);
136 switch ($this->_tableType
) {
138 $error_msg = __('Could not save recent table!');
142 $error_msg = __('Could not save favorite table!');
145 $message = PMA_Message
::error($error_msg);
146 $message->addMessage('<br /><br />');
147 $message->addMessage(
148 PMA_Message
::rawError(
149 $GLOBALS['dbi']->getError($GLOBALS['controllink'])
158 * Trim recent.favorite table according to the
159 * NumRecentTables/NumFavoriteTables configuration.
161 * @return boolean True if trimming occurred
163 public function trim()
166 $GLOBALS['cfg']['Num' . ucfirst($this->_tableType
) . 'Tables'], 0
168 $trimming_occurred = count($this->_tables
) > $max;
169 while (count($this->_tables
) > $max) {
170 array_pop($this->_tables
);
172 return $trimming_occurred;
180 public function getHtmlList()
183 if (count($this->_tables
)) {
184 if ($this->_tableType
== 'recent') {
185 foreach ($this->_tables
as $table) {
186 $html .= '<li class="warp_link">';
187 $recent_params = array(
188 'db' => $table['db'],
189 'table' => $table['table']
191 $recent_url = 'tbl_recent_favorite.php'
192 . PMA_URL_getCommon($recent_params);
193 $html .= '<a href="' . $recent_url . '">`'
194 . htmlspecialchars($table['db']) . '`.`'
195 . htmlspecialchars($table['table']) . '`</a>';
199 foreach ($this->_tables
as $table) {
200 $html .= '<li class="warp_link">';
202 $html .= '<a class="ajax favorite_table_anchor" ';
204 'db' => $table['db'],
205 'ajax_request' => true,
206 'favorite_table' => $table['table'],
207 'remove_favorite' => true
209 $fav_rm_url = 'db_structure.php'
210 . PMA_URL_getCommon($fav_params);
211 $html .= 'href="' . $fav_rm_url
212 . '" title="' . __("Remove from Favorites")
213 . '" data-favtargetn="'
214 . md5($table['db'] . "." . $table['table'])
216 . PMA_Util
::getIcon('b_favorite.png')
220 'db' => $table['db'],
221 'table' => $table['table']
223 $table_url = 'tbl_recent_favorite.php'
224 . PMA_URL_getCommon($fav_params);
225 $html .= '<a href="' . $table_url . '">`'
226 . htmlspecialchars($table['db']) . '`.`'
227 . htmlspecialchars($table['table']) . '`</a>';
232 $html .= '<li class="warp_link">'
233 . ($this->_tableType
== 'recent'
234 ?
__('There are no recent tables.')
235 :__('There are no favorite tables.'))
246 public function getHtml()
248 $html = '<div class="drop_list">';
249 if ($this->_tableType
== 'recent') {
250 $html .= '<span title="' . __('Recent tables')
251 . '" class="drop_button">'
252 . __('Recent') . '</span><ul id="pma_recent_list">';
254 $html .= '<span title="' . __('Favorite tables')
255 . '" class="drop_button">'
256 . __('Favorites') . '</span><ul id="pma_favorite_list">';
258 $html .= $this->getHtmlList();
259 $html .= '</ul></div>';
264 * Add recently used or favorite tables.
266 * @param string $db database name where the table is located
267 * @param string $table table name
269 * @return true|PMA_Message True if success, PMA_Message if not
271 public function add($db, $table)
273 // If table does not exist, do not add._getPmaTable()
274 if (! $GLOBALS['dbi']->getColumns($db, $table)) {
278 $table_arr = array();
279 $table_arr['db'] = $db;
280 $table_arr['table'] = $table;
282 // add only if this is new table
283 if (! isset($this->_tables
[0]) ||
$this->_tables
[0] != $table_arr) {
284 array_unshift($this->_tables
, $table_arr);
285 $this->_tables
= array_merge(array_unique($this->_tables
, SORT_REGULAR
));
287 if ($this->_getPmaTable()) {
288 return $this->saveToDb();
295 * Removes recent/favorite tables that don't exist.
297 * @param string $db database
298 * @param string $table table
300 * @return boolean|PMA_Message True if invalid and removed, False if not invalid,
301 * PMA_Message if error while removing
303 public function removeIfInvalid($db, $table)
305 foreach ($this->_tables
as $tbl) {
306 if ($tbl['db'] == $db && $tbl['table'] == $table) {
307 // TODO Figure out a better way to find the existence of a table
308 if (! $GLOBALS['dbi']->getColumns($tbl['db'], $tbl['table'])) {
309 return $this->remove($tbl['db'], $tbl['table']);
317 * Remove favorite tables.
319 * @param string $db database name where the table is located
320 * @param string $table table name
322 * @return true|PMA_Message True if success, PMA_Message if not
324 public function remove($db, $table)
326 $table_arr = array();
327 $table_arr['db'] = $db;
328 $table_arr['table'] = $table;
329 foreach ($this->_tables
as $key => $value) {
330 if ($value['db'] == $db && $value['table'] == $table) {
331 unset($this->_tables
[$key]);
334 if ($this->_getPmaTable()) {
335 return $this->saveToDb();
341 * Generate Html for sync Favorite tables anchor. (from localStorage to pmadb)
345 public function getHtmlSyncFavoriteTables()
348 $server_id = $GLOBALS['server'];
349 // Not to show this once list is synchronized.
350 $is_synced = isset($_SESSION['tmpval']['favorites_synced'][$server_id]) ?
353 $params = array('ajax_request' => true, 'favorite_table' => true,
354 'sync_favorite_tables' => true);
355 $url = 'db_structure.php' . PMA_URL_getCommon($params);
356 $retval = '<a class="hide" id="sync_favorite_tables"';
357 $retval .= ' href="' . $url . '"></a>';
363 * Generate Html to update recent tables.
365 * @return string html
367 public static function getHtmlUpdateRecentTables()
369 $params = array('ajax_request' => true, 'recent_table' => true);
370 $url = 'index.php' . PMA_URL_getCommon($params);
371 $retval = '<a class="hide" id="update_recent_tables"';
372 $retval .= ' href="' . $url . '"></a>';
377 * Reutrn the name of the configuration storage table
379 * @return string pma table name
381 private function _getPmaTable()
383 $cfgRelation = PMA_getRelationsParam();
384 if (! empty($cfgRelation['db'])
385 && ! empty($cfgRelation[$this->_tableType
])
387 return PMA_Util
::backquote($cfgRelation['db']) . "."
388 . PMA_Util
::backquote($cfgRelation[$this->_tableType
]);