============================================================================================================================
Modification Name: Last Post Subject v1.1

Author: GuldantheWarlock (GuldantheWarlock@xmbgarage.com)

Original Code by: dnar

Code Updated by: John Clawson

Last Updated: March 26, 2010

Description: This modification will display the subject of the Last Post made in each forum on the index and subforum rows.

Supported Version: XMB 1.9.5 SP1

Notes: This modification is released under the GPL. A copy is provided with this software package.

Please backup your files before installing this modification.
Neither XMB Garage nor the author can be held responsible if your board stops functioning properly due to you installing this modification.
============================================================================================================================
=======
Step 1:
=======
===================================
Go to Administration Panel --> Insert Raw SQL
===================================
==================================
Paste the Following Code and Submit Changes
==================================

ALTER TABLE $table_forums CHANGE lastpost lastpost VARCHAR(75) NOT NULL;

============================================================================================================================
=======
Step 2:
=======
===============
Edit File: post.php
===============
==========
Find Code:
==========

        $db->query("UPDATE $table_threads SET lastpost=concat(lastpost, '|".$pid."') WHERE tid='$tid'");

===============
Add Code Below:
===============

        // Last Post Subject Mod Begin
        $lpsubject = shortenString($subject, 30, X_SHORTEN_HARD);
        // Last Post Subject Mod End

==========
Find Code:
==========

        $db->query("UPDATE $table_forums SET lastpost='$thatime|$username|$pid', threads=threads+1, posts=posts+1 WHERE fid='$for[fup]'");

================
Replace Code With:
================

        $db->query("UPDATE $table_forums SET lastpost='$thatime|$username|$pid|$lpsubject', threads=threads+1, posts=posts+1 WHERE fid='$for[fup]'");

==========
Find Code:
==========

            $db->query("UPDATE $table_forums SET lastpost='$thatime|$username|$pid', threads=threads+1, posts=posts+1 WHERE fid='$fid'");

================
Replace Code With:
================

            $db->query("UPDATE $table_forums SET lastpost='$thatime|$username|$pid|$lpsubject', threads=threads+1, posts=posts+1 WHERE fid='$fid'");

=====================
Find Code (first occurance):
=====================

        if ((X_STAFF) && $closetopic == "yes") {
            $db->query("UPDATE $table_threads SET closed='yes' WHERE tid='$tid' AND fid='$fid'");
        }

===============
Add Code Below:
===============

        // Last Post Subject Mod Begin
        $lpsubject = shortenString($thread['subject'], 30, X_SHORTEN_HARD);
        // Last Post Subject Mod End

==========
Find Code:
==========

                $db->query("UPDATE $table_forums SET lastpost='$thatime|$username|$pid', posts=posts+1 WHERE fid='$for[fup]'");

================
Replace Code With:
================

                $db->query("UPDATE $table_forums SET lastpost='$thatime|$username|$pid|$lpsubject', posts=posts+1 WHERE fid='$for[fup]'");

==========
Find Code:
==========

            $db->query("UPDATE $table_forums SET lastpost='$thatime|$username|$pid', posts=posts+1 WHERE fid='$fid'");

================
Replace Code With:
================

            $db->query("UPDATE $table_forums SET lastpost='$thatime|$username|$pid|$lpsubject', posts=posts+1 WHERE fid='$fid'");

============================================================================================================================
=======
Step 3:
=======
==================
Edit File: functions.php
==================
==========
Find Code:
==========

    if (isset($forum['moderator']) && $forum['lastpost'] != '') {
        $lastpost = explode('|', $forum['lastpost']);
        $dalast = $lastpost[0];
        if ($lastpost[1] != 'Anonymous' && $lastpost[1] != '') {
            $lastpost[1] = '<a href="member.php?action=viewpro&amp;member='.rawurlencode($lastpost[1]).'">'.$lastpost[1].'</a>';
        } else {
            $lastpost[1] = $lang['textanonymous'];
        }

        $lastPid = isset($lastpost[2]) ? $lastpost[2] : 0;

        $lastpostdate = gmdate($dateformat, $lastpost[0] + ($timeoffset * 3600) + ($SETTINGS['addtime'] * 3600));
        $lastposttime = gmdate($timecode, $lastpost[0] + ($timeoffset * 3600) + ($SETTINGS['addtime'] * 3600));
        $lastpost = $lastpostdate.' '.$lang['textat'].' '.$lastposttime.'<br />'.$lang['textby'].' '.$lastpost[1];
        eval('$lastpostrow = "'.template($template.'_lastpost').'";');
    } else {
        $dalast = 0;
        $lastpost = $lang['textnever'];
        eval('$lastpostrow = "'.template($template.'_nolastpost').'";');
    }

