MVC: In order to extend the separate nodes of an arraytype field, we should be able to control the container creation. This commit adds newContainerField() which is responsible for spawning new container fields and attaching them to the current model.

Internally the object model looks like this:

ArrayField - [
	ContainerField - [
		TextField
		EmailField
		....
	]
	ContainerField - [
		{same types as first row}
	]
]

uuid's are attached to the ContainerField types by the ArrayField (or on load by BaseModel)

ref: https://github.com/opnsense/plugins/issues/1720
This commit is contained in:
Ad Schellevis 2020-03-04 15:09:37 +01:00
parent a8b2fb5e92
commit 550bcea155
2 changed files with 22 additions and 10 deletions

View File

@ -232,7 +232,9 @@ abstract class BaseModel
}
// iterate array items from config data
$child_node = new ContainerField($fieldObject->__reference . "." . $tagUUID, $tagName);
$child_node = $fieldObject->newContainerField(
$fieldObject->__reference . "." . $tagUUID, $tagName
);
$this->parseXml($xmlNode, $conf_section, $child_node);
if (!isset($conf_section->attributes()->uuid)) {
// if the node misses a uuid, copy it to this nodes attributes
@ -243,7 +245,9 @@ abstract class BaseModel
} else {
// There's no content in config.xml for this array node.
$tagUUID = $internal_data->generateUUID();
$child_node = new ContainerField($fieldObject->__reference . "." . $tagUUID, $tagName);
$child_node = $fieldObject->newContainerField(
$fieldObject->__reference . "." . $tagUUID, $tagName
);
$child_node->setInternalIsVirtual();
$this->parseXml($xmlNode, $config_section_data, $child_node);
$fieldObject->addChildNode($tagUUID, $child_node);
@ -270,7 +274,7 @@ abstract class BaseModel
$internalConfigHandle = Config::getInstance();
// init new root node, all details are linked to this
$this->internalData = new FieldTypes\ContainerField();
$this->internalData = new ContainerField();
// determine our caller's filename and try to find the model definition xml
// throw error on failure

View File

@ -60,6 +60,20 @@ class ArrayField extends BaseField
}
}
/**
* Construct new content container and attach to this items model
* @param $ref
* @param $tagname
* @return ContainerField
*/
public function newContainerField($ref, $tagname)
{
$container_node = new ContainerField($ref, $tagname);
$parentmodel = $this->getParentModel();
$container_node->setParentModel($parentmodel);
return $container_node;
}
/**
* retrieve read only template with defaults (copy of internal structure)
* @return null|BaseField template node
@ -87,13 +101,7 @@ class ArrayField extends BaseField
}
$nodeUUID = $this->generateUUID();
$container_node = new ContainerField(
$this->__reference . "." . $nodeUUID,
$this->internalXMLTagName
);
$parentmodel = $this->getParentModel();
$container_node->setParentModel($parentmodel);
$container_node = $this->newContainerField($this->__reference . "." . $nodeUUID, $this->internalXMLTagName);
foreach ($new_record as $key => $node) {
// initialize field with new internal id and defined default value
$node->setInternalReference($container_node->__reference . "." . $key);