Skip to content

Commit f325f69

Browse files
committed
Implement servers on user profile
1 parent e66220a commit f325f69

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

src/controllers/User/View.php

+19-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use \BNETDocs\Libraries\Exceptions\UserProfileNotFoundException;
99
use \BNETDocs\Libraries\NewsPost;
1010
use \BNETDocs\Libraries\Packet;
11+
use \BNETDocs\Libraries\Server;
1112
use \BNETDocs\Libraries\User as UserLib;
1213
use \BNETDocs\Libraries\UserProfile;
1314
use \BNETDocs\Models\User\View as UserViewModel;
@@ -133,7 +134,9 @@ protected function getUserInfo(UserViewModel &$model) {
133134
$model->packets = ($model->sum_packets ?
134135
Packet::getPacketsByUserId($model->user_id) : null
135136
);
136-
$model->servers = ($model->sum_servers ? true : null);
137+
$model->servers = ($model->sum_servers ?
138+
Server::getServersByUserId($model->user_id) : null
139+
);
137140

138141
// Process documents
139142
if ($model->documents) {
@@ -158,15 +161,15 @@ protected function getUserInfo(UserViewModel &$model) {
158161

159162
// Process news posts
160163
if ($model->news_posts) {
161-
// Alphabetically sort the documents
164+
// Alphabetically sort the news posts
162165
usort($model->news_posts, function($a, $b){
163166
$a1 = $a->getTitle();
164167
$b1 = $b->getTitle();
165168
if ($a1 == $b1) return 0;
166169
return ($a1 < $b1 ? -1 : 1);
167170
});
168171

169-
// Remove documents that are not published
172+
// Remove news posts that are not published
170173
$i = count($model->news_posts) - 1;
171174
while ($i >= 0) {
172175
if (!($model->news_posts[$i]->getOptionsBitmask()
@@ -179,15 +182,15 @@ protected function getUserInfo(UserViewModel &$model) {
179182

180183
// Process packets
181184
if ($model->packets) {
182-
// Alphabetically sort the documents
185+
// Alphabetically sort the packets
183186
usort($model->packets, function($a, $b){
184187
$a1 = $a->getPacketName();
185188
$b1 = $b->getPacketName();
186189
if ($a1 == $b1) return 0;
187190
return ($a1 < $b1 ? -1 : 1);
188191
});
189192

190-
// Remove documents that are not published
193+
// Remove packets that are not published
191194
$i = count($model->packets) - 1;
192195
while ($i >= 0) {
193196
if (!($model->packets[$i]->getOptionsBitmask()
@@ -198,6 +201,17 @@ protected function getUserInfo(UserViewModel &$model) {
198201
}
199202
}
200203

204+
// Process servers
205+
if ($model->servers) {
206+
// Alphabetically sort the servers
207+
usort($model->servers, function($a, $b){
208+
$a1 = $a->getName();
209+
$b1 = $b->getName();
210+
if ($a1 == $b1) return 0;
211+
return ($a1 < $b1 ? -1 : 1);
212+
});
213+
}
214+
201215
}
202216

203217
}

src/libraries/Server.php

+41
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,47 @@ public function getPort() {
147147
return $this->port;
148148
}
149149

150+
public static function getServersByUserId($user_id) {
151+
if (!isset(Common::$database)) {
152+
Common::$database = DatabaseDriver::getDatabaseObject();
153+
}
154+
try {
155+
$stmt = Common::$database->prepare('
156+
SELECT
157+
`address`,
158+
`created_datetime`,
159+
`id`,
160+
`label`,
161+
`port`,
162+
`status_bitmask`,
163+
`type_id`,
164+
`updated_datetime`,
165+
`user_id`
166+
FROM `servers`
167+
WHERE `user_id` = :user_id
168+
ORDER BY `id` ASC;
169+
');
170+
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
171+
if (!$stmt->execute()) {
172+
throw new QueryException('Cannot query servers by user id');
173+
}
174+
$ids = [];
175+
$objects = [];
176+
while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
177+
$ids[] = (int) $row->id;
178+
$objects[] = new self($row);
179+
Common::$cache->set(
180+
'bnetdocs-server-' . $row->id, serialize($row), 300
181+
);
182+
}
183+
$stmt->closeCursor();
184+
return $objects;
185+
} catch (PDOException $e) {
186+
throw new QueryException('Cannot query servers by user id', $e);
187+
}
188+
return null;
189+
}
190+
150191
public function getStatusBitmask() {
151192
return $this->status_bitmask;
152193
}

src/templates/User/View.phtml

+5-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ require("./header.inc.phtml");
135135
<article>
136136
<header>Servers</header>
137137
<section>
138-
<?php require("./NYI.inc.phtml"); ?>
138+
<table><tbody>
139+
<?php foreach ($this->getContext()->servers as $server) { ?>
140+
<tr><td><a href="<?php echo $server->getURI(); ?>"><?php echo htmlspecialchars($server->getName()); ?></a></td></tr>
141+
<?php } ?>
142+
</tbody></table>
139143
</section>
140144
</article>
141145
<?php } ?>

0 commit comments

Comments
 (0)