================
Replace Code With:
================

    if (isset($forum['moderator']) && $forum['lastpost'] != '') {
        $lastpost = explode('|', $forum['lastpost']);
        $dalast = $lastpost[0];
        if ( $lastpost[1] != 'Anonymous' && $lastpost[1] != '') {
            $lastpost[1] = '<a href="member.php?action=viewpro&amp;member='.rawurlencode($lastpost[1]).'">'.$lastpost[1].'</a>';
        } else {
            $lastpost[1] = $lang['textanonymous'];
        }
        
        $lastPid = isset($lastpost[2]) ? $lastpost[2] : 0;
        
        $lastpostdate = gmdate($dateformat, $lastpost[0] + ($timeoffset * 3600) + ($SETTINGS['addtime'] * 3600));
        $lastposttime = gmdate($timecode, $lastpost[0] + ($timeoffset * 3600) + ($SETTINGS['addtime'] * 3600));
        $lpsubject = isset($lastpost[3]) ? '<a href="./viewthread.php?goto=lastpost&amp;fid='.$forum['fid'].'">'.$lastpost[3].'</a>' : 'N/A';
        $lastpost = $lang['texton'].': '.$lastpostdate.'<br />'.$lang['textat'].': '.$lastposttime.'<br />'.$lang['textby'].': '.$lastpost[1].'<br />'.$lang['textin'].': '.$lpsubject;
        eval('$lastpostrow = "'.template($template.'_lastpost').'";');
    } else {
        $dalast = 0;
        $lastpost = $lang['textnever'];
        eval('$lastpostrow = "'.template($template.'_nolastpost').'";');
    }

==========
Find Code:
==========

    $query = $db->query("SELECT dateline, author, pid FROM $table_posts WHERE tid='$tid' ORDER BY dateline DESC LIMIT 1");

================
Replace Code With:
================

    $query = $db->query("SELECT p.dateline, p.author, p.pid, t.subject FROM $table_posts p LEFT JOIN $table_threads t ON p.tid=t.tid WHERE p.tid='$tid' ORDER BY p.dateline DESC LIMIT 1");

==========
Find Code:
==========

    $lastpost = $lp['dateline'].'|'.$lp['author'].'|'.$lp['pid'];
    
=============
Replace With:
=============

    // Last Post Subject Mod Begin
    $lpsubject = shortenString($lp['subject'], 30, X_SHORTEN_HARD);
    $lastpost = $lp['dateline'].'|'.$lp['author'].'|'.$lp['pid'].'|'.$lpsubject;
    // Last Post Subject Mod End

==========
Find Code:
==========

    $query = $db->query("SELECT t.lastpost FROM $table_threads t, $table_forums f WHERE (t.fid=f.fid AND f.fid='$fid') OR (t.fid=f.fid AND f.fup='$fid') ORDER BY t.lastpost DESC LIMIT 0,1");
    $lp = $db->fetch_array($query);
    $db->query("UPDATE $table_forums SET posts='$postcount', threads='$threadcount', lastpost='$lp[lastpost]' WHERE fid='$fid'");
    $db->free_result($query);

