Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > vBulletin 4 Articles
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools
How to use the vBulletin 4 Lightbox Anywhere
Ideal Web Tech's Avatar
Ideal Web Tech
Join Date: Feb 2008
Posts: 273

 

Show Printable Version Email this Page Subscription
Ideal Web Tech Ideal Web Tech is offline 03-29-2010, 10:00 PM

This tutorial will explain how to use the vB 4 lightbox anywhere in the site for images other than attachments.

In order to accomplish this you will have to make the following:
  • A file that returns xml & processes the images
  • Template edits where you want to use the lightbox
  • A database table containing the filename, upload/creation time of the image & a unique id. (could be done without a database table, but a database table would be best)

The File:
Create a file, name it whatever you like, in this tutorial we will use "testlightbox.php" which should be placed in the root directory with the following:
  1. PHP Code:
    // if $_POST['ajax'] is set, we need to set a $_REQUEST['do'] so we can precache the lightbox template
    if (!empty($_POST['ajax']) AND isset($_POST['uniqueid']))
    {
        
    $_REQUEST['do'] = 'lightbox';

    What that does is checks if we are loading the lightbox or just the image so that the lightbox template can be precached & later we can create the xml needed for the lightbox. (Will fail and just display the image if javascript isn't enabled.)
  2. PHP Code:
    // #################### PRE-CACHE TEMPLATES AND DATA ######################
    // get special phrase groups
    $phrasegroups = array();

    // get special data templates from the datastore
    $specialtemplates = array();

    // pre-cache templates used by all actions
    $globaltemplates = array();

    // pre-cache templates used by specific actions
    $actiontemplates = array('lightbox' => array('lightbox'));

    /*
    The following headers are usually handled internally. The cache-control is to stop caches keeping private images and the Vary header is to deal with the fact the filename encoding changes.
    */
    header('Cache-Control: private');
    header('Vary: User-Agent');

    // ########################## REQUIRE BACK-END ############################
    require_once('./global.php'); 
    This is some generic back-end settings.
  3. After completing steps 1 & 2 we need to process the values passed in though posts/requests. This can be done using:
    PHP Code:
    $vbulletin->input->clean_array_gpc('r', array(
        
    'id'       => TYPE_UINT
    ));

    $imgid $vbulletin->GPC['id'];

    $vbulletin->input->clean_array_gpc('p', array(
        
    'ajax'     => TYPE_BOOL,
        
    'uniqueid' => TYPE_UINT
    )); 
  4. Now you will need to retrieve the information for the images being loaded from the database.
  5. Now at this point if we are loading the lightbox we will break off into it, if not we will process and display the image.
    PHP Code:
    if ($_REQUEST['do'] == 'lightbox')
    {
        
    //Setup values that will be passed by the ajax caller
        
    $vbulletin->input->clean_array_gpc('r', array(
            
    'width'   => TYPE_UINT,
            
    'height'  => TYPE_UINT,
            
    'first'   => TYPE_BOOL,
            
    'last'    => TYPE_BOOL,
            
    'current' => TYPE_UINT,
            
    'total'   => TYPE_UINT
        
    ));

        
    $width $vbulletin->GPC['width'];
        
    $height $vbulletin->GPC['height'];
        
    $first $vbulletin->GPC['first'];
        
    $last $vbulletin->GPC['last'];
        
    $current $vbulletin->GPC['current'];
        
    $total $vbulletin->GPC['total'];

        
    //Setup the XML generation engine
        
    require_once(DIR '/includes/class_xml.php');
        
    $xml = new vB_AJAX_XML_Builder($vbulletin'text/xml');

            
    // Realistically this information would be queried from the  database
            
    $imageinfo $imgfiles["$imgid"];

            
    $uniqueid $vbulletin->GPC['uniqueid'];

            
    //The url to the image
            
    $imagelink 'testlightbox.php?id=' $imgid .  $vbulletin->session->vars['sessionurl'];
            
            
    //Date strings used by lightbox
            
    $imageinfo['date_string'] =  vbdate($vbulletin->options['dateformat'], $imageinfo['dateline']);
            
    $imageinfo['time_string'] =  vbdate($vbulletin->options['timeformat'], $imageinfo['dateline']);

            
    $templater vB_Template::create('lightbox');
                
    $templater->register('attachmentinfo'$imageinfo);  //  This one is named attachmentinfo because of the current variable used in  the defualt lightbox template
                
    $templater->register('current'$current);
                
    $templater->register('first'$first);
                
    $templater->register('height'$height);
                
    $templater->register('imagelink'$imagelink);
                
    $templater->register('last'$last);
                
    $templater->register('total'$total);
                
    $templater->register('uniqueid'$uniqueid);
                
    $templater->register('width'$width);
            
    $html $templater->render(true);

            
    //Build the xml tags
            
    $xml->add_group('img');
            
    $xml->add_tag('html'process_replacement_vars($html));
            
    $xml->add_tag('link'$imagelink);
            
    $xml->add_tag('name'$imageinfo['filename']);
            
    $xml->add_tag('date'$imageinfo['date_string']);
            
    $xml->add_tag('time'$imageinfo['time_string']);
            
    $xml->close_group();

        
    //Spitout the xml and get out of here
        
    $xml->print_xml();
    }
    else
    {
        
    header('Content-Type: image/png');
         
    readfile("./{$imgfiles[$imgid][filename]}");

Now this file should look something like this:
PHP Code:
<?php
// ######################## SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);

// if $_POST['ajax'] is set, we need to set a $_REQUEST['do'] so we can precache the lightbox template
if (!empty($_POST['ajax']) AND isset($_POST['uniqueid']))
{
    
$_REQUEST['do'] = 'lightbox';
}

// #################### PRE-CACHE TEMPLATES AND DATA ######################
// get special phrase groups
$phrasegroups = array();

// get special data templates from the datastore
$specialtemplates = array();

// pre-cache templates used by all actions
$globaltemplates = array();

// pre-cache templates used by specific actions
$actiontemplates = array('lightbox' => array('lightbox'), 'demo' => array('iwt_lightbox_demo'));

/*
The following headers are usually handled internally. The cache-control is to stop caches keeping private images and the Vary header is to deal with the fact the filename encoding changes.
*/
header('Cache-Control: private');
header('Vary: User-Agent');

// ########################## REQUIRE BACK-END ############################
require_once('./global.php');

$vbulletin->input->clean_array_gpc('r', array(
    
'id'       => TYPE_UINT
));

$imgid $vbulletin->GPC['id'];

$vbulletin->input->clean_array_gpc('p', array(
    
'ajax'     => TYPE_BOOL,
    
'uniqueid' => TYPE_UINT
));

// ### Code to retrive data from database #################################

// handle lightbox requests
if ($_REQUEST['do'] == 'lightbox')
{
    
//Setup values that will be passed by the ajax caller
    
$vbulletin->input->clean_array_gpc('r', array(
        
'width'   => TYPE_UINT,
        
'height'  => TYPE_UINT,
        
'first'   => TYPE_BOOL,
        
'last'    => TYPE_BOOL,
        
'current' => TYPE_UINT,
        
'total'   => TYPE_UINT
    
));

    
$width $vbulletin->GPC['width'];
    
$height $vbulletin->GPC['height'];
    
$first $vbulletin->GPC['first'];
    
$last $vbulletin->GPC['last'];
    
$current $vbulletin->GPC['current'];
    
$total $vbulletin->GPC['total'];

    
//Setup the XML generation engine
    
require_once(DIR '/includes/class_xml.php');
    
$xml = new vB_AJAX_XML_Builder($vbulletin'text/xml');

        
// Realistically this information would be queried from the database
        
$imageinfo $imgfiles["$imgid"];

        
$uniqueid $vbulletin->GPC['uniqueid'];

        
//The url to the image
        
$imagelink 'testlightbox.php?id=' $imgid $vbulletin->session->vars['sessionurl'];
        
        
//Date strings used by lightbox
        
$imageinfo['date_string'] = vbdate($vbulletin->options['dateformat'], $imageinfo['dateline']);
        
$imageinfo['time_string'] = vbdate($vbulletin->options['timeformat'], $imageinfo['dateline']);

        
$templater vB_Template::create('lightbox');
            
$templater->register('attachmentinfo'$imageinfo);  // This one is named attachmentinfo because of the current variable used in the defualt lightbox template
            
$templater->register('current'$current);
            
$templater->register('first'$first);
            
$templater->register('height'$height);
            
$templater->register('imagelink'$imagelink);
            
$templater->register('last'$last);
            
$templater->register('total'$total);
            
$templater->register('uniqueid'$uniqueid);
            
$templater->register('width'$width);
        
$html $templater->render(true);

        
//Build the xml tags
        
$xml->add_group('img');
        
$xml->add_tag('html'process_replacement_vars($html));
        
$xml->add_tag('link'$imagelink);
        
$xml->add_tag('name'$imageinfo['filename']);
        
$xml->add_tag('date'$imageinfo['date_string']);
        
$xml->add_tag('time'$imageinfo['time_string']);
        
$xml->close_group();

    
//Spitout the xml and get out of here
    
$xml->print_xml();
}
else
{
    
//Send the image type header (png in this case since for the demo we are using only pngs)
    
header('Content-Type: image/png');

    
//This works for the demo purposes but readfile could be replaced with a more effective method for handling larger images  (such as a buffering system like attachments uses) 
    
readfile("./{$imgfiles[$imgid][filename]}");
}

?>
The Template Edits:

For the lightbox to work for your images you will need to place the following in the appropriate templates:
  1. HTML Code:
    <link rel="stylesheet" type="text/css" href="{vb:raw vbcsspath}lightbox.css" />
    <script type="text/javascript" src="clientscript/vbulletin_lightbox.js?v=402"></script>
    In the head tag of the template.
  2. A div around all the links you want to be affected. (Can be multiple divs if needed).
  3. Each div will need a unique id value.
  4. Each image you want linked should be wrapped with the following:
    HTML Code:
    <a href="IMAGEURL" rel="Lightbox_GROUPNUMBER" id="IMAGEID">IMAGE TAG</a>
    Where:
    • IMAGEURL = the url to the file we just made & a reference to the image to load (ex. testlightbox.php?id=2)
    • GROUPNUMBER = A number (All images with identical groupnumbers will be grouped together when shown in the lightbox.)
    • IMAGEID = A unique id for the image (Can be just about anything)
  5. Lastly after all the divs, preferably right before the footer is called, you will need to put:
    HTML Code:
    <script type="text/javascript">      <!--     vBulletin.register_control("vB_Lightbox_Container", "DIV_ID", 1);     //-->     </script>
    Where DIV_ID is equal to the id of the div made in step 2.
Example Template:
HTML Code:
{vb:stylevar htmldoctype}
<html xmlns="http://www.w3.org/1999/xhtml" <vb:if condition="!is_browser('ie') OR is_browser('ie',8)"> dir="{vb:stylevar textdirection}"</vb:if> lang="{vb:stylevar languagecode}" id="vbulletin_html">
<head>
    {vb:raw headinclude}
    <title>{vb:raw pagetitle}</title>
    <vb:if condition="$includecss">
        <vb:each from="includecss" value="file">
        <link rel="stylesheet" type="text/css" href="{vb:raw vbcsspath}{vb:raw file}.css" />
        </vb:each>
    </vb:if>
<link rel="stylesheet" type="text/css" href="{vb:raw vbcsspath}lightbox.css" />
        {vb:raw headinclude_bottom}
<script type="text/javascript" src="clientscript/vbulletin_lightbox.js?v=402"></script>
</head>
<body>
{vb:raw header}
{vb:raw navbar}

<div id="lightboximages" class="blockbody">
    <h2 class="blockhead">Sample Images</h2>
    <div class="blockrow">
        <div style="padding-bottom: 10px; text-align: center;">
            This image is not grouped:<br /><br />
            <a href="iwtlightboxdemo/testlightbox.php?id=1" rel="Lightbox_0" id="image1">
                <img class="thumbnail" src="iwtlightboxdemo/IWT_Products_System_Thumb.png" alt="test"/>
            </a>
        </div>
        <div style="text-align: center;">
            These two are grouped:<br /><br />
            <a href="iwtlightboxdemo/testlightbox.php?id=2" rel="Lightbox_1" id="image2">
                <img class="thumbnail" src="iwtlightboxdemo/IWT_Registration_Imposter_Blocker_Thumb.png" alt="test"/>
            </a>&nbsp;    
            <a href="iwtlightboxdemo/testlightbox.php?id=3" rel="Lightbox_1" id="image3">
                <img class="thumbnail" src="iwtlightboxdemo/IWT_Time_Spent_Online_Thumb.png" alt="test"/>
            </a>
        </div>
    </div>
</div>

<script type="text/javascript">
<!--
vBulletin.register_control("vB_Lightbox_Container", "lightboximages", 1);
//-->
</script>

{vb:raw footer}
</body>
</html>
Below you will find a zip that contains this tutorial & a sample of how this works. To use the sample simply upload the contents of the uploads folder to your forum root directory, install the product, and open www.yourdomain.com/forumroot/iwtlightboxdemo/testlightbox.php in your browser.

An example of it can also be found at www.idealwebtech.com/demos/vb_lightbox_demo/lightbox.php

This tutorial brought to you by Ideal Web Technologies.
Attached Files
File Type: zip IWT - vB Lightbox Demo (v1.0.0).zip (153.5 KB, 194 views)
Reply With Quote
  #2  
Old 03-30-2010, 02:08 PM
MARCO1's Avatar
MARCO1 MARCO1 is offline
 
Join Date: Jun 2008
Posts: 872
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Excellent tutorial
Reply With Quote
  #3  
Old 04-07-2010, 08:28 AM
derfelix derfelix is offline
 
Join Date: Nov 2001
Posts: 204
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Excellent...
What I have been trying to do without success is using lightbox to access images outside the vbulletin folder

example :
root -- specialimages/ image1.jpg
root -- forums/pagefile.php

when calling pagefile, i wanted to display thumbnails from the specialimage folder (thats not a problem)
but displaying the images with the lightbox doesnt work for me..

F.
Reply With Quote
  #4  
Old 04-07-2010, 05:20 PM
Ideal Web Tech's Avatar
Ideal Web Tech Ideal Web Tech is offline
 
Join Date: Feb 2008
Posts: 273
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by derfelix View Post
Excellent...
What I have been trying to do without success is using lightbox to access images outside the vbulletin folder

example :
root -- specialimages/ image1.jpg
root -- forums/pagefile.php

when calling pagefile, i wanted to display thumbnails from the specialimage folder (thats not a problem)
but displaying the images with the lightbox doesnt work for me..

F.
Were you just setting the path of the image as "specialimages/ image1.jpg"?

If so you will need to include a "../" in front of it to take you out of the forums folder and back to the root directory.
Reply With Quote
  #5  
Old 04-07-2010, 05:50 PM
derfelix derfelix is offline
 
Join Date: Nov 2001
Posts: 204
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

ahhh.. thx..
no i actually have to calculate the url from db..
i was doing the error by wanting to do it inside lightbox.. but if done on the page it works..

Thank you
Reply With Quote
  #6  
Old 04-08-2010, 07:25 AM
xman_79's Avatar
xman_79 xman_79 is offline
 
Join Date: Jun 2006
Location: Romania
Posts: 65
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Excellent

Thank you
Reply With Quote
  #7  
Old 03-06-2011, 04:34 PM
valdet's Avatar
valdet valdet is offline
 
Join Date: Feb 2007
Posts: 505
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Sorry for bumping this thread.

Does anyone know how can we implement a simple lightbox, which will show registration fields.

This is very popular for list building in Wordpress blogs, but I haven't seen anything like that on vBulletin boards.
Reply With Quote
  #8  
Old 03-07-2011, 03:06 PM
fluidswork's Avatar
fluidswork fluidswork is offline
 
Join Date: Apr 2010
Location: India
Posts: 143
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Great idea............
Reply With Quote
  #9  
Old 03-07-2011, 06:12 PM
ErnestO999 ErnestO999 is offline
 
Join Date: Apr 2010
Location: San Francisco, CA
Posts: 11
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks For The Tutorial I Mark this thread excellent
Reply With Quote
  #10  
Old 04-06-2011, 06:45 PM
KProjects KProjects is offline
 
Join Date: Feb 2006
Posts: 143
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

nice - will be giving this a try later tonight
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 02:14 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.10285 seconds
  • Memory Usage 2,426KB
  • Queries Executed 24 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (4)bbcode_html
  • (5)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (9)postbit
  • (1)postbit_attachment
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_attachment
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete