mirror of
https://github.com/lucaspalomodevelop/core.git
synced 2026-03-16 01:24:38 +00:00
(mvc) some refactoring for https://github.com/opnsense/core/pull/1124
This commit is contained in:
parent
3235ee2690
commit
7a67a2c03d
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (C) 2016 IT-assistans Sverige AB
|
||||
* Copyright (C) 2016 Deciso B.V.
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
@ -28,7 +29,6 @@
|
||||
*/
|
||||
namespace OPNsense\Base;
|
||||
|
||||
use OPNsense\Base\ApiControllerBase;
|
||||
|
||||
/**
|
||||
* Class ApiModelControllerBase, inherit this class to implement
|
||||
@ -40,22 +40,71 @@ use OPNsense\Base\ApiControllerBase;
|
||||
*/
|
||||
abstract class ApiModelControllerBase extends ApiControllerBase
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string this implementations internal model name to use (in set/get output)
|
||||
*/
|
||||
protected $internalModelName = null;
|
||||
|
||||
/**
|
||||
* @var string model class name to use
|
||||
*/
|
||||
protected $internalModelClass = null;
|
||||
|
||||
/**
|
||||
* @var null|BaseModel model object to work on
|
||||
*/
|
||||
private $modelHandle = null;
|
||||
|
||||
/**
|
||||
* validate on initialization
|
||||
* @throws Exception
|
||||
*/
|
||||
public function initialize()
|
||||
{
|
||||
parent::initialize();
|
||||
if (empty($this->internalModelClass)) {
|
||||
throw new \Exception('cannot instantiate without internalModelClass defined.');
|
||||
}
|
||||
if (empty($this->internalModelName)) {
|
||||
throw new \Exception('cannot instantiate without internalModelName defined.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* retrieve model settings
|
||||
* @return array settings
|
||||
*/
|
||||
public function getAction()
|
||||
{
|
||||
// define list of configurable settings
|
||||
$result = array();
|
||||
if ($this->request->isGet()) {
|
||||
$mdl = $this->getModel();
|
||||
$result[$this->getModelName()] = $this->getModelNodes($mdl);
|
||||
$result[$this->internalModelName] = $this->getModelNodes();
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
abstract protected function getModel();
|
||||
abstract protected function getModelName();
|
||||
|
||||
/**
|
||||
* override this to customize what part of the model gets exposed
|
||||
* @return array
|
||||
*/
|
||||
protected function getModelNodes($mdl) {
|
||||
return $mdl->getNodes();
|
||||
protected function getModelNodes()
|
||||
{
|
||||
return $this->getModel()->getNodes();
|
||||
}
|
||||
|
||||
/**
|
||||
* override this to customize the model binding behavior
|
||||
* @return null|BaseModel
|
||||
*/
|
||||
protected function getModel()
|
||||
{
|
||||
if ($this->modelHandle == null) {
|
||||
$this->modelHandle = (new \ReflectionClass($this->internalModelClass))->newInstance();
|
||||
}
|
||||
|
||||
return $this->modelHandle;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (C) 2016 IT-assistans Sverige AB
|
||||
* Copyright (C) 2016 Deciso B.V.
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
@ -28,7 +29,7 @@
|
||||
*/
|
||||
namespace OPNsense\Base;
|
||||
|
||||
use OPNsense\Base\ApiModelControllerBase;
|
||||
use \OPNsense\Core\Config;
|
||||
|
||||
/**
|
||||
* Class ApiMutableModelControllerBase, inherit this class to implement
|
||||
@ -40,13 +41,17 @@ use OPNsense\Base\ApiModelControllerBase;
|
||||
*/
|
||||
abstract class ApiMutableModelControllerBase extends ApiModelControllerBase
|
||||
{
|
||||
/**
|
||||
* update model settings
|
||||
* @return array status / validation errors
|
||||
*/
|
||||
public function setAction()
|
||||
{
|
||||
$result = array("result"=>"failed");
|
||||
if ($this->request->isPost()) {
|
||||
// load model and update with provided data
|
||||
$mdl = $this->getModel();
|
||||
$mdl->setNodes($this->request->getPost(getModelName()));
|
||||
$mdl->setNodes($this->request->getPost($this->internalModelName));
|
||||
|
||||
// perform validation
|
||||
$valMsgs = $mdl->performValidation();
|
||||
@ -54,7 +59,7 @@ abstract class ApiMutableModelControllerBase extends ApiModelControllerBase
|
||||
if (!array_key_exists("validations", $result)) {
|
||||
$result["validations"] = array();
|
||||
}
|
||||
$result["validations"][$this->getModelName().".".$msg->getField()] = $msg->getMessage();
|
||||
$result["validations"][$this->internalModelName.".".$msg->getField()] = $msg->getMessage();
|
||||
}
|
||||
|
||||
// serialize model to config and save
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user