=============
Replace With:
=============

    $query = $db->query("SELECT t.lastpost, t.subject FROM $table_threads t, $table_forums f WHERE (t.fid=f.fid AND f.fid='$fid') OR (t.fid=f.fid AND f.fup='$fid') ORDER BY t.lastpost DESC LIMIT 0,1");
    $lp = $db->fetch_array($query);
    $db->query("UPDATE $table_forums SET posts='$postcount', threads='$threadcount', lastpost='$lp[lastpost]' WHERE fid='$fid'");
    $lpsubject = shortenString($lp['subject'], 30, X_SHORTEN_HARD);
    $db->query("UPDATE $table_forums SET lastpost=CONCAT(lastpost, '|".$lpsubject."') WHERE fid='$fid'");
    $db->free_result($query);

============================================================================================================================
=======
Step 4:
=======
===============
Edit File: tools.php
===============
==========
Find Code:
==========

    case 'fixlastposts':
        $q = $db->query("SELECT fid FROM $table_forums WHERE (fup = '0' OR fup = '') AND type = 'forum'");
        while ($loner = $db->fetch_array($q)) {
            $lastpost = array();
            $subq = $db->query("SELECT fid FROM $table_forums WHERE fup = '$loner[fid]'");
            while ($sub = $db->fetch_array($subq)) {
                $pq = $db->query("SELECT author, dateline, pid FROM $table_posts WHERE fid = '$sub[fid]' ORDER BY pid DESC LIMIT 1");
                if ($db->num_rows($pq) > 0) {
                    $curr = $db->fetch_array($pq);
                    $lastpost[] = $curr;
                    $lp = $curr['dateline'].'|'.$curr['author'].'|'.$curr['pid'];
                } else {
                    $lp = '';
                }
                $db->query("UPDATE $table_forums SET lastpost = '$lp' WHERE fid = '$sub[fid]'");
                $db->free_result($pq);
            }
            $db->free_result($subq);
            $pq = $db->query("SELECT author, dateline, pid FROM $table_posts WHERE fid = '$loner[fid]' ORDER BY pid DESC LIMIT 1");
            if ($db->num_rows($pq) > 0) {
                $lastpost[] = $db->fetch_array($pq);
            }
            $db->free_result($pq);
            if (count($lastpost) == 0) {
                $lastpost = '';
            } else {
                $top = 0;
                $mkey = -1;
                foreach ($lastpost as $key => $v) {
                    if ($v['dateline'] > $top) {
                        $mkey = $key;
                        $top = $v['dateline'];
                    }
                }
                $lastpost = $lastpost[$mkey]['dateline'].'|'.$lastpost[$mkey]['author'].'|'.$lastpost[$mkey]['pid'];
            }
            $db->query("UPDATE $table_forums SET lastpost = '$lastpost' WHERE fid = '$loner[fid]'");
        }
        $db->free_result($q);
        $q = $db->query("SELECT fid FROM $table_forums WHERE type = 'group'");
        while ($cat = $db->fetch_array($q)) {
            $fq = $db->query("SELECT fid FROM $table_forums WHERE type = 'forum' AND fup = '$cat[fid]'");
            while ($forum = $db->fetch_array($fq)) {
                $lastpost = array();
                $subq = $db->query("SELECT fid FROM $table_forums WHERE fup = '$forum[fid]'");
                while ($sub = $db->fetch_array($subq)) {
                    $pq = $db->query("SELECT author, dateline, pid FROM $table_posts WHERE fid = '$sub[fid]' ORDER BY pid DESC LIMIT 1");
                    if ($db->num_rows($pq) > 0) {
                        $curr = $db->fetch_array($pq);
                        $lastpost[] = $curr;
                        $lp = $curr['dateline'].'|'.$curr['author'].'|'.$curr['pid'];
                    } else {
                        $lp = '';
                    }
                    $db->free_result($pq);
                    $db->query("UPDATE $table_forums SET lastpost = '$lp' WHERE fid = '$sub[fid]'");
                }
                $db->free_result($subq);
                $pq = $db->query("SELECT author, dateline, pid FROM $table_posts WHERE fid = '$forum[fid]' ORDER BY pid DESC LIMIT 1");
                if ($db->num_rows($pq) > 0) {
                    $lastpost[] = $db->fetch_array($pq);
                }
                $db->free_result($pq);
                if (count($lastpost) == 0) {
                    $lastpost = '';
                } else {
                    $top = 0;
                    $mkey = -1;
                    foreach ($lastpost as $key => $v) {
                        if ($v['dateline'] > $top) {
                            $mkey = $key;
                            $top = $v['dateline'];
                        }
                    }
                    $lastpost = $lastpost[$mkey]['dateline'].'|'.$lastpost[$mkey]['author'].'|'.$lastpost[$mkey]['pid'];
                }
                $db->query("UPDATE $table_forums SET lastpost = '$lastpost' WHERE fid = '$forum[fid]'");
            }
            $db->free_result($fq);
        }
        $db->free_result($q);
        $q = $db->query("SELECT tid FROM $table_threads");
        while ($thread = $db->fetch_array($q)) {
            $lastpost = array();
            $pq = $db->query("SELECT author, dateline, pid FROM $table_posts WHERE tid = '$thread[tid]' ORDER BY pid DESC LIMIT 1");
            if ($db->num_rows($pq) > 0) {
                $curr = $db->fetch_array($pq);
                $lastpost[] = $curr;
                $lp = $curr['dateline'].'|'.$curr['author'].'|'.$curr['pid'];
            } else {
                $lp = '';
            }
            $db->free_result($pq);
            $db->query("UPDATE $table_threads SET lastpost = '$lp' WHERE tid = '$thread[tid]'");
        }
        $db->free_result($q);
        nav($lang['tools']);
        echo "<tr bgcolor=\"$altbg2\" class=\"ctrtablerow\"><td>$lang[tool_completed] - $lang[tool_lastpost]</td></tr></table></table>";
        end_time();
        eval('echo "'.template('footer').'";');
        exit;
        break;

