Commit a3e5de62 authored by Ad Schellevis's avatar Ad Schellevis

(mvc) remove previous sample from codebase, the helloworld plugin covers more....

(mvc) remove previous sample from codebase, the helloworld plugin covers more. leave template sample in place, because it provides some additional options of the framework.
parent d11e9756
<?php
/**
* Copyright (C) 2015 Deciso B.V.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
namespace OPNsense\Sample\Api;
use \OPNsense\Base\ApiControllerBase;
/**
* Class IndexController
* @package OPNsense\Sample
*/
class IndexController extends ApiControllerBase
{
/**
* @return array
*/
public function indexAction()
{
if ($this->request->hasPost("message")) {
$message = $this->request->getPost("message");
} else {
$message = " " ;
}
return array("message" => "you send : ". $message);
}
}
<?php
/**
* Copyright (C) 2015 Deciso B.V.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
namespace OPNsense\Sample;
/**
* Class IndexController
* @package OPNsense\Sample
*/
class IndexController extends \OPNsense\Base\IndexController
{
public function indexAction()
{
$this->view->title = $this->request->getURI();
$this->view->pick('OPNsense/Sample/index');
}
}
<?php
/**
* Copyright (C) 2015 Deciso B.V.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
namespace OPNsense\Sample;
use Phalcon\Mvc\Controller;
use \OPNsense\Base\ControllerBase;
use \OPNsense\Core\Config;
/**
* Class PageController
* @package OPNsense\Sample
*/
class PageController extends ControllerBase
{
/**
* update model with data from our request
* @param BaseModel &$mdlSample model to update
*/
private function updateModelWithPost(&$mdlSample)
{
// 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 (see prefix in view)
$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 == 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();
$mdlSample->setNodeByReference(implode(".", $refparts), $value);
}
}
}
}
/**
* perform validation, forward to index on failure.
* @param $mdlSample Sample model
* @return bool validation status
*/
private function performFormValidation($mdlSample)
{
$validationOutput = $mdlSample->performValidation();
if ($validationOutput->count() > 0) {
// forward to index including errors
$error_msgs = array();
foreach ($validationOutput as $msg) {
$error_msgs[] = array("field" => $msg-> getField(), "msg" => $msg->getMessage());
}
// redirect to index
$this->dispatcher->forward(array(
"action" => "index",
"params" => array($error_msgs)
));
return false;
}
return true;
}
/**
* controller for sample index page, defaults to http://<host>/sample/page
* @param array $error_msg error messages
*/
public function indexAction($error_msg = array())
{
// load model and send to view, this model is automatically filled with data from the config.xml
$mdlSample = new Sample();
$this->view->sample = $mdlSample;
// got forwarded with errors, load form data from post and update on our model
if ($this->request->isPost() == true && count($error_msg) >0) {
$this->updateModelWithPost($mdlSample);
}
// send error messages to view
$this->view->error_messages = $error_msg;
// set title and pick a template
$this->view->title = "test page";
$this->view->pick('OPNsense/Sample/page');
}
/**
* Example save action
* @throws \Phalcon\Validation\Exception
*/
public function saveAction()
{
// save action should be a post
if ($this->request->isPost() == true) {
// create model(s)
$mdlSample = new Sample();
// update model with request data
$this->updateModelWithPost($mdlSample);
if ($this->request->getPost("form_action") == "add") {
// implement addRow, append new model row and serialize to config if form is valid
$mdlSample->childnodes->section->add();
if ($this->performFormValidation($mdlSample) == false) {
return false;
}
$mdlSample->serializeToConfig();
} elseif ($this->request->getPost("form_action") == "save") {
// implement save, possible removing
if ($this->request->hasPost("delete")) {
// delete selected Rows, cannot combine with add because of the index numbering
foreach ($this->request->getPost("delete") as $node_ref => $option_key) {
$refparts = explode(".", $node_ref);
$delete_key = array_pop($refparts);
$parentNode = $mdlSample->getNodeByReference(implode(".", $refparts));
if ($parentNode != null) {
$parentNode->del($delete_key);
}
}
}
// form validation
if ($this->performFormValidation($mdlSample) == false) {
return false;
}
// save data to config
$mdlSample->serializeToConfig();
$cnf = Config::getInstance();
$cnf->save();
}
}
// redirect to index
$this->dispatcher->forward(array(
"action" => "index"
));
return true;
}
}
<?php
/**
* Copyright (C) 2015 Deciso B.V.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
namespace OPNsense\Sample;
use OPNsense\Base\BaseModel;
class Sample extends BaseModel
{
}
<model>
<mount>//test/sample</mount>
<description>
Sample model.
The mountpoint is a reference to the config.xml file and this part (ROOT/test/sample)
should only be used by this model.
By defining full sections per model we should avoid clashes and keep serializing data back.
</description>
<items>
<tag1 type="TextField">
<Mask>/^([A-Z,1-9]){0,10}$/</Mask>
<ValidationMessage>test</ValidationMessage>
</tag1>
<optionTag type="OptionField">
<default>A</default>
<OptionValues>
<A>description 1</A>
<B>description 2</B>
</OptionValues>
</optionTag>
<tagX>
<tag1 type="EmailField">
<Default>test1234</Default>
<ValidationMessage>you should input a valid email address</ValidationMessage>
</tag1>
<tag2>
<tagZ type="TextField"/>
</tag2>
</tagX>
<childnodes>
<section type="ArrayField">
<node1 type="TextField"/>
<node2 type="TextField"/>
</section>
</childnodes>
<childnodes1>
<section1 type="ArrayField">
<nodeX type="TextField"></nodeX>
<nodeZ type="TextField"></nodeZ>
</section1>
</childnodes1>
</items>
</model>
<h1> OPNsense sample module </h1>
A simple input form for the "sample" model can be found <a href="page/">here </a><br/>
<hr/>
To perform a call to the api, press this button : <br/>
fill in a message : <input type="text" value="" id="msg"> </br>
<button class="btn btn-default" id="restcall" type="button">do REST call!</button>
<br/>
API call result : <div id="msgid"></div>
<script type="text/javascript">
$( "#restcall" ).click( function() {
$.ajax({
type: "POST",
url: "/api/sample/",
success: function(data){
$("#msgid").html( data.message );
},
data:{message:$("#msg").val()}
});
});
</script>
<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;border-color:#aabcfe;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:0px;overflow:hidden;word-break:normal;border-color:#aabcfe;color:#669;background-color:#e8edff;border-top-width:1px;border-bottom-width:1px;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:0px;overflow:hidden;word-break:normal;border-color:#aabcfe;color:#039;background-color:#b9c9fe;border-top-width:1px;border-bottom-width:1px;}
.tg .tg-vn4c{background-color:#D2E4FC}
</style>
<div style="width:600px">
This a sample page for the OPNsense MVC framework. This page demonstrates how to create a model (OPNsense\Sample\Sample.xml) and bind this to data in our config.xml.
<br/><br/>
The index controller (controllers\OPNsense\Sample\PageController) binds some data to this form like the title ({{title|default("'no title set'") }}).
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.
<br/><br/>
When errors occur while saving this form, they will be shown below: <br/>
{% for error_message in error_messages %}
<i style="color:red"> {{ error_message['field'] }} : {{ error_message['msg'] }} </i> <br/>
{% endfor %}
<br/><br/>
Edit the data
</div>
<form action="save" method="post">
<input type="hidden" name="{{ csrf_tokenKey }}" value="{{ csrf_token }}"/>
<table class="tg">
<tr>
<td>{{ lang._('item') }} </td>
<td>{{ lang._('internal reference') }}</td>
<td>{{ lang._('input') }}</td>
</tr>
<tr>
<td>tag1 </td>
<td>{{sample.tag1.__reference}}</td>
<td>
{# for demonstration purposes, use a partial for this field #}
{{ partial("layout_partials/sample_input_field", ['field_type': 'text','field_content':sample.tag1,'field_name':sample.tag1.__reference,'field_name_prefix':'sample.']) }}
</td>
</tr>
<tr>
<td>tagX/tag1 </td>
<td>{{sample.tagX.tag1.__reference}}</td>
<td><input type="text" name="sample.{{sample.tagX.tag1.__reference}}" value="{{sample.tagX.tag1}}"><br><br></td>
</tr>
<tr> <td colspan=3>detail items within sample model</td> </tr>
{# traverse details #}
{% for section_item in sample.childnodes.section.__items %}
<tr> <td colspan=3>##ROW## delete node {{section_item.__reference}} <input type="checkbox" name="delete[{{section_item.__reference}}]" value="1"> </td> </tr>
<tr>
<td>childnodes/section/[]/node1</td>
<td>{{section_item.node1.__reference}}</td>
<td><input type="text" name="sample.{{section_item.node1.__reference}}" value="{{section_item.node1}}"></td>
</tr>
<tr>
<td>childnodes/section/[]/node2</td>
<td>{{section_item.node2.__reference}}</td>
<td><input type="text" name="sample.{{section_item.node2.__reference}}" value="{{section_item.node2}}"></td>
</tr>
{% endfor %}
<tr>
<td></td>
<td></td>
<td><input type="submit" value="add" name="form_action" ></td>
</tr>
<tr> <td colspan=3> </td> </tr>
<tr> <td colspan=3><input type="submit" value="save" name="form_action" > </td> </tr>
</table>
</form>
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