Code snippets, tech tricks and other bits and bobs

Gavick Pro 'GK Gamebox' Joomla Template - popup login disappears when clicking on username or password input in Chrome

Symptom:

In Google Chrome, when using the popup login form accessed from the login link at the top of the template, if you click on the username or password inputs, the popup disappears.

Cause:

The mootools script is designed so that when a user clicks out of the DOM object popup_login, it fades out. This is done with any general document click IF the mouse is out of popup_login. This is determined by checking a variable set by mouseenter and mouseleave.

In Chrome, mouseleave is triggered when you enter child elements - not our desired functionality!

Solution:

templates\gk_gamebox\js\gk.script.js 

130a131
> <span style="white-space: pre;"> </span>$('popup_login').getElements('input').addEvent('mouseenter',function(){login_over = true;});

(Unix diff formatting)

Filed under  //   Gavick Pro   Google Chrome   Javascript   Joomla!   MooTools   Templates  

Rotating background image (or other PHP function) in CubeCart templates

I have a site where the main site picks an image from a particular folder for the background every time you hit a page, rotating through the available images in a loop. We also have a CubeCart store, which has had the template skinned to fit the main site, so obviously enough, I wanted to have the same background image feature for the shop.

The thing with CubeCart templates is that you can’t just add php to the .tpl files – you have to do all your PHP jiggery pokery in include files, and assign values to object properties, and include a placeholder in the tpl.

There are two key CubeCart files to make this change over the whole shop:

{www,yourdomain.com}/{shop_location}/includes/global/index.inc.php {www,yourdomain.com}/{shop_location}/skins/{your_skin}/styleTemplates/global/index.tpl

First off you want to get your functions together from the main site. Mine all live in a lib.php file, but as I don’t need to load all of them into CubeCart, I pulled the relevant ones into a new file, /lib/php/rotatebg_lib.php. Now I can include this file in my original lib.php as well as in CubeCart, and reduce unnecessary memory overheads.

The system relies on three functions:

files_in_dir($start_dir) creates an array of the files in a directory. This is one of my standard ‘off the shelf’ functions

index_background_images() checks for files with a .jpg extentsion, creates an array of them and stores it in a $_SESSION variable, called only when a session is started. There are other ways of checking for file types – the most reliable is to mime_content_type() function, but if you trust the files on your server, my method is fine. If you are dealing with user uploaded files, remember you should NEVER trust the file to be what it says it is!

rotate_background() checks for a session, calls the indexing function if necessary and otherwise increments the index counter.

rotatebg_lib.php

$background_image_path = '/img/backgrounds/';

function rotate_background(){
/* check for session - if none, index bg files, else increment counter */
    if(isset($_SESSION['backgrounds'])){
        $_SESSION['backgrounds']['index']++;
        if($_SESSION['backgrounds']['index'] >= count($_SESSION['backgrounds']['files'])){
            $_SESSION['backgrounds']['index'] = 0;
        }
        $background = $_SESSION['backgrounds']['index'];
    }else{
        $_SESSION['backgrounds']['files'] = index_background_images();
        $_SESSION['backgrounds']['index'] = 0;
        $background = 0;
    }
    return $background;
}

function index_background_images(){
/* return array of files with .jpg extension */
    global $background_image_path;
    $files = files_in_dir($_SERVER['DOCUMENT_ROOT'] . $background_image_path);
    foreach($files as $file){
        if(substr($file,0,10) == 'background' && substr($file,-4,4) == '.jpg'){
            $bg_images[]=$file;
        }
    }
    return $bg_images;
}

function files_in_dir($start_dir){
/* returns an array of files in $start_dir (not recursive) */
    $files = array();
    $dir = opendir($start_dir);
    while(($myfile = readdir($dir)) !== false){
        if($myfile != '.' && $myfile != '..' && !is_file($myfile) && $myfile != 'resource.frk' && !eregi('^Icon',$myfile) ){
            $files[] = $myfile;
        }
    }
    closedir($dir);
    return $files;
}

Now we need to include and run these functions in the CubeCart global index include. Line numbers may vary dependant on your template, but essentially you want to add this just after the $body template has been defined

{your_skin}/includes/global/index.inc.php

Replace:

$body = new XTemplate ("global".CC_DS."index.tpl");

With:

$body = new XTemplate ("global".CC_DS."index.tpl");

include($_SERVER['DOCUMENT_ROOT'].'/inc/php/rotatebg_lib.php');
$background = rotate_background();
$body->assign('VAL_CUSTOM_BACKGROUND', $_SESSION['backgrounds']['files'][$background]);

And finally, insert VAL_CUSTOM_BACKGROUND into the index.tpl file. This needs to be added in the head section, I prefer the end of the head section as CSS latency will ensure that the rule is obeyed. Again, line numbers may vary.

{your_skin}/styleTemplates/global/index.tpl

Replace:

</head>

With

<style type="text/css">
#pageSurround{
    background-image:url("/img/backgrounds/{VAL_CUSTOM_BACKGROUND}");
}
</style>
</head>

Shazzamwhazzam. We are done.

Filed under  //   CSS   CubeCart   PHP   Templates