From 42c01cb89ad902cd5edc1c93de16c9e87fc408df Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Fri, 29 May 2015 15:04:47 +0200 Subject: [PATCH] (mvc) add interface type model field --- .../Base/FieldTypes/InterfaceField.php | 155 ++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/InterfaceField.php diff --git a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/InterfaceField.php b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/InterfaceField.php new file mode 100644 index 000000000..afa2f5e20 --- /dev/null +++ b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/InterfaceField.php @@ -0,0 +1,155 @@ +internalCacheKey, self::$internalOptionList)) { + self::$internalOptionList[$this->internalCacheKey] = array(); + + $allInterfaces = array(); + $configObj = Config::getInstance()->object(); + // Iterate over all interfaces configuration and collect data + foreach ($configObj->interfaces->children() as $key => $value) { + $allInterfaces[$key] = $value; + } + + foreach ($allInterfaces as $key => $value) { + // use filters to determine relevance + $isMatched = true; + foreach ($this->internalFilters as $filterKey => $filterData) { + if (isset($value->$filterKey)) { + $fieldData = $value->$filterKey; + } else { + // not found, might be a boolean. + $fieldData = "0"; + } + + if (!preg_match('/'.$filterData.'/', $fieldData)) { + $isMatched = false; + } + } + if ($isMatched) { + if ($value->descr == '') { + self::$internalOptionList[$this->internalCacheKey][$key] = $key; + } else { + self::$internalOptionList[$this->internalCacheKey][$key] = $value->descr->__toString(); + } + } + } + } + } + + /** + * set filters to use (in regex) per field, all tags are combined and cached for the next object using the same filters + * @param $filters filters to use + */ + public function setFilters($filters) + { + if (is_array($filters)) { + $this->internalFilters = $filters; + $this->internalCacheKey = md5(serialize($this->internalFilters)); + } + } + + /** + * get valid options, descriptions and selected value + * @return array + */ + public function getNodeData() + { + $result = array (); + foreach (self::$internalOptionList[$this->internalCacheKey] as $optKey => $optValue) { + if ($optKey == $this->internalValue) { + $selected = 1; + } else { + $selected = 0; + } + $result[$optKey] = array("value"=>$optValue, "selected" => $selected); + } + + return $result; + } + + /** + * @return array returns Text/regex validator + */ + public function getValidators() + { + + if ($this->internalValidationMessage == null) { + $msg = "please specify a valid interface"; + } else { + $msg = $this->internalValidationMessage; + } + + if (($this->internalIsRequired == true || $this->internalValue != null)) { + return array(new InclusionIn(array('message' => $msg, + 'domain'=>array_keys(self::$internalOptionList[$this->internalCacheKey])))); + } else { + // empty field and not required, skip this validation. + return array(); + } + } +} +