SimplePortal

Customization => Custom Coding => Topic started by: Cyst on September 18, 2008, 06:49:09 AM

Title: Custom Gallery Block
Post by: Cyst on September 18, 2008, 06:49:09 AM
I was wondering if there is a way to write a custom Block to pull not only a random image from the SMF Gallery, but also the description of the image under it?

Cheers :)
Title: Re: Custom Gallery Block
Post by: Cyst on September 18, 2008, 03:40:19 PM
By the way if it's not possible, this might be more useful in the feature request section :)
Title: Re: Custom Gallery Block
Post by: [SiNaN] on September 18, 2008, 04:12:14 PM
Maybe we can add the description field too, not sure how it looks.

Try creating a php block with this content:

Code: [Select]
global $modSettings, $context, $user_info, $scripturl, $sc, $ID_MEMBER, $db_prefix, $txt;

$GD_Installed = function_exists('imagecreate');

$dbresult = db_query("
SELECT p.ID_PICTURE
FROM {$db_prefix}gallery_pic as p
ORDER BY ID_PICTURE DESC
LIMIT 1", __FILE__, __LINE__);

$max = mysql_fetch_assoc($dbresult);
$check = 0;

while($check == 0) {

$idpic = rand(1, $max['ID_PICTURE']);

$dbresult = db_query("
SELECT p.ID_PICTURE, p.commenttotal, p.filesize, p.views, p.thumbfilename, p.filename, p.height, p.width, p.title, p.ID_MEMBER,
m.memberName, m.realName, p.date, p.description
FROM {$db_prefix}gallery_pic as p
LEFT JOIN {$db_prefix}members AS m on ( p.ID_MEMBER = m.ID_MEMBER)
WHERE p.approved = 1 AND p.ID_PICTURE = $idpic
ORDER BY ID_PICTURE DESC", __FILE__, __LINE__);
$gallery_picture = array();
while($row = mysql_fetch_assoc($dbresult))
{
$gallery_picture = array (
'id' => $row['ID_PICTURE'],
'title' => $row['title'],
'desc' => $row['description'],
'views' => $row['views'],
'senderid' => $row['ID_MEMBER'],
'sendername' => $row['realName'],
'thumbfilename' => $row['thumbfilename'],
'filename' => $row['filename']
);
}

mysql_free_result($dbresult);

if(!empty($gallery_picture) || empty($max))
$check = 1;
}

if (!empty($gallery_picture)) {
echo '<div align="center">';
echo '
<a href="' . $scripturl . '?action=gallery;sa=view;id=' . $gallery_picture['id'] . '"><img ' . ($GD_Installed == true ?  'src="' . $modSettings['gallery_url'] . $gallery_picture['thumbfilename'] . '" ' : 'src="' . $modSettings['gallery_url'] . $gallery_picture['filename'] . '" height="78" width="120" ')  . ' border="0" /></a>
<br />' . $gallery_picture['title'] . '<span class="smalltext"><br />
' . $gallery_picture['desc'] . '<br />
<b>' . $txt['sp-grpictureviews'] . '</b> ' . $gallery_picture['views'] . '<br />';
if ($gallery_picture['sendername'] != '')
echo '<b>' . $txt['sp-grpicturesender'] . '</b> <a href="' . $scripturl . '?action=profile;u=' . $gallery_picture['senderid'] . '">'  . $gallery_picture['sendername'] . '</a><br />';
else
echo '<b>' . $txt['sp-grpicturesender'] . '</b> ' . $txt['sp-grpictureguest'] . '<br />';
echo '</span>';
echo '</div>';
}
else
echo 'There is no picture in the gallery.';
Title: Re: Custom Gallery Block
Post by: Cyst on September 18, 2008, 04:22:30 PM
Wow, it worked a treat. With small sentences it does a great job.

It has a little trouble with long descriptions which is a shame though (links aren't formatted either which is a shame) is there anyway to force it to stretch the block vertically instead of horizontally and to format links? I feel like I'm asking for a bit too much, sorry!  :wave:

edit: Or perhaps a sort of cut off on the amount of characters shown and a small '...' link to the rest in the gallery would be better?
Title: Re: Custom Gallery Block
Post by: [SiNaN] on September 18, 2008, 04:24:47 PM
We can cut some part of the description if you want, just tell me the char limit. And what you mean by "format links"?

Feel free to ask whatever you want, we are here to answer them. ;)
Title: Re: Custom Gallery Block
Post by: Cyst on September 18, 2008, 04:44:43 PM
Hmm, maybe a 60 character limit would work? From the looks of it someone with a 101 characters on their description has stretched the block, while someone with 53 characters looks fine.
Would there be a way to add a link to the rest of the cut off description in the gallery, some thing like three periods that when clicked takes you there? I imagine that would be hard considering it's random...

By formatting of links I mean if someone posts
Code: [Select]
[url=http://url]text[/url] it shows the raw code instead of the actual link.
Title: Re: Custom Gallery Block
Post by: [SiNaN] on September 18, 2008, 04:57:49 PM
Ah, you mean BBC...

Edit the block, and use this codes instead:

Code: [Select]
global $modSettings, $context, $user_info, $scripturl, $sc, $ID_MEMBER, $db_prefix, $txt;

$GD_Installed = function_exists('imagecreate');

$dbresult = db_query("
SELECT p.ID_PICTURE
FROM {$db_prefix}gallery_pic as p
ORDER BY ID_PICTURE DESC
LIMIT 1", __FILE__, __LINE__);

$max = mysql_fetch_assoc($dbresult);
$check = 0;

while($check == 0) {

$idpic = rand(1, $max['ID_PICTURE']);

$dbresult = db_query("
SELECT p.ID_PICTURE, p.commenttotal, p.filesize, p.views, p.thumbfilename, p.filename, p.height, p.width, p.title, p.ID_MEMBER,
m.memberName, m.realName, p.date, p.description
FROM {$db_prefix}gallery_pic as p
LEFT JOIN {$db_prefix}members AS m on ( p.ID_MEMBER = m.ID_MEMBER)
WHERE p.approved = 1 AND p.ID_PICTURE = $idpic
ORDER BY ID_PICTURE DESC", __FILE__, __LINE__);
$gallery_picture = array();
while($row = mysql_fetch_assoc($dbresult))
{
$gallery_picture = array (
'id' => $row['ID_PICTURE'],
'title' => $row['title'],
'desc' => $row['description'],
'views' => $row['views'],
'senderid' => $row['ID_MEMBER'],
'sendername' => $row['realName'],
'thumbfilename' => $row['thumbfilename'],
'filename' => $row['filename']
);
}

mysql_free_result($dbresult);

if(!empty($gallery_picture) || empty($max))
$check = 1;
}

if (!empty($gallery_picture)) {
echo '<div align="center">';
echo '
<a href="' . $scripturl . '?action=gallery;sa=view;id=' . $gallery_picture['id'] . '"><img ' . ($GD_Installed == true ?  'src="' . $modSettings['gallery_url'] . $gallery_picture['thumbfilename'] . '" ' : 'src="' . $modSettings['gallery_url'] . $gallery_picture['filename'] . '" height="78" width="120" ')  . ' border="0" /></a>
<br />' . $gallery_picture['title'] . '<span class="smalltext"><br />
' . parse_bbc($gallery_picture['desc']) . '<br />
<b>' . $txt['sp-grpictureviews'] . '</b> ' . $gallery_picture['views'] . '<br />';
if ($gallery_picture['sendername'] != '')
echo '<b>' . $txt['sp-grpicturesender'] . '</b> <a href="' . $scripturl . '?action=profile;u=' . $gallery_picture['senderid'] . '">'  . $gallery_picture['sendername'] . '</a><br />';
else
echo '<b>' . $txt['sp-grpicturesender'] . '</b> ' . $txt['sp-grpictureguest'] . '<br />';
echo '</span>';
echo '</div>';
}
else
echo 'There is no picture in the gallery.';

It will make those raws codes normal. But with this, limiting the chars could result with descriptions like this:

Quote
This is a description. <a href="http://gotosomewhere.net">Tes...

Normally it should be

Quote
This is a description. Testing
Word "Testing" is link to http://gotosomewhere.net.

If you want, we can remove those raw codes, but they won't be shown as link; just as a plain text without raw codes.
Title: Re: Custom Gallery Block
Post by: Cyst on September 18, 2008, 05:04:21 PM
If you want, we can remove those raw codes, but they won't be shown as link; just as a plain text without raw codes.

This seems like a better idea, yes :) That way the tables won't get broken and there will be no BBC codes showing up either.
Title: Re: Custom Gallery Block
Post by: [SiNaN] on September 18, 2008, 05:12:19 PM
This will remove BBCodes and just show 150 chars:

Code: [Select]
global $modSettings, $context, $user_info, $scripturl, $sc, $ID_MEMBER, $db_prefix, $txt, $func;

$GD_Installed = function_exists('imagecreate');

$dbresult = db_query("
SELECT p.ID_PICTURE
FROM {$db_prefix}gallery_pic as p
ORDER BY ID_PICTURE DESC
LIMIT 1", __FILE__, __LINE__);

$max = mysql_fetch_assoc($dbresult);
$check = 0;

while($check == 0) {

$idpic = rand(1, $max['ID_PICTURE']);

$dbresult = db_query("
SELECT p.ID_PICTURE, p.commenttotal, p.filesize, p.views, p.thumbfilename, p.filename, p.height, p.width, p.title, p.ID_MEMBER,
m.memberName, m.realName, p.date, p.description
FROM {$db_prefix}gallery_pic as p
LEFT JOIN {$db_prefix}members AS m on ( p.ID_MEMBER = m.ID_MEMBER)
WHERE p.approved = 1 AND p.ID_PICTURE = $idpic
ORDER BY ID_PICTURE DESC", __FILE__, __LINE__);
$gallery_picture = array();
while($row = mysql_fetch_assoc($dbresult))
{

$row['description'] = strip_tags(strtr(parse_bbc($row['description'], 1), array('<br />' => '')));
if ($func['strlen']($row['description']) > 128)
$row['description'] = $func['substr']($row['description'], 0, 150) . '...';

$gallery_picture = array (
'id' => $row['ID_PICTURE'],
'title' => $row['title'],
'desc' => $row['description'],
'views' => $row['views'],
'senderid' => $row['ID_MEMBER'],
'sendername' => $row['realName'],
'thumbfilename' => $row['thumbfilename'],
'filename' => $row['filename']
);
}

mysql_free_result($dbresult);

if(!empty($gallery_picture) || empty($max))
$check = 1;
}

if (!empty($gallery_picture)) {
echo '<div align="center">';
echo '
<a href="' . $scripturl . '?action=gallery;sa=view;id=' . $gallery_picture['id'] . '"><img ' . ($GD_Installed == true ?  'src="' . $modSettings['gallery_url'] . $gallery_picture['thumbfilename'] . '" ' : 'src="' . $modSettings['gallery_url'] . $gallery_picture['filename'] . '" height="78" width="120" ')  . ' border="0" /></a>
<br />' . $gallery_picture['title'] . '<span class="smalltext"><br />
' . $gallery_picture['desc'] . '<br />
<b>' . $txt['sp-grpictureviews'] . '</b> ' . $gallery_picture['views'] . '<br />';
if ($gallery_picture['sendername'] != '')
echo '<b>' . $txt['sp-grpicturesender'] . '</b> <a href="' . $scripturl . '?action=profile;u=' . $gallery_picture['senderid'] . '">'  . $gallery_picture['sendername'] . '</a><br />';
else
echo '<b>' . $txt['sp-grpicturesender'] . '</b> ' . $txt['sp-grpictureguest'] . '<br />';
echo '</span>';
echo '</div>';
}
else
echo 'There is no picture in the gallery.';

If you want more or less, just change the number 150 in the codes.
Title: Re: Custom Gallery Block
Post by: Cyst on September 18, 2008, 05:17:16 PM
Thanks Sinan, that's perfect :) Was making the "..." after the character limit cut off link to the rest of the description possible too? If not I'm very happy anyway!
Title: Re: Custom Gallery Block
Post by: [SiNaN] on September 18, 2008, 05:24:09 PM
Try this:

Code: [Select]
global $modSettings, $context, $user_info, $scripturl, $sc, $ID_MEMBER, $db_prefix, $txt, $func;

$GD_Installed = function_exists('imagecreate');

$dbresult = db_query("
SELECT p.ID_PICTURE
FROM {$db_prefix}gallery_pic as p
ORDER BY ID_PICTURE DESC
LIMIT 1", __FILE__, __LINE__);

$max = mysql_fetch_assoc($dbresult);
$check = 0;

while($check == 0) {

$idpic = rand(1, $max['ID_PICTURE']);

$dbresult = db_query("
SELECT p.ID_PICTURE, p.commenttotal, p.filesize, p.views, p.thumbfilename, p.filename, p.height, p.width, p.title, p.ID_MEMBER,
m.memberName, m.realName, p.date, p.description
FROM {$db_prefix}gallery_pic as p
LEFT JOIN {$db_prefix}members AS m on ( p.ID_MEMBER = m.ID_MEMBER)
WHERE p.approved = 1 AND p.ID_PICTURE = $idpic
ORDER BY ID_PICTURE DESC", __FILE__, __LINE__);
$gallery_picture = array();
while($row = mysql_fetch_assoc($dbresult))
{

$row['description'] = strip_tags(strtr(parse_bbc($row['description'], 1), array('<br />' => '')));
if ($func['strlen']($row['description']) > 128)
$row['description'] = $func['substr']($row['description'], 0, 150) . '<a href="' . $scripturl . '?action=gallery;sa=view;id=' . $row['ID_PICTURE'] . '">...</a>';

$gallery_picture = array (
'id' => $row['ID_PICTURE'],
'title' => $row['title'],
'desc' => $row['description'],
'views' => $row['views'],
'senderid' => $row['ID_MEMBER'],
'sendername' => $row['realName'],
'thumbfilename' => $row['thumbfilename'],
'filename' => $row['filename']
);
}

mysql_free_result($dbresult);

if(!empty($gallery_picture) || empty($max))
$check = 1;
}

if (!empty($gallery_picture)) {
echo '<div align="center">';
echo '
<a href="' . $scripturl . '?action=gallery;sa=view;id=' . $gallery_picture['id'] . '"><img ' . ($GD_Installed == true ?  'src="' . $modSettings['gallery_url'] . $gallery_picture['thumbfilename'] . '" ' : 'src="' . $modSettings['gallery_url'] . $gallery_picture['filename'] . '" height="78" width="120" ')  . ' border="0" /></a>
<br />' . $gallery_picture['title'] . '<span class="smalltext"><br />
' . $gallery_picture['desc'] . '<br />
<b>' . $txt['sp-grpictureviews'] . '</b> ' . $gallery_picture['views'] . '<br />';
if ($gallery_picture['sendername'] != '')
echo '<b>' . $txt['sp-grpicturesender'] . '</b> <a href="' . $scripturl . '?action=profile;u=' . $gallery_picture['senderid'] . '">'  . $gallery_picture['sendername'] . '</a><br />';
else
echo '<b>' . $txt['sp-grpicturesender'] . '</b> ' . $txt['sp-grpictureguest'] . '<br />';
echo '</span>';
echo '</div>';
}
else
echo 'There is no picture in the gallery.';

Hope I got what you mean. :)
Title: Re: Custom Gallery Block
Post by: Cyst on September 18, 2008, 05:28:59 PM
Exactly what I was after, thanks Sinan. It looks great in action  8)
Title: Re: Custom Gallery Block
Post by: [SiNaN] on September 18, 2008, 05:29:51 PM
You're welcome. ;)
Title: Re: Custom Gallery Block
Post by: Cyst on September 18, 2008, 05:47:47 PM
This is weird, it was working perfectly a second ago and now it's allowing descriptions bigger than 60 characters to show again, I did change the 150 to 60, did I break anything?  :D

Code: [Select]
global $modSettings, $context, $user_info, $scripturl, $sc, $ID_MEMBER, $db_prefix, $txt, $func;

$GD_Installed = function_exists('imagecreate');

$dbresult = db_query("
SELECT p.ID_PICTURE
FROM {$db_prefix}gallery_pic as p
ORDER BY ID_PICTURE DESC
LIMIT 1", __FILE__, __LINE__);

$max = mysql_fetch_assoc($dbresult);
$check = 0;

while($check == 0) {

$idpic = rand(1, $max['ID_PICTURE']);

$dbresult = db_query("
SELECT p.ID_PICTURE, p.commenttotal, p.filesize, p.views, p.thumbfilename, p.filename, p.height, p.width, p.title, p.ID_MEMBER,
m.memberName, m.realName, p.date, p.description
FROM {$db_prefix}gallery_pic as p
LEFT JOIN {$db_prefix}members AS m on ( p.ID_MEMBER = m.ID_MEMBER)
WHERE p.approved = 1 AND p.ID_PICTURE = $idpic
ORDER BY ID_PICTURE DESC", __FILE__, __LINE__);
$gallery_picture = array();
while($row = mysql_fetch_assoc($dbresult))
{

$row['description'] = strip_tags(strtr(parse_bbc($row['description'], 1), array('<br />' => '')));
if ($func['strlen']($row['description']) > 128)
$row['description'] = $func['substr']($row['description'], 0, 60) . '<a href="' . $scripturl . '?action=gallery;sa=view;id=' . $row['ID_PICTURE'] . '">...</a>';

$gallery_picture = array (
'id' => $row['ID_PICTURE'],
'title' => $row['title'],
'desc' => $row['description'],
'views' => $row['views'],
'senderid' => $row['ID_MEMBER'],
'sendername' => $row['realName'],
'thumbfilename' => $row['thumbfilename'],
'filename' => $row['filename']
);
}

mysql_free_result($dbresult);

if(!empty($gallery_picture) || empty($max))
$check = 1;
}

if (!empty($gallery_picture)) {
echo '<div align="center">';
echo '
<a href="' . $scripturl . '?action=gallery;sa=view;id=' . $gallery_picture['id'] . '"><img ' . ($GD_Installed == true ?  'src="' . $modSettings['gallery_url'] . $gallery_picture['thumbfilename'] . '" ' : 'src="' . $modSettings['gallery_url'] . $gallery_picture['filename'] . '" height="78" width="120" ')  . ' border="0" /></a>
<br />' . $gallery_picture['title'] . '<span class="smalltext"><br />
' . $gallery_picture['desc'] . '<br />
<b>' . $txt['sp-grpictureviews'] . '</b> ' . $gallery_picture['views'] . '<br />';
if ($gallery_picture['sendername'] != '')
echo '<b>' . $txt['sp-grpicturesender'] . '</b> <a href="' . $scripturl . '?action=profile;u=' . $gallery_picture['senderid'] . '">'  . $gallery_picture['sendername'] . '</a><br />';
else
echo '<b>' . $txt['sp-grpicturesender'] . '</b> ' . $txt['sp-grpictureguest'] . '<br />';
echo '</span>';
echo '</div>';
}
else
echo 'There is no picture in the gallery.';
Title: Re: Custom Gallery Block
Post by: [SiNaN] on September 18, 2008, 05:57:17 PM
Sorry, missed a point. Change 128 in the codes as 60.
Title: Re: Custom Gallery Block
Post by: Cyst on September 18, 2008, 06:06:36 PM
Perfect! Thanks again, sorry I took up so much time there  :-[
Title: Re: Custom Gallery Block
Post by: edi67 on November 12, 2008, 04:47:40 PM
great work sinan is possible have code for smf 2?
Title: Re: Custom Gallery Block
Post by: [SiNaN] on November 13, 2008, 01:36:27 AM
Very soon...
Title: Re: Custom Gallery Block
Post by: dabeer on February 21, 2009, 02:01:27 PM
I'm using this code (really cool, thank you!) and I just thought of the following additions to this block:
- the number of comments for the displayed picture
- total number of pictures in the gallery

Something like:

--

[THUMBNAIL]

Title
Description

Views: 120
Author: sAmpLe
Comments: 3

Total pictures in Gallery: 44

--

Is there an easy way of implementing these changes?
SimplePortal 2.3.8 © 2008-2024, SimplePortal