From e373a5a0295de3dbd618d7939d22e6a5de6fafc7 Mon Sep 17 00:00:00 2001 From: Smita Kumari Date: Mon, 16 Jun 2014 12:53:40 +0530 Subject: [PATCH] add user defined column to central list Signed-off-by: Smita Kumari using functions from tbl_columns_definition_form.lib.php to create the add column form Signed-off-by: Smita Kumari fix Coding styl Signed-off-by: Smita Kumari added unit test for new function PMA_getHTMLforAddNewColumn Signed-off-by: Smita Kumari bug fix: added function to get columns count in central list Signed-off-by: Smita Kumari using functions from tbl_columns_definition_form.lib.php to edit columns in central list Signed-off-by: Smita Kumari improved test Signed-off-by: Smita Kumari Bug/coding style fix Signed-off-by: Smita Kumari Fix delete issue Signed-off-by: Smita Kumari added missing doc element Signed-off-by: Smita Kumari --- db_central_columns.php | 43 +++-- js/db_central_columns.js | 70 ++++++-- libraries/central_columns.lib.php | 176 ++++++++++++++++---- test/libraries/PMA_central_columns_test.php | 137 ++++++++++++++- 4 files changed, 364 insertions(+), 62 deletions(-) diff --git a/db_central_columns.php b/db_central_columns.php index 93833a85db..39009aa9a2 100644 --- a/db_central_columns.php +++ b/db_central_columns.php @@ -10,22 +10,32 @@ * Gets some core libraries */ require_once 'libraries/common.inc.php'; +require_once 'libraries/tbl_columns_definition_form.lib.php'; require_once 'libraries/central_columns.lib.php'; -if (isset($_POST['edit_save'])) { +if (isset($_POST['edit_save']) || isset($_POST['add_new_column'])) { $col_name = $_POST['col_name']; - $orig_col_name = $_POST['orig_col_name']; + if (isset($_POST['edit_save'])) { + $orig_col_name = $_POST['orig_col_name']; + } $col_default = $_POST['col_default']; $col_extra = $_POST['col_extra']; $col_isNull = isset($_POST['col_isNull'])?1:0; $col_length = $_POST['col_length']; $col_type = $_POST['col_type']; $collation = $_POST['collation']; - echo PMA_updateOneColumn( - $db, $orig_col_name, $col_name, $col_type, - $col_length, $col_isNull, $collation, $col_extra, $col_default - ); - exit; + if (isset($orig_col_name) && $orig_col_name) { + echo PMA_updateOneColumn( + $db, $orig_col_name, $col_name, $col_type, + $col_length, $col_isNull, $collation, $col_extra, $col_default + ); + exit; + } else { + $tmp_msg = PMA_updateOneColumn( + $db, "", $col_name, $col_type, + $col_length, $col_isNull, $collation, $col_extra, $col_default + ); + } } if (isset($_POST['populateColumns'])) { $selected_tbl = $_POST['selectedTable']; @@ -60,18 +70,21 @@ if (isset($_POST['delete_save'])) { if (isset($_REQUEST['total_rows']) && $_REQUEST['total_rows']) { $total_rows = $_REQUEST['total_rows']; } else { - $result = PMA_getColumnsList($db, 0, 0); - $total_rows = count($result); + $total_rows = PMA_getCentralColumnsCount($db); } if (isset($_REQUEST['pos'])) { $pos = $_REQUEST['pos']; } else { $pos = 0; } +$addNewColumn = PMA_getHTMLforAddNewColumn($db); +$response->addHTML($addNewColumn); if ($total_rows <= 0) { $response->addHTML( - '
There are no columns in central list to display for the ' - . 'current database.
' + '
' . __( + 'There are no columns in central list to display for the current ' + . 'database.' + ) . '
' ); $columnAdd = PMA_getHTMLforAddCentralColumn($total_rows, $pos, $db); $response->addHTML($columnAdd); @@ -93,7 +106,9 @@ $table_struct = '
' . ''; $response->addHTML($table_struct); -$tableheader = PMA_getCentralColumnsTableHeader(); +$tableheader = PMA_getCentralColumnsTableHeader( + 'column_heading', __('Click to sort'), 2 +); $response->addHTML($tableheader); $result = PMA_getColumnsList($db, $pos, $max_rows); $odd_row = false; @@ -110,7 +125,7 @@ $response->addHTML('
'); $message = PMA_Message::success( sprintf(__('Showing row(s) %1$s - %2$s'), ($pos + 1), ($pos + count($result))) ); -if (isset($tmp_msg) && $tmp_msg != true) { - $message->addMessage($tmp_msg); +if (isset($tmp_msg) && $tmp_msg !== true) { + $message = $tmp_msg; } ?> diff --git a/js/db_central_columns.js b/js/db_central_columns.js index 2a57d6b9f4..906baa2ab6 100644 --- a/js/db_central_columns.js +++ b/js/db_central_columns.js @@ -25,13 +25,27 @@ AJAX.registerTeardown('db_central_columns.js', function () { $('.column_heading').unbind('hover'); $('#table-select').unbind('change'); $('#column-select').unbind('change'); + $("#add_col_div>a").unbind('click'); + $('#add_new').unbind('submit'); + $("select.default_type").unbind('change'); }); AJAX.registerOnload('db_central_columns.js', function () { - $('#tableslistcontainer input,#tableslistcontainer select').hide(); + $('#tableslistcontainer input,#tableslistcontainer select,.default_value,.open_enum_editor').hide(); if ($('#table_columns tbody tr').length > 0) { $("#table_columns").tablesorter(); } + $('#add_new td').each(function(){ + if ($(this).attr('name') !== 'undefined') { + $(this).find('input,select:first').attr('name', $(this).attr('name')); + } + }); + $("#add_new #field_0_0").attr('required','required'); + $('#add_new input[type="text"], #add_new input[type="number"], #add_new select') + .css({ + 'width' : '10em', + '-moz-box-sizing' : 'border-box' + }); $('.column_heading').hover(function(){ $(this).css("cursor","move"); PMA_tooltip( @@ -40,6 +54,7 @@ AJAX.registerOnload('db_central_columns.js', function () { PMA_messages.strSortHint ); }); + window.scrollTo(0, 0); $(".filter_rows").live("keyup", function () { var cols = ["Name", "Type", "Length/Values", "Collation", "Null", "Extra", "Default"]; $.uiTableFilter($("#table_columns"), $(this).val(), cols, null, "td span"); @@ -49,13 +64,18 @@ AJAX.registerOnload('db_central_columns.js', function () { $('#save_'+rownum).show(); $(this).hide(); $('#f_'+rownum+' td span').hide(); - $('#f_'+rownum +' input, #f_'+rownum+' select').show(); + $('#f_'+rownum +' input, #f_'+rownum+' select, #f_'+rownum+' .open_enum_editor').show(); + var extra_val = $('#f_'+rownum +' td[name=col_extra] span').html(); + $('#f_'+rownum+' select[name=col_extra] option[value="'+extra_val+'"]').attr("selected","selected"); + if($('#f_'+rownum+' .default_type').val() === 'USER_DEFINED') { + $('#f_'+rownum+' .default_type').siblings('.default_value').show(); + } else { + $('#f_'+rownum+' .default_type').siblings('.default_value').hide(); + } }); $(".del_row").click(function() { - //alert('del') rownum = $(this).data('rownum'); - //alert($('#f_'+rownum +' input[name=col_name]').val()); - $("#del_col_name").val($('#f_'+rownum +' input[name=col_name]').val()); + $("#del_col_name").val($('#f_'+rownum +' td[name=col_name] span').html()); $("#del_form").submit(); }); $('.edit_cancel_form').click(function(event) { @@ -65,13 +85,24 @@ AJAX.registerOnload('db_central_columns.js', function () { $('#save_'+rownum).hide(); $('#edit_'+rownum).show(); $('#f_'+rownum+' td span').show(); - $('#f_'+rownum +' input, #f_'+rownum+' select').hide(); + $('#f_'+rownum +' input, #f_'+rownum+' select,#f_'+rownum+' .default_value, #f_'+rownum+' .open_enum_editor').hide(); }); $('.edit_save_form').click(function(event) { //alert(1); event.preventDefault(); event.stopPropagation(); rownum = $(this).data('rownum'); + $('#f_'+rownum+' td').each(function() { + if ($(this).attr('name') !== 'undefined') { + $(this).find(':input[type!="hidden"],select:first') + .attr('name', $(this).attr('name')); + } + }); + if($('#f_'+rownum+' .default_type').val() === 'USER_DEFINED') { + $('#f_'+rownum+' .default_type').attr('name','col_default_sel'); + } else { + $('#f_'+rownum+' .default_value').attr('name','col_default_val'); + } // alert(rownum); var datastring = $('#f_'+rownum+' :input').serialize(); //console.log(datastring); @@ -94,14 +125,13 @@ AJAX.registerOnload('db_central_columns.js', function () { $('#f_'+rownum +' td[name=col_length] span').text($('#f_'+rownum +' input[name=col_length]').val()).html(); $('#f_'+rownum +' td[name=collation] span').text($('#f_'+rownum +' select[name=collation]').val()).html(); $('#f_'+rownum +' td[name=col_isNull] span').text($('#f_'+rownum +' input[name=col_isNull]').val()).html(); - $('#f_'+rownum +' td[name=col_extra] span').text($('#f_'+rownum +' input[name=col_extra]').val()).html(); - $('#f_'+rownum +' td[name=col_default] span').text($('#f_'+rownum +' input[name=col_default]').val()).html(); + $('#f_'+rownum +' td[name=col_extra] span').text($('#f_'+rownum +' select[name=col_extra]').val()).html(); + $('#f_'+rownum +' td[name=col_default] span').text($('#f_'+rownum +' :input[name=col_default]').val()).html(); } - $('#save_'+rownum).hide(); $('#edit_'+rownum).show(); $('#f_'+rownum+' td span').show(); - $('#f_'+rownum +' input, #f_'+rownum+' select').hide(); + $('#f_'+rownum +' input, #f_'+rownum+' select,#f_'+rownum+' .default_value, #f_'+rownum+' .open_enum_editor').hide(); }, error: function() { PMA_ajaxShowMessage( @@ -138,4 +168,24 @@ AJAX.registerOnload('db_central_columns.js', function () { $("#add_column").submit(); } }); + $("#add_col_div>a").click(function(event){ + $('#add_new').slideToggle("slow"); + if($("#add_col_div>a span").html() === '+') { + $("#add_col_div>a span").html('-'); + } else { + $("#add_col_div>a span").html('+'); + } + }); + $('#add_new').submit(function(event){ + $('#add_new').toggle(); + }); + $("select.default_type").change(function () { + if ($(this).val() === 'USER_DEFINED') { + $(this).siblings('.default_value').attr('name','col_default'); + $(this).attr('name','col_default_sel'); + } else { + $(this).attr('name','col_default'); + $(this).siblings('.default_value').attr('name','col_default_val'); + } + }); }); diff --git a/libraries/central_columns.lib.php b/libraries/central_columns.lib.php index 694274a6be..0466a65013 100644 --- a/libraries/central_columns.lib.php +++ b/libraries/central_columns.lib.php @@ -73,17 +73,28 @@ function PMA_getColumnsList($db, $from=0, $num=25) return $has_list; } -/* to do block, need to complete +/** + * get the number of columns present in central list for given db + * + * @param string $db current database + * + * @return int number of columns in central list of columns for $db + */ function PMA_getCentralColumnsCount($db) { - $pmadb = $GLOBALS['cfg']['Server']['pmadb']; + $cfgCentralColumns = PMA_centralColumnsGetParams(); + if (empty($cfgCentralColumns)) { + return 0; + } + $pmadb = $cfgCentralColumns['db']; $GLOBALS['dbi']->selectDb($pmadb); - $central_list_table = $GLOBALS['cfg']['Server']['central_columns']; + $central_list_table = $cfgCentralColumns['table']; $query = 'SELECT count(db_name) FROM ' . - * PMA_Util::backquote($central_list_table) . ' ' + PMA_Util::backquote($central_list_table) . ' ' . 'WHERE db_name = \'' . $db . '\';'; - return $GLOBALS['dbi']->fetchResult($query); -}*/ + $res = $GLOBALS['dbi']->fetchResult($query); + return $res[0]; +} /** * return the existing columns in central list among the given list of columns * @@ -489,6 +500,18 @@ function PMA_updateOneColumn($db, $orig_col_name, $col_name, $col_type, } $centralTable = $cfgCentralColumns['table']; $GLOBALS['dbi']->selectDb($cfgCentralColumns['db'], $GLOBALS['controllink']); + if ($orig_col_name == "") { + $def = array(); + $def['Type'] = $col_type; + if ($col_length) { + $def['Type'] .= '(' . $col_length . ')'; + } + $def['Collation'] = $collation; + $def['Null'] = $col_isNull?__('YES'):__('NO'); + $def['Extra'] = $col_extra; + $def['Default'] = $col_default; + $query = PMA_getInsertQuery($col_name, $def, $db, $centralTable); + } else { $query = 'UPDATE ' . PMA_Util::backquote($centralTable) . ' SET col_type = \'' . PMA_Util::sqlAddSlashes($col_type) . '\'' . ',col_name = \'' . PMA_Util::sqlAddSlashes($col_name) . '\'' @@ -500,6 +523,7 @@ function PMA_updateOneColumn($db, $orig_col_name, $col_name, $col_type, . ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\' ' . 'AND col_name = \'' . PMA_Util::sqlAddSlashes($orig_col_name) . '\''; + } if (!$GLOBALS['dbi']->tryQuery($query, $GLOBALS['controllink'])) { return PMA_Message::error( $GLOBALS['dbi']->getError($GLOBALS['controllink']) @@ -522,7 +546,7 @@ function PMA_getHTMLforTableNavigation($total_rows, $pos, $db) $max_rows = $GLOBALS['cfg']['MaxRows']; $pageNow = ($pos / $max_rows) + 1; $nbTotalPage = ceil($total_rows / $max_rows); - $table_navigation_html = '' . '' . ''; @@ -586,27 +610,35 @@ function PMA_getHTMLforTableNavigation($total_rows, $pos, $db) /** * function generate and return the table header for central columns page * + * @param string $class styling class of 'th' elements + * @param string $title title of the 'th' elements + * @param integer $actionCount number of actions + * * @return html for table header in central columns view/edit page */ -function PMA_getCentralColumnsTableHeader() +function PMA_getCentralColumnsTableHeader($class='', $title='', $actionCount=0) { + $action = ''; + if ($actionCount > 0) { + $action .= ''; + } $tableheader = ''; $tableheader .= '' - . '' + . $action . '' - . '' - . '' - . '' - . '' - . '' + . '' - . '' - . '' . ''; $tableheader .= ''; @@ -673,7 +705,7 @@ function PMA_getHTMLforColumnDropdown($db, $selected_tbl) */ function PMA_getHTMLforAddCentralColumn($total_rows, $pos, $db) { - $columnAdd = '
' . __('Action') . '
' . __('Action') . '' + . '' . __('Name') . '' + . '' . __('Type') . '' + . '' . __('Length/Values') . '' - . __('Collation') . '' + . '' . __('Collation') . '' . __('Null') . '' + . '' . __('Extra') . '' + . '' . __('Default') . '
' . '' . '' @@ -736,50 +768,67 @@ function PMA_getHTMLforCentralColumnsTableRow($row, $odd_row, $row_num, $db) . '' . htmlspecialchars($row['col_name']) . '' . '' - . ''; + . PMA_getHtmlForColumnName( + $row_num, 0, 0, array('Field'=>$row['col_name']), + array('central_columnswork'=>false) + ) + . ''; $tableHtml .= ''; + . PMA_getHtmlForColumnType( + $row_num, 1, 0, strtoupper($row['col_type']), array() + ) + . ''; $tableHtml .= ''; $tableHtml .= ''; $tableHtml .= ''; $tableHtml .= ''; - + $meta = array(); + if (!isset($row['col_default']) || $row['col_default'] == '') { + $meta['DefaultType'] = 'NONE'; + } else { + if ($row['col_default'] == 'CURRENT_TIMESTAMP' + || $row['col_default'] == 'NULL' + ) { + $meta['DefaultType'] = $row['col_default']; + } else { + $meta['DefaultType'] = 'USER_DEFINED'; + $meta['DefaultValue'] = $row['col_default']; + } + } $tableHtml .= ''; $tableHtml .= ''; return $tableHtml; @@ -825,4 +874,59 @@ function PMA_getCentralColumnsListRaw($db, $table) return json_encode($columns_list); } +/** + * build html for adding a new user defined column to central list + * + * @param string $db current database + * + * @return html of the form to let user add a new user defined column to the list + */ +function PMA_getHTMLforAddNewColumn($db) +{ + $addNewColumn = '
' + . '+ ' . __('Add new column') . '' + . '' + . PMA_URL_getHiddenInputs( + $db + ) + . '' + . '
' . htmlspecialchars($row['col_type']) . '' - . '' . '' . ($row['col_length']?htmlspecialchars($row['col_length']):"") . '' - . '' + . PMA_getHtmlForColumnLength($row_num, 2, 0, 8, $row['col_length']) . '' . '' . htmlspecialchars($row['col_collation']) . '' - . PMA_generateCharsetDropdownBox( - PMA_CSDROPDOWN_COLLATION, null, - null, $row['col_collation'] + . PMA_getHtmlForColumnCollation( + $row_num, 3, 0, array('Collation'=>$row['col_collation']) ) . '' . '' . ($row['col_isNull'] ? __('Yes') : __('No')) - . '' + . '' + . PMA_getHtmlForColumnNull($row_num, 4, 0, array('Null'=>$row['col_isNull'])) . '' . htmlspecialchars($row['col_extra']) . '' - . '' + . '' . '' . (isset($row['col_default']) ? htmlspecialchars($row['col_default']) : 'None') - . '' + . '' + . PMA_getHtmlForColumnDefault( + $row_num, 5, 0, strtoupper($row['col_type']), '', $meta + ) . '
'; + $addNewColumn .= PMA_getCentralColumnsTableHeader(); + $addNewColumn .= '' + . '' + . '' + . '' + . '' + . '' + . '' + . '' + . ' ' + . ''; + $addNewColumn .= '
' + . PMA_getHtmlForColumnName( + 0, 0, 0, array(), array('central_columnswork'=>false) + ) + . '' + . PMA_getHtmlForColumnType(0, 1, 0, '', array()) + . '' + . PMA_getHtmlForColumnLength(0, 2, 0, 8, '') + . '' + . PMA_getHtmlForColumnCollation( + 0, 3, 0, array() + ) + . '' + . PMA_getHtmlForColumnNull(0, 4, 0, array()) + . '' + . '' + . '' + . PMA_getHtmlForColumnDefault(0, 5, 0, '', '', array()) + . '' + . '
'; + return $addNewColumn; +} ?> diff --git a/test/libraries/PMA_central_columns_test.php b/test/libraries/PMA_central_columns_test.php index 7894409745..d94809c27b 100644 --- a/test/libraries/PMA_central_columns_test.php +++ b/test/libraries/PMA_central_columns_test.php @@ -9,6 +9,7 @@ /* * Include to test. */ +$GLOBALS['server'] = 1; require_once 'libraries/Util.class.php'; require_once 'libraries/php-gettext/gettext.inc'; require_once 'libraries/database_interface.inc.php'; @@ -17,6 +18,9 @@ require_once 'libraries/relation.lib.php'; require_once 'libraries/Message.class.php'; require_once 'libraries/url_generating.lib.php'; require_once 'libraries/Theme.class.php'; +require_once 'libraries/tbl_columns_definition_form.lib.php'; +require_once 'libraries/Types.class.php'; +require_once 'libraries/mysql_charsets.inc.php'; require_once 'libraries/central_columns.lib.php'; /** @@ -33,6 +37,7 @@ class PMA_Central_Columns_Test extends PHPUnit_Framework_TestCase */ public function setUp() { + $GLOBALS['PMA_Types'] = new PMA_Types_MySQL(); $GLOBALS['cfg']['Server']['user'] = 'pma_user'; $GLOBALS['cfg']['Server']['pmadb'] = 'phpmyadmin'; $GLOBALS['cfg']['Server']['central_columns'] = 'pma_central_columns'; @@ -40,6 +45,7 @@ class PMA_Central_Columns_Test extends PHPUnit_Framework_TestCase $GLOBALS['cfg']['ServerDefault'] = "PMA_server"; $GLOBALS['cfg']['ActionLinksMode'] = 'icons'; $GLOBALS['pmaThemeImage'] = 'image'; + $GLOBALS['cfg']['CharEditing'] = ''; //$_SESSION $GLOBALS['server'] = 1; @@ -72,6 +78,19 @@ class PMA_Central_Columns_Test extends PHPUnit_Framework_TestCase ); } + /** + * Test for PMA_getCentralColumnsCount + * + * @return void + */ + function testPMAGetCentralColumnsCount() + { + $this->assertEquals( + 0, + PMA_getCentralColumnsCount('phpmyadmin') + ); + } + /** * Test for PMA_syncUniqueColumns * @@ -138,9 +157,40 @@ class PMA_Central_Columns_Test extends PHPUnit_Framework_TestCase */ public function testPMAGetHTMLforTableNavigation() { + $result = PMA_getHTMLforTableNavigation(0, 0, 'phpmyadmin'); $this->assertTag( array('tag' => 'table'), - PMA_getHTMLforTableNavigation(1, 0, 'phpmyadmin') + $result + ); + $this->assertContains( + __('Search this table'), + $result + ); + $result_1 = PMA_getHTMLforTableNavigation(25, 10, 'phpmyadmin'); + $this->assertContains( + '
' + . PMA_URL_getHiddenInputs( + 'phpmyadmin' + ), + $result_1 + ); + $this->assertContains( + '', + $result_1 + ); + $this->assertContains( + PMA_Util::pageselector( + 'pos', 10, 2, 3 + ), + $result_1 + ); + $this->assertContains( + '', + $result_1 ); } @@ -152,7 +202,70 @@ class PMA_Central_Columns_Test extends PHPUnit_Framework_TestCase public function testPMAGetCentralColumnsTableHeader() { $this->assertTag( - array('tag' => 'thead'), PMA_getCentralColumnsTableHeader() + array('tag' => 'thead'), PMA_getCentralColumnsTableHeader( + 'column_heading', __('Click to sort'), 2 + ) + ); + } + + /** + * Test for PMA_getHTMLforCentralColumnsTableRow + * + * @return void + */ + public function testPMAGetHTMLforCentralColumnsTableRow() + { + $row = array( + 'col_name'=>'col_test', + 'col_type'=>'int', + 'col_length'=>12, + 'col_collation'=>'utf8_general_ci', + 'col_isNull'=>1, + 'col_extra'=>'' + ); + $result = PMA_getHTMLforCentralColumnsTableRow($row, false, 1, 'phpmyadmin'); + $this->assertTag( + array('tag' => 'tr'), $result + ); + $this->assertContains( + PMA_URL_getHiddenInputs('phpmyadmin'), + $result + ); + $this->assertTag( + array('tag' => 'span', 'content'=>'col_test'), $result + ); + $this->assertContains( + __('on update CURRENT_TIMESTAMP'), + $result + ); + $this->assertContains( + PMA_getHtmlForColumnDefault( + 1, 5, 0, strtoupper($row['col_type']), '', + array('DefaultType'=>'NONE') + ), + $result + ); + $row['col_default'] = 100; + $result_1 = PMA_getHTMLforCentralColumnsTableRow( + $row, false, 1, 'phpmyadmin' + ); + $this->assertContains( + PMA_getHtmlForColumnDefault( + 1, 5, 0, strtoupper($row['col_type']), '', + array('DefaultType'=>'USER_DEFINED', 'DefaultValue'=>100) + ), + $result_1 + ); + $row['col_default'] = 'CURRENT_TIMESTAMP'; + $result_2 = PMA_getHTMLforCentralColumnsTableRow( + $row, false, 1, 'phpmyadmin' + ); + $this->assertContains( + PMA_getHtmlForColumnDefault( + 1, 5, 0, strtoupper($row['col_type']), '', + array('DefaultType'=>'CURRENT_TIMESTAMP') + ), + $result_2 ); } @@ -169,4 +282,24 @@ class PMA_Central_Columns_Test extends PHPUnit_Framework_TestCase ); } + /** + * Test for PMA_getHTMLforAddNewColumn + * + * @return void + */ + public function testPMAGetHTMLforAddNewColumn() + { + $result = PMA_getHTMLforAddNewColumn('phpmyadmin'); + $this->assertTag( + array('tag' => 'form','tag'=>'table'), $result + ); + $this->assertContains( + __('Add new column'), + $result + ); + $this->assertContains( + PMA_URL_getHiddenInputs('phpmyadmin'), + $result + ); + } } \ No newline at end of file