From 416faefa752702b70a7f8a3ba8d43a3a1d345670 Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Tue, 3 Sep 2024 14:30:12 +0200 Subject: [PATCH] devel/helloworld - upgrade to latest standards. --- .../HelloWorld/Api/ServiceController.php | 13 ++--- .../HelloWorld/Api/SettingsController.php | 57 ++----------------- .../Api/SimplifiedSettingsController.php | 43 -------------- .../app/views/OPNsense/HelloWorld/index.volt | 21 +++---- .../OPNsense/HelloWorld/helloworld.conf | 2 +- 5 files changed, 18 insertions(+), 118 deletions(-) delete mode 100644 devel/helloworld/src/opnsense/mvc/app/controllers/OPNsense/HelloWorld/Api/SimplifiedSettingsController.php diff --git a/devel/helloworld/src/opnsense/mvc/app/controllers/OPNsense/HelloWorld/Api/ServiceController.php b/devel/helloworld/src/opnsense/mvc/app/controllers/OPNsense/HelloWorld/Api/ServiceController.php index 6c7073da1..5b068bae4 100644 --- a/devel/helloworld/src/opnsense/mvc/app/controllers/OPNsense/HelloWorld/Api/ServiceController.php +++ b/devel/helloworld/src/opnsense/mvc/app/controllers/OPNsense/HelloWorld/Api/ServiceController.php @@ -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"]; } } diff --git a/devel/helloworld/src/opnsense/mvc/app/controllers/OPNsense/HelloWorld/Api/SettingsController.php b/devel/helloworld/src/opnsense/mvc/app/controllers/OPNsense/HelloWorld/Api/SettingsController.php index 750e1b986..440ff3ce1 100644 --- a/devel/helloworld/src/opnsense/mvc/app/controllers/OPNsense/HelloWorld/Api/SettingsController.php +++ b/devel/helloworld/src/opnsense/mvc/app/controllers/OPNsense/HelloWorld/Api/SettingsController.php @@ -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'; } diff --git a/devel/helloworld/src/opnsense/mvc/app/controllers/OPNsense/HelloWorld/Api/SimplifiedSettingsController.php b/devel/helloworld/src/opnsense/mvc/app/controllers/OPNsense/HelloWorld/Api/SimplifiedSettingsController.php deleted file mode 100644 index 72167c215..000000000 --- a/devel/helloworld/src/opnsense/mvc/app/controllers/OPNsense/HelloWorld/Api/SimplifiedSettingsController.php +++ /dev/null @@ -1,43 +0,0 @@ - $( 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']); + } }); - }); @@ -63,6 +60,6 @@ POSSIBILITY OF SUCH DAMAGE.
- - + +
diff --git a/devel/helloworld/src/opnsense/service/templates/OPNsense/HelloWorld/helloworld.conf b/devel/helloworld/src/opnsense/service/templates/OPNsense/HelloWorld/helloworld.conf index 7ccefce07..28199b146 100644 --- a/devel/helloworld/src/opnsense/service/templates/OPNsense/HelloWorld/helloworld.conf +++ b/devel/helloworld/src/opnsense/service/templates/OPNsense/HelloWorld/helloworld.conf @@ -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("") }}