================
Replace Code With:
================

    case 'fixlastposts':
        $q = $db->query("SELECT fid FROM $table_forums WHERE (fup='0' OR fup='') AND type='forum'");
        while ($loner = $db->fetch_array($q)) {
            $lastpost = array();
            $subq = $db->query("SELECT fid FROM $table_forums WHERE fup='$loner[fid]'");
            while ($sub = $db->fetch_array($subq)) {
                $pq = $db->query("SELECT p.author, p.dateline, p.pid, p.tid, t.subject FROM $table_posts p LEFT JOIN $table_threads t ON p.tid=t.tid WHERE p.fid='$sub[fid]' ORDER BY p.pid DESC LIMIT 1");
                if ($db->num_rows($pq) > 0) {
                    $curr = $db->fetch_array($pq);
                    $lastpost[] = $curr;
                    $lpsubject = shortenString($curr['subject'], 30, X_SHORTEN_HARD);
                    $lp = $curr['dateline'].'|'.$curr['author'].'|'.$curr['pid'].'|'.$lpsubject;
                } else {
                    $lp = '';
                }
                $db->query("UPDATE $table_forums SET lastpost='$lp' WHERE fid='$sub[fid]'");
            }
            $pq = $db->query("SELECT p.author, p.dateline, p.pid, p.tid, t.subject FROM $table_posts p LEFT JOIN $table_threads t ON p.tid=t.tid WHERE p.fid='$loner[fid]' ORDER BY p.pid DESC LIMIT 1");
            if ($db->num_rows($pq) > 0) {
                $lastpost[] = $db->fetch_array($pq);
            }

            if (count($lastpost) == 0) {
                $lastpost = '';
            } else {
                $top = 0;
                $mkey = -1;
                foreach ($lastpost as $key=>$v) {
                    $db->query("UPDATE $table_threads SET lastpost='$v[dateline]|$v[author]|$v[pid]' WHERE tid='$v[tid]'");
                    if ($v['dateline'] > $top) {
                        $mkey = $key;
                        $top = $v['dateline'];
                    }
                }
                $lpsubject = shortenString($lastpost[$mkey]['subject'], 30, X_SHORTEN_HARD);
                $lastpost = $lastpost[$mkey]['dateline'].'|'.$lastpost[$mkey]['author'].'|'.$lastpost[$mkey]['pid'].'|'.$lpsubject;
            }
            $db->query("UPDATE $table_forums SET lastpost='$lastpost' WHERE fid = '$loner[fid]'");
        }

        // now to go through categories :|

        $q = $db->query("SELECT fid FROM $table_forums WHERE type='group'");
        while ($cat = $db->fetch_array($q)) {
            // select forums
            $fq = $db->query("SELECT fid FROM $table_forums WHERE type='forum' AND fup='$cat[fid]'");
            while ($forum = $db->fetch_array($fq)) {
                $lastpost = array();
                $subq = $db->query("SELECT fid FROM $table_forums WHERE fup='$forum[fid]'");
                while ($sub = $db->fetch_array($subq)) {
                    $pq = $db->query("SELECT p.author, p.dateline, p.pid, t.subject FROM $table_posts p LEFT JOIN $table_threads t ON p.tid=t.tid WHERE p.fid='$sub[fid]' ORDER BY p.pid DESC LIMIT 1");
                    if ($db->num_rows($pq) > 0)  {
                        $curr = $db->fetch_array($pq);
                        $lastpost[] = $curr;
                        $lpsubject = shortenString($curr['subject'], 30, X_SHORTEN_HARD);
                        $lp = $curr['dateline'].'|'.$curr['author'].'|'.$curr['pid'].'|'.$lpsubject;
                    } else {
                        $lp = '';
                    }
                    $db->query("UPDATE $table_forums SET lastpost='$lp' WHERE fid='$sub[fid]'");
                }
                $pq = $db->query("SELECT p.author, p.dateline, p.pid, t.subject FROM $table_posts p LEFT JOIN $table_threads t ON p.tid=t.tid WHERE p.fid='$forum[fid]' ORDER BY p.pid DESC LIMIT 1");
                if ($db->num_rows($pq) > 0) {
                    $lastpost[] = $db->fetch_array($pq);
                }


                if (count($lastpost) == 0) {
                    $lastpost = '';
                } else {
                    $top = 0;
                    $mkey = -1;
                    foreach ($lastpost as $key=>$v) {
                        if ($v['dateline'] > $top) {
                            $mkey = $key;
                            $top = $v['dateline'];
                        }
                    }
                    $lpsubject = shortenString($lastpost[$mkey]['subject'], 30, X_SHORTEN_HARD);
                    $lastpost = $lastpost[$mkey]['dateline'].'|'.$lastpost[$mkey]['author'].'|'.$lastpost[$mkey]['pid'].'|'.$lpsubject;
                }
                $db->query("UPDATE $table_forums SET lastpost='$lastpost' WHERE fid = '$forum[fid]'");
            }
        }
        
        $q = $db->query("SELECT tid FROM $table_threads");
        while($thread = $db->fetch_array($q)) {
        	$lpq = $db->query("SELECT author, dateline, pid FROM $table_posts WHERE tid='$thread[tid]' ORDER BY pid DESC LIMIT 1");
        	$lp = $db->fetch_array($lpq);
        	$db->query("UPDATE $table_threads SET lastpost='$lp[dateline]|$lp[author]|$lp[pid]' WHERE tid='$thread[tid]'");
        }

        nav($lang['tools']);
        echo "<tr bgcolor=\"$altbg2\" class=\"ctrtablerow\"><td>$lang[tool_completed] - $lang[tool_lastpost]</td></tr></table></table>";
        end_time();
        eval("echo (\"".template('footer')."\");");
        exit;
        break;

