From 4b16c7531e01e1d84fa0d4111a0b7c233475669c Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Sun, 1 Feb 2015 17:22:51 +0100 Subject: [PATCH] add post loading event to BaseField --- .../OPNsense/Sample/PageController.php | 19 ++++++---------- .../app/models/OPNsense/Base/BaseModel.php | 3 ++- .../OPNsense/Base/FieldTypes/ArrayField.php | 8 +++---- .../OPNsense/Base/FieldTypes/BaseField.php | 22 ++++++++++++++++++- .../mvc/app/views/OPNsense/Sample/page.volt | 4 ++-- 5 files changed, 35 insertions(+), 21 deletions(-) diff --git a/src/opnsense/mvc/app/controllers/OPNsense/Sample/PageController.php b/src/opnsense/mvc/app/controllers/OPNsense/Sample/PageController.php index 1409a054d..1b0ff1491 100644 --- a/src/opnsense/mvc/app/controllers/OPNsense/Sample/PageController.php +++ b/src/opnsense/mvc/app/controllers/OPNsense/Sample/PageController.php @@ -54,7 +54,7 @@ class PageController extends ControllerBase $node_found = $mdlSample->setNodeByReference(implode(".", $refparts), $value); // new node in the post which is not on disc, create a new child node // we need to create new nodes in memory for Array types - if ($node_found == null && strpos($key, 'childnodes_section_') !== false) { + if ($node_found == false && strpos($key, 'childnodes_section_') !== false) { // because all the array items are numbered in order, we know that any item not found // must be a new one. $mdlSample->childnodes->section->add(); @@ -140,17 +140,12 @@ class PageController extends ControllerBase $cnf = Config::getInstance(); $cnf->save(); } - - // redirect to index - $this->dispatcher->forward(array( - "action" => "index" - )); - - } else { - // Forward flow to the index action - $this->dispatcher->forward(array( - "action" => "index" - )); } + + // redirect to index + $this->dispatcher->forward(array( + "action" => "index" + )); + } } diff --git a/src/opnsense/mvc/app/models/OPNsense/Base/BaseModel.php b/src/opnsense/mvc/app/models/OPNsense/Base/BaseModel.php index 2056cb364..173c1b832 100644 --- a/src/opnsense/mvc/app/models/OPNsense/Base/BaseModel.php +++ b/src/opnsense/mvc/app/models/OPNsense/Base/BaseModel.php @@ -192,7 +192,8 @@ abstract class BaseModel // We've loaded the model template, now let's parse it into this object $this->parseXml($model_xml->items, $config_array, $this->internalData) ; - //print_r($this->internalData); + // trigger post loading event + $this->internalData->eventPostLoading(); // call Model initializer $this->init(); 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 47aad1fb7..fada8f1d7 100644 --- a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/ArrayField.php +++ b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/ArrayField.php @@ -56,9 +56,10 @@ class ArrayField extends BaseField } /** - * copy first node pointer as template node to make sure we always have a template to create new nodes from. + * Copy first node pointer as template node to make sure we always have a template to create new nodes from. + * If the first node is virtual (no source data), remove that from the list. */ - private function internalCopyStructure() + protected function actionPostLoadingEvent() { // always make sure there's a node to copy our structure from if ($this->internalTemplateNode ==null) { @@ -80,8 +81,6 @@ class ArrayField extends BaseField */ public function add() { - $this->internalCopyStructure(); - $new_record = array(); foreach ($this->internalTemplateNode->__items as $key => $node) { if ($node->isContainer()) { @@ -113,7 +112,6 @@ class ArrayField extends BaseField */ public function del($index) { - $this->internalCopyStructure(); if (array_key_exists((string)$index, $this->internalChildnodes)) { unset($this->internalChildnodes[$index]); } diff --git a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/BaseField.php b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/BaseField.php index ecd310aa4..934da9132 100644 --- a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/BaseField.php +++ b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/BaseField.php @@ -80,6 +80,25 @@ abstract class BaseField */ protected $internalIsVirtual = false ; + /** + * Template action for post loading actions, triggered by eventPostLoadingEvent. + * Overwrite this method for custom loading hooks. + */ + protected function actionPostLoadingEvent() + { + return; + } + + /** + * trigger post loading event. (executed by BaseModel) + */ + public function eventPostLoading() + { + foreach ($this->internalChildnodes as $nodeName => $node) { + $node->eventPostLoading(); + } + $this->actionPostLoadingEvent(); + } /** * @return bool returns if this a container type object (no data) @@ -242,7 +261,7 @@ abstract class BaseField } /** - * Recursive method to flatten tree structure for easy validation. + * Recursive method to flatten tree structure for easy validation, returns only leaf nodes. * @return array named array with field type nodes, using the internal reference. */ public function getFlatNodes() @@ -326,4 +345,5 @@ abstract class BaseField $parts = explode("\\", get_class($this)); return $parts[count($parts)-1]; } + } diff --git a/src/opnsense/mvc/app/views/OPNsense/Sample/page.volt b/src/opnsense/mvc/app/views/OPNsense/Sample/page.volt index 6acc3a257..8d35b0f4b 100644 --- a/src/opnsense/mvc/app/views/OPNsense/Sample/page.volt +++ b/src/opnsense/mvc/app/views/OPNsense/Sample/page.volt @@ -12,10 +12,10 @@ and the actual data from the Sample model, which is a combination of the data presented in the config.xml and the defaults set in the model xml.

- When errors occur while saving this form, they will be shown below: + When errors occur while saving this form, they will be shown below:
{% for error_message in error_messages %} - {{ error_message['field'] }} : {{ error_message['msg'] }}
+ {{ error_message['field'] }} : {{ error_message['msg'] }}
{% endfor %}