Skip to content

Commit

Permalink
Add Latest Profile Posts widget
Browse files Browse the repository at this point in the history
  • Loading branch information
tadhgboyle committed Aug 4, 2020
1 parent 95430d2 commit f698c5e
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 11 deletions.
2 changes: 1 addition & 1 deletion core/classes/Timeago.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/

/*
* Modified by Samerton for NamelessMC (https://github.com/samerton/NamelessMC)
* Modified by Samerton for NamelessMC (https://github.com/NamelessMC/Nameless)
*/


Expand Down
4 changes: 3 additions & 1 deletion custom/languages/EnglishUK/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@
'confirm_leave' => 'Are you sure you want to leave this conversation?',
'one_or_more_users_blocked' => 'You cannot send private messages to at least one member of the conversation.',
'messages' => 'Messages',

'latest_profile_posts' => 'Latest Profile Posts',
'no_profile_posts' => 'No profile posts.',

/*
* Infractions area
*/
Expand Down
2 changes: 1 addition & 1 deletion custom/templates/DefaultRevamp/profile.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
<div class="metadata">
<span class="date" data-toggle="tooltip" data-content="{$post.date}">{$post.date_rough}</span>
</div>
<div class="text forum_post">
<div class="text forum_post" id="post-{$post.id}/" name="post-{$post.id}/">
{$post.content}
</div>
<div class="actions">
Expand Down
27 changes: 27 additions & 0 deletions custom/templates/DefaultRevamp/widgets/profile_posts.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<div class="ui fluid card" id="widget-latest-posts">
<div class="content">
<h4 class="ui header">{$LATEST_PROFILE_POSTS}</h4>
<div class="description">
{if isset($POSTS)}
{foreach from=$POSTS name=latest_posts item=post}
<div class="ui relaxed list">
<div class="item">
<img class="ui mini circular image" src="{$post.avatar}" alt="{$post.username}">
<div class="content">
<a class="header" href="{$post.link}">{$post.content}</a>
<div class="ui wide popup">
<h4 class="ui header">{$post.topic_title}</h4>
{$BY|capitalize} <a href="{$post.user_profile_link}" style="{$post.username_style}">{$post.username}</a> | {$post.last_reply}
</div>
<a href="{$post.user_profile_link}" style="{$post.username_style}}" data-poload="{$USER_INFO_URL}{$post.user_id}">{$post.username}</a>
&middot; <span data-toggle="tooltip" data-content="{$post.date_ago}">{$post.ago}</span>
</div>
</div>
</div>
{/foreach}
{else}
{$NO_PROFILE_POSTS}
{/if}
</div>
</div>
</div>
9 changes: 1 addition & 8 deletions error.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@
<div class="jumbotron">
<div style="text-align:center">
<h2><?php echo $language->get('errors', 'fatal_error_title'); ?></h2>
<?php
if($user->isLoggedIn() && $user->hasPermission('admincp.errors')){
?>
<h4><?php echo $language->get('errors', 'fatal_error_message_admin'); ?></h4>
<div class="card card-default">
<div class="card-body">
Expand All @@ -53,11 +50,7 @@
</div>
<?php
echo '<div style="overflow-x: scroll;">' . str_replace('{x}', Output::getClean($errfile), $language->get('errors', 'in_file')) . '</div>' . str_replace('{x}', Output::getClean($errline), $language->get('errors', 'on_line')) . '<hr />';
} else {
?>
<h4><?php echo $language->get('errors', 'fatal_error_message_user'); ?></h4>
<?php
}

