devel/helloworld - upgrade to latest standards.

This commit is contained in:
Ad Schellevis 2024-09-03 14:30:12 +02:00
parent 0e03ffbd6f
commit 416faefa75
5 changed files with 18 additions and 118 deletions

View file

@ -46,13 +46,9 @@ class ServiceController extends ApiControllerBase
{
$status = "failed";
if ($this->request->isPost()) {
$backend = new Backend();
$bckresult = trim($backend->configdRun('template reload OPNsense/HelloWorld'));
if ($bckresult == "OK") {
$status = "ok";
}
$status = strtolower(trim((new Backend())->configdRun('template reload OPNsense/HelloWorld')));
}
return array("status" => $status);
return ["status" => $status];
}
/**
@ -61,13 +57,12 @@ class ServiceController extends ApiControllerBase
public function testAction()
{
if ($this->request->isPost()) {
$backend = new Backend();
$bckresult = json_decode(trim($backend->configdRun("helloworld test")), true);
$bckresult = json_decode(trim((new Backend())->configdRun("helloworld test")), true);
if ($bckresult !== null) {
// only return valid json type responses
return $bckresult;
}
}
return array("message" => "unable to run config action");
return ["message" => "unable to run config action"];
}
}

View file

@ -30,63 +30,14 @@
namespace OPNsense\HelloWorld\Api;
use OPNsense\Base\ApiControllerBase;
use OPNsense\HelloWorld\HelloWorld;
use OPNsense\Core\Config;
use OPNsense\Base\ApiMutableModelControllerBase;
/**
* Class SettingsController Handles settings related API actions for the HelloWorld module
* @package OPNsense\Helloworld
*/
class SettingsController extends ApiControllerBase
class SettingsController extends ApiMutableModelControllerBase
{
/**
* retrieve HelloWorld general settings
* @return array general settings
* @throws \OPNsense\Base\ModelException
* @throws \ReflectionException
*/
public function getAction()
{
// define list of configurable settings
$result = array();
if ($this->request->isGet()) {
$mdlHelloWorld = new HelloWorld();
$result['helloworld'] = $mdlHelloWorld->getNodes();
}
return $result;
}
/**
* update HelloWorld settings
* @return array status
* @throws \OPNsense\Base\ModelException
* @throws \ReflectionException
*/
public function setAction()
{
$result = array("result" => "failed");
if ($this->request->isPost()) {
// load model and update with provided data
$mdlHelloWorld = new HelloWorld();
$mdlHelloWorld->setNodes($this->request->getPost("helloworld"));
// perform validation
$valMsgs = $mdlHelloWorld->performValidation();
foreach ($valMsgs as $field => $msg) {
if (!array_key_exists("validations", $result)) {
$result["validations"] = array();
}
$result["validations"]["helloworld." . $msg->getField()] = $msg->getMessage();
}
// serialize model to config and save
if ($valMsgs->count() == 0) {
$mdlHelloWorld->serializeToConfig();
Config::getInstance()->save();
$result["result"] = "saved";
}
}
return $result;
}
protected static $internalModelClass = 'OPNsense\HelloWorld\HelloWorld';
protected static $internalModelName = 'helloworld';
}

View file

@ -1,43 +0,0 @@
<?php
/**
* Copyright (C) 2015-2019 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\HelloWorld\Api;
use OPNsense\Base\ApiMutableModelControllerBase;
/**
* a simplified settings controller for our HelloWorld app, uses our ApiMutableModelControllerBase type
* @package OPNsense\Helloworld
*/
class SimplifiedSettingsController extends ApiMutableModelControllerBase
{
protected static $internalModelName = 'helloworld';
protected static $internalModelClass = 'OPNsense\HelloWorld\HelloWorld';
}

View file

@ -28,14 +28,13 @@ POSSIBILITY OF SUCH DAMAGE.
<script>
$( document ).ready(function() {
var data_get_map = {'frm_GeneralSettings':"/api/helloworld/settings/get"};
mapDataToFormUI(data_get_map).done(function(data){
mapDataToFormUI({'frm_GeneralSettings':"/api/helloworld/settings/get"}).done(function(data){
// place actions to run after load, for example update form styles.
});
// link save button to API set action
$("#saveAct").click(function(){
saveFormToEndpoint(url="/api/helloworld/settings/set",formid='frm_GeneralSettings',callback_ok=function(){
saveFormToEndpoint("/api/helloworld/settings/set",'frm_GeneralSettings',function(){
// action to run after successful save, for example reconfigure service.
ajaxCall(url="/api/helloworld/service/reload", sendData={},callback=function(data,status) {
// action to run after reload
@ -43,14 +42,12 @@ POSSIBILITY OF SUCH DAMAGE.
});
});
$("#testAct").click(function(){
$("#responseMsg").removeClass("hidden");
ajaxCall(url="/api/helloworld/service/test", sendData={},callback=function(data,status) {
// action to run after reload
$("#responseMsg").html(data['message']);
});
// use a SimpleActionButton() to call /api/helloworld/service/test
$("#testAct").SimpleActionButton({
onAction: function(data) {
$("#responseMsg").removeClass("hidden").html(data['message']);
}
});
});
</script>
@ -63,6 +60,6 @@ POSSIBILITY OF SUCH DAMAGE.
</div>
<div class="col-md-12">
<button class="btn btn-primary" id="saveAct" type="button"><b>{{ lang._('Save') }}</b></button>
<button class="btn btn-primary" id="testAct" type="button"><b>{{ lang._('Test') }}</b></button>
<button class="btn btn-primary" id="saveAct" type="button"><b>{{ lang._('Save') }}</b></button>
<button class="btn btn-primary" id="testAct" data-endpoint="/api/helloworld/service/test" data-label="{{ lang._('Test') }}"></button>
</div>

View file

@ -1,4 +1,4 @@
{% if helpers.exists('OPNsense.helloworld.general') and OPNsense.helloworld.general.Enabled|default("0") == "1" %}
{% if not helpers.empty('OPNsense.helloworld.general.Enabled') %}
[general]
SMTPHost={{ OPNsense.helloworld.general.SMTPHost|default("") }}
FromEmail={{ OPNsense.helloworld.general.FromEmail|default("") }}