============================================================================================================================
=======
Step 5:
=======
===================
Edit File: topicadmin.php
===================
==========
Find Code:
==========

            $pid = $db->result($db->query("SELECT pid FROM $table_posts WHERE tid='$tid' ORDER BY pid DESC LIMIT 1"), 0);

            $db->query("UPDATE $table_threads SET lastpost='".$onlinetime."|$xmbuser|$pid' WHERE tid=$tid AND fid=$fid");
            $db->query("UPDATE $table_forums SET lastpost='".$onlinetime."|$xmbuser|$pid' WHERE fid=$fid");
            
=============
Replace With:
=============

            $lp = $db->fetch_array($db->query("SELECT p.pid, t.subject FROM $table_posts p LEFT JOIN $table_threads t ON p.tid=t.tid WHERE p.tid='$tid' ORDER BY p.pid DESC LIMIT 1"));
            $lpsubject = shortenString($lp['subject'], 30, X_SHORTEN_HARD);

            $db->query("UPDATE $table_threads SET lastpost='".$onlinetime."|$xmbuser|".$lp['pid']."' WHERE tid=$tid AND fid=$fid");
            $db->query("UPDATE $table_forums SET lastpost='".$onlinetime."|$xmbuser|".$lp['pid']."|".$lpsubject."' WHERE fid=$fid");

