Skip to content

Commit

Permalink
Add table pma_ui_prefs for remembering table sorting persistently
Browse files Browse the repository at this point in the history
  • Loading branch information
arisferyanto committed May 27, 2011
1 parent 4280031 commit e4ba1dc
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 15 deletions.
19 changes: 19 additions & 0 deletions Documentation.html
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,25 @@ <h2 id="config">Configuration</h2>
</ul>
</dd>

<dt id="table_uiprefs">
<span id="cfg_Servers_table_uiprefs">$cfg['Servers'][$i]['table_uiprefs']</span> string
</dt>
<dd>
Since release 3.5.0 phpMyAdmin can be configured to remember several things
(table sorting
<a href="#cfg_RememberSorting" class="configrule">$cfg['RememberSorting']</a>
, etc.) for browsing tables.
Without configuring the storage, these features still can be used,
but the values will disappear after you logout.<br/><br/>

To allow the usage of these functionality persistently:

<ul>
<li>set up <a href="#pmadb">pmadb</a> and the phpMyAdmin configuration storage</li>
<li>put the table name in <tt>$cfg['Servers'][$i]['table_uiprefs']</tt> (e.g. 'pma_table_uiprefs')</li>
</ul>
</dd>

<dt id="tracking">
<span id="cfg_Servers_tracking">$cfg['Servers'][$i]['tracking']</span> string
</dt>
Expand Down
1 change: 1 addition & 0 deletions config.sample.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
// $cfg['Servers'][$i]['column_info'] = 'pma_column_info';
// $cfg['Servers'][$i]['history'] = 'pma_history';
// $cfg['Servers'][$i]['recent'] = 'pma_recent';
// $cfg['Servers'][$i]['table_uiprefs'] = 'pma_table_uiprefs';
// $cfg['Servers'][$i]['tracking'] = 'pma_tracking';
// $cfg['Servers'][$i]['designer_coords'] = 'pma_designer_coords';
// $cfg['Servers'][$i]['userconfig'] = 'pma_userconfig';
Expand Down
146 changes: 146 additions & 0 deletions libraries/Table.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
*/
class PMA_Table
{
/**
* UI preferences property: sorted column
*/
const PROP_SORTED_COLUMN = 'sorted_col';

static $cache = array();

Expand Down Expand Up @@ -39,6 +43,11 @@ class PMA_Table
*/
var $settings = array();

/**
* @var array UI preferences
*/
var $uiprefs;

/**
* @var array errors occured
*/
Expand Down Expand Up @@ -1185,5 +1194,142 @@ public function getIndexedColumns($backquoted = true)

return $return;
}

/**
* Return UI preferences for this table from phpMyAdmin database.
*
* @uses PMA_query_as_controluser()
* @uses PMA_DBI_fetch_array()
* @uses json_decode()
*
* @return array
*/
protected function getUiPrefsFromDb()
{
$pma_table = PMA_backquote($GLOBALS['cfg']['Server']['pmadb']) .".".
PMA_backquote($GLOBALS['cfg']['Server']['table_uiprefs']);

// Read from phpMyAdmin database
$sql_query =
" SELECT `prefs` FROM " . $pma_table .
" WHERE `username` = '" . $GLOBALS['cfg']['Server']['user'] . "'" .
" AND `db_name` = '" . $this->db_name . "'" .
" AND `table_name` = '" . $this->name . "'";

$row = PMA_DBI_fetch_array(PMA_query_as_controluser($sql_query));
if (isset($row[0])) {
return json_decode($row[0], true);
} else {
return array();
}
}

/**
* Save this table's UI preferences into phpMyAdmin database.
*
* @uses PMA_DBI_try_query()
* @uses json_decode()
* @uses PMA_Message
*
* @return true|PMA_Message
*/
protected function saveUiPrefsToDb()
{
$pma_table = PMA_backquote($GLOBALS['cfg']['Server']['pmadb']) .".".
PMA_backquote($GLOBALS['cfg']['Server']['table_uiprefs']);

$username = $GLOBALS['cfg']['Server']['user'];
$sql_query =
" REPLACE INTO " . $pma_table .
" VALUES ('" . $username . "', '" . $this->db_name . "', '" .
$this->name . "', '" . PMA_sqlAddslashes(json_encode($this->uiprefs)) . "')";

$success = PMA_DBI_try_query($sql_query, $GLOBALS['controllink']);

if (!$success) {
$message = PMA_Message::error(__('Could not save table UI preferences'));
$message->addMessage('<br /><br />');
$message->addMessage(PMA_Message::rawError(PMA_DBI_getError($GLOBALS['controllink'])));
return $message;
}
return true;
}

