Autor Tópico: AJAX Recent Topics  (Lida 6608 vezes)

0 Membros e 3 Visitantes estão a ver este tópico.

Offline candidosa2

  • Suporte Tecnico
  • Fundador
  • *
  • Mensagens: 5073
  • Rank: 1658
  • Sexo: Masculino
  • Suporte
    • Hosting Moçambique
AJAX Recent Topics
« em: 07 de Janeiro de 2009, 05:59 »
./index.php
Pesquisar
Código: [Seleccione]
'recent' => array('Recent.php', 'RecentPosts'),
Adicionar alterado
Código: [Seleccione]
'recenttopics' => array('Recent.php', 'RecentTopics')
./Sources/Recent.php
Pesquisar
Código: [Seleccione]
?>    

Adicionar
Código: [Seleccione]
function RecentTopics()
{
global $context, $settings, $scripturl, $txt, $db_prefix;
global $user_info, $modSettings, $board;

loadTemplate('Recent');
$context['page_title'] = $txt['recent_topics'];
$context['sub_template'] = isset($_REQUEST['xml']) ? 'recent_xml' : 'recent';

if (!empty($_REQUEST['c']) && empty($board))
{
$_REQUEST['c'] = explode(',', $_REQUEST['c']);
foreach ($_REQUEST['c'] as $i => $c)
$_REQUEST['c'][$i] = (int) $c;

if (count($_REQUEST['c']) == 1)
{
$request = db_query("
SELECT name
FROM {$db_prefix}categories
WHERE ID_CAT = " . $_REQUEST['c'][0] . "
LIMIT 1", __FILE__, __LINE__);
list ($name) = mysql_fetch_row($request);
mysql_free_result($request);

if (empty($name))
fatal_lang_error(1, false);

$context['linktree'][] = array(
'url' => $scripturl . '#' . (int) $_REQUEST['c'],
'name' => $name
);
}

$request = db_query("
SELECT b.ID_BOARD
FROM {$db_prefix}boards AS b
WHERE b.ID_CAT IN (" . implode(', ', $_REQUEST['c']) . ")
AND $user_info[query_see_board]", __FILE__, __LINE__);
$boards = array();
while ($row = mysql_fetch_assoc($request))
$boards[] = $row['ID_BOARD'];
mysql_free_result($request);

if (empty($boards))
fatal_lang_error('error_no_boards_selected', false);

$query_this_board = 'b.ID_BOARD IN (' . implode(', ', $boards) . ')';
}
elseif (!empty($_REQUEST['boards']))
{
$_REQUEST['boards'] = explode(',', $_REQUEST['boards']);
foreach ($_REQUEST['boards'] as $i => $b)
$_REQUEST['boards'][$i] = (int) $b;

$request = db_query("
SELECT b.ID_BOARD
FROM {$db_prefix}boards AS b
WHERE b.ID_BOARD IN (" . implode(', ', $_REQUEST['boards']) . ")
AND $user_info[query_see_board]
LIMIT " . count($_REQUEST['boards']), __FILE__, __LINE__);
$boards = array();
while ($row = mysql_fetch_assoc($request))
$boards[] = $row['ID_BOARD'];
mysql_free_result($request);

if (empty($boards))
fatal_lang_error('error_no_boards_selected', false);

$query_this_board = 'b.ID_BOARD IN (' . implode(', ', $boards) . ')';
}
elseif (!empty($board))
$query_this_board = 'b.ID_BOARD = ' . $board;
else
$query_this_board = $user_info['query_see_board'] . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? "
AND b.ID_BOARD != $modSettings[recycle_board]" : '');

$context['linktree'][] = array(
'url' => $scripturl . '?action=' . $_REQUEST['action'],
'name' => $txt['recent_topics'],
);

   $latest_post = !empty($_REQUEST['latest']) ? 'AND m.PosterTime > ' . (int) $_REQUEST['latest'] : '';
   
   $min_msg_id = $modSettings['maxMsgID'] - (int) (($modSettings['totalMessages'] / $modSettings['totalTopics']) * $settings['number_recent_topics']);
   
   $context['topics'] = array();
   // Find all the posts in distinct topics.  Newer ones will have higher IDs.
   $request = db_query("
      SELECT
         ms.posterTime as firstTime, m.posterTime as lastTime, ms.subject, m.ID_TOPIC, t.numReplies,
         ms.ID_MEMBER as ID_FIRST_POSTER, m.ID_MEMBER as ID_LAST_POSTER, m.ID_MSG, b.ID_BOARD, b.name AS bName,
         IFNULL(mem2.realName, ms.posterName) AS firstPoster,
         IFNULL(mem.realName, m.posterName) AS lastPoster
      FROM {$db_prefix}topics AS t
         INNER JOIN {$db_prefix}messages AS m ON (m.id_msg = t.id_last_msg)
         INNER JOIN {$db_prefix}boards AS b ON (b.id_board = t.id_board)
         INNER JOIN {$db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)
         LEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = m.ID_MEMBER)
         LEFT JOIN {$db_prefix}members AS mem2 ON (mem2.ID_MEMBER = t.ID_MEMBER_STARTED)
      WHERE t.ID_LAST_MSG >= $min_msg_id
         AND $query_this_board
         $latest_post
      ORDER BY t.ID_LAST_MSG DESC
      LIMIT " . $settings['number_recent_topics'], __FILE__, __LINE__);
 
while ($row = mysql_fetch_assoc($request))
{
$context['topics'][] = array(
'id' => $row['ID_TOPIC'],
'subject' => $row['subject'],
'href' => $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0',
'link' => '<a href="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0">' . $row['subject'] . '</a>',
'replies' => $row['numReplies'],
'board' => array(
'id' => $row['ID_BOARD'],
'name' => $row['bName'],
'href' => $scripturl . '?board=' . $row['ID_BOARD'] . '.0',
'link' => '<a href="' . $scripturl . '?board=' . $row['ID_BOARD'] . '.0">' . $row['bName'] . '</a>'
),
'firstPoster' => array(
'id' => $row['ID_FIRST_POSTER'],
'name' => $row['firstPoster'],
'time' => (time() - $row['firstTime'] < 3600) ? round((time() - $row['firstTime'])/60, 0) . $txt['minutes_ago'] : timeformat($row['firstTime']),
'href' => $scripturl . '?action=profile;u=' . $row['ID_FIRST_POSTER'],
'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['ID_FIRST_POSTER'] . '">' . $row['firstPoster'] . '</a>'
),
'lastPoster' => array(
'id' => $row['ID_LAST_POSTER'],
'name' => $row['lastPoster'],
'time' => (time() - $row['lastTime'] < 3600) ? round((time() - $row['lastTime'])/60, 0) . $txt['minutes_ago'] : timeformat($row['lastTime']),
'href' => $scripturl . '?action=profile;u=' . $row['ID_LAST_POSTER'],
'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['ID_LAST_POSTER'] . '">' . $row['lastPoster'] . '</a>'
),
'lastPost' => array(
'time' => $row['lastTime'],
'href' => $scripturl . '?topic=' . $row['ID_TOPIC'] . '.new;topicseen#new',
'link' => '<a href="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . '.new;topicseen#new">' . $row['subject'] . '</a>'
),
);
}
mysql_free_result($request);
if (!empty($context['topics']))
$context['last_post_time'] = $context['topics'][0]['lastPost']['time'];

// If we have an XML request, we need to reverse the array
if (!empty($latest_post))
$context['topics'] = array_reverse($context['topics']);
}


./Themes/default/index.template.php

Pesquisar
Código: [Seleccione]
if (in_array($context['current_action'], array('search', 'admin', 'calendar', 'profile', 'mlist', 'register', 'login', 'help', 'pm')))
Adicionar
Código: [Seleccione]
if (in_array($context['current_action'], array('search', 'admin', 'calendar', 'profile', 'mlist', 'register', 'login', 'help', 'pm', 'recenttopics')))
Pesquisar
Código: [Seleccione]
// the [member] list button
Adicionar
Código: [Seleccione]
// The [recenttopics]!
echo ($current_action == 'recenttopics' || $context['browser']['is_ie4']) ? '<td class="maintab_active_' . $first . '">&nbsp;</td>' : '' , '
<td valign="top" class="maintab_' , $current_action == 'recenttopics' ? 'active_back' : 'back' , '">
<a href="', $scripturl, '?action=recenttopics">' , $txt['recent_topics'] , '</a>
</td>' , $current_action == 'recenttopics' ? '<td class="maintab_active_' . $last . '">&nbsp;</td>' : '';


$languagedir/Modifications.portuguese_pt.php
Código: [Seleccione]
// AJAX Recent Topics Portuguese translate by candidosa2
$txt['recent_topics'] = 'T&oacute;picos recentes';
$txt['minutes_ago'] = ' minutos atr&aacute;s';
$txt['number_recent_topics_interval'] = 'Tempo demora (em segundos) entre os testes para actualiza&ccedil;&atilde;o dos t&oacute;picos';
$txt['number_recent_topics_interval_desc'] = 'N&atilde;o muito baixo, segundo uma 5/10 demora &eacute; razo&aacute;vel';
$txt['number_recent_topics'] = 'T&oacute;picos para serem exibidos em recente T&oacute;pico p&aacute;gina';


$themedir/Recent.template.php
Pesquisar
Código: [Seleccione]
?>
Adicionar
Código: [Seleccione]
function template_recent()
{
global $context, $txt, $settings, $scripturl;

echo '
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td>', theme_linktree(), '</td>
</tr>
</table>';

echo '
<div class="tborder" ', $context['browser']['needs_size_fix'] && !$context['browser']['is_ie6'] ? 'style="width: 100%;"' : '', '>
<table border="0" width="100%" cellspacing="0" cellpadding="4" class="bordercolor" id="topicTable">
<tr>';

// Are there actually any topics to show?
echo '
<td width="15%" class="catbg3"><strong>', $txt['smf82'], '</strong></td>
<td class="catbg3"><strong>', $txt[118], '</strong></td>
<td width="50" class="catbg3" align="center"><strong>', $txt[110], '</strong></td>
<td width="150" class="catbg3" align="center"><strong>', $txt[109], '</strong></td>
<td width="150" class="catbg3" align="center"><strong>', $txt[22], '</strong></td>
<td width="16" class="catbg3"></td>';

// No topics.... just say, "sorry bub".
if (empty($context['topics']))
echo '
</tr>
<tr id="no_topics">
<td class="windowbg2" width="100%" colspan="6"><strong>', $txt[151], '</strong></td>';

echo '
</tr>';

foreach ($context['topics'] as $topic)
{
echo '
<tr class="windowbg2" id="topic_', $topic['id'], '">
<td class="smalltext" style="padding-left: 10px; border-bottom: 1px solid rgb(204, 204, 204);">', $topic['board']['link'], '</td>
<td style="padding-left: 10px; border-bottom: 1px solid rgb(204, 204, 204);">', $topic['link'], '</td>
<td align="center" class="smalltext" style="border-bottom: 1px solid rgb(204, 204, 204);">', $topic['replies'], '</td>
<td align="center" class="smalltext" style="border-bottom: 1px solid rgb(204, 204, 204);">', $topic['firstPoster']['link'], '<br />', $topic['firstPoster']['time'], '</td>
<td align="center" class="smalltext" style="border-bottom: 1px solid rgb(204, 204, 204);">', $topic['lastPoster']['link'], '<br />', $topic['lastPoster']['time'], '</td>
<td align="center" style="border-bottom: 1px solid rgb(204, 204, 204);"><a href="', $topic['lastPost']['href'], '"><img src="', $settings['images_url'], '/icons/last_post.gif" alt="', $txt[111], '" title="', $txt[111], '" /></a></td>
</tr>';
}

echo '
</table>
</div>';

// Now for all of the javascript stuff
echo '
<script language="Javascript" type="text/javascript"><!-- // --><![CDATA[
var last_post = ', (!empty($context['last_post_time']) ? $context['last_post_time'] : 0), ';
var time_interval = ', $settings['number_recent_topics_interval'] * 1000, ';
var max_topics = ', $settings['number_recent_topics'], ';

var interval_id = setInterval( "getTopics()", time_interval);

function getTopics()
{
if (window.XMLHttpRequest)
getXMLDocument("', $scripturl, '?action=recenttopics;latest=" + last_post + ";xml", gotTopics);
else
clearInterval(interval_id);
}

function gotTopics(XMLDoc)
{
var updated_time = XMLDoc.getElementsByTagName("smf")[0].getElementsByTagName("lastTime")[0];
var topics = XMLDoc.getElementsByTagName("smf")[0].getElementsByTagName("topic");
var topic, id_topic, board, subject, replies, firstPost, lastPost, link;
var myTable = document.getElementById("topicTable"), oldRow, myRow, myCell, myData, rowCount;

// If this exists, we have at least one updated/new topic
if (updated_time)
{
// Update the last post time
last_post = updated_time.childNodes[0].nodeValue;

// No Messages message?  Ditch it!
// Note, this should only happen if there are literally zero topics
// on the board when a user visits this page.
if (document.getElementById("no_topics") != null)
myTable.deleteRow(-1);

// If the topic is already in the list, remove it
for (var i = 0; i < topics.length; i++)
{
topic = XMLDoc.getElementsByTagName("smf")[0].getElementsByTagName("topic")[i];
id_topic = topic.getElementsByTagName("id")[0].childNodes[0].nodeValue;
if ((oldRow = document.getElementById("topic_" + id_topic)) != null)
myTable.deleteRow(oldRow.rowIndex);
}

// Are we going to exceed the maximum topic count allowed?
while (((myTable.rows.length - 1 + topics.length) - max_topics) > 0)
myTable.deleteRow(-1);

// Now start the insertion
for (var i = 0; i < topics.length; i++)
{
// Lets get all of our data
topic = XMLDoc.getElementsByTagName("smf")[0].getElementsByTagName("topic")[i];
id_topic = topic.getElementsByTagName("id")[0].childNodes[0].nodeValue;
board = topic.getElementsByTagName("board")[0].childNodes[0].nodeValue;
subject = topic.getElementsByTagName("subject")[0].childNodes[0].nodeValue;
replies = topic.getElementsByTagName("replies")[0].childNodes[0].nodeValue;
firstPost = topic.getElementsByTagName("first")[0].childNodes[0].nodeValue;
lastPost = topic.getElementsByTagName("last")[0].childNodes[0].nodeValue;
link = topic.getElementsByTagName("lastLink")[0].childNodes[0].nodeValue;

// Now to create the new row...
myRow = myTable.insertRow(1);
myRow.id = "topic_" + id_topic;
myRow.className = "windowbg";

// First the Board
myCell = myRow.insertCell(-1);
myCell.className = "smalltext";
myCell.style.paddingLeft = "10px";
myCell.style.borderBottom = "1px solid rgb(204, 204, 204)";
setInnerHTML(myCell, board);

// Then subject
myCell = myRow.insertCell(-1);
myCell.style.paddingLeft = "10px";
myCell.style.borderBottom = "1px solid rgb(204, 204, 204)";
setInnerHTML(myCell, subject);

// replies
myCell = myRow.insertCell(-1);
myCell.className = "smalltext";
myCell.align = "center"
myCell.style.borderBottom = "1px solid rgb(204, 204, 204)";
setInnerHTML(myCell, replies);

// first post
myCell = myRow.insertCell(-1);
myCell.className = "smalltext";
myCell.align = "center"
myCell.style.borderBottom = "1px solid rgb(204, 204, 204)";
setInnerHTML(myCell, firstPost);

// last post
myCell = myRow.insertCell(-1);
myCell.className = "smalltext";
myCell.align = "center"
myCell.style.borderBottom = "1px solid rgb(204, 204, 204)";
setInnerHTML(myCell, lastPost);

// last post
myCell = myRow.insertCell(-1);
myCell.align = "center"
myCell.style.borderBottom = "1px solid rgb(204, 204, 204)";
setInnerHTML(myCell, link);
}
}
}
// ]]]]><![CDATA[></script>';
}

function template_recent_xml()
{
global $context, $settings, $txt;

echo '<?xml version="1.0" encoding="', $context['character_set'], '"?>
<smf>';

if (!empty($context['topics']))
echo '
<lastTime><!', '[CDATA[', $context['last_post_time'], ']', ']></lastTime>';

foreach ($context['topics'] as $topic)
echo '
<topic>
<id><!', '[CDATA[', $topic['id'], ']', ']></id>
<board><!', '[CDATA[', str_replace(']'.']>', ']]]'.']><!'.'[CDATA[>', $topic['board']['link']), ']', ']></board>
<subject><!', '[CDATA[', str_replace(']'.']>', ']]]'.']><!'.'[CDATA[>', $topic['link']), ']', ']></subject>
<replies><!', '[CDATA[', $topic['replies'], ']', ']></replies>
<first><!', '[CDATA[', str_replace(']'.']>', ']]]'.']><!'.'[CDATA[>', $topic['firstPoster']['link'] . '<br />' . $topic['firstPoster']['time']), ']', ']></first>
<last><!', '[CDATA[', str_replace(']'.']>', ']]]'.']><!'.'[CDATA[>', $topic['lastPoster']['link'] . '<br />' . $topic['lastPoster']['time']), ']', ']></last>
<lastLink><!', '[CDATA[<a href="', $topic['lastPost']['href'], '"><img src="', $settings['images_url'], '/icons/last_post.gif" alt="', $txt[111], '" title="', $txt[111], '" /></a>]', ']></lastLink>
</topic>';

echo '
</smf>';
}


./Themes/default/Settings.template.php
Pesquisar
Código: [Seleccione]
'description' => $txt['number_recent_posts_desc'],
'type' => 'number',
),

Adicionar
Código: [Seleccione]
array(
'id' => 'number_recent_topics',
'label' => $txt['number_recent_topics'],
'type' => 'number',
),
array(
'id' => 'number_recent_topics_interval',
'label' => $txt['number_recent_topics_interval'],
'description' => $txt['number_recent_topics_interval_desc'],
'type' => 'number',
),