diff --git a/api/actions/addevent.php b/api/actions/addevent.php new file mode 100644 index 0000000..5f3c3ca --- /dev/null +++ b/api/actions/addevent.php @@ -0,0 +1,48 @@ +hasPermission("MACHINEMANAGER_EDIT") && !$user->hasPermission("MACHINEMANAGER_EVENTS")) { + http_response_code(403); + sendJsonResp("You don't have permission to add events.", "ERROR"); +} + +if (empty($VARS["date"]) || (bool)strtotime($VARS["date"]) !== true) { + sendJsonResp("Invalid or missing event date.", "ERROR"); +} +if (empty($VARS["time"]) || (bool)strtotime($VARS["time"]) !== true) { + sendJsonResp("Invalid or missing event time.", "ERROR"); +} +if (empty($VARS["event"])) { + sendJsonResp("Invalid or missing event type.", "ERROR"); +} + +$evt = Event::create( + $VARS['id'], + date( + "Y-m-d H:i:s", + strtotime($VARS['date'] . " " . $VARS['time']) + ), + $VARS['event'], + $user->getUID(), + $VARS['publicnotes'] ?? "", + $VARS['privatenotes'] ?? "" +); + +sendJsonResp("Event added."); \ No newline at end of file diff --git a/api/actions/geteventtypes.php b/api/actions/geteventtypes.php new file mode 100644 index 0000000..fc25975 --- /dev/null +++ b/api/actions/geteventtypes.php @@ -0,0 +1,12 @@ + "string", "publicnotes (optional)" => "string" ] + ], + "addevent" => [ + "load" => "addevent.php", + "vars" => [ + "id" => "/^[0-9a-z]+$/", + "date" => "string", + "time" => "string", + "event" => "string", + "publicnotes (optional)" => "string", + "privatenotes (optional)" => "string" + ] + ], + "geteventtypes" => [ + "load" => "geteventtypes.php", + "vars" => [ + + ] ] ]; diff --git a/lib/Event.lib.php b/lib/Event.lib.php index 91bc7b4..5d47f2e 100644 --- a/lib/Event.lib.php +++ b/lib/Event.lib.php @@ -56,6 +56,51 @@ class Event implements JsonSerializable { return $list; } + public static function getFormDataArray() { + return [ + "types" => $this->getTypes(), + "inputtypes" => [ + "machineid" => "text", + "name" => "select", + "date" => "datetime", + "privatenotes" => "textarea", + "publicnotes" => "textarea" + ], + "labels" => [ + "machineid" => $Strings->get("Machine ID", false), + "name" => $Strings->get("Event", false), + "date" => $Strings->get("Date", false), + "privatenotes" => $Strings->get("Private Notes", false), + "publicnotes" => $Strings->get("Public Notes", false) + ], + "icons" => [] + ]; + } + + public static function getTypesFormattedForForm() { + $eventtypes = Event::getTypes(); + $eventselect = []; + foreach ($eventtypes as $key => $val) { + $optgroup = trim(str_replace("[]", "", $key)); + $valprepend = strpos($key, " []") !== false ? "" : trim($key); + foreach ($val as $v) { + if (empty($valprepend)) { + $vpre = ""; + } else if (empty($v)) { + $vpre = $valprepend; + } else { + $vpre = $valprepend . ": "; + } + if (empty($v)) { + $eventselect[$optgroup][$vpre] = "[$vpre Other]"; + } else { + $eventselect[$optgroup][$vpre . $v] = $v; + } + } + } + return $eventselect; + } + public function toArray() { global $Strings; if ($this->exists) { @@ -68,26 +113,6 @@ class Event implements JsonSerializable { "techuid" => $this->getTechUID(), "publicnotes" => $this->getPublicNotes(), "privatenotes" => $this->getPrivateNotes() - ], - "formdata" => [ - "types" => $this->getTypes(), - "inputtypes" => [ - "machineid" => "text", - "name" => "select", - "date" => "datetime", - "techuid" => "text", - "privatenotes" => "textarea", - "publicnotes" => "textarea" - ], - "labels" => [ - "machineid" => $Strings->get("Machine ID", false), - "name" => $Strings->get("Event", false), - "date" => $Strings->get("Date", false), - "techuid" => $Strings->get("Technician", false), - "privatenotes" => $Strings->get("Private Notes", false), - "publicnotes" => $Strings->get("Public Notes", false) - ], - "icons" => [] ] ]; } diff --git a/pages/addevent.php b/pages/addevent.php index 3f22549..cf59eec 100644 --- a/pages/addevent.php +++ b/pages/addevent.php @@ -28,28 +28,7 @@ $form->addHiddenInput("action", "addevent"); $form->addHiddenInput("source", "viewmachine"); $form->addHiddenInput("machine", htmlspecialchars($_GET['id'])); -$eventtypes = Event::getTypes(); -$eventselect = [ - "" => "" -]; -foreach ($eventtypes as $key => $val) { - $optgroup = trim(str_replace("[]", "", $key)); - $valprepend = strpos($key, " []") !== false ? "" : trim($key); - foreach ($val as $v) { - if (empty($valprepend)) { - $vpre = ""; - } else if (empty($v)) { - $vpre = $valprepend; - } else { - $vpre = $valprepend . ": "; - } - if (empty($v)) { - $eventselect[$optgroup][$vpre] = "[$vpre Other]"; - } else { - $eventselect[$optgroup][$vpre . $v] = $v; - } - } -} +$eventselect = ["" => ""] + Event::getTypesFormattedForForm(); $form->addInput("event", "", "select", true, null, $eventselect, "Event", "fas fa-list"); $form->addInput("date", date("Y-m-d"), "date", true, null, null, "Date", "fas fa-calendar");