Integrated Contractconfig Hooks in Contractconfig/Save with restore on

error
This commit is contained in:
Frank Schubert
2023-03-21 22:04:57 +01:00
parent a27d7a7418
commit 61e84f085d
14 changed files with 5956 additions and 86 deletions
@@ -1,6 +1,7 @@
<?php
class ContractconfigController extends mfBaseController {
public $hook_errors;
protected function init() {
$this->needlogin=true;
@@ -68,13 +69,24 @@ class ContractconfigController extends mfBaseController {
$error_items = [];
$old_values = [];
foreach($r->itemvalues as $item_id => $itemvalue) {
//var_dump($item_id, $itemvalue); continue;
$item = new ContractconfigItem($item_id);
if(!$item->id) {
$this->log->warn("Tried to save non-existant ContractconfigItem $item_id");
continue;
}
$item->setContractId($contract_id);
/*if(!array_key_exists($item->name, $old_values)) {
$old_values[$item->name] = [];
}*/
$old_values[$item->name] = $item->getValue();
if(!$item->value->set($itemvalue)) {
$error_items[$item->id] = $item->name;
continue;
@@ -85,6 +97,7 @@ class ContractconfigController extends mfBaseController {
return $this->editAction();
}
}
//exit;
if(count($error_items)) {
$this->layout()->set("error_items", array_keys($error_items));
@@ -94,12 +107,74 @@ class ContractconfigController extends mfBaseController {
}
// run custom productgroup hooks
if(!$this->runAfterSaveHooks($contract)) {
$errors = [];
foreach($this->hook_errors as $item_name => $item_errors) {
if(is_array($item_errors) && count($item_errors)) {
foreach($item_errors as $error_string) {
$errors[] = "[$item_name] $error_string";
}
//var_dump($old_values[$item_name]);
// on error restore old values
if(array_key_exists($item_name, $old_values)) {
$old_item = ContractconfigItemModel::getFirst(["name" => $item_name]);
$old_item->setContractId($contract_id);
$old_item->value->set($old_values[$item_name]);
$old_item->value->save();
//var_dump($old_item->value->json);exit;
}
}
}
//var_dump($errors);exit;
$this->layout()->setFlash(implode("<br />", $errors), "error");
$this->redirect("Contract", "view", ['id' => $contract_id]);
}
$this->layout()->setFlash("Konfiguration gespeichert", "success");
$this->redirect("Contract", "view", ['id' => $contract_id]);
}
private function runAfterSaveHooks(Contract $contract) {
if(!$contract || !$contract->id) {
return false;
}
$folderpath = APPDIR."/Contractconfig/hooks/";
$dir = opendir($folderpath);
while($filename = readdir($dir)) {
if(substr($filename, 0, 1) == ".") {
continue;
}
if($filename == "Contractconfig_Hook.php") continue;
if(substr($filename, -4) != ".php") continue;
$hook_type = basename($filename, ".php");
$classname = "Contractconfig_Hook_$hook_type";
$hook_class_filename = $folderpath.$filename;
require_once $hook_class_filename;
if(!class_exists($classname)) {
continue;
}
$hook = new $classname($contract);
if($hook->isResponsible()) {
$this->log->debug("Running {$classname}->afterSave() for Contract id ".$contract->id);
$hook->afterSave();
if($hook->errors) {
$this->hook_errors = $hook->errors;
return false;
}
}
}
return true;
}
}