Translation update done using Pootle.
[phpmyadmin/ammaryasirr.git] / libraries / RecentTable.class.php
blobdef9c37c0339fafa861967bbcecd970e846c5270
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @package phpMyAdmin
6 */
8 require_once './libraries/Message.class.php';
10 /**
11 * Handles the recently used tables.
13 * @TODO Change the release version in table pma_recent (#recent in Documentation.html)
15 * @package phpMyAdmin
17 class PMA_RecentTable
19 /**
20 * Defines the internal PMA table which contains recent tables.
22 * @access private
23 * @var string
25 private $pma_table;
27 /**
28 * Reference to session variable containing recently used tables.
30 * @access public
31 * @var array
33 public $tables;
35 /**
36 * PMA_RecentTable instance.
38 * @var PMA_RecentTable
40 private static $_instance;
42 public function __construct()
44 if (strlen($GLOBALS['cfg']['Server']['pmadb']) &&
45 strlen($GLOBALS['cfg']['Server']['recent'])) {
46 $this->pma_table = PMA_backquote($GLOBALS['cfg']['Server']['pmadb']) .".".
47 PMA_backquote($GLOBALS['cfg']['Server']['recent']);
49 $server_id = $GLOBALS['server'];
50 if (! isset($_SESSION['tmp_user_values']['recent_tables'][$server_id])) {
51 $_SESSION['tmp_user_values']['recent_tables'][$server_id] =
52 isset($this->pma_table) ? $this->getFromDb() : array();
54 $this->tables =& $_SESSION['tmp_user_values']['recent_tables'][$server_id];
57 /**
58 * Returns class instance.
60 * @return PMA_RecentTable
62 public static function getInstance()
64 if (is_null(self::$_instance)) {
65 self::$_instance = new PMA_RecentTable();
67 return self::$_instance;
70 /**
71 * Returns recently used tables from phpMyAdmin database.
74 * @return array
76 public function getFromDb()
78 // Read from phpMyAdmin database, if recent tables is not in session
79 $sql_query =
80 " SELECT `tables` FROM " . $this->pma_table .
81 " WHERE `username` = '" . $GLOBALS['cfg']['Server']['user'] . "'";
83 $row = PMA_DBI_fetch_array(PMA_query_as_controluser($sql_query));
84 if (isset($row[0])) {
85 return json_decode($row[0], true);
86 } else {
87 return array();
91 /**
92 * Save recent tables into phpMyAdmin database.
95 * @return true|PMA_Message
97 public function saveToDb()
99 $username = $GLOBALS['cfg']['Server']['user'];
100 $sql_query =
101 " REPLACE INTO " . $this->pma_table . " (`username`, `tables`)" .
102 " VALUES ('" . $username . "', '" . PMA_sqlAddSlashes(json_encode($this->tables)) . "')";
104 $success = PMA_DBI_try_query($sql_query, $GLOBALS['controllink']);
106 if (!$success) {
107 $message = PMA_Message::error(__('Could not save recent table'));
108 $message->addMessage('<br /><br />');
109 $message->addMessage(PMA_Message::rawError(PMA_DBI_getError($GLOBALS['controllink'])));
110 return $message;
112 return true;
116 * Trim recent table according to the LeftRecentTable configuration.
118 * @return boolean True if trimming occurred
120 public function trim()
122 $max = max($GLOBALS['cfg']['LeftRecentTable'], 0);
123 $trimming_occured = count($this->tables) > $max;
124 while (count($this->tables) > $max) {
125 array_pop($this->tables);
127 return $trimming_occured;
131 * Return options for HTML select.
133 * @return string
135 public function getHtmlSelectOption()
137 // trim and save, in case where the configuration is changed
138 if ($this->trim() && isset($this->pma_table)) {
139 $this->saveToDb();
142 $html = '<option value="">(' . __('Recent tables') . ') ...</option>';
143 if (count($this->tables)) {
144 foreach ($this->tables as $table) {
145 $html .= '<option value="' . htmlspecialchars(json_encode($table)) . '">' .
146 htmlspecialchars('`' . $table['db'] . '`.`' . $table['table'] . '`') . '</option>';
148 } else {
149 $html .= '<option value="">' . __('There are no recent tables') . '</option>';
151 return $html;
155 * Return HTML select.
157 * @return string
159 public function getHtmlSelect()
161 $html = '<input type="hidden" name="goto" id="LeftDefaultTabTable" value="' .
162 htmlspecialchars($GLOBALS['cfg']['LeftDefaultTabTable']) . '" />';
163 $html .= '<select name="selected_recent_table" id="recentTable">';
164 $html .= $this->getHtmlSelectOption();
165 $html .= '</select>';
167 return $html;
171 * Add recently used tables.
173 * @param string $db Database name where the table is located
174 * @param string $table Table name
176 * @return true|PMA_Message True if success, PMA_Message if not
178 public function add($db, $table)
180 $table_arr = array();
181 $table_arr['db'] = $db;
182 $table_arr['table'] = $table;
184 // add only if this is new table
185 if (! isset($this->tables[0]) || $this->tables[0] != $table_arr) {
186 array_unshift($this->tables, $table_arr);
187 $this->tables = array_merge(array_unique($this->tables, SORT_REGULAR));
188 $this->trim();
189 if (isset($this->pma_table)) {
190 return $this->saveToDb();
193 return true;