SimplePortal

Customization => Blocks and Modifications => Block Requests => Topic started by: owenenene on January 13, 2017, 04:46:58 AM

Title: A few custom blocks I have need some fixes
Post by: owenenene on January 13, 2017, 04:46:58 AM
Hi guys, I'm no good a coder, but I tried and I've got a few custom blocks to work :)

They work the way I'd like them to work, however they have allot of excess code in them and I was wondering if someone could remove the excess code for me.

Number 1:
Code: [Select]
/* Display Frequent Posters for SMF 2.0x by Underdog @http://askusaquestion.net */
/* Format: Horizontal javascript pages */
/* v1.1 */

global $smcFunc, $scripturl, $boardurl;

/* Adjust these */
$number_users = 25;
$lang = ' Posts';
$show_posts = 'Show posts from this user';
$show_profile = '';
$none = 'No users selected';
$show_name = true;
$avatar_width = 50;
$avatar_height = 50;
$row_height = 50; /* <-- adjust depending on amount of text shown under avatar */
$user_name_max = 35; /* <-- max chars for user name display */
$per_row = 5;
$rows = 5;
$prev = 'Prev';
$next = 'Next';

/* Do not edit below this line */

$query = '(users.posts > 0 AND is_activated = 1)';
$datum = array( 'id_member', 'posts', 'real_name', 'avatar', 'filename', 'id_attach');
$x = 0;
$count = 0;
$users = array();
$limit = !empty($number_users) ? (int)$number_users : 20;
$cell_width = 100 / (int)$per_row;

$pages = '<script type="text/javascript"><!--
        var pager = new frequentPager("new", '.(int)$rows.');
        pager.init();
        pager.showPageNav("pager", "l8stPagePosition");
        pager.showPage(1);
    //--></script>';

/* Some js to control the pages */
echo '<script type="text/javascript">
function frequentPager(tableName, itemsPerPage) {
    this.tableName = tableName;
    this.itemsPerPage = itemsPerPage;
    this.currentPage = 1;
    this.pages = 0;
    this.initiated = false;
   
    this.showRecords = function(from, to) {       
        var rows = document.getElementById(tableName).rows;
        // i starts from 1 to skip table header row';
echo "
        for (var i = 1; i < rows.length; i++) {
            if (i < from || i > to) 
                rows[i].style.display = 'none';
            else
                rows[i].style.display = '';
        }";
echo '
    }
   
    this.showPage = function(pageNumberFrequent) {
    if (! this.initiated) {
    alert("not initiated");
    return;
    }';
echo "
        var oldPageAnchor = document.getElementById('pg'+this.currentPage);
        oldPageAnchor.className = 'smalltext pg-normal';
       
        this.currentPage = pageNumberFrequent;
        var newPageAnchor = document.getElementById('pg'+this.currentPage);
        newPageAnchor.className = 'largetext pg-selected';
       
        var from = (pageNumberFrequent - 1) * itemsPerPage + 1;
        var to = from + itemsPerPage - 1;
        this.showRecords(from, to);
    }   
   
    this.prev = function() {
        if (this.currentPage > 1)
            this.showPage(this.currentPage - 1);
    }
   
    this.next = function() {
        if (this.currentPage < this.pages) {
            this.showPage(this.currentPage + 1);
        }
    }                       
   
    this.init = function() {
        var rows = document.getElementById(tableName).rows;
        var records = (rows.length - 1);
        this.pages = Math.ceil(records / itemsPerPage);
        this.initiated = true;
    }";
echo '
    this.showPageNav = function(pagerName, positionId) {
    if (! this.initiated) {
    alert("not initiated");
    return;
    }
    var element = document.getElementById(positionId);';
echo "
    var pagerHtml = '<span onclick=";
echo '"';
echo "' + pagerName + '.prev();";
echo '" class="smalltext pg-normal"> « ', $prev, ' </span> | ';
echo "';
        for (var page = 1; page <= this.pages; page++)
            pagerHtml += '<span id=";
echo '"';
echo "pg' + page + '";
echo '" class="smalltext pg-normal" onclick="';
echo "' + pagerName + '.showPage(' + page + ');";
echo '">';
echo "' + page + '</span> | ';
        pagerHtml += '<span onclick=";
echo '"';
echo "'+pagerName+'.next();";
echo '" class="smalltext pg-normal"> ', $next,' »</span>';
echo "';           
       
        element.innerHTML = pagerHtml;
    }
}
function regenerateFrequent()
{
window.location.reload()
}