?>
<div class="btn-group" role="group" aria-label="...">
<button href="#" class="btn btn-primary btn-lg" onclick="javascript:history.go(-1)"><?php echo $language->get('general', 'back'); ?></button>
Expand Down
5 changes: 5 additions & 0 deletions modules/Core/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,11 @@ public function onPageLoad($user, $pages, $cache, $smarty, $navs, $widgets, $tem
$module_pages = $widgets->getPages('Discord');
$widgets->add(new DiscordWidget($module_pages, $language, $cache, $discord));

// Profile Posts
require_once(ROOT_PATH . '/modules/Core/widgets/ProfilePostsWidget.php');
$module_pages = $widgets->getPages('Latest Profile Posts');
$widgets->add(new ProfilePostsWidget($module_pages, $smarty, $language, $cache, $user, new Timeago(TIMEZONE)));

// Online staff
require_once(ROOT_PATH . '/modules/Core/widgets/OnlineStaff.php');
$module_pages = $widgets->getPages('Online Staff');
Expand Down
89 changes: 89 additions & 0 deletions modules/Core/widgets/ProfilePostsWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/*
* Made by Aberdeener
* https://github.com/NamelessMC/Nameless/
* NamelessMC version 2.0.0-pr8
*
* License: MIT
*
* Profile Posts Widget
*/
class ProfilePostsWidget extends WidgetBase {
private $_cache, $_smarty, $_language, $_user, $_timeago;

public function __construct($pages = array(), $smarty, $language, $cache, $user, $timeago) {
$this->_language = $language;
$this->_smarty = $smarty;
$this->_cache = $cache;
$this->_user = $user;
$this->_timeago = $timeago;

parent::__construct($pages);

// Get order
$order = DB::getInstance()->query('SELECT `order` FROM nl2_widgets WHERE `name` = ?', array('Latest Profile Posts'))->first();

// Set widget variables
$this->_module = 'Core';
$this->_name = 'Latest Profile Posts';
$this->_location = 'right';
$this->_description = 'Display the latest profile posts on your site.';
$this->_order = $order->order;
}

public function initialise() {
// Generate HTML code for widget
if ($this->_user->isLoggedIn()) {
$user_group = $this->_user->data()->group_id;
} else {
$user_group = null;
}

if ($user_group) {
$cache_name = 'profile_posts_' . $user_group;
$user_id = $this->_user->data()->id;
} else {
$cache_name = 'profile_posts_guest';
$user_id = 0;
}
$this->_cache->setCache($cache_name);

$posts_array = array();

if ($this->_cache->isCached('profile_posts_' . $user_id)) {
$posts_array = $this->_cache->retrieve('profile_posts_' . $user_id);
} else {
$posts = DB::getInstance()->query('SELECT * FROM nl2_user_profile_wall_posts ORDER BY time DESC LIMIT 5')->results();
foreach ($posts as $post) {

if ($user_group) {
if ($this->_user->isBlocked($post->author_id, $this->_user->data()->id)) continue;
if ($this->_user->isPrivateProfile($post->author_id) && !$this->_user->hasPermission('profile.private.bypass')) continue;
} else if ($this->_user->isPrivateProfile($post->author_id)) continue;

$posts_array[] = array(
'avatar' => $this->_user->getAvatar($post->author_id, "../", 64),
'username' => $this->_user->idToNickname($post->author_id),
'username_style' => $this->_user->getGroupClass($post->author_id),
'content' => Util::truncate($post->content, 20),
'link' => URL::build('/profile/' . $this->_user->idToName($post->author_id) . '/#post-' . $post->id),
'date_ago' => date('d M Y, H:i', $post->time),
'user_id' => $post->author_id,
'user_profile_link' => URL::build('/profile/' . $this->_user->idToName($post->author_id)),
'ago' => $this->_timeago->inWords(date('d M Y, H:i', $post->time), $this->_language->getTimeLanguage())
);
}
$this->_cache->store('profile_posts_' . $user_id, $posts_array, 120);
}
if (count($posts_array) >= 1) {
$this->_smarty->assign(array(
'POSTS' => $posts_array
));
}
$this->_smarty->assign(array(
'LATEST_PROFILE_POSTS' => $this->_language->get('user', 'latest_profile_posts'),
'NO_PROFILE_POSTS' => $this->_language->get('user', 'no_profile_posts')
));
$this->_content = $this->_smarty->fetch('widgets/profile_posts.tpl');;
}
}

0 comments on commit f698c5e

Please sign in to comment.