Commit 053f7f89 authored by Per von Zweigbergk's avatar Per von Zweigbergk

(base) Added hooks that can be used by controllers to take actions at the...

(base) Added hooks that can be used by controllers to take actions at the appropriate time during an action
parent 44eb7531
......@@ -111,12 +111,13 @@ abstract class ApiMutableModelControllerBase extends ApiControllerBase
* validate and save model after update or insertion.
* Use the reference node and tag to rename validation output for a specific node to a new offset, which makes
* it easier to reference specific uuids without having to use them in the frontend descriptions.
* @param $mdl The model to save
* @param $node reference node, to use as relative offset
* @param $actionHook A closure containing an action to do after validation but before saving. Must return null if successful, or an error message if not.
* @return array result / validation output
*/
protected function save($node = null)
protected function save($mdl, $node = null, $actionHook = null)
{
$mdl = $this->getModel();
$result = array("result"=>"failed","validations" => array());
// perform validation
$valMsgs = $mdl->performValidation();
......@@ -129,16 +130,31 @@ abstract class ApiMutableModelControllerBase extends ApiControllerBase
$result["validations"][$msg->getField()] = $msg->getMessage();
}
}
// serialize model to config and save when there are no validation errors
if (count($result['validations']) == 0) {
// save config if validated correctly
$mdl->serializeToConfig();
Config::getInstance()->save();
$result = array("result" => "saved");
if (count($result['validations']) != 0) {
return $result;
}
return $result;
// run the action hook (default one just always returns null)
$errorMessage = $actionHook ? $actionHook($mdl) : null;
if ($errorMessage != null) {
return array("result"=>failed, "error"=>$errorMessage);
}
// serialize model to config and save when there are no validation errors
// and the action hook completed successfully
$mdl->serializeToConfig();
Config::getInstance()->save();
return array("result"=>"saved");
}
/**
* hook to be overridden if the controller is to take an action when
* setAction is called. This hook is called after a model has been
* constructed and validated but before it serialized to the configuration
* and written to disk
* @param $mdl The validated model containing the new state of the model
* @return Error message on error, or null/void on success
*/
protected function setActionHook($mdl) { }
/**
* update model settings
* @return array status / validation errors
......@@ -150,7 +166,7 @@ abstract class ApiMutableModelControllerBase extends ApiControllerBase
// load model and update with provided data
$mdl = $this->getModel();
$mdl->setNodes($this->request->getPost(static::$internalModelName));
$result = $this->save();
$result = $this->save($mdl, null, $this->setActionHook);
}
return $result;
}
......
......@@ -36,8 +36,6 @@ namespace OPNsense\Base;
*/
abstract class ApiMutableTableModelControllerBase extends ApiMutableModelControllerBase
{
// FIXME What about validation?
static protected $modelPathPrefix = '';
static protected $gridFields = array();
static protected $gridDefaultSort = null;
......@@ -72,6 +70,16 @@ abstract class ApiMutableTableModelControllerBase extends ApiMutableModelControl
return array();
}
/**
* hook to be overridden if the controller is to take an action when
* setItemAction is called. This hook is called after a model has been
* constructed and validated but before it serialized to the configuration
* and written to disk
* @param $mdl The validated model containing the new state of the model
* @return Error message on error, or null/void on success
*/
protected function setItemActionHook($mdl) { }
/**
* update item with given properties
* @param $uuid item unique id
......@@ -85,13 +93,23 @@ abstract class ApiMutableTableModelControllerBase extends ApiMutableModelControl
$node = $this->getNodeByUUID($uuid);
if ($node != null) {
$node->setNodes($this->request->getPost(static::$internalModelName));
return $this->save($node);
return $this->save($mdl, $node, $this->setItemActionHook);
}
}
}
return array("result"=>"failed");
}
/**
* hook to be overridden if the controller is to take an action when
* addItemAction is called. This hook is called after a model has been
* constructed and validated but before it serialized to the configuration
* and written to disk
* @param $mdl The validated model containing the state of the new model
* @return Error message on error, or null/void on success
*/
protected function addItemActionHook($mdl) { }
/**
* add new item and set with attributes from post
* @return array
......@@ -103,11 +121,21 @@ abstract class ApiMutableTableModelControllerBase extends ApiMutableModelControl
$mdl = $this->getModel();
$node = $this->getNodes()->add();
$node->setNodes($this->request->getPost(static::$internalModelName));
return $this->save($node);
return $this->save($mdl, $node, $this->addItemActionHook);
}
return $result;
}
/**
* hook to be overridden if the controller is to take an action when
* delItemAction is called. This hook is called after a model has been
* constructed and validated but before it serialized to the configuration
* and written to disk
* @param $uuid The UUID of the item to be deleted
* @return Error message on error, or null/void on succes s
*/
protected function delItemActionHook($uuid) { }
/**
* delete item by uuid
* @param $uuid item unique id
......@@ -119,7 +147,10 @@ abstract class ApiMutableTableModelControllerBase extends ApiMutableModelControl
if ($this->request->isPost()) {
$mdl = $this->getModel();
if ($uuid != null) {
if (getNodes()->del($uuid)) {
$errorMessage = delItemActionHook($uuid);
if ($errorMessage) {
$result['error'] = $errorMessage;
} else if (getNodes()->del($uuid)) {
// if item is removed, serialize to config and save
$mdl->serializeToConfig();
Config::getInstance()->save();
......@@ -132,6 +163,17 @@ abstract class ApiMutableTableModelControllerBase extends ApiMutableModelControl
return $result;
}
/**
* hook to be overridden if the controller is to take an action when
* toggleItemAction is called. This hook is called after a model has been
* constructed and validated but before it serialized to the configuration
* and written to disk
* @param $uuid The UUID of the item to be toggled
* @param $enabled desired state enabled(1)/disabled(1), leave empty for toggle
* @return Error message on error, or null/void on succes s
*/
protected function toggleItemActionHook($uuid, $enabled) { }
/**
* toggle item by uuid (enable/disable)
* @param $uuid item unique id
......@@ -146,17 +188,22 @@ abstract class ApiMutableTableModelControllerBase extends ApiMutableModelControl
if ($uuid != null) {
$node = $mdl->getNodeByUUID($uuid);
if ($node != null) {
if ($enabled == "0" || $enabled == "1") {
$node->enabled = (string)$enabled;
} elseif ($node->enabled->__toString() == "1") {
$node->enabled = "0";
$errorMessage = toggleItemActionHook($uuid, $enabled);
if ($errorMessage) {
$result['error'] = $errorMessage;
} else {
$node->enabled = "1";
if ($enabled == "0" || $enabled == "1") {
$node->enabled = (string)$enabled;
} elseif ($node->enabled->__toString() == "1") {
$node->enabled = "0";
} else {
$node->enabled = "1";
}
$result['result'] = $node->enabled;
// if item has toggled, serialize to config and save
$mdl->serializeToConfig();
Config::getInstance()->save();
}
$result['result'] = $node->enabled;
// if item has toggled, serialize to config and save
$mdl->serializeToConfig();
Config::getInstance()->save();
}
}
}
......
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