needlogin=true;
$me = new User();
$me->loadMe();
$this->me = $me;
$this->layout()->set("me",$me);
if(!$me->is(["Admin"])) {
$this->redirect("Dashboard");
}
}
protected function indexAction() {
}
protected function viewAction() {
}
protected function editAction() {
$this->layout()->setTemplate("Contractconfig/Form");
$contract_id = $this->request->contract_id;
if(!$contract_id) {
$this->layout()->setFlash("Contract ID nicht gefunden.","error");
$this->redirect("Contract");
}
$contract = new Contract($contract_id);
if(!$contract_id) {
$this->layout()->setFlash("Contract ID nicht gefunden.","error");
$this->redirect("Contract");
}
if(!is_array($contract->configgroups) || !count($contract->configgroups)) {
$this->layout()->setFlash("Produkt hat keine Vertragsconfiggruppen hinterlegt.","info");
$this->redirect("Contract", "View", ["id" => $contract_id]);
}
$this->layout()->set("contract", $contract);
$this->layout()->set("groups", $contract->configgroups);
}
protected function saveAction() {
$r = $this->request;
$contract_id = $r->contract_id;
if(!is_numeric($contract_id) || $contract_id < 1) {
$this->layout()->setFlash("Contract ID nicht gefunden.","error");
$this->redirect("Contract");
}
$contract = new Contract($contract_id);
if(!$contract->id) {
$this->layout()->setFlash("Contract ID nicht gefunden.","error");
$this->redirect("Contract");
}
if(!is_array($r->itemvalues) || !count($r->itemvalues)) {
$this->layout()->setFlash("Keine Änderungen.","info");
$this->redirect("Contract");
}
$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;
}
if(!$item->value->save()) {
$this->layout()->set("request", $r);
$this->layout()->setFlash("Fehler beim Speichern von".$item->name,"info");
return $this->editAction();
}
}
//exit;
if(count($error_items)) {
$this->layout()->set("error_items", array_keys($error_items));
$this->layout()->set("request", $r);
$this->layout()->setFlash("Konfiguration nicht vollständig gespeichert. Fehler in folgenden Feldern:\n
".implode(", ", $error_items), "warning");
return $this->editAction();
}
// 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("
", $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;
}
}