Skip to content

Commit

Permalink
Feature request: phpmyadmin#1476 Favourite tables select box.
Browse files Browse the repository at this point in the history
Signed-off-by: Ashutosh Dhundhara <ashutoshdhundhara@yahoo.com>

Changes in "create_tables.sql".

Signed-off-by: Ashutosh Dhundhara <ashutoshdhundhara@yahoo.com>

Minor fix in structure.lib.php.

Signed-off-by: Ashutosh Dhundhara <ashutoshdhundhara@yahoo.com>

Ripped Trailing White spaces.

Signed-off-by: Ashutosh Dhundhara <ashutoshdhundhara@yahoo.com>

Removed irrelevant comments from "FavouriteTable.class.php".

Signed-off-by: Ashutosh Dhundhara <ashutoshdhundhara@yahoo.com>

Fixed Table Summary colspan.

Signed-off-by: Ashutosh Dhundhara <ashutoshdhundhara@yahoo.com>

Changed Favourite to Favorite

Signed-off-by: Ashutosh Dhundhara <ashutoshdhundhara@yahoo.com>

Fixed failing test.

Signed-off-by: Ashutosh Dhundhara <ashutoshdhundhara@yahoo.com>

Feature request: phpmyadmin#1476 Favourite tables select box. (Icon only)

Signed-off-by: Ashutosh Dhundhara <ashutoshdhundhara@yahoo.com>

Gray and Golden Icons.

Signed-off-by: Ashutosh Dhundhara <ashutoshdhundhara@yahoo.com>

Removed unwanted icon from "img".

Signed-off-by: Ashutosh Dhundhara <ashutoshdhundhara@yahoo.com>

Fixed error in PMA_buildActionTitles_test.php.

Signed-off-by: Ashutosh Dhundhara <ashutoshdhundhara@yahoo.com>

Re-positioned star icon and added tooltip to it.

Signed-off-by: Ashutosh Dhundhara <ashutoshdhundhara@yahoo.com>

Ripped trailing spaces from "structure.lib.php" and "db_structure.js".

Signed-off-by: Ashutosh Dhundhara <ashutoshdhundhara@yahoo.com>
  • Loading branch information
ashutoshdhundhara committed Mar 26, 2014
1 parent 69e2d84 commit 8bc21b7
Show file tree
Hide file tree
Showing 20 changed files with 434 additions and 8 deletions.
1 change: 1 addition & 0 deletions config.sample.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
// $cfg['Servers'][$i]['designer_coords'] = 'pma__designer_coords';
// $cfg['Servers'][$i]['userconfig'] = 'pma__userconfig';
// $cfg['Servers'][$i]['recent'] = 'pma__recent';
// $cfg['Servers'][$i]['favorite'] = 'pma__favorite';
// $cfg['Servers'][$i]['users'] = 'pma__users';
// $cfg['Servers'][$i]['usergroups'] = 'pma__usergroups';
// $cfg['Servers'][$i]['navigationhiding'] = 'pma__navigationhiding';
Expand Down
23 changes: 22 additions & 1 deletion db_structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
$_POST['message'] = PMA_Message::success();
}
}

require 'libraries/db_common.inc.php';
$url_query .= '&amp;goto=db_structure.php';

Expand Down Expand Up @@ -132,12 +133,14 @@
$hidden_fields = array();
$odd_row = true;
$sum_row_count_pre = '';

