Commit 3ee37bec authored by Carsten  Rose's avatar Carsten Rose
Browse files

HelperFormElement.php: new function duplicateRetypeElements()

AbstractBuildForm.php: added new 'data-compare-element' for buildInput().
QuickFormQuery.php: Adjsut native FormElements in case some of them are 'retype=1'
parent e1277e62
......@@ -351,7 +351,6 @@ abstract class AbstractBuildForm {
$this->checkAutoFocus();
// Iterate over all FormElements
foreach ($this->feSpecNative as $fe) {
......@@ -667,6 +666,11 @@ abstract class AbstractBuildForm {
$attribute = Support::doAttribute('name', $htmlFormElementId);
$attribute .= Support::doAttribute('class', 'form-control');
if (isset($formElement[FE_RETYPE]) && $formElement[FE_RETYPE] == '1') {
$htmlFormElementIdRetype = str_replace($formElement[FE_NAME], $formElement[FE_NAME] . RETYPE_FE_NAME_EXTENSION, $htmlFormElementId);
$attribute .= Support::doAttribute('data-compare-element', $htmlFormElementIdRetype);
}
// Check for input type 'textarea'
$colsRows = explode(',', $formElement['size'], 2);
if (count($colsRows) === 2) {
......@@ -2274,8 +2278,8 @@ abstract class AbstractBuildForm {
$html = '<fieldset ' . $attribute . '>';
if ($formElement['label'] !== '') {
$html .= '<legend>' . $formElement['label'] . '</legend>';
if ($formElement[FE_LABEL] !== '') {
$html .= '<legend>' . $formElement[FE_LABEL] . '</legend>';
}
$html .= $this->wrap[WRAP_SETUP_IN_FIELDSET][WRAP_SETUP_START];
......
......@@ -436,7 +436,9 @@ class QuickFormQuery {
break;
}
// Expand F_FE_PARAMETER ('parameter')
HelperFormElement::explodeParameterInArrayElements($this->feSpecNative);
$this->feSpecNative = HelperFormElement::duplicateRetypeElements($this->feSpecNative);
return $formName;
}
......
......@@ -46,16 +46,16 @@ class HelperFormElement
public static function explodeParameter(array &$element)
{
// Something to explode?
if (isset($element['parameter']) && $element['parameter'] !== '') {
if (isset($element[F_FE_PARAMETER]) && $element[F_FE_PARAMETER] !== '') {
// Explode
$arr = KeyValueStringParser::parse($element['parameter'], "=", "\n");
$arr = KeyValueStringParser::parse($element[F_FE_PARAMETER], "=", "\n");
// Check if some of the exploded keys conflict with existing keys
$checkKeys = array_keys($arr);
foreach ($checkKeys AS $checkKey) {
if (isset($element[$checkKey])) {
$store = Store::getInstance();
$store->setVar(SYSTEM_FORM_ELEMENT, Logger::formatFormElementName($element), STORE_SYSTEM);
$store->setVar(SYSTEM_FORM_ELEMENT_COLUMN, 'parameter', STORE_SYSTEM);
$store->setVar(SYSTEM_FORM_ELEMENT_COLUMN, F_FE_PARAMETER, STORE_SYSTEM);
throw new UserFormException("Found reserved keyname '$checkKey'", ERROR_RESERVED_KEY_NAME);
}
}
......@@ -86,4 +86,47 @@ class HelperFormElement
return ('_' . $index . '_' . $field);
}
/**
* Check all FormElements if there are some with the attribute FE_RETYPE.
* If yes: duplicate such elements and update FE_NAME, FE_LABEL, FE_NOTE.
*
* @param array $elements
* @return array
*/
public static function duplicateRetypeElements(array $elements) {
$arr = array();
foreach ($elements as $fe) {
// adjust FE_RETYPE
if (isset($fe[FE_RETYPE]) && ($fe[FE_RETYPE] == '' || $fe[FE_RETYPE] == '1')) {
$fe[FE_RETYPE] = '1';
}
$arr[] = $fe;
if (isset($fe[FE_RETYPE]) && $fe[FE_RETYPE] == '1') {
// Create copy of FE, adjust name, label, note
$fe[FE_NAME] .= RETYPE_FE_NAME_EXTENSION;
if (isset($fe[FE_RETYPE_LABEL])) {
$fe[FE_LABEL] = $fe[FE_RETYPE_LABEL];
unset($fe[FE_RETYPE_LABEL]);
}
if (isset($fe[FE_RETYPE_NOTE])) {
$fe[FE_NOTE] = $fe[FE_RETYPE_NOTE];
unset($fe[FE_RETYPE_NOTE]);
}
unset($fe[FE_RETYPE]);
$arr[] = $fe;
}
}
return $arr;
}
}
\ No newline at end of file
......@@ -53,4 +53,44 @@ class HelperFormElementTest extends \PHPUnit_Framework_TestCase {
}
public function testduplicateRetypeElements() {
$a = [0 => [FE_NAME => 'hello', FE_LABEL => 'my label', FE_NOTE => 'my note']];
$b = HelperFormElement::duplicateRetypeElements($a);
$this->assertEquals($a, $b, "Both arrays should be equal");
$a = [0 => [FE_NAME => 'hello', FE_LABEL => 'my label', FE_NOTE => 'my note', FE_RETYPE => '']];
$b = HelperFormElement::duplicateRetypeElements($a);
$this->assertEquals(count($a) + 1, count($b), "Both arrays should be same count");
$this->assertEquals('1', $b[0][FE_RETYPE], "FE_RETYPE should be set to '1'");
$a = [0 => [FE_NAME => 'hello', FE_LABEL => 'my label', FE_NOTE => 'my note', FE_RETYPE => '0']];
$b = HelperFormElement::duplicateRetypeElements($a);
$this->assertEquals($a, $b, "Both arrays should be same count");
$this->assertEquals('0', $b[0][FE_RETYPE], "FE_RETYPE should be unchanged");
$a = [0 => [FE_NAME => 'hello', FE_LABEL => 'my label', FE_NOTE => 'my note', FE_RETYPE => '1']];
$b = HelperFormElement::duplicateRetypeElements($a);
$this->assertEquals(count($a) + 1, count($b), "Both arrays should be same count");
$this->assertEquals('1', $b[0][FE_RETYPE], "FE_RETYPE should be unchanged");
$this->assertEquals($b[0][FE_NAME] . RETYPE_FE_NAME_EXTENSION, $b[1][FE_NAME], "New name should be extended by: " . RETYPE_FE_NAME_EXTENSION);
$this->assertEquals($b[0][FE_LABEL], $b[1][FE_LABEL], "Both text should be equal");
$this->assertEquals($b[0][FE_NOTE], $b[1][FE_NOTE], "Both text should be equal");
$a = [0 => [FE_NAME => 'hello', FE_LABEL => 'my label', FE_NOTE => 'my note', FE_RETYPE => '1', FE_RETYPE_LABEL => '', FE_RETYPE_NOTE => '']];
$b = HelperFormElement::duplicateRetypeElements($a);
$this->assertEquals('', $b[1][FE_LABEL], "String should be empty");
$this->assertEquals('', $b[1][FE_NOTE], "String should be empty");
$a = [0 => [FE_NAME => 'hello', FE_LABEL => 'my label', FE_NOTE => 'my note', FE_RETYPE => '1', FE_RETYPE_LABEL => 'new label', FE_RETYPE_NOTE => 'new note']];
$b = HelperFormElement::duplicateRetypeElements($a);
$this->assertEquals('my label', $b[0][FE_LABEL], "String should be equal");
$this->assertEquals('my note', $b[0][FE_NOTE], "String should be equal");
$this->assertEquals('new label', $b[1][FE_LABEL], "String should be equal");
$this->assertEquals('new note', $b[1][FE_NOTE], "String should be equal");
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment