mc-cms-namelessmc/modules/Forum/widgets/LatestPostsWidget.php

124 lines
5.7 KiB
PHP
Raw Permalink Normal View History

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/
* 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;
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.';
$this->_settings = ROOT_PATH . '/modules/Forum/widgets/admin/latest_posts.php';
$this->_engine = $engine;
Reactions revamp & profile widgets (#3272) * Initial work on reactions revamp and profile widgets * Remove unneeded changes * Remove unneeded change * fix style * Render emojis in reaction list StaffCP * wip minecraft account profile widget, sticky widgets on profile page * Add server name and IP to last seen * remove * rename to reaction score to match member list * align reactions table contents center * fix * wip - nicer abstraction, widgets never query db, skin 3d viewer (undecided) * wip - make reaction modals ajax, copy new reaction bar to profile posts * wip reaction modals * wip proper sorting * wip modals * wip get reaction submission working on profile wall posts * add back helpful and creative default reactions * reduce query amount on view topic * remove debug statement * wip * wip * simplify ProfileWidget pages * Revert forum dropdown * fix css syntax * fix emoji title and alt being strange * add back order input * sort widgets by order in list * fix settings link always showing * phpdoc * add widget name to exception * insert default widget data if not found * missing end if * properly cache forum news per-group * fix * fix cache key, news permission check * fix styling * wip * respect reaction ordering * wip * wip * support custom reaction scores, update member list to properly calculate * don't hardcode words * wip * allow multiple reactions * redirect to post * code style * phpstan * wip * remove hardcoded terms + fix copy * use npm for skinview3d * create ReactionContext and refactor * fix cache * docblocks * fix spelling * pr amendments
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
$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
]);
}
2021-10-07 14:02:46 -07:00
public function initialise(): void {
2021-12-07 22:14:12 -08:00
$forum = new Forum();
$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), '-'));
$template_array = $this->_cache->fetch('discussions', function () use ($forum, $user_groups, $db, $timeago) {
$limit = (int) Settings::get('latest_posts_limit', 5, 'Forum');
2021-12-07 22:14:12 -08:00
// Generate latest posts
$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
foreach ($discussions as $discussion) {
2021-12-07 22:14:12 -08:00
// Get the name of the forum from the ID
$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
$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?
if ($discussion->label != 0) {
2021-12-07 22:14:12 -08:00
// Get label
$label = $db->get('forums_topic_labels', ['id', $discussion->label])->results();
2021-12-07 22:14:12 -08:00
if (count($label)) {
$label = $label[0];
$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
$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[] = [
'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),
'topic_created_style' => $topic_creator->getGroupStyle(),
'topic_created_user_id' => Output::getClean($discussion->topic_creator),
'locked' => $discussion->locked,
2021-12-07 22:14:12 -08:00
'forum_name' => $forum_name,
'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),
'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),
'last_reply_style' => $last_reply_user->getGroupStyle(),
'last_reply_user_id' => Output::getClean($discussion->topic_last_user),
2021-12-07 22:14:12 -08:00
'label' => $label,
'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(),
'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
}
return $template_array;
}, 60);
2021-12-07 22:14:12 -08:00
// Generate HTML code for widget
$this->_engine->addVariable('LATEST_POSTS_ARRAY', $template_array);
2017-08-08 20:48:45 +01:00
$this->_content = $this->_engine->fetch('widgets/forum/latest_posts');
2017-08-08 20:48:45 +01:00
}
}