Add machine editing APIs
parent
6c2ef83ec6
commit
d156725bf7
@ -1,4 +1,3 @@
|
|||||||
# Rewrite for Nextcloud Notes API
|
|
||||||
<IfModule mod_rewrite.c>
|
<IfModule mod_rewrite.c>
|
||||||
RewriteEngine on
|
RewriteEngine on
|
||||||
RewriteRule ([a-zA-Z0-9]+) index.php?action=$1 [PT]
|
RewriteRule ([a-zA-Z0-9]+) index.php?action=$1 [PT]
|
||||||
|
@ -1,21 +0,0 @@
|
|||||||
<?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 (empty($VARS["id"]) || (!Machine::exists($VARS["id"]) && !Machine::serialExists($VARS["id"]))) {
|
|
||||||
sendJsonResp("Requested ID does not exist.", "ERROR");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Machine::exists($VARS["id"])) {
|
|
||||||
$machine = new Machine($VARS['id']);
|
|
||||||
} else {
|
|
||||||
$machine = new Machine(Machine::getIDFromSerial($VARS['id']));
|
|
||||||
}
|
|
||||||
|
|
||||||
header("Content-Type: application/json");
|
|
||||||
exit(json_encode($machine));
|
|
@ -0,0 +1,69 @@
|
|||||||
|
<?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 (!empty($VARS["id"])) {
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$machine = Machine::create();
|
||||||
|
}
|
||||||
|
|
||||||
|
$user = getRequestUser();
|
||||||
|
|
||||||
|
|
||||||
|
if ($VARS["action"] != "lookup") {
|
||||||
|
|
||||||
|
if (!$user->hasPermission("MACHINEMANAGER_EDIT")) {
|
||||||
|
http_response_code(403);
|
||||||
|
sendJsonResp("You don't have permission to edit.", "ERROR");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($VARS["model"])) {
|
||||||
|
$machine->setModel($VARS['model']);
|
||||||
|
}
|
||||||
|
if (!empty($VARS["client"])) {
|
||||||
|
$machine->setClientID($VARS['client']);
|
||||||
|
}
|
||||||
|
if (!empty($VARS["os"])) {
|
||||||
|
$machine->setOS($VARS['os']);
|
||||||
|
}
|
||||||
|
if (!empty($VARS["serial"])) {
|
||||||
|
$machine->setSerial($VARS['serial']);
|
||||||
|
}
|
||||||
|
if (!empty($VARS["manufacturer"])) {
|
||||||
|
$machine->setManufacturer($VARS['manufacturer']);
|
||||||
|
}
|
||||||
|
if (!empty($VARS["condition"])) {
|
||||||
|
$machine->setCondition($VARS['condition']);
|
||||||
|
}
|
||||||
|
if (!empty($VARS["price"])) {
|
||||||
|
$machine->setPrice($VARS['price']);
|
||||||
|
}
|
||||||
|
if (!empty($VARS["privatenotes"])) {
|
||||||
|
$machine->setPrivateNotes($VARS['privatenotes']);
|
||||||
|
}
|
||||||
|
if (!empty($VARS["publicnotes"])) {
|
||||||
|
$machine->setPublicNotes($VARS['publicnotes']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$machine->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
header("Content-Type: application/json");
|
||||||
|
|
||||||
|
$output = $machine->toArray();
|
||||||
|
$output["editable"] = $user->hasPermission("MACHINEMANAGER_EDIT");
|
||||||
|
|
||||||
|
exit(json_encode($output));
|
@ -0,0 +1,148 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* 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/.
|
||||||
|
*/
|
||||||
|
|
||||||
|
require __DIR__ . "/../../required.php";
|
||||||
|
|
||||||
|
session_start();
|
||||||
|
|
||||||
|
header("Content-Security-Policy: default-src '*';");
|
||||||
|
header('Access-Control-Allow-Origin: *');
|
||||||
|
|
||||||
|
$redirecttologin = false;
|
||||||
|
|
||||||
|
$thisurl = "https://track.netsyms.com/api/login/index.php";
|
||||||
|
$iconurl = "https://static.netsyms.net/images/products/machinemanager/logo.svg";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show a simple HTML page with a line of text and a button. Matches the UI of
|
||||||
|
* the AccountHub login flow.
|
||||||
|
*
|
||||||
|
* @global type $SETTINGS
|
||||||
|
* @global type $SECURE_NONCE
|
||||||
|
* @global type $Strings
|
||||||
|
* @param string $title Text to show, passed through i18n
|
||||||
|
* @param string $button Button text, passed through i18n
|
||||||
|
* @param string $url URL for the button
|
||||||
|
*/
|
||||||
|
function showHTML(string $title, string $button = "", string $url = "", string $message = "") {
|
||||||
|
global $SETTINGS, $iconurl;
|
||||||
|
?>
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
|
||||||
|
<title><?php echo $SETTINGS['app_name']; ?></title>
|
||||||
|
|
||||||
|
<link rel="icon" href="<?php echo $iconurl; ?>">
|
||||||
|
|
||||||
|
<link href="https://static.netsyms.net/bootstrap/4/bootstrap.materia.min.css" rel="stylesheet">
|
||||||
|
<style>
|
||||||
|
.display-5 {
|
||||||
|
font-size: 2.5rem;
|
||||||
|
font-weight: 300;
|
||||||
|
line-height: 1.2;
|
||||||
|
}
|
||||||
|
|
||||||
|
.banner-image {
|
||||||
|
max-height: 100px;
|
||||||
|
margin: 2em auto;
|
||||||
|
border: 1px solid grey;
|
||||||
|
border-radius: 15%;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<div class="container mt-4">
|
||||||
|
<div class="row justify-content-center">
|
||||||
|
<div class="col-12 text-center">
|
||||||
|
<img class="banner-image" src="<?php echo $iconurl; ?>" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="col-12 text-center">
|
||||||
|
<h1 class="display-5 mb-4"><?php echo $title; ?></h1>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<?php if (!empty($message)) { ?>
|
||||||
|
<div class="col-12 col-sm-8 col-lg-6">
|
||||||
|
<div class="card mt-4">
|
||||||
|
<div class="card-body">
|
||||||
|
<p>
|
||||||
|
<?php echo $message; ?>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<?php } ?>
|
||||||
|
|
||||||
|
<?php if (!empty($button)) { ?>
|
||||||
|
<div class="col-12 col-sm-8 col-lg-6">
|
||||||
|
<div class="card mt-4">
|
||||||
|
<div class="card-body">
|
||||||
|
<a href="<?php echo $url; ?>" class="btn btn-primary btn-block"><?php echo $button; ?></a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<?php } ?>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<?php
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($_SESSION["login_code"])) {
|
||||||
|
$redirecttologin = true;
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
$uidinfo = AccountHubApi::get("checkloginkey", ["code" => $_SESSION["login_code"]]);
|
||||||
|
|
||||||
|
if ($uidinfo["status"] == "ERROR") {
|
||||||
|
throw new Exception();
|
||||||
|
}
|
||||||
|
if (is_numeric($uidinfo['uid'])) {
|
||||||
|
$user = new User($uidinfo['uid'] * 1);
|
||||||
|
|
||||||
|
$addpassresp = AccountHubApi::get(
|
||||||
|
"addapppassword",
|
||||||
|
[
|
||||||
|
"desc" => $SETTINGS["app_name"],
|
||||||
|
"username" => $user->getUsername()
|
||||||
|
],
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
$_SESSION["login_code"] = null;
|
||||||
|
|
||||||
|
$redirecturl = "https://apploginhelper.netsyms.net/?user:" . htmlentities($user->getUsername()) . "&password:" . htmlentities($addpassresp["pass"]);
|
||||||
|
|
||||||
|
header("Location: $redirecturl");
|
||||||
|
showHTML("Continue", "Click Here", $redirecturl);
|
||||||
|
} else {
|
||||||
|
throw new Exception();
|
||||||
|
}
|
||||||
|
} catch (Exception $ex) {
|
||||||
|
$redirecttologin = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($redirecttologin) {
|
||||||
|
try {
|
||||||
|
$codedata = AccountHubApi::get("getloginkey", ["appname" => $SETTINGS["app_name"], "appicon" => $iconurl], true);
|
||||||
|
|
||||||
|
if ($codedata['status'] != "OK") {
|
||||||
|
throw new Exception("There was a problem. Try again later.");
|
||||||
|
}
|
||||||
|
|
||||||
|
$_SESSION["login_code"] = $codedata["code"];
|
||||||
|
|
||||||
|
$locationurl = $codedata["loginurl"] . "?code=" . htmlentities($codedata["code"]) . "&redirect=" . htmlentities($thisurl);
|
||||||
|
|
||||||
|
header("Location: $locationurl");
|
||||||
|
showHTML("Continue", "Continue", $locationurl);
|
||||||
|
die();
|
||||||
|
} catch (Exception $ex) {
|
||||||
|
showHTML("Error", "", "", $ex->getMessage());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"Type": "Type",
|
||||||
|
"Tested": "Tested"
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"Date": "Date",
|
||||||
|
"Technician": "Technician"
|
||||||
|
}
|
@ -0,0 +1,195 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/* 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/. */
|
||||||
|
|
||||||
|
class Event implements JsonSerializable {
|
||||||
|
|
||||||
|
private $id = "";
|
||||||
|
private $event = [];
|
||||||
|
private $exists = false;
|
||||||
|
|
||||||
|
public function __construct($eventid) {
|
||||||
|
global $database;
|
||||||
|
$this->id = $eventid;
|
||||||
|
if (\Event::exists($eventid)) {
|
||||||
|
$this->exists = true;
|
||||||
|
$this->event = $database->get('events', ['machineid', 'eventid', 'techuid', 'date', 'privatenotes', 'publicnotes'], ['historyid' => $eventid]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function create(string $machineid = "", string $date = "", int $event = 0, string $techuid = "", string $publicnotes = "", string $privatenotes = ""): Event {
|
||||||
|
if ($machineid == "" || $date == "" || $event == 0) {
|
||||||
|
return new Event("");
|
||||||
|
} else {
|
||||||
|
if (!Machine::exists($machineid)) {
|
||||||
|
throw new Exception("Invalid machine ID $machineid");
|
||||||
|
}
|
||||||
|
if (strtotime($date) === false) {
|
||||||
|
throw new Exception("Invalid date.");
|
||||||
|
}
|
||||||
|
$date = date("Y-m-d H:i:s", strtotime($date));
|
||||||
|
if (!array_key_exists($event, \Event::getTypes())) {
|
||||||
|
throw new Exception("Invalid event type.");
|
||||||
|
}
|
||||||
|
$evt = new Event("");
|
||||||
|
|
||||||
|
$evt->setMachineID($machineid);
|
||||||
|
$evt->setTypeID($event);
|
||||||
|
$evt->setDate($date);
|
||||||
|
$evt->setTechUID($techuid);
|
||||||
|
$evt->setPrivateNotes($privatenotes);
|
||||||
|
$evt->setPublicNotes($publicnotes);
|
||||||
|
$evt->save();
|
||||||
|
|
||||||
|
return $evt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function exists($id): bool {
|
||||||
|
global $database;
|
||||||
|
return $database->has('events', ['historyid' => $id]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getTypes() {
|
||||||
|
global $database;
|
||||||
|
$types = $database->select("event_types", ["eventid (id)", "eventname (name)"]);
|
||||||
|
|
||||||
|
$list = [];
|
||||||
|
foreach ($types as $t) {
|
||||||
|
$list[$t['id']] = $t['name'];
|
||||||
|
}
|
||||||
|
return $list;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toArray() {
|
||||||
|
global $Strings;
|
||||||
|
if ($this->exists) {
|
||||||
|
return [
|
||||||
|
"info" => [
|
||||||
|
"id" => $this->getID(),
|
||||||
|
"machineid" => $this->getMachineID(),
|
||||||
|
"type" => $this->getTypeID(),
|
||||||
|
"date" => $this->getDate(),
|
||||||
|
"techuid" => $this->getTechUID(),
|
||||||
|
"publicnotes" => $this->getPublicNotes(),
|
||||||
|
"privatenotes" => $this->getPrivateNotes()
|
||||||
|
],
|
||||||
|
"formdata" => [
|
||||||
|
"types" => $this->getTypes(),
|
||||||
|
"inputtypes" => [
|
||||||
|
"machineid" => "text",
|
||||||
|
"type" => "number",
|
||||||
|
"date" => "datetime",
|
||||||
|
"techuid" => "text",
|
||||||
|
"privatenotes" => "textarea",
|
||||||
|
"publicnotes" => "textarea"
|
||||||
|
],
|
||||||
|
"labels" => [
|
||||||
|
"machineid" => $Strings->get("Machine ID", false),
|
||||||
|
"type" => $Strings->get("Type", false),
|
||||||
|
"date" => $Strings->get("Date", false),
|
||||||
|
"techuid" => $Strings->get("Technician", false),
|
||||||
|
"privatenotes" => $Strings->get("Private Notes", false),
|
||||||
|
"publicnotes" => $Strings->get("Public Notes", false)
|
||||||
|
],
|
||||||
|
"icons" => []
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function jsonSerialize() {
|
||||||
|
return $this->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function save() {
|
||||||
|
global $database;
|
||||||
|
if ($this->exists) {
|
||||||
|
$database->update("events", $this->event, ["historyid" => $this->id]);
|
||||||
|
} else {
|
||||||
|
$database->insert("events", $this->event);
|
||||||
|
$this->id = $database->id();
|
||||||
|
$this->exists = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getID(): string {
|
||||||
|
return $this->id . "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getMachineID(): string {
|
||||||
|
if (!empty($this->event["machineid"])) {
|
||||||
|
return $this->event["machineid"];
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getTypeID(): int {
|
||||||
|
if (!empty($this->event["eventid"])) {
|
||||||
|
return $this->event["eventid"] * 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getName(): string {
|
||||||
|
return $this->getTypes()[$this->getTypeID()] ?? "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getDate(): string {
|
||||||
|
if (!empty($this->event["date"])) {
|
||||||
|
return $this->event["date"];
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getTechUID(): string {
|
||||||
|
if (!empty($this->event["techuid"])) {
|
||||||
|
return $this->event["techuid"];
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getPublicNotes(): string {
|
||||||
|
if (!empty($this->event["publicnotes"])) {
|
||||||
|
return $this->event["publicnotes"];
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getPrivateNotes(): string {
|
||||||
|
if (!empty($this->event["privatenotes"])) {
|
||||||
|
return $this->event["privatenotes"];
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setMachineID($id) {
|
||||||
|
$this->event["machineid"] = $id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setTypeID(int $id) {
|
||||||
|
if (!empty($this->getTypes()[$id])) {
|
||||||
|
$this->event["eventid"] = $id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setTechUID($uid) {
|
||||||
|
$this->event["techuid"] = $uid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setDate(string $date) {
|
||||||
|
$this->event["date"] = date("Y-m-d H:i:s", strtotime($date));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setPrivateNotes(string $notes) {
|
||||||
|
$this->event["privatenotes"] = $notes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setPublicNotes(string $notes) {
|
||||||
|
$this->event["publicnotes"] = $notes;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Binary file not shown.
Before Width: | Height: | Size: 10 KiB After Width: | Height: | Size: 8.0 KiB |
Before Width: | Height: | Size: 2.9 KiB After Width: | Height: | Size: 4.7 KiB |
Loading…
Reference in New Issue