Skip to content

Commit 17686a4

Browse files
committed
Show news posts and packets on user profile page
1 parent ca1b393 commit 17686a4

File tree

3 files changed

+104
-7
lines changed

3 files changed

+104
-7
lines changed

src/controllers/User/View.php

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use \BNETDocs\Libraries\Exceptions\UnspecifiedViewException;
1010
use \BNETDocs\Libraries\Exceptions\UserNotFoundException;
1111
use \BNETDocs\Libraries\Exceptions\UserProfileNotFoundException;
12+
use \BNETDocs\Libraries\NewsPost;
13+
use \BNETDocs\Libraries\Packet;
1214
use \BNETDocs\Libraries\Router;
1315
use \BNETDocs\Libraries\User as UserLib;
1416
use \BNETDocs\Libraries\UserProfile;
@@ -138,8 +140,12 @@ protected function getUserInfo(UserViewModel &$model) {
138140
$model->documents = ($model->sum_documents ?
139141
Document::getDocumentsByUserId($this->user_id) : null
140142
);
141-
$model->news_posts = ($model->sum_news_posts ? true : null);
142-
$model->packets = ($model->sum_packets ? true : null);
143+
$model->news_posts = ($model->sum_news_posts ?
144+
NewsPost::getNewsPostsByUserId($this->user_id): null
145+
);
146+
$model->packets = ($model->sum_packets ?
147+
Packet::getPacketsByUserId($this->user_id) : null
148+
);
143149
$model->servers = ($model->sum_servers ? true : null);
144150

145151
// Process documents
@@ -162,6 +168,48 @@ protected function getUserInfo(UserViewModel &$model) {
162168
--$i;
163169
}
164170
}
171+
172+
// Process news posts
173+
if ($model->news_posts) {
174+
// Alphabetically sort the documents
175+
usort($model->news_posts, function($a, $b){
176+
$a1 = $a->getTitle();
177+
$b1 = $b->getTitle();
178+
if ($a1 == $b1) return 0;
179+
return ($a1 < $b1 ? -1 : 1);
180+
});
181+
182+
// Remove documents that are not published
183+
$i = count($model->news_posts) - 1;
184+
while ($i >= 0) {
185+
if (!($model->news_posts[$i]->getOptionsBitmask()
186+
& NewsPost::OPTION_PUBLISHED)) {
187+
unset($model->news_posts[$i]);
188+
}
189+
--$i;
190+
}
191+
}
192+
193+
// Process packets
194+
if ($model->packets) {
195+
// Alphabetically sort the documents
196+
usort($model->packets, function($a, $b){
197+
$a1 = $a->getPacketName();
198+
$b1 = $b->getPacketName();
199+
if ($a1 == $b1) return 0;
200+
return ($a1 < $b1 ? -1 : 1);
201+
});
202+
203+
// Remove documents that are not published
204+
$i = count($model->packets) - 1;
205+
while ($i >= 0) {
206+
if (!($model->packets[$i]->getOptionsBitmask()
207+
& Packet::OPTION_PUBLISHED)) {
208+
unset($model->packets[$i]);
209+
}
210+
--$i;
211+
}
212+
}
165213
}
166214

167215
}

src/libraries/NewsPost.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,47 @@ public static function getAllNews($reverse) {
174174
return null;
175175
}
176176

177+
public static function getNewsPostsByUserId($user_id) {
178+
if (!isset(Common::$database)) {
179+
Common::$database = DatabaseDriver::getDatabaseObject();
180+
}
181+
try {
182+
$stmt = Common::$database->prepare("
183+
SELECT
184+
`category_id`,
185+
`content`,
186+
`created_datetime`,
187+
`edited_count`,
188+
`edited_datetime`,
189+
`id`,
190+
`options_bitmask`,
191+
`title`,
192+
`user_id`
193+
FROM `news_posts`
194+
WHERE `user_id` = :user_id
195+
ORDER BY `id` ASC;
196+
");
197+
$stmt->bindParam(":user_id", $user_id, PDO::PARAM_INT);
198+
if (!$stmt->execute()) {
199+
throw new QueryException("Cannot query news posts by user id");
200+
}
201+
$ids = [];
202+
$objects = [];
203+
while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
204+
$ids[] = (int) $row->id;
205+
$objects[] = new self($row);
206+
Common::$cache->set(
207+
"bnetdocs-newspost-" . $row->id, serialize($row), 300
208+
);
209+
}
210+
$stmt->closeCursor();
211+
return $objects;
212+
} catch (PDOException $e) {
213+
throw new QueryException("Cannot query news posts by user id", $e);
214+
}
215+
return null;
216+
}
217+
177218
public function getCategory() {
178219
return new NewsCategory($this->category_id);
179220
}

src/templates/User/View.phtml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,27 +96,35 @@ require("./header.inc.phtml");
9696
<article>
9797
<header>Documents</header>
9898
<section>
99-
<ul>
99+
<table><tbody>
100100
<?php foreach ($this->getContext()->documents as $document) { ?>
101-
<li><a href="<?php echo $document->getURI(); ?>"><?php echo htmlspecialchars($document->getTitle()); ?></a></li>
101+
<tr><td><a href="<?php echo $document->getURI(); ?>"><?php echo htmlspecialchars($document->getTitle()); ?></a></td></tr>
102102
<?php } ?>
103-
</ul>
103+
</tbody></table>
104104
</section>
105105
</article>
106106
<?php } ?>
107107
<?php if (isset($this->getContext()->news_posts)) { ?>
108108
<article>
109109
<header>News Posts</header>
110110
<section>
111-
<?php require("./NYI.inc.phtml"); ?>
111+
<table><tbody>
112+
<?php foreach ($this->getContext()->news_posts as $news_post) { ?>
113+
<tr><td><a href="<?php echo $news_post->getURI(); ?>"><?php echo htmlspecialchars($news_post->getTitle()); ?></a></td></tr>
114+
<?php } ?>
115+
</tbody></table>
112116
</section>
113117
</article>
114118
<?php } ?>
115119
<?php if (isset($this->getContext()->packets)) { ?>
116120
<article>
117121
<header>Packets</header>
118122
<section>
119-
<?php require("./NYI.inc.phtml"); ?>
123+
<table><tbody>
124+
<?php foreach ($this->getContext()->packets as $packet) { ?>
125+
<tr><td><a href="<?php echo $packet->getURI(); ?>"><?php echo htmlspecialchars($packet->getPacketDirectionTag(), ENT_HTML5, "UTF-8"); ?> <?php echo htmlspecialchars($packet->getPacketId(true), ENT_HTML5, "UTF-8"); ?> <?php echo htmlspecialchars($packet->getPacketName()); ?></a></td></tr>
126+
<?php } ?>
127+
</tbody></table>
120128
</section>
121129
</article>
122130
<?php } ?>

0 commit comments

Comments
 (0)