From 550bcea155570df7cd1794f577beb79629372976 Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Wed, 4 Mar 2020 15:09:37 +0100 Subject: [PATCH] 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 --- .../app/models/OPNsense/Base/BaseModel.php | 10 ++++++--- .../OPNsense/Base/FieldTypes/ArrayField.php | 22 +++++++++++++------ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/opnsense/mvc/app/models/OPNsense/Base/BaseModel.php b/src/opnsense/mvc/app/models/OPNsense/Base/BaseModel.php index 529b16f02..c9d9d3a78 100644 --- a/src/opnsense/mvc/app/models/OPNsense/Base/BaseModel.php +++ b/src/opnsense/mvc/app/models/OPNsense/Base/BaseModel.php @@ -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 diff --git a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/ArrayField.php b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/ArrayField.php index 51a4b776f..4dcd5cc6b 100644 --- a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/ArrayField.php +++ b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/ArrayField.php @@ -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);