Commit 249de9df authored by Ad Schellevis's avatar Ad Schellevis

work in progress OPNsense frontend mvc framework

parent 1dc13c29
......@@ -43,4 +43,22 @@ class ControllerBase extends Controller
{
$this->view->setTemplateBefore('default');
}
/**
* @param $dispatcher
*/
public function beforeExecuteRoute($dispatcher)
{
// Execute before every found action
// TODO: implement default behavior
}
/**
* @param $dispatcher
*/
public function afterExecuteRoute($dispatcher)
{
// Executed after every found action
// TODO: implement default behavior
}
}
......@@ -30,6 +30,7 @@ namespace OPNsense\Sample;
use Phalcon\Mvc\Controller;
use \OPNsense\Base\ControllerBase;
use \OPNsense\Core\Config;
/**
* Class PageController
......@@ -37,12 +38,52 @@ use \OPNsense\Base\ControllerBase;
*/
class PageController extends ControllerBase
{
/**
* controller for sample index page, defaults to http://<host>/sample/page
*/
public function indexAction()
{
$this->view->title = "XXX";
// load model and send to view, this model is automatically filled with data from the config.xml
$mdlSample = new Sample();
$this->view->sample = $mdlSample;
// set title and pick a template
$this->view->title = "test page";
$this->view->pick('OPNsense/Sample/page');
}
public function saveAction()
{
// save action should be a post, redirect to index
if ($this->request->isPost() == true) {
// create model(s)
$mdlSample = new Sample();
// Access POST data and save parts to model
foreach ($this->request->getPost() as $key => $value) {
$refparts = explode("_", $key);
if (array_shift($refparts) == "sample") {
// this post item belongs to the Sample model (prefixed in view)
$mdlSample->setNodeByReference(implode(".", $refparts), $value);
}
}
$mdlSample->serializeToConfig();
$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"
));
}
}
public function showAction($postId)
{
$sample = new Sample();
......
......@@ -338,4 +338,44 @@ abstract class BaseModel
}
$this->internalSerializeToConfig();
}
/**
* find node by reference starting at the root node
* @param BaseField $node source node
* @param string $reference node reference (point separated "node.subnode.subsubnode")
* @return BaseField|null field node by reference (or null if not found)
*/
public function getNodeByReference($reference)
{
$parts = explode(".", $reference);
$node = $this->internalData;
while (count($parts)>0) {
$childName = array_shift($parts);
if (array_key_exists($childName, $node->getChildren())) {
$node = $node->getChildren()[$childName];
} else {
return null;
}
}
return $node;
}
/**
* set node value by name (if reference exists)
* @param string $reference node reference (point separated "node.subnode.subsubnode")
* @param string $value
* @return bool value saved yes/no
*/
public function setNodeByReference($reference, $value)
{
$node =$this->getNodeByReference($reference);
if ($node != null) {
$node->setValue($value);
return true;
} else {
return false;
}
}
}
......@@ -56,7 +56,7 @@ class ArrayField extends BaseField
}
/**
*
* copy first node pointer as template node to make sure we always have a template to create new nodes from.
*/
private function internalCopyStructure()
{
......
......@@ -306,4 +306,5 @@ abstract class BaseField
$this->internalIsRequired = false;
}
}
}
test sample -> page
<div>
<i>this is a sample page for the OPNsense mvc frontend framework</i>
</div>
{{ sample.tag1 }}
<form action="save" method="post">
<input type="text" name="sample.{{sample.tag1.__reference}}" value="{{sample.tag1}}"><br>
<input type="text" name="sample.{{sample.tagX.tag1.__reference}}" value="{{sample.tagX.tag1}}"><br>
<input type="submit" value="Submit">
</form>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment