Extract HTML from InsertEdit::getHtmlInput()

Signed-off-by: Maurício Meneghini Fauth <mauricio@mfauth.net>
This commit is contained in:
Maurício Meneghini Fauth 2025-05-29 16:47:35 -03:00
parent 403f5512ff
commit 5df240f767
No known key found for this signature in database
GPG Key ID: 6A16FD38AFC89CC8
7 changed files with 98 additions and 171 deletions

View File

@ -18345,7 +18345,7 @@ parameters:
Use dependency injection instead\.$#
'''
identifier: staticMethod.deprecated
count: 33
count: 32
path: tests/unit/InsertEditTest.php
-

View File

@ -10948,7 +10948,6 @@
<code><![CDATA[Config::getInstance()]]></code>
<code><![CDATA[Config::getInstance()]]></code>
<code><![CDATA[Config::getInstance()]]></code>
<code><![CDATA[Config::getInstance()]]></code>
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
</DeprecatedMethod>
@ -10984,9 +10983,6 @@
<code><![CDATA[$result]]></code>
<code><![CDATA[$result]]></code>
<code><![CDATA[$result]]></code>
<code><![CDATA[$result]]></code>
<code><![CDATA[$result]]></code>
<code><![CDATA[$result]]></code>
</MixedAssignment>
<PossiblyInvalidCast>
<code><![CDATA[$value]]></code>

View File

@ -105,7 +105,10 @@
{% else %}
{{ backup_field|raw }}
<input type="hidden" name="fields_type[multi_edit][{{ row_id }}][{{ column.md5 }}]" value="hex">
{{ input_field_html|raw }}
<input type="text" name="fields[multi_edit][{{ row_id }}][{{ column.md5 }}]"
value="{{ special_chars }}" size="{{ hex_input_size }}" data-type="HEX" class="textfield"
onchange="return verificationsAfterFieldChange('{{ column.md5|escape('js') }}', '{{ row_id|escape('js') }}', '{{ column.trueType }}')"
id="field_{{ id_index }}_3">
{% endif %}
{% if is_upload and column.isBlob %}
<br>

View File

@ -1,15 +1,24 @@
{{ backup_field|raw }}
{% if is_textarea %}
{{- html_field|raw -}}
{{- textarea_html|raw -}}
{% else %}
{{- html_field|raw -}}
<input type="text" name="fields{{ column_name_appendix }}"
value="{{ input.value }}" size="{{ input.size }}"
{%- if input.is_char %} data-maxlength="{{ input.size }}"{% endif %}
{%- if input.is_integer %} min="{{ input.min }}" max="{{ input.max }}"{% endif %}
data-type="{{ input.is_integer ? 'INT' : input.data_type }}"
class="textfield{{ true_type == 'date' ? ' datefield' }}{{ true_type == 'time' ? ' timefield' }}{{ true_type == 'datetime' or true_type == 'timestamp' ? ' datetimefield' }}"
onchange="{{ input.on_change_clause|e('html_attr') }}"
tabindex="{{ input.field_index }}"{{ input.is_integer ? ' inputmode="numeric"' }}
id="field_{{ input.field_index }}_3">
{%- if extra matches '/(VIRTUAL|PERSISTENT|GENERATED)/' and 'DEFAULT_GENERATED' not in extra -%}
<input type="hidden" name="virtual{{ columnNameAppendix }}" value="1">
<input type="hidden" name="virtual{{ column_name_appendix }}" value="1">
{%- endif -%}
{%- if extra == 'auto_increment' -%}
<input type="hidden" name="auto_increment{{ columnNameAppendix }}" value="1">
<input type="hidden" name="auto_increment{{ column_name_appendix }}" value="1">
{%- endif -%}
{%- if trueType == 'bit' or trueType == 'uuid' or trueType == 'timestamp' or trueType == 'datetime' or trueType == 'date' -%}
<input type="hidden" name="fields_type{{ columnNameAppendix }}" value="{{ trueType }}">
{%- if true_type == 'bit' or true_type == 'uuid' or true_type == 'timestamp' or true_type == 'datetime' or true_type == 'date' -%}
<input type="hidden" name="fields_type{{ column_name_appendix }}" value="{{ true_type }}">
{%- endif -%}
{% endif %}

View File

@ -382,60 +382,28 @@ class InsertEdit
. '</textarea>';
}
/**
* Get HTML input type
*
* @param InsertEditColumn $column description of column in given table
* @param string $columnNameAppendix the name attribute
* @param string $specialChars special characters
* @param int $fieldsize html field size
* @param string $onChangeClause onchange clause for fields
* @param string $dataType the html5 data-* attribute type
*
* @return string an html snippet
*/
private function getHtmlInput(
InsertEditColumn $column,
string $columnNameAppendix,
string $specialChars,
int $fieldsize,
string $onChangeClause,
string $dataType,
): string {
$theClass = 'textfield';
if ($column->trueType === 'date') {
$theClass .= ' datefield';
} elseif ($column->trueType === 'time') {
$theClass .= ' timefield';
} elseif ($column->trueType === 'datetime' || $column->trueType === 'timestamp') {
$theClass .= ' datetimefield';
}
$inputMinMax = '';
/** @return object{isInteger: bool, minValue: string, maxValue: string} */
private function getIntegerRange(InsertEditColumn $column): object
{
$minValue = '';
$maxValue = '';
$isInteger = in_array($column->trueType, $this->dbi->types->getIntegerTypes(), true);
if ($isInteger) {
$extractedColumnspec = Util::extractColumnSpec($column->type);
$isUnsigned = $extractedColumnspec['unsigned'];
$extractedColumnSpec = Util::extractColumnSpec($column->type);
$isUnsigned = $extractedColumnSpec['unsigned'];
$minMaxValues = $this->dbi->types->getIntegerRange($column->trueType, ! $isUnsigned);
$inputMinMax = 'min="' . $minMaxValues[0] . '" '
. 'max="' . $minMaxValues[1] . '"';
$dataType = 'INT';
$minValue = $minMaxValues[0];
$maxValue = $minMaxValues[1];
}
// do not use the 'date' or 'time' types here; they have no effect on some
// browsers and create side effects (see bug #4218)
return '<input type="text"'
. ' name="fields' . $columnNameAppendix . '"'
. ' value="' . $specialChars . '" size="' . $fieldsize . '"'
. ($column->isChar
? ' data-maxlength="' . $fieldsize . '"'
: '')
. ($inputMinMax !== '' ? ' ' . $inputMinMax : '')
. ' data-type="' . $dataType . '"'
. ' class="' . $theClass . '" onchange="' . htmlspecialchars($onChangeClause, ENT_COMPAT) . '"'
. ' tabindex="' . $this->fieldIndex . '"'
. ($isInteger ? ' inputmode="numeric"' : '')
. ' id="field_' . $this->fieldIndex . '_3">';
return new class ($isInteger, $minValue, $maxValue) {
public function __construct(
public readonly bool $isInteger,
public readonly string $minValue,
public readonly string $maxValue,
) {
}
};
}
/**
@ -525,11 +493,13 @@ class InsertEdit
$dataType = $this->dbi->types->getTypeClass($column->trueType);
$fieldsize = $this->getColumnSize($column, $specInBrackets);
$input = [];
$textareaHtml = '';
$isTextareaRequired = $column->isChar
&& ($this->config->settings['CharEditing'] === 'textarea' || str_contains($data, "\n"));
if ($isTextareaRequired) {
$this->config->settings['CharEditing'] = $defaultCharEditing;
$htmlField = $this->getTextarea(
$textareaHtml = $this->getTextarea(
$column,
$backupField,
$columnNameAppendix,
@ -538,23 +508,28 @@ class InsertEdit
$dataType,
);
} else {
$htmlField = $this->getHtmlInput(
$column,
$columnNameAppendix,
$specialChars,
$fieldsize,
$onChangeClause,
$dataType->value,
);
$integerRange = $this->getIntegerRange($column);
$input = [
'value' => $specialChars,
'size' => $fieldsize,
'is_char' => $column->isChar,
'is_integer' => $integerRange->isInteger,
'min' => $integerRange->minValue,
'max' => $integerRange->maxValue,
'data_type' => $dataType->value,
'on_change_clause' => $onChangeClause,
'field_index' => $this->fieldIndex,
];
}
return $this->template->render('table/insert/value_column_for_other_datatype', [
'html_field' => $htmlField,
'input' => $input,
'textarea_html' => $textareaHtml,
'backup_field' => $backupField,
'is_textarea' => $isTextareaRequired,
'columnNameAppendix' => $columnNameAppendix,
'column_name_appendix' => $columnNameAppendix,
'extra' => $column->extra,
'trueType' => $column->trueType,
'true_type' => $column->trueType,
]);
}
@ -1655,7 +1630,7 @@ class InsertEdit
$blobValueUnit = '';
$maxUploadSize = 0;
$selectOptionForUpload = '';
$inputFieldHtml = '';
$hexInputSize = 0;
if ($transformedHtml === '') {
if ($foreignData->dispRow !== null) {
$foreignDropdown = $this->relation->foreignDropdown(
@ -1714,14 +1689,7 @@ class InsertEdit
! $isColumnProtectedBlob
&& ! ($column->isBlob || ($column->length > $this->config->settings['LimitChars']))
) {
$inputFieldHtml = $this->getHtmlInput(
$column,
$columnNameAppendix,
$specialChars,
min(max($column->length * 2, 4), $this->config->settings['LimitChars']),
$onChangeClause,
'HEX',
);
$hexInputSize = min(max($column->length * 2, 4), $this->config->settings['LimitChars']);
}
} else {
$columnValue = $this->getValueColumnForOtherDatatypes(
@ -1777,7 +1745,7 @@ class InsertEdit
'max_upload_size' => $maxUploadSize,
'select_option_for_upload' => $selectOptionForUpload,
'limit_chars' => $this->config->settings['LimitChars'],
'input_field_html' => $inputFieldHtml,
'hex_input_size' => $hexInputSize,
'field_title' => $this->getColumnTitle($column->field, $commentsMap),
]);
}

View File

@ -71,33 +71,41 @@ final class ChangeControllerTest extends AbstractTestCase
$actual = $response->getHTMLResult();
self::assertStringContainsString($pageSettings->getHTML(), $actual);
// phpcs:disable Generic.Files.LineLength.TooLong
self::assertStringContainsString(
'<input type="text" name="fields[multi_edit][0][b80bb7740288fda1f201890375a60c8f]" value="NULL"'
. ' size="4" min="-2147483648" max="2147483647" data-type="INT" class="textfield"'
. ' onchange="return'
. ' verificationsAfterFieldChange(&quot;b80bb7740288fda1f201890375a60c8f&quot;,'
. ' &quot;0&quot;,&quot;int(11)&quot;)"'
. ' tabindex="1" inputmode="numeric" id="field_1_3"><input type="hidden"'
. ' name="auto_increment[multi_edit][0][b80bb7740288fda1f201890375a60c8f]" value="1">',
<<<'HTML'
<input type="text" name="fields[multi_edit][0][b80bb7740288fda1f201890375a60c8f]"
value="NULL" size="4" min="-2147483648" max="2147483647" data-type="INT"
class="textfield"
onchange="return&#x20;verificationsAfterFieldChange&#x28;&quot;b80bb7740288fda1f201890375a60c8f&quot;,&#x20;&quot;0&quot;,&quot;int&#x28;11&#x29;&quot;&#x29;"
tabindex="1" inputmode="numeric"
id="field_1_3"><input type="hidden" name="auto_increment[multi_edit][0][b80bb7740288fda1f201890375a60c8f]" value="1">
HTML,
$actual,
);
self::assertStringContainsString(
'<input type="text" name="fields[multi_edit][0][b068931cc450442b63f5b3d276ea4297]" value="NULL" size="20"'
. ' data-maxlength="20" data-type="CHAR" class="textfield" onchange="return'
. ' verificationsAfterFieldChange(&quot;b068931cc450442b63f5b3d276ea4297&quot;,'
. ' &quot;0&quot;,&quot;varchar(20)&quot;)"'
. ' tabindex="2" id="field_2_3">',
<<<'HTML'
<input type="text" name="fields[multi_edit][0][b068931cc450442b63f5b3d276ea4297]"
value="NULL" size="20" data-maxlength="20" data-type="CHAR"
class="textfield"
onchange="return&#x20;verificationsAfterFieldChange&#x28;&quot;b068931cc450442b63f5b3d276ea4297&quot;,&#x20;&quot;0&quot;,&quot;varchar&#x28;20&#x29;&quot;&#x29;"
tabindex="2"
id="field_2_3">
HTML,
$actual,
);
self::assertStringContainsString(
'<input type="text" name="fields[multi_edit][0][a55dbdcc1a45ed90dbee68864d566b99]" value="NULL.000000"'
. ' size="4" data-type="DATE" class="textfield datetimefield" onchange="return'
. ' verificationsAfterFieldChange(&quot;a55dbdcc1a45ed90dbee68864d566b99&quot;,'
. ' &quot;0&quot;,&quot;datetime&quot;)"'
. ' tabindex="3" id="field_3_3"><input type="hidden"'
. ' name="fields_type[multi_edit][0][a55dbdcc1a45ed90dbee68864d566b99]" value="datetime">',
<<<'HTML'
<input type="text" name="fields[multi_edit][0][a55dbdcc1a45ed90dbee68864d566b99]"
value="NULL.000000" size="4" data-type="DATE"
class="textfield datetimefield"
onchange="return&#x20;verificationsAfterFieldChange&#x28;&quot;a55dbdcc1a45ed90dbee68864d566b99&quot;,&#x20;&quot;0&quot;,&quot;datetime&quot;&#x29;"
tabindex="3"
id="field_3_3"><input type="hidden" name="fields_type[multi_edit][0][a55dbdcc1a45ed90dbee68864d566b99]" value="datetime">
HTML,
$actual,
);
// phpcs:enable
self::assertStringContainsString(
'<th><a href="index.php?route=/table/change&lang=en" data-post="db=test_db&table=test_table'
. '&ShowFieldTypesInDataEditView=0&ShowFunctionFields=1'

View File

@ -586,70 +586,6 @@ class InsertEditTest extends AbstractTestCase
);
}
/**
* Test for getHtmlInput
*/
public function testGetHTMLinput(): void
{
Config::getInstance()->settings['ShowFunctionFields'] = true;
$column = new InsertEditColumn('f', 'date', false, 'PRI', null, '', -1, false, false, false, false);
(new ReflectionProperty(InsertEdit::class, 'fieldIndex'))->setValue($this->insertEdit, 23);
$result = $this->callFunction(
$this->insertEdit,
InsertEdit::class,
'getHtmlInput',
[$column, 'a', 'b', 30, 'c', 'DATE'],
);
self::assertSame(
'<input type="text" name="fieldsa" value="b" size="30" data-type="DATE"'
. ' class="textfield datefield" onchange="c" tabindex="23" id="field_23_3">',
$result,
);
// case 2 datetime
$column = new InsertEditColumn('f', 'datetime', false, 'PRI', null, '', -1, false, false, false, false);
$result = $this->callFunction(
$this->insertEdit,
InsertEdit::class,
'getHtmlInput',
[$column, 'a', 'b', 30, 'c', 'DATE'],
);
self::assertSame(
'<input type="text" name="fieldsa" value="b" size="30" data-type="DATE"'
. ' class="textfield datetimefield" onchange="c" tabindex="23" id="field_23_3">',
$result,
);
// case 3 timestamp
$column = new InsertEditColumn('f', 'timestamp', false, 'PRI', null, '', -1, false, false, false, false);
$result = $this->callFunction(
$this->insertEdit,
InsertEdit::class,
'getHtmlInput',
[$column, 'a', 'b', 30, 'c', 'DATE'],
);
self::assertSame(
'<input type="text" name="fieldsa" value="b" size="30" data-type="DATE"'
. ' class="textfield datetimefield" onchange="c" tabindex="23" id="field_23_3">',
$result,
);
// case 4 int
$column = new InsertEditColumn('f', 'int(11)', false, 'PRI', null, '', -1, false, false, false, false);
$result = $this->callFunction(
$this->insertEdit,
InsertEdit::class,
'getHtmlInput',
[$column, 'a', 'b', 11, 'c', 'INT'],
);
self::assertSame(
'<input type="text" name="fieldsa" value="b" size="11" min="-2147483648" max="2147483647" data-type="INT"'
. ' class="textfield" onchange="c" tabindex="23" inputmode="numeric" id="field_23_3">',
$result,
);
}
/**
* Test for getMaxUploadSize
*/
@ -754,14 +690,20 @@ class InsertEditTest extends AbstractTestCase
],
);
// phpcs:disable Generic.Files.LineLength.TooLong
self::assertSame(
"a\n"
. '<input type="text" name="fieldsb" value="&lt;" size="20" data-type="'
. 'DATE" class="textfield datetimefield" onchange="c" tabindex="22" id="field_22_3"'
. '><input type="hidden" name="auto_incrementb" value="1">'
. '<input type="hidden" name="fields_typeb" value="timestamp">',
<<<'HTML'
a
<input type="text" name="fieldsb"
value="&amp;lt;" size="20" data-type="DATE"
class="textfield datetimefield"
onchange="c"
tabindex="22"
id="field_22_3"><input type="hidden" name="auto_incrementb" value="1"><input type="hidden" name="fields_typeb" value="timestamp">
HTML,
$result,
);
// phpcs:enable
// case 3: (else -> datetime)
$column = new InsertEditColumn(
@ -2515,7 +2457,8 @@ class InsertEditTest extends AbstractTestCase
self::assertStringContainsString('<option>UUID</option>', $actual);
self::assertStringContainsString('<span class="column_type" dir="ltr">datetime</span>', $actual);
self::assertStringContainsString(
'<input type="text" name="fields[multi_edit][0][d8578edf8458ce06fbc5bb76a58c5ca4]" value="12-10-14.000000"',
'<input type="text" name="fields[multi_edit][0][d8578edf8458ce06fbc5bb76a58c5ca4]"' . "\n"
. ' value="12-10-14.000000"',
$actual,
);