Separate methods for charset and collation dropdowns

They do not share much code in the end and this way the code is more
readable.

Signed-off-by: Michal Čihař <michal@cihar.com>
This commit is contained in:
Michal Čihař 2016-04-11 13:04:41 +02:00
parent 3e2029e750
commit b311780d76
10 changed files with 93 additions and 58 deletions

View File

@ -214,8 +214,7 @@ if ($server > 0 || count($cfg['Servers']) > 1
. ': ' . "\n"
. ' </label>' . "\n"
. Charsets::generateCharsetDropdownBox(
PMA_CSDROPDOWN_COLLATION,
. Charsets::getCollationDropdownBox(
'collation_connection',
'select_collation_connection',
$collation_connection,

View File

@ -8,9 +8,6 @@
namespace PMA\libraries;
use PMA\libraries\Util;
define('PMA_CSDROPDOWN_COLLATION', 0);
define('PMA_CSDROPDOWN_CHARSET', 1);
/**
* Class used to manage MySQL charsets
*
@ -162,7 +159,6 @@ class Charsets
/**
* Generate charset dropdown box
*
* @param int $type Type
* @param string $name Element name
* @param string $id Element id
* @param null|string $default Default value
@ -171,17 +167,13 @@ class Charsets
*
* @return string
*/
public static function generateCharsetDropdownBox($type = PMA_CSDROPDOWN_COLLATION,
public static function getCharsetDropdownBox(
$name = null, $id = null, $default = null, $label = true,
$submitOnChange = false
) {
self::loadCollations();
if (empty($name)) {
if ($type == PMA_CSDROPDOWN_COLLATION) {
$name = 'collation';
} else {
$name = 'character_set';
}
$name = 'character_set';
}
$return_str = '<select lang="en" dir="ltr" name="'
@ -190,7 +182,7 @@ class Charsets
. ($submitOnChange ? ' class="autosubmit"' : '') . '>' . "\n";
if ($label) {
$return_str .= '<option value="">'
. ($type == PMA_CSDROPDOWN_COLLATION ? __('Collation') : __('Charset'))
. __('Charset')
. '</option>' . "\n";
}
$return_str .= '<option value=""></option>' . "\n";
@ -203,26 +195,68 @@ class Charsets
? $current_charset
: self::$_charsets_descriptions[$current_charset];
if ($type == PMA_CSDROPDOWN_COLLATION) {
$return_str .= '<optgroup label="' . $current_charset
. '" title="' . $current_cs_descr . '">' . "\n";
foreach (self::$_collations[$current_charset] as $current_collation) {
if (!self::$_collations_available[$current_collation]) {
continue;
}
$return_str .= '<option value="' . $current_collation
. '" title="' . self::getCollationDescr($current_collation) . '"'
. ($default == $current_collation ? ' selected="selected"' : '')
. '>'
. $current_collation . '</option>' . "\n";
}
$return_str .= '</optgroup>' . "\n";
} else {
$return_str .= '<option value="' . $current_charset
. '" title="' . $current_cs_descr . '"'
. ($default == $current_charset ? ' selected="selected"' : '') . '>'
. $current_charset . '</option>' . "\n";
$return_str .= '<option value="' . $current_charset
. '" title="' . $current_cs_descr . '"'
. ($default == $current_charset ? ' selected="selected"' : '') . '>'
. $current_charset . '</option>' . "\n";
}
$return_str .= '</select>' . "\n";
return $return_str;
}
/**
* Generate collation dropdown box
*
* @param string $name Element name
* @param string $id Element id
* @param null|string $default Default value
* @param bool $label Label
* @param bool $submitOnChange Submit on change
*
* @return string
*/
public static function getCollationDropdownBox(
$name = null, $id = null, $default = null, $label = true,
$submitOnChange = false
) {
self::loadCollations();
if (empty($name)) {
$name = 'collation';
}
$return_str = '<select lang="en" dir="ltr" name="'
. htmlspecialchars($name) . '"'
. (empty($id) ? '' : ' id="' . htmlspecialchars($id) . '"')
. ($submitOnChange ? ' class="autosubmit"' : '') . '>' . "\n";
if ($label) {
$return_str .= '<option value="">'
. __('Collation')
. '</option>' . "\n";
}
$return_str .= '<option value=""></option>' . "\n";
foreach (self::$_charsets as $current_charset) {
if (!self::$_charsets_available[$current_charset]) {
continue;
}
$current_cs_descr
= empty(self::$_charsets_descriptions[$current_charset])
? $current_charset
: self::$_charsets_descriptions[$current_charset];
$return_str .= '<optgroup label="' . $current_charset
. '" title="' . $current_cs_descr . '">' . "\n";
foreach (self::$_collations[$current_charset] as $current_collation) {
if (!self::$_collations_available[$current_collation]) {
continue;
}
$return_str .= '<option value="' . $current_collation
. '" title="' . self::getCollationDescr($current_collation) . '"'
. ($default == $current_collation ? ' selected="selected"' : '')
. '>'
. $current_collation . '</option>' . "\n";
}
$return_str .= '</optgroup>' . "\n";
}
$return_str .= '</select>' . "\n";

View File

@ -937,8 +937,8 @@ function PMA_getHTMLforCentralColumnsTableRow($row, $odd_row, $row_num, $db)
$tableHtml .=
'<td name="collation" class="nowrap">'
. '<span>' . htmlspecialchars($row['col_collation']) . '</span>'
. Charsets::generateCharsetDropdownBox(
PMA_CSDROPDOWN_COLLATION, 'field_collation[' . $row_num . ']',
. Charsets::getCollationDropdownBox(
'field_collation[' . $row_num . ']',
'field_' . $row_num . '_4', $row['col_collation'], false
)
. '</td>';
@ -1079,8 +1079,8 @@ function PMA_getHTMLforCentralColumnsEditTableRow($row, $odd_row, $row_num)
. '</td>';
$tableHtml .=
'<td name="collation" class="nowrap">'
. Charsets::generateCharsetDropdownBox(
PMA_CSDROPDOWN_COLLATION, 'field_collation[' . $row_num . ']',
. Charsets::getCollationDropdownBox(
'field_collation[' . $row_num . ']',
'field_' . $row_num . '_4', $row['col_collation'], false
)
. '</td>';
@ -1324,8 +1324,8 @@ function PMA_getHTMLforAddNewColumn($db)
)
. '</td>'
. '<td name="collation" class="nowrap">'
. Charsets::generateCharsetDropdownBox(
PMA_CSDROPDOWN_COLLATION, 'field_collation[0]',
. Charsets::getCollationDropdownBox(
'field_collation[0]',
'field_0_4', null, false
)
. '</td>'

View File

@ -194,8 +194,7 @@ function PMA_getHtmlForImportCharset()
} else {
$html .= '<label for="charset_of_file">' . __('Character set of the file:')
. '</label>' . "\n";
$html .= Charsets::generateCharsetDropdownBox(
PMA_CSDROPDOWN_CHARSET,
$html .= Charsets::getCharsetDropdownBox(
'charset_of_file',
'charset_of_file',
'utf8',

View File

@ -288,8 +288,7 @@ function PMA_getHtmlForChangeDatabaseCharset($db, $table)
$html_output .= '<label for="select_db_collation">' . __('Collation')
. '</label>' . "\n"
. '</legend>' . "\n"
. Charsets::generateCharsetDropdownBox(
PMA_CSDROPDOWN_COLLATION,
. Charsets::getCollationDropdownBox(
'db_collation',
'select_db_collation',
isset($_REQUEST['db_collation']) ? $_REQUEST['db_collation'] : '',
@ -1062,8 +1061,7 @@ function PMA_getTableOptionFieldset($comment, $tbl_collation,
//Table character set
$html_output .= '<tr><td class="vmiddle">' . __('Collation') . '</td>'
. '<td>'
. Charsets::generateCharsetDropdownBox(
PMA_CSDROPDOWN_COLLATION,
. Charsets::getCollationDropdownBox(
'tbl_collation', null, $tbl_collation, false
)
. '</td>'

View File

@ -746,8 +746,7 @@ function PMA_RTN_getParameterRow($routine = array(), $index = null, $class = '')
$retval .= " </td>\n";
$retval .= " <td class='hide no_len'>---</td>\n";
$retval .= " <td class='routine_param_opts_text'>\n";
$retval .= Charsets::generateCharsetDropdownBox(
PMA_CSDROPDOWN_CHARSET,
$retval .= Charsets::getCharsetDropdownBox(
"item_param_opts_text[$index]",
null,
$routine['item_param_opts_text'][$i]
@ -962,8 +961,7 @@ function PMA_RTN_getEditorForm($mode, $operation, $routine)
$retval .= "<tr class='routine_return_row" . $isfunction_class . "'>";
$retval .= " <td>" . __('Return options') . "</td>";
$retval .= " <td><div>";
$retval .= Charsets::generateCharsetDropdownBox(
PMA_CSDROPDOWN_CHARSET,
$retval .= Charsets::getCharsetDropdownBox(
"item_returnopts_text",
null,
$routine['item_returnopts_text']

View File

@ -54,8 +54,7 @@ $ci_offset = -1;
<td class="center">
<!-- column collation -->
<?php $tmp_collation = empty($columnMeta['Collation']) ? null : $columnMeta['Collation']; ?>
<?= PMA\libraries\Charsets::generateCharsetDropdownBox(
PMA_CSDROPDOWN_COLLATION,
<?= PMA\libraries\Charsets::getCollationDropdownBox(
'field_collation[' . $columnNumber . ']',
'field_' . $columnNumber . '_' . ($ci - $ci_offset),
$tmp_collation,

View File

@ -114,8 +114,7 @@ use PMA\libraries\URL;
</td>
<td width="25">&nbsp;</td>
<td>
<?= PMA\libraries\Charsets::generateCharsetDropdownBox(
PMA_CSDROPDOWN_COLLATION,
<?= PMA\libraries\Charsets::getCollationDropdownBox(
'tbl_collation',
null,
isset($_REQUEST['tbl_collation']) ? $_REQUEST['tbl_collation'] : null,

View File

@ -18,7 +18,7 @@
<input type="text" name="new_db" value="<?= $GLOBALS['db_to_create']; ?>"
maxlength="64" class="textfield" id="text_create_db" required
placeholder="<?= __('Database name'); ?>" />
<?= PMA\libraries\Charsets::generateCharsetDropdownBox(PMA_CSDROPDOWN_COLLATION, 'db_collation', null, null, true); ?>
<?= PMA\libraries\Charsets::getCollationDropdownBox('db_collation', null, null, true); ?>
<input type="submit" value="<?= __('Create'); ?>" id="buttonGo" />
</form>
<?php else: ?>

View File

@ -116,14 +116,14 @@ class CharsetsTest extends PHPUnit_Framework_TestCase
}
/**
* Test for PMA\libraries\Charsets::generateCharsetDropdownBox
* Test for PMA\libraries\Charsets::getCollationDropdownBox
*
* @return void
* @test
*/
public function testGenerateCharsetDropdownBox()
public function testGetCollationDropdownBox()
{
$result = PMA\libraries\Charsets::generateCharsetDropdownBox();
$result = PMA\libraries\Charsets::getCollationDropdownBox();
$this->assertContains('name="collation"', $result);
$this->assertNotContains('id="', $result);
@ -135,9 +135,18 @@ class CharsetsTest extends PHPUnit_Framework_TestCase
$this->assertContains('title="cp1252', $result);
$this->assertNotContains('value="latin2_general1_ci"', $result);
$this->assertContains('title="Swedish', $result);
}
$result = PMA\libraries\Charsets::generateCharsetDropdownBox(
2, null, "test_id", "latin1", false, true
/**
* Test for PMA\libraries\Charsets::getCharsetDropdownBox
*
* @return void
* @test
*/
public function testGetCharsetDropdownBox()
{
$result = PMA\libraries\Charsets::getCharsetDropdownBox(
null, "test_id", "latin1", false, true
);
$this->assertContains('name="character_set"', $result);
$this->assertNotContains('Charset</option>', $result);