/**
* Loads the UI preferences for this table.
* If pmadb and table_uiprefs is set, it will load the UI preferences from
* phpMyAdmin database.
*
* @uses getUiPrefsFromDb()
*/
protected function loadUiPrefs()
{
// set session variable if it's still undefined
if (! isset($_SESSION['tmp_user_values']['table_uiprefs'][$this->db_name][$this->name])) {
$_SESSION['tmp_user_values']['table_uiprefs'][$this->db_name][$this->name] =
// check whether we can get from pmadb
(strlen($GLOBALS['cfg']['Server']['pmadb'])
&& strlen($GLOBALS['cfg']['Server']['table_uiprefs'])) ?
$this->getUiPrefsFromDb() : array();
}
$this->uiprefs =& $_SESSION['tmp_user_values']['table_uiprefs'][$this->db_name][$this->name];
}

/**
* Get UI preferences array for this table.
* If pmadb and table_uiprefs is set, it will get the UI preferences from
* phpMyAdmin database.
*
* @return array
*/
public function getUiPrefs()
{
if (! isset($this->uiprefs)) {
$this->loadUiPrefs();
}
return $this->uiprefs;
}

/**
* Get a property from UI preferences.
* Return false if the property is not found.
* Available property:
* - PROP_SORTED_COLUMN
*
* @uses loadUiPrefs()
*
* @param string $property
* @return mixed
*/
public function getUiProp($property)
{
if (! isset($this->uiprefs)) {
$this->loadUiPrefs();
}
return isset($this->uiprefs[$property]) ? $this->uiprefs[$property] : false;
}

/**
* Set a property from UI preferences.
* If pmadb and table_uiprefs is set, it will save the UI preferences to
* phpMyAdmin database.
*
* @param string $property
* @param mixed $value
* @return true|PMA_Message
*/
public function setUiProp($property, $value)
{
if (! isset($this->uiprefs)) {
$this->loadUiPrefs();
}
$this->uiprefs[$property] = $value;
// check if pmadb is set
if (strlen($GLOBALS['cfg']['Server']['pmadb'])
&& strlen($GLOBALS['cfg']['Server']['table_uiprefs'])) {
return $this->saveUiprefsToDb();
}
return true;
}
}
?>
7 changes: 7 additions & 0 deletions libraries/config.default.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,13 @@
*/
$cfg['Servers'][$i]['recent'] = '';

/**
* table to store UI preferences for tables
* - leave blank for no "persistent" UI preferences
* SUGGESTED: 'pma_table_uiprefs'
*/
$cfg['Servers'][$i]['table_uiprefs'] = '';