function regenerateFrequentPosters()
{
if (document.layers)
{
appearFrequent()
setTimeout(";
echo '"window.onresize=regenerateFrequent",450)
}
}

function changeFrequentText(whichcontent)
{
if (document.all||document.getElementById)
{
cross_el=document.getElementById? document.getElementById("descriptions"):document.all.descriptions
cross_el.innerHTML=';
echo "'<div style=";
echo '"font-family:Arial Narrow Bold;font-size:small;">';
echo "'+whichcontent+'</div>'
}
else if (document.layers)
{
document.d1.document.d2.document.write('<div style=";
echo '"font-family:Arial Narrow Bold;font-size:small;">';
echo "'+whichcontent+'</div>')
document.d1.document.d2.document.close()
}

}

function appearFrequent()
{
document.d1.visibility='show'
}
</script>";
/* end of js controller */

$result = $smcFunc['db_query']('', "SELECT users.id_member, users.posts, users.real_name, users.avatar, users.is_activated, att.id_attach, att.filename FROM {db_prefix}members AS users
LEFT JOIN {db_prefix}attachments AS att ON (att.id_member = users.id_member)
WHERE {$query} ORDER BY users.posts DESC LIMIT {$limit}");
while ($val = $smcFunc['db_fetch_assoc']($result))
{                     
foreach ($datum as $data)
{
if (empty($val[$data]))
$val[$data] = false;

$users[$count][$data] = $val[$data];                                               
}
$count++;
}
$smcFunc['db_free_result']($result);
echo '
<script type="text/javascript">
<!--
window.onload=regenerateFrequentPosters
//-->
</script>';
if ($count == 0)
echo '
<div style="text-align:center">', $none, '</div>';
else
{
echo '
<style style="text/css">
img.opacity {
opacity: 1;
filter: alpha(opacity=100);
}

img.opacity:hover {
opacity: 0.5;
filter: alpha(opacity=50);
}
</style>
<table id ="new" style="width:100%;">
';
foreach ($users as $user)
{
if ((empty($user['posts'])) || !$user['posts'])
continue;
if ($x == 0)
echo '
<tr>';

$x++;
 
if (!$user['avatar'] && !$user['filename'])
$user['avatar'] = $boardurl . '/avatars/noavatar.gif';
elseif (!$user['avatar'])
$user['avatar'] = $scripturl . '?action=dlattach;attach='.$user['id_attach'].';type=avatar';
elseif (substr($user['avatar'], 0, 4) != 'http')
$user['avatar'] = $boardurl .'/avatars/' . $user['avatar'];

$name = strlen($user['real_name']) >= (int)$user_name_max ? $name = substr($user['real_name'],0,(int)$user_name_max).'...':$user['real_name'];
echo '
<td style="text-align:center;width:'.(int)$cell_width.'%;">
<div style="height:'.$row_height.'px;overflow:hidden;">
<a href="'.$scripturl.'?action=profile;u='.$user['id_member'].'" title="'.$show_profile,$user['real_name'].'">
<img src="'. $user['avatar'] . '" style="max-height:'.$avatar_height.'px;max-width:'.$avatar_width.'px;" alt="" class="opacity" />
</a>
</div>
</td>';
if ($x > ((int)$per_row -1))
{
$x=0;
echo '
</tr>';
}
}
echo '
</table>';

if (((int)$per_row * (int)$rows) != (int)$number_users)
echo '
<br />
<div id="l8stPagePosition" style="text-align:center;"> </div>
<div style="text-align:center;">', $pages, '</div>';
}
This is the top posters block, you can find a live example at: www.gameguides.pro/f right side, near the bottom titled "TOP POSTERS"

Instead of going for the same old boring "list" style of top posters, nearly every forum has, I went for a more unique approach. I went and made it into a "wall" of profile pictures, 5 in each row, 5 collumns, total of 25 profile pictures will be displayed. Hovering over a display picture will highlight it, and clicking it will redirect to the  profile. The profiles are in order, from left to right.

The code has allot of excess, from what I can see.. :s



Number 2.

This is a recent topics block I changed about.

Code: [Select]
<?php
/*
Block: Recent posts with post preview
Author: Blue @ Simple Portal.net
Version: 1.7
*/

/* [SETUP WHAT YOU WANT HERE] */

$topics_posts 1; // TOPICS - 1 | MESSAGES - 2
$limit 5; // How many recent posts do you want to output?
$number 70; // How many characters do you want to output?
$exclude_boards null; // IF null guests can see all boards | IF you want to hide some boards use: array( ID1, ID2, ID3) where ID is the board's ID
$htmlspecialcharacters false// Do you need to show html special characters like Greek characters?
$default_avatar 'http://www.gameguides.pro/f/Themes/default/images/default_avatar.png';

// Scroll Enable? Height and Speed?
$scrolling 2;       // ENABLE - 1 | DISABLE - 2
$speed 3;           // SLOW - 1 | MEDIUM - 10 | FAST - 20
$height "150px";
$scrollbar 2;       // ENABLE - 1 | DISABLE - 2

// Do you want to translate it to your own language? :P
$text['sportal_false'] = 'Simple Portal not found';
$text['board'] = 'in';
$text['who'] = 'by';

/* [STOP!] - THIS IS THE END OF SETUP */

//Only code from now on ;)
global $smcFunc$scripturl$sourcedir$modSettings$user_info$settings$context;

// Lets see if you are using Simple Portal. If not...well...go get them :D
if (!file_exists($sourcedir '/PortalBlocks.php'))
{
echo $text['sportal_false'];
return;
}

// Let's grab some database results
if ($topics_posts == 1) {

if (is_array($exclude_boards) || (int) $exclude_boards === $exclude_boards)
{
$exclude_boards is_array($exclude_boards) ? $exclude_boards : array($exclude_boards);
}
elseif ($exclude_boards != null)
{
$output_method $exclude_boards;
$exclude_boards = array();
}

$posts_result $smcFunc['db_query']('''
   SELECT m.poster_time, m.id_msg, t.id_member_updated, m.subject, m.body, m.id_topic, b.name, t.id_last_msg, u.avatar, g.online_color,' 
. ($user_info['is_guest'] ? '1 AS is_read, 0 AS new_from' '
IFNULL(u.real_name, m.poster_name) AS poster_name,
IFNULL(lt.id_msg, IFNULL(lmr.id_msg, -1)) + 1 AS new_from'
) . '
   FROM {db_prefix}topics AS t
   LEFT JOIN {db_prefix}boards AS b ON (t.id_board = b.id_board)
   LEFT JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_last_msg)
   LEFT JOIN {db_prefix}members AS u ON (t.id_member_updated = u.id_member)
   LEFT JOIN {db_prefix}membergroups AS g ON (g.id_group = CASE WHEN u.id_group = 0 THEN u.id_post_group ELSE u.id_group END)
   ' 
. (!$user_info['is_guest'] ? '
LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = m.id_topic AND lt.id_member = ' 
$user_info['id'] . ')
LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = b.id_board AND lmr.id_member = ' 
$user_info['id'] . ')' '') . '     
   WHERE m.approved=1'   
. (empty($exclude_boards) ? '' '
AND b.id_board NOT IN ({array_int:exclude_boards})'
) . ' AND {query_see_board}
   ORDER BY t.id_last_msg DESC
   LIMIT ' 
$limit
array(
'exclude_boards' => empty($exclude_boards) ? '' $exclude_boards,
)   
   );
$posts = array();
while (
$row_posts $smcFunc['db_fetch_assoc']($posts_result))
{

      global 
$memberContext;
      
loadMemberData($row_posts['id_member_updated']);
      
loadMemberContext($row_posts['id_member_updated']);

   
$posts[] = array(
  'id' => $row_posts['id_member_updated'],
      
'username' => '<a style="color: ' $row_posts['online_color'] . ';" href="' $scripturl '?action=profile;u=' $row_posts['id_member_updated'] . '">' $row_posts['poster_name'] . '</a>',
      
'subject' => '<a style="font-weight: bold;" title="' $text['board'] . '&nbsp;' $row_posts['name'] .'" href="' $scripturl '?topic=' $row_posts['id_topic'] . '.msg' $row_posts['id_last_msg'] . ';topicseen#new">' $row_posts['subject'] . '</a>',
      
'body' => $row_posts['body'],
  'avatar' => $row_posts['avatar'] == '' $memberContext[$row_posts['id_member_updated']]['avatar']['href'] : (stristr($row_posts['avatar'], 'http://') ? $row_posts['avatar'] : $modSettings['avatar_url'] . '/' $row_posts['avatar']),
  'board' => $row_posts['name'], 
  'time' => timeformat($row_posts['poster_time']),
  'new' => !empty($row_posts['is_read'])
   );
}
$smcFunc['db_free_result']($posts_result);

} else {

if (is_array($exclude_boards) || (int) $exclude_boards === $exclude_boards)
{
$exclude_boards is_array($exclude_boards) ? $exclude_boards : array($exclude_boards);
}
elseif ($exclude_boards != null)
{
$output_method $exclude_boards;
$exclude_boards = array();
}

$posts_result $smcFunc['db_query']('''
   SELECT m.poster_time, m.id_msg, m.id_member, m.subject, m.body, m.id_topic, b.name, b.id_board, u.avatar, g.online_color,' 
. ($user_info['is_guest'] ? '1 AS is_read, 0 AS new_from' '
IFNULL(u.real_name, m.poster_name) AS poster_name,
IFNULL(lt.id_msg, IFNULL(lmr.id_msg, -1)) + 1 AS new_from'
) . '
   FROM {db_prefix}messages AS m
   LEFT JOIN {db_prefix}boards AS b ON (m.id_board = b.id_board)
   LEFT JOIN {db_prefix}members AS u ON (m.id_member = u.id_member)
   LEFT JOIN {db_prefix}membergroups AS g ON (g.id_group = CASE WHEN u.id_group = 0 THEN u.id_post_group ELSE u.id_group END)
   ' 
. (!$user_info['is_guest'] ? '
LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = m.id_topic AND lt.id_member = ' 
$user_info['id'] . ')
LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = b.id_board AND lmr.id_member = ' 
$user_info['id'] . ')' '') . '   
   WHERE m.approved=1'   
. (empty($exclude_boards) ? '' '
AND b.id_board NOT IN ({array_int:exclude_boards})'
) . ' AND {query_see_board}
   ORDER BY m.id_msg DESC
   LIMIT ' 
$limit
array(
'exclude_boards' => empty($exclude_boards) ? '' $exclude_boards,
)   
   );
$posts = array();
while (
$row_posts $smcFunc['db_fetch_assoc']($posts_result))
{

      global 
$memberContext;
      
loadMemberData($row_posts['id_member']);
      
loadMemberContext($row_posts['id_member']);

   
$posts[] = array(
  'id' => $row_posts['id_member'],
      
'username' => '<a style="color: ' $row_posts['online_color'] . ';" href="' $scripturl '?action=profile;u=' $row_posts['id_member'] . '">' $row_posts['poster_name'] . '</a>',
      
'subject' => '<a style="font-weight: bold;" title="' $text['board'] . '&nbsp;' $row_posts['name'] .'" href="' $scripturl '?topic=' $row_posts['id_topic'] . '.msg' $row_posts['id_msg'] . ';topicseen#new">' $row_posts['subject'] . '</a>',
      
'body' => $row_posts['body'],
  'avatar' => $row_posts['avatar'] == '' $memberContext[$row_posts['id_member']]['avatar']['href'] : (stristr($row_posts['avatar'], 'http://') ? $row_posts['avatar'] : $modSettings['avatar_url'] . '/' $row_posts['avatar']),
  'board' => $row_posts['name'],  
  'time' => timeformat($row_posts['poster_time']),
  'new' => !empty($row_posts['is_read'])
   );
}
$smcFunc['db_free_result']($posts_result);

}

//Finally the Output

//Scrolling xD
if ($scrolling == 1)
echo 
'<div style="overflow: hidden;"><marquee height=' $height ' behavior="scroll" direction="up" scrollamount="' $speed '" onmouseover="this.stop()" onmouseout="this.start()">';

if (
$scrollbar == 1)
echo 
'<div style="height:' $height '; overflow-y: scroll; overflow-x: hidden;">';

foreach (
$posts as $post) {

// Lets fix the BBCode bug and Strip the Text
$content1 str_replace("[""<"$post['body']);
$content2 str_replace("]"">"$content1); 
$content3 strip_tags($content2);
$preview substr ($content3,0,$number);

echo
'
<style style="text/css">
   .hoverTable{
width:100%; 
border-collapse:collapse;
}
/* Define the hover highlight color for the table row */
    .hoverTable tr:hover {
          background-color: #1C1C1C;
    }
.img-circle {
    border-radius: 50%;
}
</style>
<table class="hoverTable">
<tr>
<td style="width: 40px;">'
;
if (!empty(
$post['avatar'])){
echo '<img class="img-circle" src="' $post['avatar'] .'" alt="" width="40px" height="40px" />';
} else {
echo '<img class="img-circle" src="' $default_avatar .'" alt="" width="40px" height="40px" />';
}

echo 
'</td>
<td>
&nbsp;' 
$post['subject'];

if (!$post['new'] && $context['user']['is_logged'])
echo '<br />
&nbsp;' 
$text['who'] . '&nbsp;' $post['username'] . '
</td>
</tr>
</table>'
;

echo 
'';
}


        
//Scrolling xD
if ($scrollbar == 1)
echo 
'</div>'

if (
$scrolling == 1)
echo 
'</marquee></div>';
?>

Again, live example at gameguides.pro/f right side.

The block hasn't been changed much, but again, it does have allot of exess code such as the scrollbar feature.



Like i said, I aint got much experience or whatever, but I'm sure excess code isn't a good thing? right? like does it slow it down by a few miliseconds? add some extra load? :P

If someone can let me know if it is okay for me to continue using the blocks in the current state it would be great, and if someone could remove the excess code it would be even better!

ps: i will be creating a few topics on my forum on all the coding i've used, from on the pages to the blocks themselves. So i will be referencing back to this website if it's okay as allot of the code is from this webiste :)
SimplePortal 2.3.8 © 2008-2024, SimplePortal