Files
thetool/application/MaintenanceNotification/MaintenanceNotificationController.php
2024-11-12 18:31:21 +01:00

162 lines
4.9 KiB
PHP

<?php
class MaintenanceNotificationController 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() {
if($this->request->resetFilter) {
unset($_SESSION[MFAPPNAME.'-MaintenanceNotification-filter']);
}
$filter = [];
if(is_array($this->request->filter)) {
$filter = $this->request->filter;
$_SESSION[MFAPPNAME.'-MaintenanceNotification-filter'] = $filter;
} else {
if(array_key_exists(MFAPPNAME.'-MaintenanceNotification-filter', $_SESSION) && count($_SESSION[MFAPPNAME.'-MaintenanceNotification-filter'])) {
$filter = $_SESSION[MFAPPNAME.'-MaintenanceNotification-filter'];
}
}
$this->layout->set("filter", $filter);
$filter = $this->getPreparedFilter($filter);
// pagination defaults
$pagination = [];
$pagination['start'] = 0;
$pagination['count'] = 20;
$pagination['maxItems'] = 0;
if(is_numeric($this->request->s)) {
$pagination['start'] = intval($this->request->s);
}
$pagination['maxItems'] = MaintenanceNotification::count($filter);
$this->layout()->set("pagination", $pagination);
$templates = MaintenanceNotification::search($filter, $pagination);
$this->layout()->set("notifications", $templates);
}
private function getPreparedFilter($filter) : array
{
$new_filter = [];
if (is_array($filter) && count($filter)) {
foreach ($filter as $name => $value) {
$new_filter[$name] = $value;
}
}
return $new_filter;
}
protected function addAction() {
$this->layout()->setTemplate("MaintenanceNotification/Form");
}
protected function editAction() {
$id = $this->request->id;
if(!is_numeric($id) || $id < 1) {
$this->layout()->setFlash("Wartungsmeldung nicht gefunden", "error");
$this->redirect("MaintenanceNotification");
}
$notification = new MaintenanceNotification($id);
if(!$notification || !$notification->id) {
$this->layout()->setFlash("Wartungsmeldung nicht gefunden", "error");
$this->redirect("MaintenanceNotification");
}
$this->layout()->set("notification", $notification);
$this->addAction();
}
protected function saveAction() {
$r = $this->request;
var_dump($r->get());
$id = $r->id;
if(is_numeric($id) && $id > 0) {
$mode = "edit";
$notification = new MaintenanceNotification($id);
if(!$notification->id) {
$this->layout()->setFlash("Wartungsmeldung nicht gefunden", "error");
$this->redirect("MaintenanceNotification");
}
} else {
$mode = "add";
}
$data = [];
$data["subject_id"] = $r->subject_id;
$data["text"] = $r->text;
// plz -> json array
$plzs = $r->plz;
if($plzs) {
$plz_string = preg_replace('/[^0-9]+/', ",", $plzs);
$data["plz"] = json_encode(explode(",", $plz_string));
}
// from / to -> date/time -> DateTime
try {
$from = new DateTime($r->from_date." ".$r->from_time);
} catch(Exception $e) {
$this->layout()->setFlash("Ungültiges Wartungsfenster Von-Zeitpunkt", "error");
if($mode = "edit") {
return $this->editAction();
}
return $this->addAction();
}
$data["from"] = $from->getTimestamp();
try {
$to = new DateTime($r->to_date." ".$r->to_time);
} catch(Exception $e) {
$this->layout()->setFlash("Ungültiges Wartungsfenster Bis-Zeitpunkt", "error");
if($mode = "edit") {
return $this->editAction();
}
return $this->addAction();
}
$data["to"] = $to->getTimestamp();
// send_ts - feld einbauen -> DateTime
//var_dump($data);exit;
if($mode == "add") {
$notification = MaintenanceNotification::create($data);
} else {
$notification->update($data);
}
if(!$notification->save()) {
$this->layout()->setFlash("Fehler beim Speichern", "error");
if($mode = "edit") {
return $this->editAction();
}
return $this->addAction();
}
$this->layout()->setFlash("Wartungsmeldung erfolgreich gespeichert", "success");
$this->redirect("MaintenanceNotification", "edit", ["id" => $notification->id]);
}
}