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; $id = $r->id; //var_dump($r);exit; 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)); } try { $from = new DateTime($r->from_date." ".$r->from_time); $from->setTimezone(new DateTimeZone("Europe/Vienna")); } 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); $to->setTimezone(new DateTimeZone("Europe/Vienna")); } 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(); try { $send_ts = new DateTime($r->sendts_day." ".$r->sendts_hour.":00"); //var_dump($send_ts);exit; } catch(Exception $e) { $this->layout()->setFlash("Ungültige Versandzeitpunkt", "error"); if($mode = "edit") { return $this->editAction(); } return $this->addAction(); } $data["send_ts"] = $send_ts->getTimestamp(); 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]); } protected function delete() { $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"); } $notification->delete(); $this->layout()->setFlash("Wartungsmeldung erfolgreich gelöscht", $notification); $this->redirect("MaintenanceNotification"); } }