Skip to content

Commit

Permalink
Avoid nesting too deeply and return early
Browse files Browse the repository at this point in the history
  • Loading branch information
buttflattery committed Mar 7, 2020
1 parent 5693efb commit 5c938e1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 34 deletions.
39 changes: 12 additions & 27 deletions src/FormWizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -526,12 +526,12 @@ private function _setDefaults()
//force bootstrap version usage
if ($this->forceBsVersion) {
$this->_bsVersion = $this->forceBsVersion;
} else {
//is bs4 version
$isBs4 = class_exists(BS4ActiveForm::class);
$this->_bsVersion = !$isBs4 ? self::BS_3 : self::BS_4;
return;
}

//is bs4 version
$isBs4 = class_exists(BS4ActiveForm::class);
$this->_bsVersion = !$isBs4 ? self::BS_3 : self::BS_4;
}

/**
Expand Down Expand Up @@ -669,6 +669,7 @@ public function createFormWizard()
//start Body steps html
$htmlSteps = Html::beginTag('div');

//add preview step config if enabled
if ($this->enablePreview) {
$steps = array_merge(
$steps,
Expand Down Expand Up @@ -794,9 +795,7 @@ public function createBody($index, $formInfoText, $step)
$limitRows = ArrayHelper::getValue($step, 'limitRows', self::ROWS_UNLIMITED);

//check if tabular step
if ($isTabularStep) {
$this->_checkTabularConstraints($step['model']);
}
$isTabularStep && $this->_checkTabularConstraints($step['model']);

//step data
$dataStep = [
Expand All @@ -815,6 +814,7 @@ public function createBody($index, $formInfoText, $step)

//Add Row Buton to add fields dynamically
if ($isTabularStep) {

$html .= Html::button(
$this->iconAdd . ' Add',
[
Expand All @@ -823,7 +823,9 @@ public function createBody($index, $formInfoText, $step)
);
}

//check if not preview step and add fields container
if (!empty($step['model'])) {

//start field container tag <div class="fields_container">
$html .= Html::beginTag('div', ["class" => "fields_container", 'data' => ['rows-limit' => $limitRows]]);
//create step fields
Expand Down Expand Up @@ -900,25 +902,8 @@ function ($element) use ($model, $isTabularStep, $modelIndex) {
$this->sortFields($fieldConfig, $attributes, $step);

//is tabular step
if ($isTabularStep) {

//limit not exceeded
if ($limitRows === self::ROWS_UNLIMITED || $limitRows > $modelIndex) {
//start the row constainer
$htmlFields .= Html::beginTag('div', ['id' => 'row_' . $modelIndex, 'class' => 'tabular-row']);

//add the remove icon if edit mode and more than one rows
($modelIndex > 0) && $htmlFields .= Html::tag('i', '', ['class' => 'remove-row formwizard-x-ico', 'data' => ['rowid' => $modelIndex]]);
} else {
//terminate the loop for the tabular step if the limit exceeds
break;
}

//generate the html for the step
$htmlFields .= $this->_createTabularStepHtml($attributes, $modelIndex, $index, $model, $isTabularStep, $fieldConfig, $stepHeadings);

//close row div
$htmlFields .= Html::endTag('div');
if ($isTabularStep && !$this->addTabularRow($limitRows,$modelIndex,$htmlFields,$attributes,$index,$model,$isTabularStep,$fieldConfig,$stepHeadings)) {
break;
}
}

Expand Down Expand Up @@ -954,7 +939,7 @@ public function createCustomInput($model, $attribute, $fieldConfig)
list(
$options, $isMultiField, $fieldType, $widget, $template, $containerOptions, $inputOptions, $itemsList, $label, $labelOptions, $hintText
) = $this->_parseFieldConfig($fieldConfig);

//create field
$field = $this->createField(
$model,
Expand Down
33 changes: 26 additions & 7 deletions src/traits/WizardTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ private function _createField($fieldType, $fieldTypeOptions, $hintText = false)
];

//create field depending on the type of the value provided
// NOTE: change to array_key_exists
if (isset($defaultFieldTypes[$fieldType])) {

$field = $defaultFieldTypes[$fieldType]($fieldTypeOptions);
return (!$hintText) ? $field : $field->hint($hintText);
}
Expand Down Expand Up @@ -341,14 +341,14 @@ private function _createTabularStepHtml($attributes, $modelIndex, $index, $model
$this->_addRestoreEvents($customFieldConfig, $attributeId);

//add dependent input script if available
if (false !== $dependentInput) {
$this->_addDependentInputScript($dependentInput, $attributeId, $model, $modelIndex);
}
$dependentInput && $this->_addDependentInputScript($dependentInput, $attributeId, $model, $modelIndex);

} else {
//default field population
$htmlFields .= $this->createDefaultInput($model, $attributeName);
//go to next iteration, add after removing the else part of this if statement
continue;
}

//default field population
$htmlFields .= $this->createDefaultInput($model, $attributeName);
}

return $htmlFields;
Expand Down Expand Up @@ -625,4 +625,23 @@ function ($item) use ($disabledFields) {
}
);
}

protected function addTabularRow($limitRows,$modelIndex,&$htmlFields,$attributes,$index,$model,$isTabularStep,$fieldConfig,$stepHeadings){
//limit not exceeded
if ($limitRows === self::ROWS_UNLIMITED || $limitRows > $modelIndex) {
//start the row constainer
$htmlFields .= Html::beginTag('div', ['id' => 'row_' . $modelIndex, 'class' => 'tabular-row']);

//add the remove icon if edit mode and more than one rows
($modelIndex > 0) && $htmlFields .= Html::tag('i', '', ['class' => 'remove-row formwizard-x-ico', 'data' => ['rowid' => $modelIndex]]);

//generate the html for the step
$htmlFields .= $this->_createTabularStepHtml($attributes, $modelIndex, $index, $model, $isTabularStep, $fieldConfig, $stepHeadings);

//close row div
$htmlFields .= Html::endTag('div');
return true;
}
return false;
}
}

0 comments on commit 5c938e1

Please sign in to comment.