// Instance of PMA_FavoriteTable class.
$fav_instance = PMA_FavoriteTable::getInstance();
foreach ($tables as $keyname => $current_table) {
// Get valid statistics whatever is the table type

$drop_query = '';
$drop_message = '';
$already_favorite = false;
$overhead = '';

$table_is_view = false;
Expand Down Expand Up @@ -251,6 +254,24 @@
list($do, $ignored) = PMA_getServerSlaveStatus(
$server_slave_status, $truename
);
// Handle favorite table list. ----START----
$already_favorite = PMA_checkFavoriteTable($db, $current_table['TABLE_NAME']);

if (isset($_REQUEST['remove_favorite'])) {
if ($already_favorite) {
// If already in favorite list, remove it.
$favorite_table = $_REQUEST['favorite_table'];
$fav_instance->remove($db, $favorite_table);
}
}

if (isset($_REQUEST['add_favorite'])) {
if (!$already_favorite) {
// Otherwise add to favorite list.
$favorite_table = $_REQUEST['favorite_table'];
$fav_instance->add($db, $favorite_table);
}
} // Handle favorite table list. ----ENDS----

list($html_output, $odd_row) = PMA_getHtmlForStructureTableRow(
$i, $odd_row, $table_is_view, $current_table,
Expand Down
14 changes: 14 additions & 0 deletions examples/create_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,20 @@ CREATE TABLE IF NOT EXISTS `pma__recent` (

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

--
-- Table structure for table `pma__favorite`
--

CREATE TABLE IF NOT EXISTS `pma__favorite` (
`username` varchar(64) NOT NULL,
`tables` text NOT NULL,
PRIMARY KEY (`username`)
)
COMMENT='Favorite tables'
DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;

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

--
-- Table structure for table `pma__table_uiprefs`
--
Expand Down
14 changes: 14 additions & 0 deletions js/db_structure.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,18 @@ AJAX.registerOnload('db_structure.js', function () {
return false;
}); //end Calculate Real End for InnoDB

PMA_tooltip(
$("select[name*='funcs']"),
'select',
PMA_messages.strFunctionHint
);
// Add tooltip to favorite icons.
$(".favorite_table_anchor").each(function () {
PMA_tooltip(
$(this),
'a',
$(this).attr("title")
);
});

}); // end $()
14 changes: 14 additions & 0 deletions js/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ $(function () {
}
});

/**
* Jump to favorite table
*/
$('#favoriteTable').live('change', function () {
if (this.value !== '') {
var arr = jQuery.parseJSON(this.value);
var $form = $(this).closest('form');
$form.find('input[name=db]').val(arr.db);
$form.find('input[name=table]').val(arr.table);
$form.submit();
$(this).prop('selectedIndex', 0);
}
});

