News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

finally got a ssi_grabMessage INCLUDING attachments, but need help expanding it

Started by Vandaahl, May 28, 2007, 11:04:47 AM

Previous topic - Next topic

Vandaahl

I finally figured out how to make a ssi function for displaying a post that includes attachments. It's based on grab a single message with ssi, to which I added the attachment stuff I found in other topics. It works, but the attachment stuff is very basic atm and only works for pictures (which is what I personally need it for).

Things I would like to add include making the thumbnail clickable after which the picture stretches to its full size (no popup). Can anyone help with that?

Here's the code:


  
//////////////////////////////
 // Return a single post //
/////////////////////////////
function ssi_grabMessage($message_id null$output_method 'echo'
{
global $scripturl$db_prefix$txt$settings$modSettings$context$attachments;
loadLanguage('Stats');

if ($message_id !== null$message_id = (int) $message_id;
elseif (isset($_GET['ID_MSG'])) $message_id = (int) $_GET['ID_MSG'];
else die("Geen message id ingevoerd");

// This is needed for loadAttachmentContext()
include_once('Sources/Display.php');

// Fetch attachments.
if (!empty($modSettings['attachmentEnable']) && allowedTo('view_attachments'))
{
$request db_query("
SELECT
a.ID_ATTACH, a.ID_MSG, a.filename, IFNULL(a.size, 0) AS filesize, a.downloads,
a.width, a.height" 
. (empty($modSettings['attachmentShowImages']) || empty($modSettings['attachmentThumbnails']) ? '' ",
IFNULL(thumb.ID_ATTACH, 0) AS ID_THUMB, thumb.width AS thumb_width, thumb.height AS thumb_height"
) . "
FROM 
{$db_prefix}attachments AS a" . (empty($modSettings['attachmentShowImages']) || empty($modSettings['attachmentThumbnails']) ? '' "
LEFT JOIN 
{$db_prefix}attachments AS thumb ON (thumb.ID_ATTACH = a.ID_THUMB)") . "
WHERE a.ID_MSG = 
$message_id
AND a.attachmentType = 0"
__FILE____LINE__);

$temp = array();
while ($row mysql_fetch_assoc($request))
{
$temp[$row['ID_ATTACH']] = $row;

if (!isset($attachments[$row['ID_MSG']]))
$attachments[$row['ID_MSG']] = array();
}
mysql_free_result($request);

// This is better than sorting it with the query...
ksort($temp);

foreach ($temp as $row)
$attachments[$row['ID_MSG']][] = $row;
}

$request db_query("
SELECT 
m.icon, m.subject, m.body, IFNULL(mem.realName, m.posterName) AS posterName,
m.posterTime, m.ID_MSG, t.ID_TOPIC, m.ID_MEMBER, m.smileysEnabled, b.name
FROM (
{$db_prefix}topics AS t, {$db_prefix}messages AS m, {$db_prefix}boards as b)
LEFT JOIN 
{$db_prefix}members AS mem ON (mem.ID_MEMBER = m.ID_MEMBER)
WHERE m.ID_MSG = 
$message_id
AND m.ID_TOPIC = t.ID_TOPIC
AND t.ID_BOARD = b.ID_BOARD
AND FIND_IN_SET(-1, b.memberGroups)
"
__FILE____LINE__);
$return = array();

if (mysql_num_rows($request) == 0) {
if ($output_method == 'echo') die($txt['smf_news_error2']);
else return;
}

$row mysql_fetch_assoc($request);
$row['body'] = doUBBC($row['body'], $row['smileysEnabled']);

censorText($row['subject']);
censorText($row['body']);

$return[] = array(
'icon' => '<img src="' $settings['images_url'] . '/post/' $row['icon'] . '.gif" align="middle" alt="' $row['icon'] . '" />',
'subject' => $row['subject'],
'time' => timeformat($row['posterTime']),
'body' => $row['body'],
'href' => $scripturl '?topic=' $row['ID_TOPIC'] . '.0',
'link' => '<a href="' $scripturl '?topic=' $row['ID_TOPIC'] . '.msg' $row['ID_MSG'] . '#msg' $row['ID_MSG'] . '">Read More...',
'new_comment' => '<a href="' $scripturl '?action=post;topic=' $row['ID_TOPIC'] . '.0">Reply</a>',
'poster' => array(
'id' => $row['ID_MEMBER'],
'name' => $row['posterName'],
'href' => !empty($row['ID_MEMBER']) ? $scripturl '?action=profile;u=' $row['ID_MEMBER'] : '',
'link' => !empty($row['ID_MEMBER']) ? '<a href="' $scripturl '?action=profile;u=' $row['ID_MEMBER'] . '">' $row['posterName'] . '</a>' $row['posterName']
),
'attachment' => loadAttachmentContext($row['ID_MSG']),
);
mysql_free_result($request);

if ($output_method != 'echo') return $return;

foreach ($return as $news) {
echo '
<table border="0" width="100%" align="center" class="smfNews">
<tr><td>'
$news['icon'], ' <b>'$news['subject'], '</b><span class="smaller"><br />'$news['time'], ' '$txt[525], ' '$news['poster']['link'], '<br /><br /></span></td></tr>
<tr><td>'
$news['body'], '<br /><br /></td></tr>
<tr><td>'
;
foreach ($news['attachment'] as $attachedfile) {
echo '
<a href="'
$attachedfile['href'] , '";image" id="link_'$attachedfile['id'], '" onclick="'$attachedfile['thumbnail']['javascript'], '"><img src="'$attachedfile['thumbnail']['href'] .'" id="thumb_'$attachment['id'], '"></a>';
}
echo '
</td></tr><tr><td>'
$news['link'], ' | '$news['new_comment'], '</td></tr>
</table>'
;
}

}

8105


Vandaahl

Quote from: 8105 on May 28, 2007, 03:58:43 PM
Can you post a sample page with this code in action please?

You mean you want to know how to use it? Just call the function on any php page, by using this code:


<?php
ssi_grabMessage($message_id=50379);
?>


$message_id is the number of the message you want to display.

Make sure you have posted all the code from my first post in SSI.php.

webking

Instead of a Single Message, how possible to grab the recent posts?
http://www.internetvps.com - #1 Virtual Private Server Provider!

palofdru

Quote from: webking on October 27, 2008, 11:50:32 AM
Instead of a Single Message, how possible to grab the recent posts?

grabbing recent_posts is already a function in SSI.php, neube!
My best suggestion to you is that you do whatever you feel like doing, for whatever reason you choose to make, without any required explanation nor justification. You probably will, so hop to it!

boo hoo!

Advertisement: