TinyPortal

Development => Block Codes => Topic started by: samo_zin123 on June 25, 2009, 09:28:05 AM

Title: [ Block ] Recent Image Attachments Block
Post by: samo_zin123 on June 25, 2009, 09:28:05 AM
Hello every one
this code taken from the simpleportal codes
it shows the Recent 5 Image Attachments
i modified it to mke the user able to go to the original topic when  click on the image

i hope we co-oprate to gether to make it looks better

put the following code in new php tp block


global $db_prefix, $modSettings, $scripturl, $txt, $settings;

$boards = boardsAllowedTo('view_attachments');

if (empty($boards))
return array();
elseif ($boards[0] == 0)
$boards = '';
else
$boards = ' AND m.ID_BOARD IN (' . implode(',', $boards) . ')';

$limit = empty($parameters[0]) ? 5 : (int) $parameters[0];

$request = db_query("
SELECT
att.ID_ATTACH, att.ID_MSG, att.filename, IFNULL(att.size, 0) AS filesize, att.downloads, mem.ID_MEMBER,
IFNULL(mem.realName, m.posterName) AS posterName, m.ID_TOPIC, m.subject, t.ID_BOARD, m.posterTime,
att.width, att.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 att
INNER JOIN {$db_prefix}messages AS m ON (m.ID_MSG = att.ID_MSG)
INNER JOIN {$db_prefix}topics AS t ON (t.ID_TOPIC = m.ID_TOPIC)
LEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = m.ID_MEMBER)" . (empty($modSettings['attachmentShowImages']) || empty($modSettings['attachmentThumbnails']) ? "" : "
LEFT JOIN {$db_prefix}attachments AS thumb ON (thumb.ID_ATTACH = att.ID_THUMB)") . "
WHERE att.attachmentType = 0
AND att.width != 0
$boards
ORDER BY att.ID_ATTACH DESC
LIMIT $limit", __FILE__, __LINE__);
$items = array();
while ($row = mysql_fetch_assoc($request))
{
$filename = preg_replace('~&#(\\d{1,7}|x[0-9a-fA-F]{1,6});~', '&#\\1;', htmlspecialchars($row['filename']));

$items[$row['ID_ATTACH']] = array(
'member' => array(
'id' => $row['ID_MEMBER'],
'name' => $row['posterName'],
'link' => empty($row['ID_MEMBER']) ? $row['posterName'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['ID_MEMBER'] . '">' . $row['posterName'] . '</a>',
),
'file' => array(
'filename' => $filename,
'filesize' => round($row['filesize'] /1024, 2) . $txt['smf211'],
'downloads' => $row['downloads'],
'href' => $scripturl . '?action=dlattach;topic=' . $row['ID_TOPIC'] . '.0;attach=' . $row['ID_ATTACH'],
'link' => ' <a href="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . '">' . $row['subject'] . '</a>',
'is_image' => !empty($row['width']) && !empty($row['height']) && !empty($modSettings['attachmentShowImages']),
),
'topic' => array(
'id' => $row['ID_TOPIC'],
'subject' => $row['subject'],
'href' => $scripturl . '?topic=' . $row['ID_TOPIC'] . '.msg' . $row['ID_MSG'] . '#msg' . $row['ID_MSG'],
'link' => '<a href="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . '.msg' . $row['ID_MSG'] . '#msg' . $row['ID_MSG'] . '">' . $row['subject'] . '</a>',
'time' => timeformat($row['posterTime']),
),
);

if ($items[$row['ID_ATTACH']]['file']['is_image'])
{
$id_thumb = empty($row['ID_THUMB']) ? $row['ID_ATTACH'] : $row['ID_THUMB'];
$items[$row['ID_ATTACH']]['file']['image'] = array(
'id' => $id_thumb,
'width' => $row['width'],
'height' => $row['height'],
'img' => '<img src="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0;attach=' . $row['ID_ATTACH'] . ';image" alt="' . $row['subject'] . '" />',
'thumb' => '<img src="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0;attach=' . $id_thumb . ';image" alt="' . $row['subject'] . '" />',
'href' => $scripturl . '?topic=' . $row['ID_TOPIC'] ,
'link' => '<a href="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0"><img src="' . $scripturl . '?action=dlattach;topic=' . $row['ID_TOPIC'] . '.0;attach=' . $id_thumb . ';image" alt="' . $row['subject'] . '"' . (empty($row['ID_THUMB']) ? ' width="180" height="210"' : '') . ' /></a>',
);
}
}
mysql_free_result($request);

echo '
<table style="border: 0; margin: 0 auto;">
  <tr>';

foreach ($items as $item)
  echo '
    <td width="600">
      <div class="normaltext" style="text-align: center; line-height: 1.4em; border: 1px #ededed solid; padding: 5px;">
      ', $item['file']['link'], '<br />
      ', $item['file']['image']['link'], '<br />
      ','Downloads: ', $item['file']['downloads'], '<br />
      ', 'Posted BY: ', $item['member']['link'], '
      </div>    </td>';

echo '
  </tr>
</table>';




i hope u like it
iam trying to make it shows the 10 images in more than one row
iam having aproblem with the width actually (need to show 10 images in 2 rows  or more )  lets try to make it
Title: Re: [ Block ] Recent Image Attachments Block
Post by: samo_zin123 on July 01, 2009, 07:59:33 AM
this code is better as it shows one image from the post
its ssi function u need to this code to ur ssi.php file
put this code
function ssi_recentPostsAttach($num_recent = 8, $exclude_boards = null, $output_method = 'echo')
{
   global $context, $settings, $scripturl, $txt, $db_prefix, $ID_MEMBER;
   global $user_info, $modSettings, $func;

   if ($exclude_boards === null && !empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0)
      $exclude_boards = array($modSettings['recycle_board']);
   else
      $exclude_boards = empty($exclude_boards) ? array() : $exclude_boards;

   // Find all the posts.  Newer ones will have higher IDs.
   $request = db_query("
      SELECT
         m.posterTime, m.subject, m.ID_TOPIC, m.ID_MEMBER, m.ID_MSG, m.ID_BOARD, b.name AS bName,
         IFNULL(mem.realName, m.posterName) AS posterName, " . ($user_info['is_guest'] ? '1 AS isRead, 0 AS new_from' : '
         IFNULL(lt.ID_MSG, IFNULL(lmr.ID_MSG, 0)) >= m.ID_MSG_MODIFIED AS isRead,
         IFNULL(lt.ID_MSG, IFNULL(lmr.ID_MSG, -1)) + 1 AS new_from') . ", LEFT(m.body, 384) AS body, m.smileysEnabled
      FROM ({$db_prefix}messages AS m, {$db_prefix}boards AS b)
         LEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = m.ID_MEMBER)" . (!$user_info['is_guest'] ? "
         LEFT JOIN {$db_prefix}log_topics AS lt ON (lt.ID_TOPIC = m.ID_TOPIC AND lt.ID_MEMBER = $ID_MEMBER)
         LEFT JOIN {$db_prefix}log_mark_read AS lmr ON (lmr.ID_BOARD = m.ID_BOARD AND lmr.ID_MEMBER = $ID_MEMBER)" : '') . "
      WHERE m.ID_MSG >= " . ($modSettings['maxMsgID'] - 25 * min($num_recent, 5)) . "
         AND b.ID_BOARD = m.ID_BOARD" . (empty($exclude_boards) ? '' : "
         AND b.ID_BOARD NOT IN (" . implode(', ', $exclude_boards) . ")") . "
         AND $user_info[query_see_board]
      ORDER BY m.ID_MSG DESC
      LIMIT $num_recent", __FILE__, __LINE__);
   $posts = array();
   $messages = array();
   while ($row = mysql_fetch_assoc($request))
   {
      $row['body'] = strip_tags(strtr(parse_bbc($row['body'], $row['smileysEnabled'], $row['ID_MSG']), array('<br />' => '')));
      if ($func['strlen']($row['body']) > 128)
         $row['body'] = $func['substr']($row['body'], 0, 128) . '...';

      // Censor it!
      censorText($row['subject']);
      censorText($row['body']);

      $messages[] = $row['ID_MSG'];

      // Build the array.
      $posts[$row['ID_MSG']] = array(
         '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>'
         ),
         'topic' => $row['ID_TOPIC'],
         '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']) ? $row['posterName'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['ID_MEMBER'] . '">' . $row['posterName'] . '</a>'
         ),
         'subject' => $row['subject'],
         'short_subject' => shorten_subject($row['subject'], 25),
         'preview' => $row['body'],
         'time' => timeformat($row['posterTime']),
         'timestamp' => forum_time(true, $row['posterTime']),
         'href' => $scripturl . '?topic=' . $row['ID_TOPIC'] . '.msg' . $row['ID_MSG'] . ';topicseen#new',
         'link' => '<a href="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . '.msg' . $row['ID_MSG'] . '#msg' . $row['ID_MSG'] . '">' . $row['subject'] . '</a>',
         'new' => !empty($row['isRead']),
         'new_from' => $row['new_from'],
         'images' => array(),
      );
   }
   mysql_free_result($request);

   $request = db_query("
      SELECT
         m.ID_MSG, m.ID_TOPIC,
         IFNULL(big.ID_ATTACH, 0) AS bigId, big.filename AS bigFilename, big.width AS bigWidth, big.height AS bigHeight,
         IFNULL(thn.ID_ATTACH, 0) AS thnId, thn.filename AS thnFilename, thn.width AS thnWidth, thn.height AS thnHeight
      FROM {$db_prefix}messages AS m
         LEFT JOIN {$db_prefix}attachments AS big ON (big.ID_MSG = m.ID_MSG AND big.attachmentType = 0)
         LEFT JOIN {$db_prefix}attachments AS thn ON (thn.ID_ATTACH = big.ID_THUMB)
      WHERE m.ID_MSG IN (" . implode(', ', $messages) . ")
      ORDER BY m.ID_MSG DESC", __FILE__, __LINE__);
   while ($row = mysql_fetch_assoc($request))
   {
      if ($row['thnId'] != 0)
         $image = array(
            'id' => $row['thnId'],
            'url' => $scripturl . '?action=dlattach;topic=' . $row['ID_TOPIC'] . '.0;attach=' . $row['thnId'] . ';image',
            'link' => $scripturl . '?action=dlattach;topic=' . $row['ID_TOPIC'] . '.0;attach=' . $row['bigId'] . ';image',
            'filename' => $row['thnFilename'],
            'width' => $row['thnWidth'],
            'height' => $row['thnHeight']
         );
      elseif ($row['bigId'] != 0 && $row['bigWidth'] > 0 && $row['bigHeight'] > 0)
         $image = array(
            'id' => $row['bigId'],
            'url' => $scripturl . '?action=dlattach;topic=' . $row['ID_TOPIC'] . '.0;attach=' . $row['bigId'] . ';image',
            'link' => $scripturl . '?action=dlattach;topic=' . $row['ID_TOPIC'] . '.0;attach=' . $row['bigId'] . ';image',
            'filename' => $row['bigFilename'],
            'width' => $row['bigWidth'],
            'height' => $row['bigHeight']
         );
      else
         $image = array();

      if (isset($posts[$row['ID_MSG']]['images']))
         $posts[$row['ID_MSG']]['images'][] = $image;
      else
         $posts[$row['ID_MSG']]['images'] = array($image);
   }
   mysql_free_result($request);

   foreach($posts as $key => $post)
   {
      $imagefound = false;
   
      foreach ($post['images'] as $img)
      {

         if ($imagefound == true)
            break;

         if (isset($img['id']))
         {
            $posts[$key]['image'] = '<a href="' . $post['href'] . '"><img src="' . $img['url'] . '" width="' . $img['width'] . '" height="' . $img['height'] . '" alt="" /></a>';
            $imagefound = true;
         }
      }
   }

   // Just return it.
   if ($output_method != 'echo' || empty($posts))
      return $posts;

<DIV ALIGN="CENTER">
<table width="90%">
<tr>';
   foreach ($posts as $post)
      echo '
         <tr>
            <td valign="top">
               <a href="', $post['href'], '">', $post['subject'], '</a>
             
                             ', $post['new'] ? '' : '<a href="' . $scripturl . '?topic=' . $post['topic'] . '.msg' . $post['new_from'] . ';topicseen#new"><img src="' . $settings['images_url'] . '/' . $context['user']['language'] . '/new.gif" alt="' . $txt[302] . '" border="0" /></a>', '

               ', !empty($post['image']) ? $post['image'] : '', '
            </td>
         </tr>
</tr>
</table></div>';
}


before

// Recent topic list:   [board] Subject by Poster Date

and make anew tp php block  and put this inside
ssi_recentPostsAttach();

done
Title: Re: [ Block ] Recent Image Attachments Block
Post by: Mick on October 22, 2009, 09:52:22 PM
Great concept but a few things about this hack,...

-Guest cannot view image, only text link.
-Guest can view the board and topic name when they're not allowed to.

Can anyone set the permission to this hack as used in SMF?



...also,  can you look into only fetching the last 5 images from a certain topic instead of all topics?  Maybe by having the abiltiy to add a message number or topic number?

Thanx,
Mick.