Skip to content

Commit

Permalink
Use named parameters to simplify the FieldHelper::control() function …
Browse files Browse the repository at this point in the history
…(CFM-269) (#92)
  • Loading branch information
arlen authored Jun 13, 2023
1 parent 907f70a commit 206d440
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 80 deletions.
85 changes: 44 additions & 41 deletions app/src/View/Helper/FieldHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,24 @@ public function banner(string $info): string {
* @param string $fieldName Form field
* @param array $options FormHelper control options
* @param string $labelText Label text (fieldName language key used by default)
* @param array $config Custom FormHelper configuration options
* @param string $ctrlCode Control code passed in from wrapper functions
* @param string $cssClass Start li css class passed in from wrapper functions
* @param string $beforeField Markup to be placed before/above the field
* @param string $afterField Markup to be placed after/below the field
* @param string $prefix Field prefix - used for API Usernames
* @param bool $labelIsTextOnly For fields that should not include <label> markup
* @return string HTML for control
*/

public function control(string $fieldName,
array $options=[],
string $labelText=null,
array $config=[],
string $ctrlCode=null,
string $cssClass=''): string {
array $options = [],
string $labelText = null,
string $ctrlCode = null,
string $cssClass = '',
string $beforeField = '',
string $afterField = '',
string $prefix = '',
bool $labelIsTextOnly = false): string {
$coptions = $options;
$coptions['label'] = false;
$coptions['readonly'] =
Expand All @@ -107,18 +113,11 @@ public function control(string $fieldName,
$fieldMap = $this->getView()->get('vv_field_types');
$fieldType = $fieldMap[$fieldName];

// Collect any supplemental markup and/or JavaScript to pass along for field construction.
// Suppliment is an array: supplement['beforeField' => 'string', 'afterField' => 'string'].
$fieldSupplement = !empty($config['supplement']) ? $config['supplement'] : [];

// For special fields that should not include <label> markup, allow fields to make the label text only
$labelIsTextOnly = !empty($config['labelIsTextOnly']) ? $config['labelIsTextOnly'] : false;

// Remove prefix from field value
if(isset($config['prefix'], $this->getView()->get('vv_obj')->$fieldName)) {
if(!empty($prefix) && !empty($this->getView()->get('vv_obj')->$fieldName)) {
$vv_obj = $this->getView()->get('vv_obj');
$fieldValue = $vv_obj->$fieldName;
$fieldValueTemp = str_replace($config['prefix'], '', $fieldValue);
$fieldValueTemp = str_replace($prefix, '', $fieldValue);
$vv_obj->$fieldName = $fieldValueTemp;
$this->getView()->set('vv_obj', $vv_obj);
}
Expand Down Expand Up @@ -174,9 +173,9 @@ public function control(string $fieldName,

return $this->startLine($liClass)
. $this->formNameDiv($fieldName, $labelText, $fieldType, $labelIsTextOnly)
. ( !empty($config['prefix']) ?
$this->formInfoWithPrefixDiv($controlCode, $config['prefix'], $fieldSupplement) :
$this->formInfoDiv($controlCode, $fieldSupplement) )
. ( !empty($prefix) ?
$this->formInfoWithPrefixDiv($controlCode, $prefix, $beforeField, $afterField) :
$this->formInfoDiv($controlCode, $beforeField, $afterField) )
. $this->endLine();
}

Expand Down Expand Up @@ -261,7 +260,7 @@ public function dateControl(string $fieldName, string $dateType=DateTypeEnum::St
$liClass = "fields-datepicker";

// Pass everything to the generic control() function
return $this->control($fieldName, $coptions, '', [], $controlCode, $liClass);
return $this->control($fieldName, $coptions, ctrlCode: $controlCode, cssClass: $liClass);
}

/**
Expand Down Expand Up @@ -293,18 +292,21 @@ protected function endLine(): string {
*
* @since COmanage Registry v5.0.0
* @param string $content Content HTML
* @param string $supplement Supplemental markup to place before and/or after the field control
* @param string $beforeField Markup to be placed before/above the field
* @param string $afterField Markup to be placed after/below the field
* @return string Form Info HTML
*/

protected function formInfoDiv(string $content, array $supplement): string {
protected function formInfoDiv(string $content,
string $beforeField = '',
string $afterField = ''): string {
$div = '<div class="field-info">' . PHP_EOL;
if(!empty($supplement['beforeField'])) {
$div .= $supplement['beforeField'] . PHP_EOL;
if(!empty($beforeField)) {
$div .= $beforeField . PHP_EOL;
}
$div .= $content . PHP_EOL;
if(!empty($supplement['afterField'])) {
$div .= $supplement['afterField'] . PHP_EOL;
if(!empty($afterField)) {
$div .= $afterField . PHP_EOL;
}
$div .= '</div>' . PHP_EOL;

Expand All @@ -315,25 +317,29 @@ protected function formInfoDiv(string $content, array $supplement): string {
* Generate a form info (control, value) box with a non editable prefix.
*
* @since COmanage Registry v5.0.0
* @param string $content Content HTML
* @param string $prefix Prefix value
* @param string $supplement Supplemental markup to place before and/or after the field control
* @return string Form Info HTML
* @param string $content Content HTML
* @param string $prefix Prefix value
* @param string $beforeField Markup to be placed before/above the field
* @param string $afterField Markup to be placed after/below the field
* @return string Form Info HTML
*/

protected function formInfoWithPrefixDiv(string $context, string $prefix, array $supplement): string {
protected function formInfoWithPrefixDiv(string $context,
string $prefix,
string $beforeField = '',
string $afterField = ''): string {
$div = '<div class="field-info">' . PHP_EOL;
if(!empty($supplement['beforeField'])) {
$div .= $supplement['beforeField'] . PHP_EOL;
if(!empty($beforeField)) {
$div .= $beforeField . PHP_EOL;
}
$div .= '<div class="input-group mb-3">' . PHP_EOL;
$div .= '<div class="input-group-prepend">' . PHP_EOL;
$div .= '<span class="input-group-text" id="basic-addon3">' . $prefix . '</span>';
$div .= '</div>' . PHP_EOL;
$div .= $context;
$div .= '</div>';
if(!empty($supplement['afterField'])) {
$div .= $supplement['afterField'] . PHP_EOL;
if(!empty($afterField)) {
$div .= $afterField . PHP_EOL;
}
$div .= '</div>';

Expand Down Expand Up @@ -467,15 +473,15 @@ protected function formNameDiv(string $fieldName, string $labelText=null, string
* @param string $status Status text
* @param array $link Link information, including 'url', 'label', 'class', 'confirm'
* @param string $labelText Label text (fieldName language key used by default)
* @param array $config Custom FormHelper configuration options
* @param boolean $labelIsTextOnly true if <label> wrapper should not be included in the markup
* @return string
*/

public function statusControl(string $fieldName,
string $status,
array $link=[],
string $labelText=null,
array $config=[]): string {
array $link=[],
string $labelText = null,
bool $labelIsTextOnly = false): string {
$linkHtml = $status;

if($link) {
Expand All @@ -500,9 +506,6 @@ public function statusControl(string $fieldName,
);
}
}

// For special fields that should not include <label> markup, allow fields to make the label text only
$labelIsTextOnly = !empty($config['labelIsTextOnly']) ? $config['labelIsTextOnly'] : false;

return $this->startLine()
. $this->formNameDiv($fieldName, $labelText, 'string', $labelIsTextOnly)
Expand Down
9 changes: 4 additions & 5 deletions app/templates/ApiUsers/fields.inc
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ if($vv_action == 'add' || $vv_action == 'edit') {
}

// AR-ApiUser-3 For namespacing purposes, API Users are named with a prefix consisting of the string "co_#.".
print $this->Field->control('username', [], null, ['prefix' => "co_" . $vv_cur_co->id .'.']);
print $this->Field->control('username', prefix: 'co_' . $vv_cur_co->id . '.');

// We link to the "Generate" button on edit only
$generateLink = [];
$config = [];
$labelIsTextOnly = false;

if(!empty($vv_obj->id)) {
$generateLink = [
Expand All @@ -50,14 +50,13 @@ if($vv_action == 'add' || $vv_action == 'edit') {
'confirm' => __d('operation', 'api.key.generate.confirm')
];

$config = ['labelIsTextOnly' => true];
$labelIsTextOnly = true;
}

print $this->Field->statusControl('api_key',
!empty($vv_obj->api_key) ? __d('enumeration', 'SetBooleanEnum.1') : __d('enumeration', 'SetBooleanEnum.0'),
$generateLink,
null,
$config);
labelIsTextOnly: $labelIsTextOnly);

print $this->Field->control('status', ['empty' => false]);

Expand Down
6 changes: 3 additions & 3 deletions app/templates/ExternalIdentityRoles/fields.inc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

// This view does not currently support read-only
if($vv_action == 'add' || $vv_action == 'edit') {
print $this->Field->control('affiliation_type_id', [], __d('field', 'affiliation'));
print $this->Field->control('affiliation_type_id', labelText: __d('field', 'affiliation'));

print $this->Field->control('status', ['empty' => false]);

Expand All @@ -40,9 +40,9 @@ if($vv_action == 'add' || $vv_action == 'edit') {
print $this->Field->control('department');

// XXX need to clarify this is an _identifier_ not an actual Person FK
print $this->Field->control('sponsor_identifier', [], __d('field', 'sponsor'));
print $this->Field->control('sponsor_identifier', labelText: __d('field', 'sponsor'));

print $this->Field->control('manager_identifier', [], __d('field', 'manager'));
print $this->Field->control('manager_identifier', labelText: __d('field', 'manager'));

print $this->Field->dateControl('valid_from');

Expand Down
57 changes: 27 additions & 30 deletions app/templates/Jobs/fields.inc
Original file line number Diff line number Diff line change
Expand Up @@ -38,36 +38,33 @@ if($vv_action == 'view') {
);
}

$fieldSupplement =
[
'beforeField' => '
<div class="field-suppliment">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="format-toggle">
<label class="form-check-label" for="format-toggle">' .
__d('field','format')
. '</label>
</div>
</div>
',
'afterField' => '
<script>
parametersValue = $("#parameters").val();
parametersJSON = JSON.parse(parametersValue);
$("#format-toggle").click(function() {
if($(this).is(":checked")) {
$("#parameters").val(JSON.stringify(parametersJSON, null, 4));
paramsHeight = Object.keys(parametersJSON).length + 6 + "em";
$("#parameters").css("height",paramsHeight);
} else {
$("#parameters").val(parametersValue);
$("#parameters").css("height","4em");
}
});
</script>
'
];
print $this->Field->control('parameters', [], null, ['supplement' => $fieldSupplement]);
$beforeField = '
<div class="field-suppliment">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="format-toggle">
<label class="form-check-label" for="format-toggle">' .
__d('field','format')
. '</label>
</div>
</div>
';
$afterField = '
<script>
parametersValue = $("#parameters").val();
parametersJSON = JSON.parse(parametersValue);
$("#format-toggle").click(function() {
if($(this).is(":checked")) {
$("#parameters").val(JSON.stringify(parametersJSON, null, 4));
paramsHeight = Object.keys(parametersJSON).length + 6 + "em";
$("#parameters").css("height",paramsHeight);
} else {
$("#parameters").val(parametersValue);
$("#parameters").css("height","4em");
}
});
</script>
';
print $this->Field->control('parameters', beforeField: $beforeField, afterField: $afterField);

print $this->Field->control('register_time');

Expand Down
2 changes: 1 addition & 1 deletion app/templates/PersonRoles/fields.inc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
if($vv_action == 'add' || $vv_action == 'edit') {
print $this->Field->control('cou_id');

print $this->Field->control('affiliation_type_id', [], __d('field', 'affiliation'));
print $this->Field->control('affiliation_type_id', labelText: __d('field', 'affiliation'));

print $this->Field->control('status', ['empty' => false]);

Expand Down

0 comments on commit 206d440

Please sign in to comment.