Commit f0ec037b authored by Ad Schellevis's avatar Ad Schellevis

work in progres proxy feature

parent 994c5b91
<?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\Proxy\Api;
use \OPNsense\Base\ApiControllerBase;
use \OPNsense\Proxy\General;
use \OPNsense\Core\Config;
/**
* Class SettingsController
* @package OPNsense\Proxy
*/
class SettingsController extends ApiControllerBase
{
/**
* retrieve general settings
* @return array
*/
public function getAction()
{
$result = array();
if ($this->request->isGet()) {
$mdlGeneral = new General();
$result['general'] = $mdlGeneral->getNodes();
}
return $result;
}
/**
*
* @return array
* @throws \Phalcon\Validation\Exception
*/
public function setAction()
{
$result = array("result"=>"failed");
if ($this->request->hasPost("general")) {
// load model and update with provided data
$mdlGeneral = new General();
$mdlGeneral->setNodes($this->request->getPost("general"));
// perform validation
$valMsgs = $mdlGeneral->performValidation();
foreach ($valMsgs as $field => $msg) {
if (!array_key_exists("validations", $result)) {
$result["validations"] = array();
}
$result["validations"]["general.".$msg->getField()] = $msg->getMessage();
}
// serialize model to config
if ($valMsgs->count() == 0) {
$mdlGeneral->serializeToConfig();
}
}
// save config if validated correctly
if (!array_key_exists("validations", $result)) {
$cnf = Config::getInstance();
$cnf->save();
$result["result"] = "saved";
}
return $result;
}
}
<div>
<ul class="nav nav-tabs" id="myTab">
<li><a data-toggle="tab" href="#sectionA">Section A</a></li>
<li><a data-toggle="tab" href="#sectionB">Section B</a></li>
<li class="dropdown">
<a data-toggle="dropdown" class="dropdown-toggle" href="#">Dropdown <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a data-toggle="tab" href="#dropdown1">Dropdown1</a></li>
<li><a data-toggle="tab" href="#dropdown2">Dropdown2</a></li>
</ul>
</li>
</ul>
<div class="tab-content">
<div id="sectionA" class="tab-pane fade in active">
<form id="frm_general">
{{ partial("layout_partials/form_input", ['id': 'general.port','label':'test']) }}
<div class="checkbox">
<label>
<input type="checkbox" id="general.enabled"> check
</label>
</div>
<button class="btn btn-default" id="restcall" type="button">do REST call!</button>
<br/>
</form>
</div>
<div id="sectionB" class="tab-pane fade">
</div>
<div id="dropdown1" class="tab-pane fade">
</div>
<div id="dropdown2" class="tab-pane fade">
API call result : <div id="msgid"></div>
</div>
</div>
</div>
<br/>
<button class="btn btn-default" id="save" type="button">save</button>
<script type="text/javascript">
$( "#restcall" ).click( function() {
$.ajax({
type: "POST",
url: "/api/proxy/service/status",
success: function(data){
$("#msgid").html( data.status );
},
data:{}
$( document ).ready(function() {
ajaxGet(url="/api/proxy/settings/get",sendData={},callback=function(data,status) {
if (status == "success") {
setFormData('frm_general',data);
}
});
});
$("#save").click(function(){
data = getFormData("frm_general");
ajaxCall(url="/api/proxy/settings/set",sendData=data,callback=function(data,status){
if ( status == "success") {
handleFormValidation("frm_general",data['validations']);
}
// TODO: implement error handling
//alert(status);
});
});
</script>
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