This repository has been archived by the owner on Mar 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.php
75 lines (64 loc) · 2.13 KB
/
type.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
include_once('database.php');
include_once('misc.php');
class Type {
private $id;
private $name;
private $unit;
private $conversionFactor;
private static $types = null;
private function __construct($id, $name, $unit, $conversionFactor) {
$this->id = intval($id);
$this->name = $name;
$this->unit = $unit;
$this->conversionFactor = $conversionFactor;
}
public static function getType($name) {
if (self::$types === null) {
return get(self::getTypes()[$name], null);
}
return get(self::$types[$name], null);
}
public function getId() {
return $this->id;
}
public static function getTypes() {
global $conn;
if (self::$types === null) {
self::$types = array();
if ($result = $conn->query("SELECT id, name, unit, conversion_factor FROM type;")) {
while ($row = $result->fetch_object()) {
self::$types[$row->name] = new Type($row->id, $row->name, $row->unit, $row->conversion_factor);
}
$result->close();
}
ksort(self::$types);
}
return self::$types;
}
public static function htmlTable() {
echo "<table><tr><th></th><th>Name</th><th>ID</th><th>Unit</th><th>Factor</th></tr>";
foreach(self::getTypes() as $type) {
$name = htmlspecialchars($type->name);
$id = $type->id;
$unit = htmlspecialchars($type->unit);
$factor = $type->conversionFactor;
echo "<tr><td></td><td>${name}</td><td>${id}</td><td>${unit}</td><td>${factor}</td></tr>";
}
echo "</table>";
}
public function dict() {
return array('name' => $this->name,
'id' => $this->id,
'unit' => $this->unit,
'conversion_factor' => $this->conversionFactor);
}
public static function dictAll() {
$result = array();
foreach(self::getTypes() as $type) {
$result[$type->name] = $type->dict();
}
return $result;
}
}
?>