/**
* table to store SQL tracking
* - leave blank for no SQL tracking
Expand Down
2 changes: 2 additions & 0 deletions libraries/config/messages.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,8 @@
$strConfigServers_table_coords_name = __('PDF schema: table coordinates');
$strConfigServers_table_info_desc = __('Table to describe the display columns, leave blank for no support; suggested: [kbd]pma_table_info[/kbd]');
$strConfigServers_table_info_name = __('Display columns table');
$strConfigServers_table_uiprefs_desc = __('Leave blank for no "persistent" tables\'UI preferences across sessions, suggested: [kbd]pma_table_uiprefs[/kbd]');
$strConfigServers_table_uiprefs_name = __('UI preferences table');
$strConfigServers_tracking_add_drop_database_desc = __('Whether a DROP DATABASE IF EXISTS statement will be added as first line to the log when creating a database.');
$strConfigServers_tracking_add_drop_database_name = __('Add DROP DATABASE');
$strConfigServers_tracking_add_drop_table_desc = __('Whether a DROP TABLE IF EXISTS statement will be added as first line to the log when creating a table.');
Expand Down
1 change: 1 addition & 0 deletions libraries/config/setup.forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
'column_info' => 'pma_column_info',
'history' => 'pma_history',
'recent' => 'pma_recent',
'table_uiprefs' => 'pma_table_uiprefs',
'tracking' => 'pma_tracking',
'table_coords' => 'pma_table_coords',
'pdf_pages' => 'pma_pdf_pages',
Expand Down
19 changes: 16 additions & 3 deletions libraries/relation.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ function PMA_printRelationsParamDiagnostic($cfgRelation)

PMA_printDiagMessageForFeature(__('Persistent recently used tables'), 'recentwork', $messages);

PMA_printDiagMessageForParameter('table_uiprefs', isset($cfgRelation['table_uiprefs']), $messages, 'table_uiprefs');

PMA_printDiagMessageForFeature(__('Persistent tables\' UI preferences'), 'uiprefswork', $messages);

PMA_printDiagMessageForParameter('tracking', isset($cfgRelation['tracking']), $messages, 'tracking');

PMA_printDiagMessageForFeature(__('Tracking'), 'trackingwork', $messages);
Expand Down Expand Up @@ -225,6 +229,7 @@ function PMA__getRelationsParam()
$cfgRelation['mimework'] = false;
$cfgRelation['historywork'] = false;
$cfgRelation['recentwork'] = false;
$cfgRelation['uiprefswork'] = false;
$cfgRelation['trackingwork'] = false;
$cfgRelation['designerwork'] = false;
$cfgRelation['userconfigwork'] = false;
Expand Down Expand Up @@ -278,6 +283,8 @@ function PMA__getRelationsParam()
$cfgRelation['history'] = $curr_table[0];
} elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['recent']) {
$cfgRelation['recent'] = $curr_table[0];
} elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['table_uiprefs']) {
$cfgRelation['table_uiprefs'] = $curr_table[0];
} elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['tracking']) {
$cfgRelation['tracking'] = $curr_table[0];
} elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['userconfig']) {
Expand All @@ -292,9 +299,11 @@ function PMA__getRelationsParam()
$cfgRelation['displaywork'] = true;
}
}

if (isset($cfgRelation['table_coords']) && isset($cfgRelation['pdf_pages'])) {
$cfgRelation['pdfwork'] = true;
}

if (isset($cfgRelation['column_info'])) {
$cfgRelation['commwork'] = true;

Expand Down Expand Up @@ -336,6 +345,10 @@ function PMA__getRelationsParam()
$cfgRelation['recentwork'] = true;
}

if (isset($cfgRelation['table_uiprefs'])) {
$cfgRelation['uiprefswork'] = true;
}

if (isset($cfgRelation['tracking'])) {
$cfgRelation['trackingwork'] = true;
}
Expand All @@ -357,9 +370,9 @@ function PMA__getRelationsParam()
if ($cfgRelation['relwork'] && $cfgRelation['displaywork']
&& $cfgRelation['pdfwork'] && $cfgRelation['commwork']
&& $cfgRelation['mimework'] && $cfgRelation['historywork']
&& $cfgRelation['recentwork'] && $cfgRelation['trackingwork']
&& $cfgRelation['userconfigwork'] && $cfgRelation['bookmarkwork']
&& $cfgRelation['designerwork']) {
&& $cfgRelation['recentwork'] && $cfgRelation['uiprefswork']
&& $cfgRelation['trackingwork'] && $cfgRelation['userconfigwork']
&& $cfgRelation['bookmarkwork'] && $cfgRelation['designerwork']) {
$cfgRelation['allworks'] = true;
}

Expand Down
15 changes: 15 additions & 0 deletions scripts/create_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,21 @@ CREATE TABLE IF NOT EXISTS `pma_recent` (

-- --------------------------------------------------------

--
-- Table structure for table `pma_table_uiprefs`
--

CREATE TABLE IF NOT EXISTS `pma_table_uiprefs` (
`username` varchar(64) COLLATE utf8_bin NOT NULL,
`db_name` varchar(64) COLLATE utf8_bin NOT NULL,
`table_name` varchar(64) COLLATE utf8_bin NOT NULL,
`prefs` blob NOT NULL,
PRIMARY KEY (`username`,`db_name`,`table_name`)
) ENGINE=MyISAM COMMENT='tables'' UI preferences'
DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

-- --------------------------------------------------------

--
-- Table structure for table `pma_relation`
--
Expand Down
26 changes: 14 additions & 12 deletions sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,19 +363,21 @@
&& isset($analyzed_sql[0]['queryflags']['select_from'])
&& count($analyzed_sql[0]['table_ref']) == 1
) {
if (empty($analyzed_sql[0]['order_by_clause'])
&& isset($_SESSION['tmp_user_values']['table_sorting'][$table])) {
// retrieve the remembered sorting order for current table
$sql_order_to_append = ' ORDER BY ' . $_SESSION['tmp_user_values']['table_sorting'][$table] . ' ';
$sql_query = $analyzed_sql[0]['section_before_limit'] . $sql_order_to_append . $analyzed_sql[0]['section_after_limit'];

// update the $analyzed_sql
$analyzed_sql[0]['section_before_limit'] .= $sql_order_to_append;
$analyzed_sql[0]['order_by_clause'] = $_SESSION['tmp_user_values']['table_sorting'][$table];

} else if (! empty($analyzed_sql[0]['order_by_clause'])) {
$pmatable = new PMA_Table($table, $db);
if (empty($analyzed_sql[0]['order_by_clause'])) {
$sorted_col = $pmatable->getUiProp(PMA_Table::PROP_SORTED_COLUMN);
if ($sorted_col) {
// retrieve the remembered sorting order for current table
$sql_order_to_append = ' ORDER BY ' . $sorted_col . ' ';
$sql_query = $analyzed_sql[0]['section_before_limit'] . $sql_order_to_append . $analyzed_sql[0]['section_after_limit'];

// update the $analyzed_sql
$analyzed_sql[0]['section_before_limit'] .= $sql_order_to_append;
$analyzed_sql[0]['order_by_clause'] = $sorted_col;
}
} else {
// store the remembered table into session
$_SESSION['tmp_user_values']['table_sorting'][$table] = $analyzed_sql[0]['order_by_clause'];
$pmatable->setUiProp(PMA_Table::PROP_SORTED_COLUMN, $analyzed_sql[0]['order_by_clause']);
}
}

Expand Down

0 comments on commit e4ba1dc

Please sign in to comment.