Merge remote-tracking branch 'origin/QA_4_0' into QA_4_0
[phpmyadmin.git] / libraries / RecentTable.class.php
blobfef75084ce587cb9ad64ecc2a736a7d1c74accc6
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @package PhpMyAdmin
6 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 require_once './libraries/Message.class.php';
14 /**
15 * Handles the recently used tables.
17 * @TODO Change the release version in table pma_recent
18 * (#recent in documentation)
20 * @package PhpMyAdmin
22 class PMA_RecentTable
24 /**
25 * Defines the internal PMA table which contains recent tables.
27 * @access private
28 * @var string
30 private $_pmaTable;
32 /**
33 * Reference to session variable containing recently used tables.
35 * @access public
36 * @var array
38 public $tables;
40 /**
41 * PMA_RecentTable instance.
43 * @var PMA_RecentTable
45 private static $_instance;
47 public function __construct()
49 if (strlen($GLOBALS['cfg']['Server']['pmadb'])
50 && strlen($GLOBALS['cfg']['Server']['recent'])
51 ) {
52 $this->_pmaTable
53 = PMA_Util::backquote($GLOBALS['cfg']['Server']['pmadb']) . "."
54 . PMA_Util::backquote($GLOBALS['cfg']['Server']['recent']);
56 $server_id = $GLOBALS['server'];
57 if (! isset($_SESSION['tmp_user_values']['recent_tables'][$server_id])) {
58 $_SESSION['tmp_user_values']['recent_tables'][$server_id]
59 = isset($this->_pmaTable) ? $this->getFromDb() : array();
61 $this->tables =& $_SESSION['tmp_user_values']['recent_tables'][$server_id];
64 /**
65 * Returns class instance.
67 * @return PMA_RecentTable
69 public static function getInstance()
71 if (is_null(self::$_instance)) {
72 self::$_instance = new PMA_RecentTable();
74 return self::$_instance;
77 /**
78 * Returns recently used tables from phpMyAdmin database.
80 * @return array
82 public function getFromDb()
84 // Read from phpMyAdmin database, if recent tables is not in session
85 $sql_query
86 = " SELECT `tables` FROM " . $this->_pmaTable .
87 " WHERE `username` = '" . $GLOBALS['cfg']['Server']['user'] . "'";
89 $return = array();
90 $result = PMA_queryAsControlUser($sql_query, false);
91 if ($result) {
92 $row = PMA_DBI_fetch_array($result);
93 if (isset($row[0])) {
94 $return = json_decode($row[0], true);
97 return $return;
101 * Save recent tables into phpMyAdmin database.
103 * @return true|PMA_Message
105 public function saveToDb()
107 $username = $GLOBALS['cfg']['Server']['user'];
108 $sql_query
109 = " REPLACE INTO " . $this->_pmaTable . " (`username`, `tables`)" .
110 " VALUES ('" . $username . "', '"
111 . PMA_Util::sqlAddSlashes(
112 json_encode($this->tables)
113 ) . "')";
115 $success = PMA_DBI_try_query($sql_query, $GLOBALS['controllink']);
117 if (! $success) {
118 $message = PMA_Message::error(__('Could not save recent table'));
119 $message->addMessage('<br /><br />');
120 $message->addMessage(
121 PMA_Message::rawError(PMA_DBI_getError($GLOBALS['controllink']))
123 return $message;
125 return true;
129 * Trim recent table according to the NumRecentTables configuration.
131 * @return boolean True if trimming occurred
133 public function trim()
135 $max = max($GLOBALS['cfg']['NumRecentTables'], 0);
136 $trimming_occured = count($this->tables) > $max;
137 while (count($this->tables) > $max) {
138 array_pop($this->tables);
140 return $trimming_occured;
144 * Return options for HTML select.
146 * @return string
148 public function getHtmlSelectOption()
150 // trim and save, in case where the configuration is changed
151 if ($this->trim() && isset($this->_pmaTable)) {
152 $this->saveToDb();
155 $html = '<option value="">(' . __('Recent tables') . ') ...</option>';
156 if (count($this->tables)) {
157 foreach ($this->tables as $table) {
158 $html .= '<option value="'
159 . htmlspecialchars(json_encode($table)) . '">'
160 . htmlspecialchars(
161 '`' . $table['db'] . '`.`' . $table['table'] . '`'
163 . '</option>';
165 } else {
166 $html .= '<option value="">'
167 . __('There are no recent tables')
168 . '</option>';
170 return $html;
174 * Return HTML select.
176 * @return string
178 public function getHtmlSelect()
180 $html = '<select name="selected_recent_table" id="recentTable">';
181 $html .= $this->getHtmlSelectOption();
182 $html .= '</select>';
184 return $html;
188 * Add recently used tables.
190 * @param string $db database name where the table is located
191 * @param string $table table name
193 * @return true|PMA_Message True if success, PMA_Message if not
195 public function add($db, $table)
197 $table_arr = array();
198 $table_arr['db'] = $db;
199 $table_arr['table'] = $table;
201 // add only if this is new table
202 if (! isset($this->tables[0]) || $this->tables[0] != $table_arr) {
203 array_unshift($this->tables, $table_arr);
204 $this->tables = array_merge(array_unique($this->tables, SORT_REGULAR));
205 $this->trim();
206 if (isset($this->_pmaTable)) {
207 return $this->saveToDb();
210 return true;