forked from AustinWise/fleet_links
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFleet.php
More file actions
157 lines (138 loc) · 4.35 KB
/
Copy pathFleet.php
File metadata and controls
157 lines (138 loc) · 4.35 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
require_once('DataManager.php');
class Fleet {
function __construct() {
}
// $Id is an integer, but it is stored as a string since it won't fit in int
// and when converted to a float sometimes it does not convert correctly
public $Id;
public $AllianceId;
public $Name;
public $Added;
private $inDatabase = FALSE;
public static function Get($id) {
$conn = DataManager::GetInstance()->GetConnection();
$stmt = $conn->prepare('SELECT id, allianceId, name, added FROM fleet WHERE id = ?');
$stmt->bind_param('d', $id);
$stmt->execute();
$stmt->bind_result($id, $allianceId, $name, $added);
if (!$stmt->fetch()) {
throw new Exception("Fleet not found.");
}
$f = new Fleet();
Fleet::fill($f, $id, $allianceId, $name, $added);
$stmt->close();
return $f;
}
// fills a Fleet object with data from mysql
private static function fill(&$f, $id, $allianceId, $name, $added) {
$f->Id = $id;
$f->AllianceId = (int)$allianceId;
$f->Name = $name;
$f->Added = strtotime($added);
$f->inDatabase = TRUE;
}
// Returns array contain all the Fleets.
public static function GetAll() {
$query = DataManager::GetInstance()->Query("SELECT id, allianceId, name, added FROM fleet");
$items = array();
while ($assoc = $query->fetch_assoc()) {
$f = new Fleet();
Fleet::fill($f, $assoc['id'], $assoc['allianceId'], $assoc['name'], $assoc['added']);
$items[] = $f;
}
$query->close();
return $items;
}
public static function GetFleetsForAlliance($allianceId) {
if (!is_int($allianceId)) {
throw new Exception("allianceId was not an int.");
}
$conn = DataManager::GetInstance()->GetConnection();
$stmt = $conn->prepare('SELECT id, allianceid, name, added FROM fleet WHERE added > ? AND allianceid = ?');
$stmt->bind_param('si', DataManager::FormatTimestampForSql(LastDowntimeMidpoint()), $allianceId);
$stmt->execute();
$stmt->bind_result($id, $allianceId, $name, $added);
$fleets = array();
while ($stmt->fetch()) {
$f = new Fleet();
Fleet::fill($f, $id, $allianceId, $name, $added);
$fleets[] = $f;
}
$stmt->close();
return $fleets;
}
public static function DeleteOldFleets() {
$conn = DataManager::GetInstance()->GetConnection();
$stmt = $conn->prepare('DELETE FROM fleet WHERE added < ?');
$stmt->bind_param('s', DataManager::GetInstance()->FormatTimestampForSql(LastDowntimeMidpoint()));
$stmt->execute();
$stmt->close();
}
public static function DeleteFleet($id) {
$conn = DataManager::GetInstance()->GetConnection();
$stmt = $conn->prepare('DELETE FROM fleet WHERE id = ?');
$stmt->bind_param('d', $id);
$stmt->execute();
$stmt->close();
}
public function Delete() {
if (!$this->inDatabase)
return;
Fleet::DeleteFleet($this->Id);
$this->inDatabase = FALSE;
}
// Returns TRUE if the Fleet record was added or updated in
// the database, FALSE otherwise.
// Update not implemented yet.
public function Save() {
if (!$this->Validate())
throw new Exception('Fleet not valid; unable to save.');
$conn = DataManager::GetInstance()->GetConnection();
if (!$this->inDatabase) {
$stmt = $conn->prepare('INSERT INTO fleet (id, allianceId, name, added) VALUES (?, ?, ?, ?)');
$stmt->bind_param('diss', $this->Id, $this->AllianceId, $this->Name, DataManager::FormatTimestampForSql($this->Added));
$stmt->execute();
$rows = $stmt->affected_rows;
$stmt->close();
if ($rows === 1) {
$this->inDatabase = TRUE;
return TRUE;
}
else
return FALSE;
}
else {
$stmt = $conn->prepare('UPDATE fleet SET allianceId=?, name=?, added=? WHERE id=?');
$stmt->bind_param('issd', $this->AllianceId, $this->Name, DataManager::FormatTimestampForSql($this->Added), $this->Id);
$stmt->execute();
$rows = $stmt->affected_rows;
$stmt->close();
if ($rows === 1)
return TRUE;
else
return FALSE;
}
return FALSE;
}
// Returns TRUE if the Fleet is valid and ready to be
// saved to the database.
public function Validate() {
if (!isset($this->Id) || !preg_match('/^\d+$/', $this->Id))
return FALSE;
if (!isset($this->AllianceId) || !is_int($this->AllianceId))
return FALSE;
if (!isset($this->Added) || !is_int($this->Added))
return FALSE;
if (isset($this->Name) && is_string($this->Name)) {
$strlen = strlen($this->Name);
if ($strlen < 1 || $strlen > 50)
return FALSE;
}
else {
return FALSE;
}
return TRUE;
}
}
?>