Files
thetool/application/Contractconfig/ContractconfigController.php
2022-12-15 20:48:48 +01:00

96 lines
2.8 KiB
PHP

<?php
class ContractconfigController extends mfBaseController {
protected function init() {
$this->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");
}
if(!is_array($r->itemvalues) || !count($r->itemvalues)) {
$this->layout()->setFlash("Keine Änderungen.","info");
$this->redirect("Contract");
}
$error_items = [];
foreach($r->itemvalues as $item_id => $itemvalue) {
$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(!$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();
}
}
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<br />".implode(", ", $error_items), "warning");
return $this->editAction();
}
$this->layout()->setFlash("Konfiguration gespeichert", "success");
$this->redirect("Contract", "view", ['id' => $contract_id]);
}
}