Allow lookup by serial number

master
Skylar Ittner 4 years ago
parent a39f28b445
commit 2cf5183f14

@ -35,6 +35,31 @@ class Machine {
return $database->has('machines', ['machineid' => $id]);
}
/**
* Check if a given serial number can identify exactly one machine.
* @global $database $database
* @param type $serial
* @return bool
*/
public static function serialExists(string $serial): bool {
global $database;
return $database->has('machines', ['serial' => $serial]) && $database->count('machines', ['serial' => $serial]) == 1;
}
/**
* Convert a serial into a machine ID.
* @global $database $database
* @param string $serial
* @return string|bool machine ID if found, otherwise false.
*/
public static function getIDFromSerial(string $serial) {
global $database;
if (Machine::serialExists($serial)) {
return $database->get('machines', 'machineid', ['serial' => $serial]) . "";
}
return false;
}
public function save() {
global $database;
if ($this->exists) {

@ -23,7 +23,7 @@ require_once __DIR__ . "/../lib/required_public.php";
<div class="row justify-content-center">
<div class="col-12 col-md-8">
<?php
if (empty($_GET["id"]) || !Machine::exists($_GET["id"])) {
if (empty($_GET["id"]) || (!Machine::exists($_GET["id"]) && !Machine::serialExists($_GET["id"]))) {
?>
<div class="card">
<h3 class="card-header d-flex">
@ -33,7 +33,7 @@ require_once __DIR__ . "/../lib/required_public.php";
</h3>
<div class="card-body">
<?php
if (!empty($_GET["id"]) && !Machine::exists($_GET["id"])) {
if (!empty($_GET["id"])) {
?>
<p class="text-danger">No machine with ID <code><?php echo htmlspecialchars($_GET['id']); ?></code> could be found.</p>
<?php
@ -50,7 +50,11 @@ require_once __DIR__ . "/../lib/required_public.php";
</div>
<?php
} else {
if (Machine::exists($_GET["id"])) {
$machine = new Machine($_GET['id']);
} else {
$machine = new Machine(Machine::getIDFromSerial($_GET['id']));
}
?>
<div class="card">
<h3 class="card-header d-flex">

Loading…
Cancel
Save