============================================================================================================================
=======
Step 6:
=======
=======================
Edit File: lang/English.lang.php
=======================
=======================
Add To End Of File Above ?>
=======================

// Last Post Subject Mod Begin
$lang['textin'] = "In";
// Last Post Subject Mod End

============================================================================================================================
=======
Step 7:
=======
===============================
Go To Administration Panel --> Templates
===============================
==================================
Edit Template: forumdisplay_subforum_lastpost
==================================
================
Replace Code With:
================

<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr align="left" class="tablerow">
<td bgcolor="$altbg1" nowrap="nowrap"><font class="smalltxt">$lastpost</font></td>
<td bgcolor="$altbg1" nowrap="nowrap">&nbsp;<a href="viewthread.php?goto=lastpost&amp;fid=$forum[fid]"><img src="$imgdir/lastpost.gif" border="0" alt="$lang[altlastpost]" /></a></td>
</tr>
</table>

============================================================================================================================
=======
Step 8:
=======
===============================
Go To Administration Panel --> Templates
===============================
====================================
Edit Template: forumdisplay_subforum_nolastpost
====================================
================
Replace Code With:
================

<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr align="left" class="tablerow">
<td bgcolor="$altbg1" nowrap="nowrap"><font class="smalltxt">$lang[textnever]</font></td>
</tr>
</table>

============================================================================================================================
=======
Step 9:
=======
===============================
Go To Administration Panel --> Templates
===============================
================================
Edit Template: forumdisplay_thread_lastpost
================================
================
Replace Code With:
================

<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr align="left" class="tablerow">
<td bgcolor="$altbg2" nowrap="nowrap"><font class="smalltxt">$lastpost</font></td>
<td bgcolor="$altbg2" nowrap="nowrap">&nbsp;<a href="viewthread.php?goto=lastpost&amp;tid=$thread[tid]"><img src="$imgdir/lastpost.gif" border="0" alt="$lang[altlastpost]" /></a></td>
</tr>
</table>

============================================================================================================================
========
Step 10:
========
===============================
Go To Administration Panel --> Templates
===============================
===========================
Edit Template: index_forum_lastpost
===========================
================
Replace Code With:
================

<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr align="left" class="tablerow">
<td bgcolor="$altbg1" nowrap="nowrap"><font class="smalltxt">$lastpost</font></td>
<td bgcolor="$altbg1" nowrap="nowrap">&nbsp;<a href="./viewthread.php?goto=lastpost&amp;fid=$forum[fid]"><img src="$imgdir/lastpost.gif" border="0" alt="$lang[altlastpost]" /></a></td>
</tr>
</table>

============================================================================================================================
========
Step 11:
========
===============================
Go To Administration Panel --> Templates
===============================
=============================
Edit Template: index_forum_nolastpost
=============================
================
Replace Code With:
================

<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr align="left" class="tablerow">
<td bgcolor="$altbg1" nowrap="nowrap"><font class="smalltxt">$lang[textnever]</font></td>
</tr>
</table>

============================================================================================================================
========
Step 12:
========
=====================
Go to Administration Panel
=====================

Run the "Fix Last Posts" tool.

============================================================================================================================
Enjoy!