Added MaintenanceNotification

This commit is contained in:
Frank Schubert
2024-11-12 18:31:21 +01:00
parent 3a39731ffd
commit 667a985293
11 changed files with 1840 additions and 0 deletions

View File

@@ -0,0 +1,200 @@
<?php
class MaintenanceNotification extends mfBaseModel {
private $plzs;
public function getProperty($name) {
if($this->$name == null) {
if($name == "plzs") {
if(!$this->plz) return [];
$plzs = json_decode($this->plz);
if(!is_array($plzs)) return [];
$this->plzs = $plzs;
return $this->plzs;
}
if($name == "creator") {
$user = mfValuecache::singleton()->get("Worker-id-".$this->create_by);
if($user) {
$this->creator = $user;
return $this->creator;
}
$this->creator = new User($this->create_by);
if($this->creator->id) {
mfValuecache::singleton()->set("Worker-id-".$this->create_by, $this->creator);
}
return $this->creator;
}
if($name == "editor") {
$this->editor = new User($this->edit_by);
return $this->editor;
}
$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = mfValuecache::singleton()->get("mfObjectmodel-$name-".$this->$idfield);
if(!$this->$name) {
$this->$name = new $classname($this->$idfield);
}
if($this->$name->id) {
mfValuecache::singleton()->set("mfObjectmodel-$name-".$this->$name->id, $this->$name);
return $this->$name;
} else {
return null;
}
}
return $this->$name;
}
/********************************
* Begin static Model functions
*/
public static function create(Array $data) {
$model = new MaintenanceNotification();
$table_fields = [
"subject_id", "text", "plz", "from", "to", "send_ts", "sent", "sent_by",
"create_by", "edit_by", "create", "edit"
];
foreach($data as $field => $value) {
if(in_array($field, $table_fields)) {
$model->$field = $value;
}
}
$me = new User();
$me->loadMe();
if($model->create_by === null) {
$model->create_by = $me->id;
}
if($model->edit_by === null) {
$model->edit_by = $me->id;
}
return $model;
}
public static function getAll() {
$items = [];
$db = FronkDB::singleton();
$res = $db->select("MaintenanceNotification", "*", "1 = 1 ORDER BY `create`");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new MaintenanceNotification($data);
}
}
return $items;
}
public static function getFirst($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM MaintenanceNotification
WHERE $where
ORDER BY adb_hausnummer_id LIMIT 1";
//var_dump($sql);exit;
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new MaintenanceNotification($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function count($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT COUNT(*) as cnt FROM MaintenanceNotification
WHERE $where";
//mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
return $data->cnt;
}
return 0;
}
public static function search($filter, $limit = false, $order = false) {
//var_dump($filter);exit;
$items = [];
if(!$order) {
$order = "`create` ASC";
}
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM MaintenanceNotification
WHERE $where
ORDER BY $order";
if(is_array($limit) && count($limit)) {
if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['start'].", ".$limit['count'];
} elseif(is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['count'];
}
}
mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[$data->id] = new MaintenanceNotification($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
if(array_key_exists("termination_id", $filter)) {
$termination_id = $filter['termination_id'];
if(is_numeric($termination_id)) {
$where .= " AND MaintenanceNotification.termination_id=$termination_id";
}
}
if(array_key_exists("object_type", $filter)) {
$object_type = FronkDB::singleton()->escape($filter["object_type"]);
if($object_type) {
$where .= " AND object_type='$object_type'";
}
}
if(array_key_exists("add-where", $filter)) {
$where .= " ".$filter['add-where'];
}
//var_dump($filter, $where);exit;
return $where;
}
}

View File

@@ -0,0 +1,162 @@
<?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]);
}
}