2017-08-08 20:48:45 +01:00
|
|
|
<?php
|
2021-12-07 22:14:12 -08:00
|
|
|
|
2017-08-08 20:48:45 +01:00
|
|
|
/*
|
2022-05-27 11:54:24 +03:00
|
|
|
* Made by Samerton
|
2017-08-08 20:48:45 +01:00
|
|
|
* https://github.com/NamelessMC/Nameless/
|
2022-10-13 20:00:41 +02:00
|
|
|
* NamelessMC version 2.1.0
|
2017-08-08 20:48:45 +01:00
|
|
|
*
|
|
|
|
|
* License: MIT
|
|
|
|
|
*
|
|
|
|
|
* Latest Posts Widget
|
|
|
|
|
*/
|
2021-12-07 22:14:12 -08:00
|
|
|
|
2017-08-08 20:48:45 +01:00
|
|
|
class LatestPostsWidget extends WidgetBase {
|
2020-12-14 14:20:28 -08:00
|
|
|
|
2021-10-07 14:02:46 -07:00
|
|
|
private Language $_language;
|
|
|
|
|
private Cache $_cache;
|
|
|
|
|
private User $_user;
|
2018-09-10 01:14:58 +01:00
|
|
|
|
2025-02-24 18:44:29 +00:00
|
|
|
public function __construct(Language $forum_language, TemplateEngine $engine, Cache $cache, User $user, Language $language) {
|
2017-10-06 20:22:17 +01:00
|
|
|
$this->_module = 'Forum';
|
2017-08-08 20:48:45 +01:00
|
|
|
$this->_name = 'Latest Posts';
|
|
|
|
|
$this->_description = 'Display latest posts from your forum.';
|
2022-10-13 20:00:41 +02:00
|
|
|
$this->_settings = ROOT_PATH . '/modules/Forum/widgets/admin/latest_posts.php';
|
2025-02-24 18:44:29 +00:00
|
|
|
$this->_engine = $engine;
|
2023-06-21 10:19:46 -07:00
|
|
|
$this->_cache = $cache;
|
|
|
|
|
$this->_user = $user;
|
|
|
|
|
$this->_language = $language;
|
2017-08-08 20:48:45 +01:00
|
|
|
|
2025-02-24 18:44:29 +00:00
|
|
|
$this->_engine->addVariables([
|
2023-03-23 20:01:23 -07:00
|
|
|
'LATEST_POSTS' => $forum_language->get('forum', 'latest_posts'),
|
|
|
|
|
'NO_POSTS_FOUND' => $forum_language->get('forum', 'no_posts_found'),
|
|
|
|
|
'BY' => $forum_language->get('forum', 'by'),
|
2021-10-29 22:00:05 -07:00
|
|
|
]);
|
2018-09-10 01:14:58 +01:00
|
|
|
}
|
|
|
|
|
|
2021-10-07 14:02:46 -07:00
|
|
|
public function initialise(): void {
|
2021-12-07 22:14:12 -08:00
|
|
|
$forum = new Forum();
|
2022-05-28 14:10:01 +02:00
|
|
|
$db = DB::getInstance();
|
2021-12-07 22:14:12 -08:00
|
|
|
$timeago = new TimeAgo(TIMEZONE);
|
|
|
|
|
|
|
|
|
|
// Get user group IDs
|
|
|
|
|
$user_groups = $this->_user->getAllGroupIds();
|
|
|
|
|
|
|
|
|
|
$this->_cache->setCache('forum_discussions_' . rtrim(implode('-', $user_groups), '-'));
|
2025-05-20 18:37:24 -07:00
|
|
|
$template_array = $this->_cache->fetch('discussions', function () use ($forum, $user_groups, $db, $timeago) {
|
2023-06-12 10:30:49 -06:00
|
|
|
$limit = (int) Settings::get('latest_posts_limit', 5, 'Forum');
|
2021-12-07 22:14:12 -08:00
|
|
|
// Generate latest posts
|
2023-01-18 10:21:41 -08:00
|
|
|
$discussions = $forum->getLatestDiscussions($user_groups, ($this->_user->isLoggedIn() ? $this->_user->data()->id : 0), $limit);
|
2021-12-07 22:14:12 -08:00
|
|
|
|
|
|
|
|
$template_array = [];
|
|
|
|
|
|
|
|
|
|
// Generate an array to pass to template
|
2023-01-18 10:21:41 -08:00
|
|
|
foreach ($discussions as $discussion) {
|
2021-12-07 22:14:12 -08:00
|
|
|
// Get the name of the forum from the ID
|
2022-06-14 20:30:54 -06:00
|
|
|
$forum_name = $db->get('forums', ['id', $discussion->forum_id])->results();
|
2022-03-31 21:56:57 +02:00
|
|
|
$forum_name = Output::getPurified($forum_name[0]->forum_title);
|
2021-12-07 22:14:12 -08:00
|
|
|
|
|
|
|
|
// Get the number of replies
|
2022-11-18 19:16:24 +00:00
|
|
|
$posts = $db->query('SELECT COUNT(*) as c FROM nl2_posts WHERE `topic_id` = ? AND `deleted` = 0', [$discussion->id])->first()->c;
|
2021-12-07 22:14:12 -08:00
|
|
|
|
|
|
|
|
// Is there a label?
|
2022-06-14 20:30:54 -06:00
|
|
|
if ($discussion->label != 0) {
|
2021-12-07 22:14:12 -08:00
|
|
|
// Get label
|
2022-06-14 20:30:54 -06:00
|
|
|
$label = $db->get('forums_topic_labels', ['id', $discussion->label])->results();
|
2021-12-07 22:14:12 -08:00
|
|
|
if (count($label)) {
|
|
|
|
|
$label = $label[0];
|
|
|
|
|
|
2022-05-28 14:10:01 +02:00
|
|
|
$label_html = $db->get('forums_labels', ['id', $label->label])->results();
|
2021-12-07 22:14:12 -08:00
|
|
|
if (count($label_html)) {
|
|
|
|
|
$label_html = $label_html[0]->html;
|
|
|
|
|
$label = str_replace('{x}', Output::getClean($label->name), $label_html);
|
|
|
|
|
} else {
|
|
|
|
|
$label = '';
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$label = '';
|
|
|
|
|
}
|
|
|
|
|
} else { // no
|
|
|
|
|
$label = '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add to array
|
2022-06-14 20:30:54 -06:00
|
|
|
$topic_creator = new User($discussion->topic_creator);
|
|
|
|
|
$last_reply_user = new User($discussion->topic_last_user);
|
2021-12-07 22:14:12 -08:00
|
|
|
$template_array[] = [
|
2022-06-14 20:30:54 -06:00
|
|
|
'topic_title' => Output::getClean($discussion->topic_title),
|
|
|
|
|
'topic_id' => $discussion->id,
|
|
|
|
|
'topic_created_rough' => $timeago->inWords($discussion->topic_date, $this->_language),
|
|
|
|
|
'topic_created' => date(DATE_FORMAT, $discussion->topic_date),
|
2021-12-07 22:14:12 -08:00
|
|
|
'topic_created_username' => $topic_creator->getDisplayname(),
|
|
|
|
|
'topic_created_mcname' => $topic_creator->getDisplayname(true),
|
2022-04-21 22:35:56 +02:00
|
|
|
'topic_created_style' => $topic_creator->getGroupStyle(),
|
2022-06-14 20:30:54 -06:00
|
|
|
'topic_created_user_id' => Output::getClean($discussion->topic_creator),
|
|
|
|
|
'locked' => $discussion->locked,
|
2021-12-07 22:14:12 -08:00
|
|
|
'forum_name' => $forum_name,
|
2022-06-14 20:30:54 -06:00
|
|
|
'forum_id' => $discussion->forum_id,
|
|
|
|
|
'views' => $discussion->topic_views,
|
2021-12-07 22:14:12 -08:00
|
|
|
'posts' => $posts,
|
|
|
|
|
'last_reply_avatar' => $last_reply_user->getAvatar(64),
|
2022-06-14 20:30:54 -06:00
|
|
|
'last_reply_rough' => $timeago->inWords($discussion->topic_reply_date, $this->_language),
|
|
|
|
|
'last_reply' => date(DATE_FORMAT, $discussion->topic_reply_date),
|
2021-12-07 22:14:12 -08:00
|
|
|
'last_reply_username' => $last_reply_user->getDisplayname(),
|
|
|
|
|
'last_reply_mcname' => $last_reply_user->getDisplayname(true),
|
2022-04-21 22:35:56 +02:00
|
|
|
'last_reply_style' => $last_reply_user->getGroupStyle(),
|
2022-06-14 20:30:54 -06:00
|
|
|
'last_reply_user_id' => Output::getClean($discussion->topic_last_user),
|
2021-12-07 22:14:12 -08:00
|
|
|
'label' => $label,
|
2022-06-14 20:30:54 -06:00
|
|
|
'link' => URL::build('/forum/topic/' . urlencode($discussion->id) . '-' . $forum->titleToURL($discussion->topic_title)),
|
|
|
|
|
'forum_link' => URL::build('/forum/forum/' . $discussion->forum_id),
|
2021-12-07 22:14:12 -08:00
|
|
|
'author_link' => $topic_creator->getProfileURL(),
|
|
|
|
|
'last_reply_profile_link' => $last_reply_user->getProfileURL(),
|
2022-06-14 20:30:54 -06:00
|
|
|
'last_reply_link' => URL::build('/forum/topic/' . $discussion->id . '-' . $forum->titleToURL($discussion->topic_title), 'pid=' . $discussion->last_post_id)
|
2021-10-29 22:00:05 -07:00
|
|
|
];
|
2021-12-07 22:14:12 -08:00
|
|
|
}
|
2018-09-10 01:14:58 +01:00
|
|
|
|
2025-05-20 18:37:24 -07:00
|
|
|
return $template_array;
|
|
|
|
|
}, 60);
|
2018-09-10 01:14:58 +01:00
|
|
|
|
2021-12-07 22:14:12 -08:00
|
|
|
// Generate HTML code for widget
|
2025-02-24 18:44:29 +00:00
|
|
|
$this->_engine->addVariable('LATEST_POSTS_ARRAY', $template_array);
|
2017-08-08 20:48:45 +01:00
|
|
|
|
2025-02-24 18:44:29 +00:00
|
|
|
$this->_content = $this->_engine->fetch('widgets/forum/latest_posts');
|
2017-08-08 20:48:45 +01:00
|
|
|
}
|
2022-03-29 15:09:21 -07:00
|
|
|
}
|