Add event APIs

master
Skylar Ittner 4 years ago
parent 122c85387c
commit 4e123691d5

@ -0,0 +1,48 @@
<?php
/*
* Copyright 2020 Netsyms Technologies.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
if (Machine::exists($VARS["id"])) {
$machine = new Machine($VARS['id']);
} else if (Machine::serialExists($VARS["id"])) {
$machine = new Machine(Machine::getIDFromSerial($VARS['id']));
} else {
http_response_code(404);
sendJsonResp("Requested ID does not exist.", "ERROR");
}
$user = getRequestUser();
if (!$user->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.");

@ -0,0 +1,12 @@
<?php
/*
* Copyright 2020 Netsyms Technologies.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
header("Content-Type: application/json");
exit(json_encode(Event::getTypesFormattedForForm()));

@ -48,5 +48,22 @@ $APIS = [
"privatenotes (optional)" => "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" => [
]
]
];

@ -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" => []
]
];
}

@ -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");

Loading…
Cancel
Save