Message
Author
Board
Date
January 18, 2025, 01:56:06 AM
January 18, 2025, 12:31:57 AM
January 15, 2025, 10:10:06 PM
January 11, 2025, 01:01:33 PM
January 09, 2025, 11:59:16 PM

Recent Topics [PHP Block]

Started by Chen Zhen, December 28, 2024, 04:34:03 PM

Previous topic - Next topic

Chen Zhen

Likely scenarios for using this block code may include on the home page or on the forum page when the existing SMF IC recent topics is disabled.

Block Type: PHP
global $scripturl, $settings, $sourcedir, $boarddir, $user_info, $txt;

$boards = null; // array(1,2,3);
$blockDateFormat = 'd-m-Y';
$limit = 5;
$type = 'topics'; // topics or posts
$block_settings['unique_topic'] = true;
$title_css = 'font-weight: bold;text-decoration: underline;padding: 0.1rem 0 rem 0.1rem 0rem;font-size: smaller;';
$row_css = 'padding: 0.1rem 0.05rem 0.1rem 0.05rem;font-size: smaller;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;';
$row_time_css = 'padding: 0.1rem 0 rem 0.1rem 0rem;font-size: smaller;overflow-x: clip;';
$border = 'border-bottom: 1px solid rgba(255, 0, 0, .15);';
$indentLeft = 'text-indent: 5px;';
$paddingRight = 'padding-right: 2rem;';
$title = sprintf('Recent %s', ucfirst($type));

require_once($boarddir . '/SSI.php');
$type = 'ssi_recent' . ucfirst($type);
$items = $type($limit, null, $boards, 'array');
$latest_posts = array();
if (empty($items)) {
echo 'No posts available with the given parameters';
}
else {
$items[count($items) - 1]['is_last'] = true;

echo '
<div class="cat_bar">
<h3 class="catbg">
<a href="', $scripturl, '?action=recent"><span class="main_icons recent_posts"></span> ', $title, '</a>
</h3>
</div>
<div id="recent_posts_content2">';

if ($limit == 1)
{
echo '
<p id="infocenter_onepost2" class="inline" style="overflow: hidden;">
<a href="', $scripturl, '?action=recent">', $txt['recent_view'], '</a> ', sprintf($txt['is_recent_updated'], '&quot;' . $item['href'][0]['href'] . '&quot;'), ' (', $item[0]['time'], ')<br>
</p>';
}
// Show lots of posts.
else
{
echo '
<div class="ic_recentposts2" style="display: grid;grid-template-columns: 41% 15% 29% 15%;width: 100%;overflow: hidden;white-space:nowrap;">
<div style="' . $title_css . $indentLeft . '" class="recentpost">', $txt['message'], '</div>
<div style="' . $title_css . '" class="recentposter">', $txt['author'], '</div>
<div style="' . $title_css . '" class="recentboard">', $txt['board'], '</div>
<div style="' . $title_css . $paddingRight . '" class="recenttime">', $txt['date'], '</div>';

foreach ($items as $key => $post) {
$row_css1 = $key < (count($latest_posts)-1) ? $row_css . $border : $row_css;
$row_css2 = $key < (count($latest_posts)-1) ? $row_time_css . $border : $row_time_css;
$newDateString = stripos($post['time'], 'today') !== FALSE ? date($blockDateFormat) : date($blockDateFormat, strtotime(strip_tags($post['time'])));
echo '
<div style="' . $row_css1 . $indentLeft . '" class="recentpost recentpostblock blocktextscroll"><strong><a href="' . $post['href'] . '">' . $post['subject'] . '</a></strong></div>
<div style="' . $row_css1 . '" class="recentposter blocktextscroll">', $post['poster']['link'], '</div>
<div style="' . $row_css1 . '" class="recentboard recentboardblock blocktextscroll">', $post['board']['link'], '</div>
<div style="' . $row_css2 . $paddingRight . '" class="recenttime blockposttime">', $post['time'], '</div>
<div style="display: none;' . $row_css1 . $paddingRight . '" class="blockdatex' . $key . '">', $newDateString, '</div>';
}
echo '
</div>';
}
echo '
</div>
<script>
$(document).ready(function() {
$(".blocktextscroll").css("overflow-x", "auto");
$(".blocktextscroll").on("scroll", function() {
$(this).find("a").each(function() {
$(this).text(function(index, currentText) {
return currentText.substr(0, (currentText.length - 4)) + "...";
});
});
});
$(".blocktextscroll").each(function() {
$(this).css({"white-space":"nowrap","overflow":"hidden","text-overflow":"ellipsis","padding":"0.1rem 0.05rem 0.1rem 0.05rem","font-size":"smaller"});
});
if ($(".blockposttime")[0].scrollWidth >= $(".blockposttime")[0].clientWidth) {
var xdate, xday, xmonth, xyear;
$(".blockposttime").each(function(i, obj) {
xdate = $(".blockdatex" + i).first().html();
$(this).html(xdate);
});
}
$(".blockposttime").on("scroll", function() {
var xdate, xday, xmonth, xyear;
$(".blockposttime").each(function(i, obj) {
xdate = $(".blockdatex" + i).first().html();
$(this).html(xdate);
});
});
var mobileBlockCheck = ("ontouchstart" in document.documentElement && /mobi/i.test(navigator.userAgent));
if (mobileBlockCheck) {
$(".recentposter").css("display", "none");
$(".ic_recentposts2").css("grid-template-columns", "41% 37% 22%");
var xdate, xday, xmonth, xyear;
$(".blockposttime").each(function(i, obj) {
xdate = $(".blockdatex" + i).first().html();
$(this).html(xdate);
});
}
});
</script>';
}