Skip to content

Commit

Permalink
Implement servers on user profile
Browse files Browse the repository at this point in the history
  • Loading branch information
carlbennett committed Feb 11, 2019
1 parent e66220a commit f325f69
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
24 changes: 19 additions & 5 deletions src/controllers/User/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use \BNETDocs\Libraries\Exceptions\UserProfileNotFoundException;
use \BNETDocs\Libraries\NewsPost;
use \BNETDocs\Libraries\Packet;
use \BNETDocs\Libraries\Server;
use \BNETDocs\Libraries\User as UserLib;
use \BNETDocs\Libraries\UserProfile;
use \BNETDocs\Models\User\View as UserViewModel;
Expand Down Expand Up @@ -133,7 +134,9 @@ protected function getUserInfo(UserViewModel &$model) {
$model->packets = ($model->sum_packets ?
Packet::getPacketsByUserId($model->user_id) : null
);
$model->servers = ($model->sum_servers ? true : null);
$model->servers = ($model->sum_servers ?
Server::getServersByUserId($model->user_id) : null
);

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

// Process news posts
if ($model->news_posts) {
// Alphabetically sort the documents
// Alphabetically sort the news posts
usort($model->news_posts, function($a, $b){
$a1 = $a->getTitle();
$b1 = $b->getTitle();
if ($a1 == $b1) return 0;
return ($a1 < $b1 ? -1 : 1);
});

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

// Process packets
if ($model->packets) {
// Alphabetically sort the documents
// Alphabetically sort the packets
usort($model->packets, function($a, $b){
$a1 = $a->getPacketName();
$b1 = $b->getPacketName();
if ($a1 == $b1) return 0;
return ($a1 < $b1 ? -1 : 1);
});

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

// Process servers
if ($model->servers) {
// Alphabetically sort the servers
usort($model->servers, function($a, $b){
$a1 = $a->getName();
$b1 = $b->getName();
if ($a1 == $b1) return 0;
return ($a1 < $b1 ? -1 : 1);
});
}

}

}
41 changes: 41 additions & 0 deletions src/libraries/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,47 @@ public function getPort() {
return $this->port;
}

public static function getServersByUserId($user_id) {
if (!isset(Common::$database)) {
Common::$database = DatabaseDriver::getDatabaseObject();
}
try {
$stmt = Common::$database->prepare('
SELECT
`address`,
`created_datetime`,
`id`,
`label`,
`port`,
`status_bitmask`,
`type_id`,
`updated_datetime`,
`user_id`
FROM `servers`
WHERE `user_id` = :user_id
ORDER BY `id` ASC;
');
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
if (!$stmt->execute()) {
throw new QueryException('Cannot query servers by user id');
}
$ids = [];
$objects = [];
while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
$ids[] = (int) $row->id;
$objects[] = new self($row);
Common::$cache->set(
'bnetdocs-server-' . $row->id, serialize($row), 300
);
}
$stmt->closeCursor();
return $objects;
} catch (PDOException $e) {
throw new QueryException('Cannot query servers by user id', $e);
}
return null;
}

public function getStatusBitmask() {
return $this->status_bitmask;
}
Expand Down
6 changes: 5 additions & 1 deletion src/templates/User/View.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ require("./header.inc.phtml");
<article>
<header>Servers</header>
<section>
<?php require("./NYI.inc.phtml"); ?>
<table><tbody>
<?php foreach ($this->getContext()->servers as $server) { ?>
<tr><td><a href="<?php echo $server->getURI(); ?>"><?php echo htmlspecialchars($server->getName()); ?></a></td></tr>
<?php } ?>
</tbody></table>
</section>
</article>
<?php } ?>
Expand Down

0 comments on commit f325f69

Please sign in to comment.