Tuesday, November 24, 2009

Lightbox

So, following on from the last post i've added a simple Lightbox. This needs to allow users to:

  • Add images to lightbox at various places

  • View images in their lightbox and remove one or more



At the moment a user will have a single lightbox; it's not designed to be a galleries type of system; its simply a 'bucket' to add images to in readiness for buying and downloading them.

So, its relatively straightforward; i added a component with one page and a couple of tasks; all the tasks result in the user being presented with their lightbox; the tasks them selves define whether an image (or images) are added to the lightbox or removed. The default task simply bypasses the processing and goes to the lightbox view/page.

Session
The one wrinkle is that a lightbox can be created in session by guest users OR if logged in the lightbox is saved and persists across sessions. This means that the processing has to check at various stages whether the current user is logged in. This is achieved by this code:


$user =& JFactory::getUser();
if (!$user->guest){
...
}


This 'if' block will run for logged in users. So all we do at each stage is to check if the user's logged in and then add to d/b and session (or just session) accordingly.

Both cases put the list of lightbox items in the session:


$session =& JFactory::getSession();
$session->set('PJ_IMAGES',$images);


where '$images' is an array of image 'objects'.

This object list is created from a SQL statement and the array built using
$rows = $db->loadObjectList();
; it also uses some JoomGallery helper functions.

Parameters
One thing I needed to add is the use of component parameters; these are global parameters and will beused to hold how much various images cost and need to be available in the Admin interface.

To add these is simple:


  • Add a 'config.xml' file describing the parameters

  • Add a toolbar button



the 'config.xml' file I put into the 'admin' folder (and added to the list of files in the component XML descriptor file:


<?xml version="1.0" encoding="utf-8"?>
<root>
<params>
<param type="text" name="sml_image_price" size="3" label="Small Image Price:" description="Please enter the new price" />
<param type="text" name="med_image_price" size="3" label="Medium Image Price:" description="Please enter the new price" />
<param type="text" name="lrg_image_price" size="3" label="Large Image Price:" description="Please enter the new price" />
</params>
</root>


The toolbar button is added programatically. If you dont have a default admin landing page already, create on, in the 'admin' folder root called COMPONENT_NAME.php and add this code:


<?php
JToolBarHelper::preferences( 'com_COMPONENT_NAME' );
?>


And that's it; go to the component in the admin interface and hit the Parameters button.

No comments: