Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / RecentTable.class.php
blob9468eeb1c0f5fe508f01eb8e8739db8cba94a653
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @package PhpMyAdmin
6 */
7 if (! defined('PHPMYADMIN')) {
8 exit;
11 require_once './libraries/Message.class.php';
13 /**
14 * Handles the recently used tables.
16 * @TODO Change the release version in table pma_recent
17 * (#recent in documentation)
19 * @package PhpMyAdmin
21 class PMA_RecentTable
23 /**
24 * Defines the internal PMA table which contains recent tables.
26 * @access private
27 * @var string
29 private $_pmaTable;
31 /**
32 * Reference to session variable containing recently used tables.
34 * @access public
35 * @var array
37 public $tables;
39 /**
40 * PMA_RecentTable instance.
42 * @var PMA_RecentTable
44 private static $_instance;
46 public function __construct()
48 if (strlen($GLOBALS['cfg']['Server']['pmadb'])
49 && strlen($GLOBALS['cfg']['Server']['recent'])
50 ) {
51 $this->_pmaTable
52 = PMA_Util::backquote($GLOBALS['cfg']['Server']['pmadb']) . "."
53 . PMA_Util::backquote($GLOBALS['cfg']['Server']['recent']);
55 $server_id = $GLOBALS['server'];
56 if (! isset($_SESSION['tmp_user_values']['recent_tables'][$server_id])) {
57 $_SESSION['tmp_user_values']['recent_tables'][$server_id]
58 = isset($this->_pmaTable) ? $this->getFromDb() : array();
60 $this->tables =& $_SESSION['tmp_user_values']['recent_tables'][$server_id];
63 /**
64 * Returns class instance.
66 * @return PMA_RecentTable
68 public static function getInstance()
70 if (is_null(self::$_instance)) {
71 self::$_instance = new PMA_RecentTable();
73 return self::$_instance;
76 /**
77 * Returns recently used tables from phpMyAdmin database.
79 * @return array
81 public function getFromDb()
83 // Read from phpMyAdmin database, if recent tables is not in session
84 $sql_query
85 = " SELECT `tables` FROM " . $this->_pmaTable .
86 " WHERE `username` = '" . $GLOBALS['cfg']['Server']['user'] . "'";
88 $row = PMA_DBI_fetch_array(PMA_queryAsControlUser($sql_query));
89 if (isset($row[0])) {
90 return json_decode($row[0], true);
91 } else {
92 return array();
96 /**
97 * Save recent tables into phpMyAdmin database.
99 * @return true|PMA_Message
101 public function saveToDb()
103 $username = $GLOBALS['cfg']['Server']['user'];
104 $sql_query
105 = " REPLACE INTO " . $this->_pmaTable . " (`username`, `tables`)" .
106 " VALUES ('" . $username . "', '"
107 . PMA_Util::sqlAddSlashes(
108 json_encode($this->tables)
109 ) . "')";
111 $success = PMA_DBI_try_query($sql_query, $GLOBALS['controllink']);
113 if (! $success) {
114 $message = PMA_Message::error(__('Could not save recent table'));
115 $message->addMessage('<br /><br />');
116 $message->addMessage(
117 PMA_Message::rawError(PMA_DBI_getError($GLOBALS['controllink']))
119 return $message;
121 return true;
125 * Trim recent table according to the NumRecentTables configuration.
127 * @return boolean True if trimming occurred
129 public function trim()
131 $max = max($GLOBALS['cfg']['NumRecentTables'], 0);
132 $trimming_occured = count($this->tables) > $max;
133 while (count($this->tables) > $max) {
134 array_pop($this->tables);
136 return $trimming_occured;
140 * Return options for HTML select.
142 * @return string
144 public function getHtmlSelectOption()
146 // trim and save, in case where the configuration is changed
147 if ($this->trim() && isset($this->_pmaTable)) {
148 $this->saveToDb();
151 $html = '<option value="">(' . __('Recent tables') . ') ...</option>';
152 if (count($this->tables)) {
153 foreach ($this->tables as $table) {
154 $html .= '<option value="'
155 . htmlspecialchars(json_encode($table)) . '">'
156 . htmlspecialchars(
157 '`' . $table['db'] . '`.`' . $table['table'] . '`'
159 . '</option>';
161 } else {
162 $html .= '<option value="">'
163 . __('There are no recent tables')
164 . '</option>';
166 return $html;
170 * Return HTML select.
172 * @return string
174 public function getHtmlSelect()
176 $html = '<select name="selected_recent_table" id="recentTable">';
177 $html .= $this->getHtmlSelectOption();
178 $html .= '</select>';
180 return $html;
184 * Add recently used tables.
186 * @param string $db database name where the table is located
187 * @param string $table table name
189 * @return true|PMA_Message True if success, PMA_Message if not
191 public function add($db, $table)
193 $table_arr = array();
194 $table_arr['db'] = $db;
195 $table_arr['table'] = $table;
197 // add only if this is new table
198 if (! isset($this->tables[0]) || $this->tables[0] != $table_arr) {
199 array_unshift($this->tables, $table_arr);
200 $this->tables = array_merge(array_unique($this->tables, SORT_REGULAR));
201 $this->trim();
202 if (isset($this->_pmaTable)) {
203 return $this->saveToDb();
206 return true;