Translated using Weblate (Slovenian)
[phpmyadmin.git] / libraries / RecentFavoriteTable.class.php
blob59f4c98c905459216cb55677004484f1dafbbfda
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Recent and Favorite table list handling
6 * @package PhpMyAdmin
7 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 require_once './libraries/Message.class.php';
15 /**
16 * Handles the recently used and favorite tables.
18 * @TODO Change the release version in table pma_recent
19 * (#recent in documentation)
21 * @package PhpMyAdmin
23 class PMA_RecentFavoriteTable
25 /**
26 * Reference to session variable containing recently used or favorite tables.
28 * @access private
29 * @var array
31 private $_tables;
33 /**
34 * Defines type of action, Favorite or Recent table.
36 * @access private
37 * @var string
39 private $_tableType;
41 /**
42 * PMA_RecentFavoriteTable instances.
44 * @access private
45 * @var array
47 private static $_instances = array();
49 /**
50 * Creates a new instance of PMA_RecentFavoriteTable
52 * @param string $type the table type
54 * @access private
56 private function __construct($type)
58 $this->_tableType = $type;
59 $server_id = $GLOBALS['server'];
60 if (! isset($_SESSION['tmpval'][$this->_tableType . '_tables'][$server_id])
61 ) {
62 $_SESSION['tmpval'][$this->_tableType . '_tables'][$server_id]
63 = $this->_getPmaTable() ? $this->getFromDb() : array();
65 $this->_tables
66 =& $_SESSION['tmpval'][$this->_tableType . '_tables'][$server_id];
69 /**
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];
84 /**
85 * Returns the recent/favorite tables array
87 * @return array
89 public function getTables()
91 return $this->_tables;
94 /**
95 * Returns recently used tables or favorite from phpMyAdmin database.
97 * @return array
99 public function getFromDb()
101 // Read from phpMyAdmin database, if recent tables is not in session
102 $sql_query
103 = " SELECT `tables` FROM " . $this->_getPmaTable() .
104 " WHERE `username` = '" . $GLOBALS['cfg']['Server']['user'] . "'";
106 $return = array();
107 $result = PMA_queryAsControlUser($sql_query, false);
108 if ($result) {
109 $row = $GLOBALS['dbi']->fetchArray($result);
110 if (isset($row[0])) {
111 $return = json_decode($row[0], true);
114 return $return;
118 * Save recent/favorite tables into phpMyAdmin database.
120 * @return true|PMA_Message
122 public function saveToDb()
124 $username = $GLOBALS['cfg']['Server']['user'];
125 $sql_query
126 = " REPLACE INTO " . $this->_getPmaTable() . " (`username`, `tables`)" .
127 " VALUES ('" . $username . "', '"
128 . PMA_Util::sqlAddSlashes(
129 json_encode($this->_tables)
130 ) . "')";
132 $success = $GLOBALS['dbi']->tryQuery($sql_query, $GLOBALS['controllink']);
134 if (! $success) {
135 $error_msg = '';
136 switch ($this->_tableType) {
137 case 'recent':
138 $error_msg = __('Could not save recent table!');
139 break;
141 case 'favorite':
142 $error_msg = __('Could not save favorite table!');
143 break;
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'])
152 return $message;
154 return true;
158 * Trim recent.favorite table according to the
159 * NumRecentTables/NumFavoriteTables configuration.
161 * @return boolean True if trimming occurred
163 public function trim()
165 $max = max(
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;
176 * Return HTML ul.
178 * @return string
180 public function getHtmlList()
182 $html = '';
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>';
196 $html .= '</li>';
198 } else {
199 foreach ($this->_tables as $table) {
200 $html .= '<li class="warp_link">';
202 $html .= '<a class="ajax favorite_table_anchor" ';
203 $fav_params = array(
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'])
215 . '" >'
216 . PMA_Util::getIcon('b_favorite.png')
217 . '</a>';
219 $fav_params = array(
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>';
228 $html .= '</li>';
231 } else {
232 $html .= '<li class="warp_link">'
233 . ($this->_tableType == 'recent'
234 ?__('There are no recent tables.')
235 :__('There are no favorite tables.'))
236 . '</li>';
238 return $html;
242 * Return HTML.
244 * @return string
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">';
253 } else {
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>';
260 return $html;
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)) {
275 return true;
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));
286 $this->trim();
287 if ($this->_getPmaTable()) {
288 return $this->saveToDb();
291 return true;
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']);
313 return false;
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();
337 return true;
341 * Generate Html for sync Favorite tables anchor. (from localStorage to pmadb)
343 * @return string
345 public function getHtmlSyncFavoriteTables()
347 $retval = '';
348 $server_id = $GLOBALS['server'];
349 // Not to show this once list is synchronized.
350 $is_synced = isset($_SESSION['tmpval']['favorites_synced'][$server_id]) ?
351 true : false;
352 if (!$is_synced) {
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>';
359 return $retval;
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>';
373 return $retval;
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]);
390 return null;