Translated using Weblate (Chinese (Traditional))
[phpmyadmin.git] / libraries / RecentFavoriteTable.php
blobc2f11ffdddce6e5a638dcfb84f5641ea529f15d1
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Recent and Favorite table list handling
6 * @package PhpMyAdmin
7 */
8 namespace PMA\libraries;
10 use PMA\libraries\URL;
12 /**
13 * Handles the recently used and favorite tables.
15 * @TODO Change the release version in table pma_recent
16 * (#recent in documentation)
18 * @package PhpMyAdmin
20 class RecentFavoriteTable
22 /**
23 * Reference to session variable containing recently used or favorite tables.
25 * @access private
26 * @var array
28 private $_tables;
30 /**
31 * Defines type of action, Favorite or Recent table.
33 * @access private
34 * @var string
36 private $_tableType;
38 /**
39 * RecentFavoriteTable instances.
41 * @access private
42 * @var array
44 private static $_instances = array();
46 /**
47 * Creates a new instance of RecentFavoriteTable
49 * @param string $type the table type
51 * @access private
53 private function __construct($type)
55 $this->_tableType = $type;
56 $server_id = $GLOBALS['server'];
57 if (! isset($_SESSION['tmpval'][$this->_tableType . '_tables'][$server_id])
58 ) {
59 $_SESSION['tmpval'][$this->_tableType . '_tables'][$server_id]
60 = $this->_getPmaTable() ? $this->getFromDb() : array();
62 $this->_tables
63 =& $_SESSION['tmpval'][$this->_tableType . '_tables'][$server_id];
66 /**
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];
81 /**
82 * Returns the recent/favorite tables array
84 * @return array
86 public function getTables()
88 return $this->_tables;
91 /**
92 * Returns recently used tables or favorite from phpMyAdmin database.
94 * @return array
96 public function getFromDb()
98 // Read from phpMyAdmin database, if recent tables is not in session
99 $sql_query
100 = " SELECT `tables` FROM " . $this->_getPmaTable() .
101 " WHERE `username` = '" . $GLOBALS['dbi']->escapeString($GLOBALS['cfg']['Server']['user']) . "'";
103 $return = array();
104 $result = PMA_queryAsControlUser($sql_query, false);
105 if ($result) {
106 $row = $GLOBALS['dbi']->fetchArray($result);
107 if (isset($row[0])) {
108 $return = json_decode($row[0], true);
111 return $return;
115 * Save recent/favorite tables into phpMyAdmin database.
117 * @return true|Message
119 public function saveToDb()
121 $username = $GLOBALS['cfg']['Server']['user'];
122 $sql_query
123 = " REPLACE INTO " . $this->_getPmaTable() . " (`username`, `tables`)" .
124 " VALUES ('" . $GLOBALS['dbi']->escapeString($username) . "', '"
125 . $GLOBALS['dbi']->escapeString(
126 json_encode($this->_tables)
127 ) . "')";
129 $success = $GLOBALS['dbi']->tryQuery($sql_query, $GLOBALS['controllink']);
131 if (! $success) {
132 $error_msg = '';
133 switch ($this->_tableType) {
134 case 'recent':
135 $error_msg = __('Could not save recent table!');
136 break;
138 case 'favorite':
139 $error_msg = __('Could not save favorite table!');
140 break;
142 $message = Message::error($error_msg);
143 $message->addMessage(
144 Message::rawError(
145 $GLOBALS['dbi']->getError($GLOBALS['controllink'])
147 '<br /><br />'
149 return $message;
151 return true;
155 * Trim recent.favorite table according to the
156 * NumRecentTables/NumFavoriteTables configuration.
158 * @return boolean True if trimming occurred
160 public function trim()
162 $max = max(
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;
173 * Return HTML ul.
175 * @return string
177 public function getHtmlList()
179 $html = '';
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>';
193 $html .= '</li>';
195 } else {
196 foreach ($this->_tables as $table) {
197 $html .= '<li class="warp_link">';
199 $html .= '<a class="ajax favorite_table_anchor" ';
200 $fav_params = array(
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'])
212 . '" >'
213 . Util::getIcon('b_favorite.png')
214 . '</a>';
216 $fav_params = array(
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>';
225 $html .= '</li>';
228 } else {
229 $html .= '<li class="warp_link">'
230 . ($this->_tableType == 'recent'
231 ?__('There are no recent tables.')
232 :__('There are no favorite tables.'))
233 . '</li>';
235 return $html;
239 * Return HTML.
241 * @return string
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">';
250 } else {
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>';
257 return $html;
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)) {
272 return true;
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));
283 $this->trim();
284 if ($this->_getPmaTable()) {
285 return $this->saveToDb();
288 return true;
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']);
310 return false;
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();
334 return true;
338 * Generate Html for sync Favorite tables anchor. (from localStorage to pmadb)
340 * @return string
342 public function getHtmlSyncFavoriteTables()
344 $retval = '';
345 $server_id = $GLOBALS['server'];
346 if ($server_id == 0) {
347 return '';
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>';
358 return $retval;
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>';
372 return $retval;
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]);
389 return null;