Tool to lookup computer information using a unique ID number.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.9 KiB

<?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 Machine {
private $machineid = "";
private $machine = [];
private $history = [];
private $components = [];
public function __construct($machineid) {
global $database;
if ($database->has('machines', ['machineid' => $machineid])) {
$this->machineid = $machineid;
$this->machine = $database->get('machines', ['machineid', 'notes', 'model', 'condition', 'price', 'os', 'serial', 'manufacturer'], ['machineid' => $machineid]);
$this->history = $database->select('history', ['[>]event_types' => 'eventid'], ['historyid', 'date', 'eventname', 'notes'], ['machineid' => $machineid]);
$this->components = $database->select('components', ['[>]component_types' => 'typeid'], ['compid', 'serial', 'typename', 'tested', 'notes', 'capacity', 'model'], ['machineid' => $machineid]);
} else {
throw new Exception("No machine with that ID number could be found.");
}
}
public function getHistory() {
return $this->history;
}
public function getMachineInfo() {
$info = [];
$info['machineid'] = $this->machineid;
if (!empty($this->machine['model'])) {
$info['model'] = $this->machine['model'];
}
if (!empty($this->machine['condition'])) {
$info['condition'] = $this->machine['condition'];
}
if (!empty($this->machine['price'])) {
$info['price'] = $this->machine['price'];
}
if (!empty($this->machine['os'])) {
$info['os'] = $this->machine['os'];
}
if (!empty($this->machine['serial'])) {
$info['serial'] = $this->machine['serial'];
}
if (!empty($this->machine['manufacturer'])) {
$info['manufacturer'] = $this->machine['manufacturer'];
}
$info['notes'] = $this->machine['notes'];
return $info;
}
public function getComponents() {
$info = [];
foreach ($this->components as $c) {
$info[$c['compid']] = [
'serial' => $c['serial'],
'type' => $c['typename'],
'notes' => $c['notes']
];
if (!empty($c['tested'])) {
$info[$c['compid']]['tested'] = $c['tested'];
}
if (!empty($c['capacity'])) {
$info[$c['compid']]['capacity'] = $c['capacity'];
}
if (!empty($c['model'])) {
$info[$c['compid']]['model'] = $c['model'];
}
}
return $info;
}
public function addHistory($date, $event, $notes = "") {
global $database;
if (strtotime($date) === false) {
throw new Exception("Invalid date.");
}
$date = date("Y-m-d H:i:s", strtotime($date));
if (!$database->has('event_types', ['eventid' => $event])) {
throw new Exception("Invalid event type.");
}
$event = (int) $event;
if (empty($notes)) {
$notes = "";
}
$database->insert('history', ['date' => $date, 'eventid' => $event, 'notes' => $notes, 'machineid' => $this->machineid]);
}
public function addComponent($serial, $type, $tested = null, $notes = "", $capacity = null, $model = null) {
global $database;
if (empty($serial)) {
throw new Exception("Invalid serial number.");
}
if (!$database->has('component_types', ['typeid' => $type])) {
throw new Exception("Invalid component type.");
}
$type = (int) $type;
if (!is_null($tested)) {
if (strtotime($tested) === false) {
throw new Exception("Invalid tested date.");
}
$tested = date("Y-m-d H:i:s", strtotime($tested));
}
if (empty($notes)) {
$notes = "";
}
if (empty($capacity)) {
$capacity = null;
}
if (empty($model)) {
$model = null;
}
$database->insert('components', ['serial' => $serial, 'typeid' => $type, 'tested' => $tested, 'notes' => $notes, 'capacity' => $capacity, 'model' => $model, 'machineid' => $this->machineid]);
}
/**
* Generate a random machine ID number that is not in use.
* @global $database
* @param int $min Optional minimum number.
* @param int $max Optional maximum number.
* @return int
*/
public static function generateId(int $min = 1000000000, int $max = 9999999999): int {
global $database;
$id = random_int(1000000000, 9999999999);
while ($database->has('machines', ['machineid' => $id])) {
$id = random_int(1000000000, 9999999999);
}
return $id;
}
}