You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

253 lines
7.7 KiB
PHP

<?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 Component implements JsonSerializable {
private $componentid = "";
private $component = [];
private $exists = false;
public function __construct($componentid) {
global $database;
$this->componentid = $componentid;
if (Component::exists($componentid)) {
$this->exists = true;
$this->component = $database->get("components", ["machineid", "serial", "typeid", "tested", "capacity", "model", "price", "manufacturer", "publicnotes", "privatenotes"], ["compid" => $componentid]);
}
}
public static function create(): Component {
return new Component(Component::generateId());
}
public static function exists($id): bool {
global $database;
return $database->has('components', ['compid' => $id]);
}
public static function getTypes() {
global $database;
$types = $database->select("component_types", ["typeid (id)", "typename (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(),
"serial" => $this->getSerial(),
"type" => $this->getTypeID(),
"tested" => $this->getTestedDate(),
"capacity" => $this->getCapacity(),
"model" => $this->getModel(),
"price" => $this->getPrice(),
"manufacturer" => $this->getManufacturer(),
"publicnotes" => $this->getPublicNotes(),
"privatenotes" => $this->getPrivateNotes()
],
"formdata" => [
"types" => $this->getTypes(),
"inputtypes" => [
"machineid" => "text",
"serial" => "text",
"type" => "number",
"tested" => "datetime",
"capacity" => "text",
"model" => "text",
"price" => "number",
"manufacturer" => "text",
"privatenotes" => "textarea",
"publicnotes" => "textarea"
],
"labels" => [
"machineid" => $Strings->get("Machine ID", false),
"serial" => $Strings->get("Serial", false),
"type" => $Strings->get("Type", false),
"tested" => $Strings->get("Tested", false),
"capacity" => $Strings->get("Capacity", false),
"model" => $Strings->get("Model", false),
"price" => $Strings->get("Price", false),
"manufacturer" => $Strings->get("Manufacturer", 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("components", $this->component, ["compid" => $this->componentid]);
} else {
$data = $this->component;
$data["compid"] = $this->componentid;
$database->insert("components", $data);
$this->exists = true;
}
}
public function getID(): string {
return $this->componentid . "";
}
public function getMachineID(): string {
if (!empty($this->component["machineid"])) {
return $this->component["machineid"];
}
return "";
}
public function getSerial(): string {
if (!empty($this->component["serial"])) {
return $this->component["serial"];
}
return "";
}
public function getTypeID(): int {
if (!empty($this->component["typeid"])) {
return $this->component["typeid"] * 1;
}
return 0;
}
public function getTypeName(): string {
return Component::getTypes()[$this->getTypeID()];
}
public function getTestedDate(): string {
if (!empty($this->component["tested"])) {
return $this->component["tested"];
}
return "";
}
public function getCapacity(): string {
if (!empty($this->component["capacity"])) {
return $this->component["capacity"];
}
return "";
}
public function getModel(): string {
if (!empty($this->component["model"])) {
return $this->component["model"];
}
return "";
}
public function getPrice(): float {
if (!empty($this->component["price"])) {
return $this->component["price"] * 1.0;
}
return 0.0;
}
public function getManufacturer(): string {
if (!empty($this->component["manufacturer"])) {
return $this->component["manufacturer"];
}
return "";
}
public function getPublicNotes(): string {
if (!empty($this->component["publicnotes"])) {
return $this->component["publicnotes"];
}
return "";
}
public function getPrivateNotes(): string {
if (!empty($this->component["privatenotes"])) {
return $this->component["privatenotes"];
}
return "";
}
public function setMachineID($id) {
$this->component["machineid"] = $id;
}
public function setSerial(string $serial) {
$this->component["serial"] = $serial;
}
public function setTypeID(int $id) {
if (!empty(Component::getTypes()[$id])) {
$this->component["typeid"] = $id;
}
}
public function setTestedDate(string $tested) {
$this->component["tested"] = date("Y-m-d H:i:s", strtotime($tested));
}
public function clearTestedDate() {
$this->component["tested"] = null;
}
public function setCapacity(string $capacity) {
$this->component["capacity"] = $capacity;
}
public function setModel(string $model) {
$this->component["model"] = $model;
}
public function setPrice(float $price) {
$this->component["price"] = $price;
}
public function setManufacturer(string $manufacturer) {
$this->component["manufacturer"] = $manufacturer;
}
public function setPrivateNotes(string $notes) {
$this->component["privatenotes"] = $notes;
}
public function setPublicNotes(string $notes) {
$this->component["publicnotes"] = $notes;
}
/**
* 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);
;
do {
$id = random_int(1000000000, 9999999999);
} while ($database->has('components', ['compid' => $id]) || $database->has('machines', ['machineid' => $id]));
return $id;
}
}