_dfRelations = array( * array( * '_element' => 'moreInfoType', * 'node' => '[name="moreInfoType"]:checked', * 'group' => '[name="moreInfoType"]', * 'event' => 'change', * 'rules' => array( * array( * 'value' => 'none', * '_elements' => array() * ), * array( * 'value' => 'url', * '_elements' => array('moreInfoUrl') * ), * array( * 'value' => 'page', * '_elements' => array('moreInfoPage') * ), * ) * ), * ); * @var array Node relations that will be converted to format compatible with jQuery.dynamicForm() */ protected $_dfRelations = array(); /** * @var array List on elements names that will be preserved in method $this->_dfClearUnusedValues() */ protected $_dfPreserveFields = array(); /** * Clears values of hidden nodes (values of elements listed in $this->_dfPreserveFields will be preserved) * @param array $values * @throws \Exception */ public function dfClearUnusedValues(array &$values) { foreach ($this->_dfRelations as $relation) { if (!isset($relation['element'])) { continue; } $name = $relation['element']; if (!array_key_exists($name, $values)) { continue; } $value = $values[$name]; $available = array(); $unavailable = array(); foreach ($relation['rules'] as $rule) { if (isset($rule['_value'])) { $match = ($rule['_value'] instanceof \Closure) ? $rule['_value']($value) : $rule['_value'] == $value; } else { if (!is_string($rule['value']) && !is_numeric($rule['value'])) { throw new Exception('Wrong value type in relations config "' . $rule['value'] . '"'); } $match = ($rule['value'] == $value); } if ($match) { if (!empty($rule['_elements'])) { $available = array_merge($available, $rule['_elements']); } } else { if (!empty($rule['_elements'])) { $unavailable = array_merge($unavailable, $rule['_elements']); } } } if ($unavailable) { $unavailable = array_diff($unavailable, $available); $unavailable = array_diff($unavailable, $this->_dfPreserveFields); $values = array_merge($values, array_fill_keys($unavailable, null)); } } return; } /** * Converts relations config ready to use with jQuery.dynamicForm plugin * @return array */ public function dfConvertRelations() { $relations = array(); foreach ($this->_dfRelations as $relation) { foreach ($relation['rules'] as &$rule) { if (!empty($rule['_elements'])) { foreach ($rule['_elements'] as $name) { if (($id = $this->_dfGetElementLabelId($name))) { $rule['nodes'][] = '#' . $id; } if (($id = $this->_dfGetElementContainerId($name))) { $rule['nodes'][] = '#' . $id; } } } unset($rule['_elements'], $rule['_value']); } unset($relation['_element']); $relations[] = $relation; } return $relations; } /** * @param string $name element name * @return string element label id (e.g. foo-label) */ abstract protected function _dfGetElementLabelId($name); /** * @param string $name element name * @return string element container id (e.g. foo-element) */ abstract protected function _dfGetElementContainerId($name); }