Refactor code duplication

Signed-off-by: Marc Delisle <marc@infomarc.info>
This commit is contained in:
Marc Delisle 2014-10-10 08:03:25 -04:00
parent f7140cfdd5
commit 9d187c0df6
3 changed files with 52 additions and 67 deletions

View File

@ -188,66 +188,63 @@ function PMA_urlParamsInEditMode($url_params, $where_clause_array, $where_clause
}
/**
* Show function fields in data edit view in pma
* Show type information or function selectors in Insert/Edit
*
* @param array $url_params containing url parameters
* @param boolean $showFuncFields whether to show function field
* @param string $which function|type
* @param array $url_params containing url parameters
* @param boolean $is_show whether to show the element in $which
*
* @return string an html snippet
* @return string an HTML snippet
*/
function PMA_showFunctionFieldsInEditMode($url_params, $showFuncFields)
function PMA_showTypeOrFunction($which, $url_params, $is_show)
{
$params = array();
if (! $showFuncFields) {
$params['ShowFunctionFields'] = 1;
} else {
$params['ShowFunctionFields'] = 0;
switch($which) {
case 'function':
$params['ShowFunctionFields'] = ($is_show ? 0 : 1);
$params['ShowFieldTypesInDataEditView']
= $GLOBALS['cfg']['ShowFieldTypesInDataEditView'];
break;
case 'type':
$params['ShowFieldTypesInDataEditView'] = ($is_show ? 0 : 1);
$params['ShowFunctionFields']
= $GLOBALS['cfg']['ShowFunctionFields'];
break;
}
$params['ShowFieldTypesInDataEditView']
= $GLOBALS['cfg']['ShowFieldTypesInDataEditView'];
$params['goto'] = 'sql.php';
$this_url_params = array_merge($url_params, $params);
if (! $showFuncFields) {
if (! $is_show) {
return ' : <a href="tbl_change.php'
. PMA_URL_getCommon($this_url_params) . '">'
. __('Function')
. '</a>' . "\n";
. PMA_showTypeOrFunctionLabel($which)
. '</a>';
}
return '<th><a href="tbl_change.php'
return $html = '<th><a href="tbl_change.php'
. PMA_URL_getCommon($this_url_params)
. '" title="' . __('Hide') . '">'
. __('Function')
. '</a></th>' . "\n";
. PMA_showTypeOrFunctionLabel($which)
. '</a></th>';
}
/**
* Show field types in data edit view in pma
* Show type information or function selectors labels in Insert/Edit
*
* @param array $url_params containing url parameters
* @param boolean $showColumnType whether to show column type
* @param string $which function|type
*
* @return string an html snippet
* @return string an HTML snippet
*/
function PMA_showColumnTypesInDataEditView($url_params, $showColumnType)
function PMA_showTypeOrFunctionLabel($which)
{
$params = array();
if (! $showColumnType) {
$params['ShowFieldTypesInDataEditView'] = 1;
} else {
$params['ShowFieldTypesInDataEditView'] = 0;
switch($which) {
case 'function':
return __('Function');
case 'type':
return __('Type');
}
$params['ShowFunctionFields'] = $GLOBALS['cfg']['ShowFunctionFields'];
$params['goto'] = 'sql.php';
$this_other_url_params = array_merge($url_params, $params);
if (! $showColumnType) {
return ' : <a href="tbl_change.php'
. PMA_URL_getCommon($this_other_url_params) . '">'
. __('Type') . '</a>' . "\n";
}
return '<th><a href="tbl_change.php'
. PMA_URL_getCommon($this_other_url_params)
. '" title="' . __('Hide') . '">' . __('Type') . '</a></th>' . "\n";
return $html;
}
/**
@ -1575,10 +1572,10 @@ function PMA_getHeadAndFootOfInsertRowTable($url_params)
. '<th>' . __('Column') . '</th>';
if ($GLOBALS['cfg']['ShowFieldTypesInDataEditView']) {
$html_output .= PMA_showColumnTypesInDataEditView($url_params, true);
$html_output .= PMA_showTypeOrFunction('type', $url_params, true);
}
if ($GLOBALS['cfg']['ShowFunctionFields']) {
$html_output .= PMA_showFunctionFieldsInEditMode($url_params, true);
$html_output .= PMA_showTypeOrFunction('function', $url_params, true);
}
$html_output .= '<th>' . __('Null') . '</th>'

View File

@ -159,11 +159,11 @@ if (! $cfg['ShowFunctionFields'] || ! $cfg['ShowFieldTypesInDataEditView']) {
}
if (! $cfg['ShowFunctionFields']) {
$html_output .= PMA_showFunctionFieldsInEditMode($url_params, false);
$html_output .= PMA_showTypeOrFunction('function', $url_params, false);
}
if (! $cfg['ShowFieldTypesInDataEditView']) {
$html_output .= PMA_showColumnTypesInDataEditView($url_params, false);
$html_output .= PMA_showTypeOrFunction('type', $url_params, false);
}
$GLOBALS['plugin_scripts'] = array();

View File

@ -283,64 +283,52 @@ class PMA_InsertEditTest extends PHPUnit_Framework_TestCase
}
/**
* Test for PMA_showFunctionFieldsInEditMode
* Test for PMA_showTypeOrFunction
*
* @return void
*/
public function testShowFunctionFieldsInEditMode()
public function testShowTypeOrFunction()
{
$GLOBALS['cfg']['ShowFieldTypesInDataEditView'] = true;
$GLOBALS['cfg']['ServerDefault'] = 1;
$url_params = array('ShowFunctionFields' => 2);
$result = PMA_showFunctionFieldsInEditMode($url_params, false);
$result = PMA_showTypeOrFunction('function', $url_params, false);
$this->assertEquals(
' : <a href="tbl_change.php?ShowFunctionFields=1&amp;ShowFieldTypesIn'
. 'DataEditView=1&amp;goto=sql.php&amp;lang=en&amp;token=token">'
. 'Function</a>' . "\n",
. 'Function</a>',
$result
);
// case 2
$result = PMA_showFunctionFieldsInEditMode($url_params, true);
$result = PMA_showTypeOrFunction('function', $url_params, true);
$this->assertEquals(
'<th><a href="tbl_change.php?ShowFunctionFields=0&amp;ShowFieldTypesIn'
. 'DataEditView=1&amp;goto=sql.php&amp;lang=en&amp;token=token" title='
. '"Hide">Function</a></th>' . "\n",
. '"Hide">Function</a></th>',
$result
);
}
/**
* Test for PMA_showColumnTypesInDataEditView
*
* @return void
*/
public function testShowColumnTypesInDataEditView()
{
$GLOBALS['cfg']['ShowFieldTypesInDataEditView'] = true;
$GLOBALS['cfg']['ShowFunctionFields'] = true;
$GLOBALS['cfg']['ServerDefault'] = 1;
$url_params = array('ShowFunctionFields' => 2);
$result = PMA_showColumnTypesInDataEditView($url_params, false);
// case 3
$result = PMA_showTypeOrFunction('type', $url_params, false);
$this->assertEquals(
' : <a href="tbl_change.php?ShowFunctionFields=1&amp;ShowFieldTypesIn'
. 'DataEditView=1&amp;goto=sql.php&amp;lang=en&amp;token=token">'
. 'Type</a>' . "\n",
. 'Type</a>',
$result
);
// case 2
$result = PMA_showColumnTypesInDataEditView($url_params, true);
// case 4
$result = PMA_showTypeOrFunction('type', $url_params, true);
$this->assertEquals(
'<th><a href="tbl_change.php?ShowFunctionFields=1&amp;ShowFieldTypesIn'
. 'DataEditView=0&amp;goto=sql.php&amp;lang=en&amp;token=token" title='
. '"Hide">Type</a></th>' . "\n",
. '"Hide">Type</a></th>',
$result
);
}