/** Create a Routine, Trigger or Event */
$('li.new_procedure a.ajax, li.new_function a.ajax').live('click', function (event) {
event.preventDefault();
Expand Down
271 changes: 271 additions & 0 deletions libraries/FavoriteTable.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Favorite table list handling
*
* @package PhpMyAdmin
*/

if (! defined('PHPMYADMIN')) {
exit;
}

require_once './libraries/Message.class.php';

/**
* Handles the favorite tables list.
*
*
* @package PhpMyAdmin
*/
class PMA_FavoriteTable
{
/**
* Defines the internal PMA table which contains favorite tables.
*
* @access private
* @var string
*/
private $_pmaTable;

/**
* Reference to session variable containing favorite used tables.
*
* @access public
* @var array
*/
public $tables;

/**
* PMA_FavoriteTable instance.
*
* @var PMA_FavoriteTable
*/
private static $_instance;

/**
* Creates a new instance of PMA_FavoriteTable
*/
public function __construct()
{
if (strlen($GLOBALS['cfg']['Server']['pmadb'])
&& strlen($GLOBALS['cfg']['Server']['favorite'])
) {
$this->_pmaTable
= PMA_Util::backquote($GLOBALS['cfg']['Server']['pmadb']) . "."
. PMA_Util::backquote($GLOBALS['cfg']['Server']['favorite']);
}
$server_id = $GLOBALS['server'];
if (! isset($_SESSION['tmpval']['favorite_tables'][$server_id])) {
$_SESSION['tmpval']['favorite_tables'][$server_id]
= isset($this->_pmaTable) ? $this->getFromDb() : array();
}
$this->tables =& $_SESSION['tmpval']['favorite_tables'][$server_id];
}

/**
* Returns class instance.
*
* @return PMA_FavoriteTable
*/
public static function getInstance()
{
if (is_null(self::$_instance)) {
self::$_instance = new PMA_FavoriteTable();
}
return self::$_instance;
}

/**
* Returns favorite tables from phpMyAdmin database.
*
* @return array
*/
public function getFromDb()
{
// Read from phpMyAdmin database, if favorite tables are not in session
$sql_query
= " SELECT `tables` FROM " . $this->_pmaTable .
" WHERE `username` = '" . $GLOBALS['cfg']['Server']['user'] . "'";

$return = array();
$result = PMA_queryAsControlUser($sql_query, false);
if ($result) {
$row = $GLOBALS['dbi']->fetchArray($result);
if (isset($row[0])) {
$return = json_decode($row[0], true);
}
}
return $return;
}

/**
* Save favorite tables into phpMyAdmin database.
*
* @return true|PMA_Message
*/
public function saveToDb()
{
$username = $GLOBALS['cfg']['Server']['user'];
$sql_query
= " REPLACE INTO " . $this->_pmaTable . " (`username`, `tables`)" .
" VALUES ('" . $username . "', '"
. PMA_Util::sqlAddSlashes(
json_encode($this->tables)
) . "')";

$success = $GLOBALS['dbi']->tryQuery($sql_query, $GLOBALS['controllink']);

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

/**
* Remove favorite tables from phpMyAdmin database.
*
* @return true|PMA_Message
*/
public function removeFromDb()
{
$username = $GLOBALS['cfg']['Server']['user'];
$sql_query
= " REPLACE INTO " . $this->_pmaTable . " (`username`, `tables`)" .
" VALUES ('" . $username . "', '"
. PMA_Util::sqlAddSlashes(
json_encode($this->tables)
) . "')";

$success = $GLOBALS['dbi']->tryQuery($sql_query, $GLOBALS['controllink']);

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

/**
* Trim favorite table according to the NumFavoriteTables configuration.
*
* @return boolean True if trimming occurred
*/
public function trim()
{
$max = max($GLOBALS['cfg']['NumFavoriteTables'], 0);
$trimming_occurred = count($this->tables) > $max;
while (count($this->tables) > $max) {
array_pop($this->tables);
}
return $trimming_occurred;
}

/**
* Return options for HTML select.
*
* @return string
*/
public function getHtmlSelectOption()
{
// trim and save, in case where the configuration is changed
if ($this->trim() && isset($this->_pmaTable)) {
$this->saveToDb();
}

$html = '<option value="">(' . __('Favorite tables') . ') ...</option>';
if (count($this->tables)) {
foreach ($this->tables as $table) {
$html .= '<option value="'
. htmlspecialchars(json_encode($table)) . '">'
. htmlspecialchars(
'`' . $table['db'] . '`.`' . $table['table'] . '`'
)
. '</option>';
}
} else {
$html .= '<option value="">'
. __('There are no favorite tables.')
. '</option>';
}
return $html;
}

/**
* Return HTML select.
*
* @return string
*/
public function getHtmlSelect()
{
$html = '<select name="selected_favorite_table" id="favoriteTable">';
$html .= $this->getHtmlSelectOption();
$html .= '</select>';

return $html;
}

/**
* Add favorite tables.
*
* @param string $db database name where the table is located
* @param string $table table name
*
* @return true|PMA_Message True if success, PMA_Message if not
*/
public function add($db, $table)
{
$table_arr = array();
$table_arr['db'] = $db;
$table_arr['table'] = $table;

// add only if this is new table
if (! isset($this->tables[0]) || $this->tables[0] != $table_arr) {
array_unshift($this->tables, $table_arr);
$this->tables = array_merge(array_unique($this->tables, SORT_REGULAR));
$this->trim();
if (isset($this->_pmaTable)) {
return $this->saveToDb();
}
}
return true;
}

/**
* Remove favorite tables.
*
* @param string $db database name where the table is located
* @param string $table table name
*
* @return true|PMA_Message True if success, PMA_Message if not
*/
public function remove($db, $table)
{
$table_arr = array();
$table_arr['db'] = $db;
$table_arr['table'] = $table;
foreach ($this->tables as $key => $value) {
if ($value['db'] == $db && $value['table'] == $table) {
unset($this->tables[$key]);
}
}
if (isset($this->_pmaTable)) {
return $this->removeFromDb();
}
return true;
}
}
?>
1 change: 1 addition & 0 deletions libraries/Header.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

require_once 'libraries/Scripts.class.php';
require_once 'libraries/RecentTable.class.php';
require_once 'libraries/FavoriteTable.class.php';
require_once 'libraries/Menu.class.php';
require_once 'libraries/navigation/Navigation.class.php';
require_once 'libraries/url_generating.lib.php';
Expand Down
Loading

0 comments on commit 8bc21b7

Please sign in to comment.