Skip to content

Commit

Permalink
replaced isset with array_key_exists and rearranged parameters for th…
Browse files Browse the repository at this point in the history
…e tabular section
  • Loading branch information
buttflattery committed Mar 7, 2020
1 parent 5c938e1 commit 43b1343
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/FormWizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -843,14 +843,14 @@ public function createBody($index, $formInfoText, $step)
/**
* Creates the fields for the current step
*
* @param int $index index of the current step
* @param int $stepIndex index of the current step
* @param array $step config for the current step
* @param boolean $isTabularStep if the current step is tabular or not
* @param int $limitRows the rows limit for the tabular step
*
* @return HTML
*/
public function createStepFields($index, $step, $isTabularStep, $limitRows)
public function createStepFields($stepIndex, $step, $isTabularStep, $limitRows)
{

$htmlFields = '';
Expand Down Expand Up @@ -902,7 +902,7 @@ function ($element) use ($model, $isTabularStep, $modelIndex) {
$this->sortFields($fieldConfig, $attributes, $step);

//is tabular step
if ($isTabularStep && !$this->addTabularRow($limitRows,$modelIndex,$htmlFields,$attributes,$index,$model,$isTabularStep,$fieldConfig,$stepHeadings)) {
if ($isTabularStep && !$this->addTabularRow($model, $modelIndex, $stepIndex, $htmlFields, $fieldConfig, $attributes, $limitRows, $stepHeadings)) {
break;
}
}
Expand All @@ -917,7 +917,7 @@ function ($element) use ($model, $isTabularStep, $modelIndex) {
}

//copy the fields to the javascript array for validation
$this->_allFields[$index] = $fields;
$this->_allFields[$stepIndex] = $fields;

return $htmlFields;
}
Expand Down
39 changes: 26 additions & 13 deletions src/traits/WizardTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,19 @@ private function _checkTabularConstraints($models)
* Adds tabular events for the attribute
*
* @param array $attributeConfig attribute configurations passed
* @param boolean $isTabularStep boolean if current step is tabular
* @param int $modelIndex the index of the current model
* @param string $attributeId the id of the current field
* @param int $index the index of the current step
*
* @return null
*/
private function _addTabularEvents($attributeConfig, $isTabularStep, $modelIndex, $attributeId, $index)
private function _addTabularEvents($attributeConfig, $modelIndex, $attributeId, $index)
{
//get the tabular events for the field
$tabularEvents = ArrayHelper::getValue($attributeConfig, 'tabularEvents', false);

//check if tabular step and tabularEvents provided for field
if ($isTabularStep && is_array($tabularEvents) && $modelIndex == 0) {
if (is_array($tabularEvents) && $modelIndex == 0) {

//id of the form
$formId = $this->formOptions['id'];
Expand Down Expand Up @@ -139,7 +138,7 @@ private function _bindEvents($eventName, $eventCallBack, $formId, $index, $attri
},
];
//call the event array literals
isset($formEvents[$eventName]) && $formEvents[$eventName]($eventName, $formId, $index, $eventCallBack, $attributeId);
array_key_exists($eventName, $formEvents) && $formEvents[$eventName]($eventName, $formId, $index, $eventCallBack, $attributeId);
}

/**
Expand Down Expand Up @@ -268,8 +267,7 @@ 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])) {
if (array_key_exists($fieldType, $defaultFieldTypes)) {
$field = $defaultFieldTypes[$fieldType]($fieldTypeOptions);
return (!$hintText) ? $field : $field->hint($hintText);
}
Expand All @@ -288,7 +286,7 @@ private function _createField($fieldType, $fieldTypeOptions, $hintText = false)
*
* @return mixed
*/
private function _createTabularStepHtml($attributes, $modelIndex, $index, $model, $isTabularStep, $fieldConfig, $stepHeadings)
private function _createTabularStepHtml($attributes, $modelIndex, $index, $model, $fieldConfig, $stepHeadings)
{
$htmlFields = '';

Expand Down Expand Up @@ -335,18 +333,18 @@ private function _createTabularStepHtml($attributes, $modelIndex, $index, $model
$attributeId = Html::getInputId($model, $attributeName);

//add tabular events
$this->_addTabularEvents($customFieldConfig, $isTabularStep, $modelIndex, $attributeId, $index);
$this->_addTabularEvents($customFieldConfig, $modelIndex, $attributeId, $index);

//add the restore events
$this->_addRestoreEvents($customFieldConfig, $attributeId);

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

//go to next iteration, add after removing the else part of this if statement
//go to next iteration, add after removing the else part of this if statement
continue;
}

//default field population
$htmlFields .= $this->createDefaultInput($model, $attributeName);
}
Expand Down Expand Up @@ -626,7 +624,22 @@ function ($item) use ($disabledFields) {
);
}

protected function addTabularRow($limitRows,$modelIndex,&$htmlFields,$attributes,$index,$model,$isTabularStep,$fieldConfig,$stepHeadings){
/**
* Adds a tabular row in the tabular step
*
* @param object $model the model object
* @param integer $modelIndex the model index for the tabular step model
* @param integer $stepIndex the current step index
* @param string $htmlFields the html for the fields
* @param array $fieldConfig the field configurations
* @param array $attributes the list of the attributes in the current model
* @param integer $limitRows the rows limit if set
* @param array $stepHeadings the stepheadings configurations
*
* @return boolean
*/
protected function addTabularRow($model, $modelIndex, $stepIndex, &$htmlFields, $fieldConfig, $attributes, $limitRows, $stepHeadings)
{
//limit not exceeded
if ($limitRows === self::ROWS_UNLIMITED || $limitRows > $modelIndex) {
//start the row constainer
Expand All @@ -636,12 +649,12 @@ protected function addTabularRow($limitRows,$modelIndex,&$htmlFields,$attributes
($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);
$htmlFields .= $this->_createTabularStepHtml($attributes, $modelIndex, $stepIndex, $model, $fieldConfig, $stepHeadings);

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

0 comments on commit 43b1343

Please sign in to comment.