SimplePortal

Customization => Blocks and Modifications => Mod Requests => Topic started by: lg965 on October 17, 2016, 06:49:44 PM

Title: Random Image Attachments
Post by: lg965 on October 17, 2016, 06:49:44 PM
Hello!

I'd like to have a block, with random pictures attached.
Any idea how can I do? Maybe changing the existing "last image attachments"?

Thanks!
Title: Re: Random Image Attachments
Post by: lg965 on October 18, 2016, 03:35:46 PM
In PortalBlocks.php I found this code and I think that is the right to edit...

Code: [Select]

function sp_attachmentImage($parameters, $id, $return_parameters = false)
{
global $boarddir, $txt, $color_profile;

$block_parameters = array(
'limit' => 'int',
'direction' => 'select',
'disablePoster' => 'check',
'disableDownloads' => 'check',
'disableLink' => 'check',
);

if ($return_parameters)
return $block_parameters;

$limit = empty($parameters['limit']) ? 5 : (int) $parameters['limit'];
$direction = empty($parameters['direction']) ? 0 : 1;
$type = array('jpg', 'png', 'gif', 'bmp');
$showPoster = empty($parameters['disablePoster']);
$showDownloads = empty($parameters['disableDownloads']);
$showLink = empty($parameters['disableLink']);

$items = ssi_recentAttachments($limit, $type, 'array');

if (empty($items))
{
echo '
', $txt['error_sp_no_attachments_found'];
return;
}

$colorids = array();
foreach ($items as $item)
$colorids[] = $item['member']['id'];

if (!empty($colorids) && sp_loadColors($colorids) !== false)
{
foreach ($items as $k => $p)
{
if (!empty($color_profile[$p['member']['id']]['link']))
$items[$k]['member']['link'] = $color_profile[$p['member']['id']]['link'];
}
}

echo '
<table class="sp_auto_align">', $direction ? '
<tr>' : '';

foreach ($items as $item)
{
  echo !$direction ? '
<tr>' : '', '
<td>
<div class="sp_image smalltext">', ($showLink ? '
<a href="' . $item['file']['href'] . '">' . $item['file']['filename'] . '</a><br />' : ''), '
', $item['file']['image']['link'], '<br />', ($showDownloads ? '
' . $txt['downloads'] . ': ' . $item['file']['downloads'] . '<br />' : ''), ($showPoster ? '
' . $txt['posted_by'] . ': ' . $item['member']['link'] : ''), '
</div>
</td>', !$direction ? '
</tr>' : '';
}

echo $direction ? '
</tr>' : '', '
</table>';
}


but I do not know where to start, help please  :)
Title: Re: Random Image Attachments
Post by: emanuele on October 31, 2016, 04:22:16 AM
I thought I answered that one, but probably I just wrote the answer and forgot to click "post"...
/me needs a mind-reading computer, click "post" is too boring. :P

Pick an image at random from the attachments is a bit of a pain because you have to account for:
1) avatars,
2) thumbnails,
3) permissions.
In fact you'd have to use a query that would resemble:
Code: [Select]
SELECT a.id_attach, ...
FROM {db_prefix}attachments AS a
LEFT JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg)
LEFT JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
WHERE a.attachment_type = 0
AND {query_see_board}
ORDER BY RAND()
LIMIT 1
That may be a bit on the heavy side depending on many factors (number of boards, number of messages, number of attachments.
Easier would (probably) be to show a random attachment of a specific board (you wouldn't need the query_see_board part and as a consequences you wouldn't need to join the boards table as well).
Title: Re: Random Image Attachments
Post by: ccbtimewiz on June 10, 2017, 07:26:41 PM
How did this go?
SimplePortal 2.3.8 © 2008-2024, SimplePortal