From f26e8bb7149fcf577bbf77243ca713c6af2e41ba Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Wed, 27 May 2015 17:30:05 +0200 Subject: [PATCH] (mvc model) add ModelRelationField type, creates a optionlist populated by array type items from other entities. Example definition from trafficshaper model, relating a pipe to a rule. OPNsense.TrafficShaper.TrafficShaper pipes.pipe description --- .../Base/FieldTypes/ModelRelationField.php | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/ModelRelationField.php diff --git a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/ModelRelationField.php b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/ModelRelationField.php new file mode 100644 index 000000000..cd5b3419b --- /dev/null +++ b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/ModelRelationField.php @@ -0,0 +1,129 @@ +getNodeByReference($modelData['items'])->__items as $node) { + $displayKey = $modelData['display']; + if (array_key_exists("uuid", $node->getAttributes()) && $node->$displayKey != null) { + $uuid = $node->getAttributes()['uuid']; + self::$internalOptionList[$uuid] = $node->$displayKey->__toString(); + } + } + } + unset($modelObj); + } + } + } + } + } + + /** + * get valid options, descriptions and selected value + * @return array + */ + public function getNodeData() + { + $result = array (); + if (is_array(self::$internalOptionList)) { + foreach (self::$internalOptionList as $optKey => $optValue) { + if ($optKey == $this->internalValue && $this->internalValue != null) { + $selected = 1; + } else { + $selected = 0; + } + $result[$optKey] = array("value"=>$optValue, "selected" => $selected); + } + } + + return $result; + } + + /** + * @return array returns Text/regex validator + */ + public function getValidators() + { + // build validation mask + $validationMask = '('; + $countid = 0 ; + foreach (self::$internalOptionList as $key => $value) { + if ($countid > 0) { + $validationMask .= '|'; + } + $validationMask .= $key ; + $countid++; + } + $validationMask .= ')'; + + if ($this->internalValidationMessage == null) { + $msg = "option not in list" ; + } else { + $msg = $this->internalValidationMessage; + } + if (($this->internalIsRequired == true || $this->internalValue != null) && $validationMask != null) { + return array(new Regex(array('message' => $msg,'pattern'=>trim($validationMask)))); + } else { + // empty field and not required, skip this validation. + return array(); + } + } +}