Refactored ConfigFile class so that it is no longer a singleton
[phpmyadmin.git] / libraries / RecentTable.class.php
blob4d7d0070e7d494b6a7be7222c7d4f5425ee879b4
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Recent 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 tables.
18 * @TODO Change the release version in table pma_recent
19 * (#recent in documentation)
21 * @package PhpMyAdmin
23 class PMA_RecentTable
25 /**
26 * Defines the internal PMA table which contains recent tables.
28 * @access private
29 * @var string
31 private $_pmaTable;
33 /**
34 * Reference to session variable containing recently used tables.
36 * @access public
37 * @var array
39 public $tables;
41 /**
42 * PMA_RecentTable instance.
44 * @var PMA_RecentTable
46 private static $_instance;
48 /**
49 * Creates a new instance of PMA_RecentTable
51 * @return New PMA_RecentTable
53 public function __construct()
55 if (strlen($GLOBALS['cfg']['Server']['pmadb'])
56 && strlen($GLOBALS['cfg']['Server']['recent'])
57 ) {
58 $this->_pmaTable
59 = PMA_Util::backquote($GLOBALS['cfg']['Server']['pmadb']) . "."
60 . PMA_Util::backquote($GLOBALS['cfg']['Server']['recent']);
62 $server_id = $GLOBALS['server'];
63 if (! isset($_SESSION['tmpval']['recent_tables'][$server_id])) {
64 $_SESSION['tmpval']['recent_tables'][$server_id]
65 = isset($this->_pmaTable) ? $this->getFromDb() : array();
67 $this->tables =& $_SESSION['tmpval']['recent_tables'][$server_id];
70 /**
71 * Returns class instance.
73 * @return PMA_RecentTable
75 public static function getInstance()
77 if (is_null(self::$_instance)) {
78 self::$_instance = new PMA_RecentTable();
80 return self::$_instance;
83 /**
84 * Returns recently used tables from phpMyAdmin database.
86 * @return array
88 public function getFromDb()
90 // Read from phpMyAdmin database, if recent tables is not in session
91 $sql_query
92 = " SELECT `tables` FROM " . $this->_pmaTable .
93 " WHERE `username` = '" . $GLOBALS['cfg']['Server']['user'] . "'";
95 $return = array();
96 $result = PMA_queryAsControlUser($sql_query, false);
97 if ($result) {
98 $row = $GLOBALS['dbi']->fetchArray($result);
99 if (isset($row[0])) {
100 $return = json_decode($row[0], true);
103 return $return;
107 * Save recent tables into phpMyAdmin database.
109 * @return true|PMA_Message
111 public function saveToDb()
113 $username = $GLOBALS['cfg']['Server']['user'];
114 $sql_query
115 = " REPLACE INTO " . $this->_pmaTable . " (`username`, `tables`)" .
116 " VALUES ('" . $username . "', '"
117 . PMA_Util::sqlAddSlashes(
118 json_encode($this->tables)
119 ) . "')";
121 $success = $GLOBALS['dbi']->tryQuery($sql_query, $GLOBALS['controllink']);
123 if (! $success) {
124 $message = PMA_Message::error(__('Could not save recent table'));
125 $message->addMessage('<br /><br />');
126 $message->addMessage(
127 PMA_Message::rawError(
128 $GLOBALS['dbi']->getError($GLOBALS['controllink'])
131 return $message;
133 return true;
137 * Trim recent table according to the NumRecentTables configuration.
139 * @return boolean True if trimming occurred
141 public function trim()
143 $max = max($GLOBALS['cfg']['NumRecentTables'], 0);
144 $trimming_occurred = count($this->tables) > $max;
145 while (count($this->tables) > $max) {
146 array_pop($this->tables);
148 return $trimming_occurred;
152 * Return options for HTML select.
154 * @return string
156 public function getHtmlSelectOption()
158 // trim and save, in case where the configuration is changed
159 if ($this->trim() && isset($this->_pmaTable)) {
160 $this->saveToDb();
163 $html = '<option value="">(' . __('Recent tables') . ') ...</option>';
164 if (count($this->tables)) {
165 foreach ($this->tables as $table) {
166 $html .= '<option value="'
167 . htmlspecialchars(json_encode($table)) . '">'
168 . htmlspecialchars(
169 '`' . $table['db'] . '`.`' . $table['table'] . '`'
171 . '</option>';
173 } else {
174 $html .= '<option value="">'
175 . __('There are no recent tables')
176 . '</option>';
178 return $html;
182 * Return HTML select.
184 * @return string
186 public function getHtmlSelect()
188 $html = '<select name="selected_recent_table" id="recentTable">';
189 $html .= $this->getHtmlSelectOption();
190 $html .= '</select>';
192 return $html;
196 * Add recently used tables.
198 * @param string $db database name where the table is located
199 * @param string $table table name
201 * @return true|PMA_Message True if success, PMA_Message if not
203 public function add($db, $table)
205 $table_arr = array();
206 $table_arr['db'] = $db;
207 $table_arr['table'] = $table;
209 // add only if this is new table
210 if (! isset($this->tables[0]) || $this->tables[0] != $table_arr) {
211 array_unshift($this->tables, $table_arr);
212 $this->tables = array_merge(array_unique($this->tables, SORT_REGULAR));
213 $this->trim();
214 if (isset($this->_pmaTable)) {
215 return $this->saveToDb();
218 return true;