componentid = $componentid; if (Component::exists($componentid)) { $this->exists = true; $this->component = $database->get("components", ["machineid", "serial", "typeid [Int]", "tested", "capacity", "model", "price [Number]", "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 []; } /** * Render the component's info to HTML * @param bool $public */ public function toHTML(bool $public = true): string { global $Strings; $func = function($param) { return $param; }; $line = function($type) use ($public): string { global $Strings, $SETTINGS; if ($type == "TestedDate") { if (!empty($this->getTestedDate())) { return "" . $Strings->get("Tested On", false) . ": " . date($SETTINGS["date_format"], strtotime($this->getTestedDate())) . "
"; } return ""; } if ($type == "Price") { if (!empty($this->getPrice())) { return "" . $Strings->get("Price", false) . ": $" . number_format($this->getPrice(), 2) . "
"; } return ""; } if ($type == "PublicNotes") { if (!empty($this->getPublicNotes())) { return "
" . ($public ? $Strings->get("Notes", false) : $Strings->get("Public Notes", false)) . ":
" . htmlspecialchars($this->getPublicNotes()) . "
"; } return ""; } if ($type == "PrivateNotes") { if (!empty($this->getPrivateNotes()) && $public == false) { return "
" . $Strings->get("Private Notes", false) . ":
" . htmlspecialchars($this->getPublicNotes()) . "
"; } return ""; } if (empty($this->{"get$type"}())) { return ""; } return "" . $Strings->get($type, false) . ": " . htmlspecialchars($this->{"get$type"}()) . "
"; }; $html = ""; if ($public == false) { $html .= << Detach Edit HTML; } $html .= <<{$func($this->getTypeName())}
{$line("Model")} {$line("Capacity")} {$line("Serial")} {$line("TestedDate")} {$line("Price")} {$line("Manufacturer")} {$line("PrivateNotes")} {$line("PublicNotes")} HTML; return $html; } 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 delete() { global $database; $database->delete("components", ["compid" => $this->componentid]); } 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 ID number that is not in use. * Default: 681#####[check digit] * @global $database * @param int $min Optional minimum number. * @param int $max Optional maximum number. * @return int */ public static function generateId(int $min = 68100000, int $max = 68199999): int { global $database; do { $id = random_int($min, $max); // If default gen add check digit if ($id >= 68100000 && $id <= 68199999) { $id = ("$id" . CheckDigit::S10($id)) * 1; } } while ($database->has('components', ['compid' => $id]) || $database->has('machines', ['machineid' => $id])); return $id; } }