mc-cms-namelessmc/modules/Core/widgets/ProfilePostsWidget.php

107 lines
4 KiB
PHP
Raw Permalink Normal View History

2020-08-03 23:43:19 -07:00
<?php
/*
2022-05-27 11:54:24 +03:00
* Made by Aberdeener
2020-08-03 23:43:19 -07:00
* https://github.com/NamelessMC/Nameless/
* NamelessMC version 2.2.0
2020-08-03 23:43:19 -07:00
*
* License: MIT
*
* Profile Posts Widget
*/
2021-12-07 22:14:12 -08:00
2020-08-03 23:43:19 -07:00
class ProfilePostsWidget extends WidgetBase {
2020-12-14 14:20:28 -08:00
2021-10-30 16:44:05 -07:00
private Cache $_cache;
private Language $_language;
private User $_user;
private TimeAgo $_timeago;
2020-08-03 23:43:19 -07:00
public function __construct(TemplateEngine $engine, Language $language, Cache $cache, User $user, TimeAgo $timeago) {
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->_module = 'Core';
$this->_name = 'Latest Profile Posts';
$this->_description = 'Display the latest profile posts on your site.';
$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->_language = $language;
2020-08-03 23:43:19 -07:00
$this->_cache = $cache;
$this->_user = $user;
$this->_timeago = $timeago;
}
2021-10-30 16:44:05 -07:00
public function initialise(): void {
2020-08-03 23:43:19 -07:00
// Generate HTML code for widget
if ($this->_user->isLoggedIn()) {
$user_id = $this->_user->data()->id;
} else {
$user_id = 0;
}
2020-12-19 19:25:59 +01:00
$this->_cache->setCache('profile_posts_widget');
2020-08-03 23:43:19 -07:00
$posts_array = $this->_cache->fetch('profile_posts_' . $user_id, function () {
if ($this->_user->isLoggedIn()) {
if ($this->_user->hasPermission('profile.private.bypass')) {
$posts = DB::getInstance()->query('SELECT * FROM nl2_user_profile_wall_posts ORDER BY `time` DESC LIMIT 5')->results();
} else {
$posts = DB::getInstance()->query(
<<<SQL
SELECT *
FROM nl2_user_profile_wall_posts
WHERE `user_id` NOT IN (
SELECT `id` FROM nl2_users WHERE `private_profile` = 1
)
AND EXISTS (SELECT `id` FROM nl2_blocked_users WHERE `user_blocked_id` = `user_id`) = 0
ORDER BY `time` DESC LIMIT 5
SQL,
)->results();
2021-12-07 22:14:12 -08:00
}
} else {
$posts = DB::getInstance()->query(
<<<SQL
SELECT *
FROM nl2_user_profile_wall_posts
WHERE `user_id` NOT IN (
SELECT `id` FROM nl2_users WHERE `private_profile` = 1
)
ORDER BY `time` DESC LIMIT 5
SQL,
)->results();
}
2020-08-03 23:43:19 -07:00
$posts_array = [];
foreach ($posts as $post) {
$post_author = new User($post->author_id);
$post_user = new User($post->user_id);
2025-05-15 01:12:52 +02:00
$content = EventHandler::executeEvent(new RenderContentEvent($post->content))->content;
$link = rtrim($post_user->getProfileURL(), '/');
2021-10-29 22:00:05 -07:00
$posts_array[] = [
'avatar' => $post_author->getAvatar(),
2020-11-03 21:30:55 +01:00
'username' => $post_author->getDisplayname(),
'username_style' => $post_author->getGroupStyle(),
2025-05-15 01:12:52 +02:00
'content' => Text::truncate(strip_tags($content), 20),
'link' => $link . '/#post-' . $post->id . '/',
'date_ago' => date(DATE_FORMAT, $post->time),
2020-08-03 23:43:19 -07:00
'user_id' => $post->author_id,
2020-11-03 21:30:55 +01:00
'user_profile_link' => $post_author->getProfileURL(),
Use i18n for translations (#2658) * Dependency updates * Start i18n conversion * Switch to ellipsis character * Add Dutch language json * Remove Dutch PHP language files + fix variables * Further variable fixes * WIP i18n Language class conversion * Convert Cookie Consent module to new language format * Further progress including timeago translations * Misc * Replace {x} * Replace more {x} usage * {{time}} this took way too long * replace last {x} * Remove comments So searching for {x} produces relevant results * forgot to save * move to onPageLoad * Accept both string and int * types in doc * Remove unused (broken?) function * Don't convert to string first * Last arg doesn't seem to be used anywhere * Time `{x}` replacements * Email `{x}` replacements * misc `{x}` replacements * misc `{x}` replacements * misc `{x}` replacements * misc `{x}` replacements * misc `{x}` replacements * misc `{x}` replacements * misc `{x}` replacements * misc `{x}` replacements * misc `{x}` replacements * remove comments (makes searching for "{x}" nicer) * misc `{x}` replacements * JS `{x}` replacements * Cleanup language initialization * Change language constant + add pluralForm support * Misc language update things * Add script to find unused terms * Check all quote types * Remove most english translations from non-english languages * Also match with more args * Missed some apparently * Missed some (more) apparently * Misc work * Add script to convert *all* language files to json * Fix language codes * Remove a lot of unused terms * Remove a lot more unused terms * Convert everything to JSON, delete old files, get installer working with new system * Fix general configuration language dropdown + email messages * Fix page load time, fix language term `set` method * Fix discord integration command * Misc * Misc * Uncomment RTL definition * Misc * Convert links * Convert links * Convert links * Convert links * Misc work (bolding, fixes, etc) * Style fix * Fix variable * Fix #2668 * Fix old page load timers * Fix installer group sync injector error * Convert integration language terms * Fix more integration language things * Re-add upgrade script language things * Resolve PR comments * Remove unnecessary date() * Resolve some PR issues * Fix endpoint * Style * Fix Semantic -> Fomantic * Fix Semantic -> Fomantic * Return locale short code, not language id/name Only the short code is interesting to the API, return short code in user info endpoint just like in the website info endpoint. Also renamed to locale because it's language + region * Revert "warn people to not translate old language files" This reverts commit 690b77afff5ff7cc2f931d3b49feaa9a9072002b. * Fix query when there are no filters * Update 200-pr12.php * Update footer.php * insert time into string using placeholder * Fix getDisplayname case * Clean username * PR suggestions * Include newly created language in converted languages (migration) * PR suggestions * PR suggestions * Update init.php * Fix default language Co-authored-by: samerton <samerton@users.noreply.github.com> Co-authored-by: Robin <robinslot@hotmail.nl> Co-authored-by: Robin <robin+git@rkslot.nl> Co-authored-by: Partydragen <adrian_kili@outlook.com>
2022-04-23 06:27:10 -07:00
'ago' => $this->_timeago->inWords($post->time, $this->_language)
2021-10-29 22:00:05 -07:00
];
2020-08-03 23:43:19 -07:00
}
return $posts_array;
}, 120);
2020-08-03 23:43:19 -07:00
if (count($posts_array) >= 1) {
$this->_engine->addVariables([
'PROFILE_POSTS_ARRAY' => $posts_array
2021-10-29 22:00:05 -07:00
]);
2020-08-03 23:43:19 -07:00
}
$this->_engine->addVariables([
2020-08-03 23:43:19 -07:00
'LATEST_PROFILE_POSTS' => $this->_language->get('user', 'latest_profile_posts'),
'NO_PROFILE_POSTS' => $this->_language->get('user', 'no_profile_posts')
2021-10-29 22:00:05 -07:00
]);
$this->_content = $this->_engine->fetch('widgets/profile_posts');
2020-08-03 23:43:19 -07:00
}
}