Tidy up logic from #3465, move helper functions out

This commit is contained in:
Samuel Georges 2018-04-04 14:35:29 +10:00
parent cc082d0510
commit 97e156d43d

View File

@ -1,5 +1,6 @@
<?php namespace Backend\Classes; <?php namespace Backend\Classes;
use Str;
use Html; use Html;
use October\Rain\Database\Model; use October\Rain\Database\Model;
use October\Rain\Html\Helper as HtmlHelper; use October\Rain\Html\Helper as HtmlHelper;
@ -465,12 +466,7 @@ class FormField
$triggerAction = array_get($this->trigger, 'action'); $triggerAction = array_get($this->trigger, 'action');
$triggerField = array_get($this->trigger, 'field'); $triggerField = array_get($this->trigger, 'field');
$triggerCondition = array_get($this->trigger, 'condition'); $triggerCondition = array_get($this->trigger, 'condition');
$triggerForm = $this->arrayName;
// Check for parent field reference for the trigger condition
$triggerFieldParentLevel = $this->getNumberOfPrecedingSymbols($triggerField, self::HIERARCHY_UP);
if ($triggerFieldParentLevel > 0) {
$triggerField = substr($triggerField, $triggerFieldParentLevel);
}
// Apply these to container // Apply these to container
if (in_array($triggerAction, ['hide', 'show']) && $position != 'container') { if (in_array($triggerAction, ['hide', 'show']) && $position != 'container') {
@ -483,11 +479,14 @@ class FormField
} }
// Reduce the field reference for the trigger condition field // Reduce the field reference for the trigger condition field
if ($this->arrayName && $triggerFieldParentLevel > 0) { $triggerFieldParentLevel = Str::getPrecedingSymbols($triggerField, self::HIERARCHY_UP);
$parentFormName = $this->reduceFieldNameHierarchy($this->arrayName, $triggerFieldParentLevel); if ($triggerFieldParentLevel > 0) {
$fullTriggerField = $parentFormName.'['.implode('][', HtmlHelper::nameToArray($triggerField)).']'; $triggerField = substr($triggerField, $triggerFieldParentLevel);
} elseif ($this->arrayName) { $triggerForm = HtmlHelper::reduceFieldNameHierarchy($triggerForm, $triggerFieldParentLevel);
$fullTriggerField = $this->arrayName.'['.implode('][', HtmlHelper::nameToArray($triggerField)).']'; }
if ($this->arrayName) {
$fullTriggerField = $triggerForm.'['.implode('][', HtmlHelper::nameToArray($triggerField)).']';
} }
else { else {
$fullTriggerField = $triggerField; $fullTriggerField = $triggerField;
@ -501,43 +500,10 @@ class FormField
]; ];
$attributes = $attributes + $newAttributes; $attributes = $attributes + $newAttributes;
return $attributes; return $attributes;
} }
/**
* Reduces the field name hierarchy depth by $level levels.
* country[city][0][street][0] turns into country[city][0] when reduced by 1 level;
* country[city][0][street][0] turns into country when reduced by 2 levels;
* etc.
*
* @param string $fieldName
* @param int $level
* @return string
*/
protected function reduceFieldNameHierarchy($fieldName, $level) {
$parentFormName = HtmlHelper::nameToArray($fieldName);
$sliceLength = count($parentFormName) - $level * 2;
if ($sliceLength <= 1) {
return $parentFormName[0];
}
$parentFormName = array_slice($parentFormName, 0, $sliceLength);
$parentFormNameFirst = array_shift($parentFormName);
return $parentFormNameFirst.'['.implode('][', $parentFormName).']';
}
/**
* If $string begins with any number of consecutive symbols,
* returns the number, otherwise returns 0
*
* @param string $string
* @param string $symbol
* @return int
*/
protected function getNumberOfPrecedingSymbols($string, $symbol) {
return strlen($string) - strlen(ltrim($string, $symbol));
}
/** /**
* Adds attributes used specifically by the Input Preset API * Adds attributes used specifically by the Input Preset API
* @param array $attributes * @param array $attributes