Adding custom theme option for random image

fretzl Administrator, Developer
Zenphoto 1.2.2 [3131],customized default theme.

My index.php has a random image from the gallery.

What I need to do for the user is:
  • Add a theme option to be able to choose from which album or subalbum the random image should be taken. (a list with all albums and subalbums?)
  • The user should also still be able to choose for a random image taken from the entire gallery.
    (a checkbox to override the album list?)
After two days of trial and error coding and a headache I give up :(

Any help is much appreciated.

Merry Xmas to you all !

Comments

  • There are two different functions you will be using: `getRandomImages()` for random images from the entire gallery and `getRandomImagesAlbum($album)` for random images from a particular album.

    If your user selects to use images from the entire gallery your task is fairly simple. use `$image = getRandomImages();` to establish the image. Then go to the display process below.

    If your user selects random image from a particular album do the following (I assume you have the album name as a string `$albumname`):

    create a new album: `$album = new Album($_zp_gallery, $albumname);` Then get the random image: `$image = getRandomImagesAlbum($album);`

    Now that you have an image you need to make it the current image: `makeImageCurrent($image);`

    Then you print using one of the template functions for printing images.
  • fretzl Administrator, Developer
    Thanks sbillard !

    I understand your answer (I think :)). That's the part that should be coded in my index.php.

    My real problem however is to make it a custom theme option in the
    theme options page. (admin -> options -> theme options)

    The user wants an easy way to regularely change the the source of the random image.

    I can't figure out how to make a list with all the album names to choose from and a checkbox to override this list so the image will be chosen from the entire gallery.
    (or any other construction that works)

    I'm sorry if I made myself not clear enough ...again.

    Thanks again
  • I presume you have looked at the other themes `themeoptions.php` scripts to see how theme options are done in general. You should also look at the function description for `customOptions()` for the types of options that are handled in a straight forward manner.

    So, all you really need is how to get a list of albums to use in a type 5 custom option. Your quickest solution would be to use a query.

    `$albumlist = query_full_array("SELECT folder FROM ".prefix('albums'));` will give you all the albums. You will need to extract the folder name into a single dimensioned array for the option handling:

    `foreach ($albumlist as $album) $list[] = $album['folder'];`
  • fretzl Administrator, Developer
    Great! That is exactly what I was looking for.
    Thank you sbillard!...and Merry Xmas.
  • fretzl Administrator, Developer
    Just in case anyone is interested, here's what I put together:

    On the admin -> options -> theme options page I now have a list with all albums (subalbums included) to choose from + an option to select the entire gallery.

    `In themeoptions.php:`

    function ThemeOptions() {
    setOptionDefault('Allow_comments', true);
    setOptionDefault('Allow_search', true);
    added `'setOptionDefault('Randomimage, 'Gallery');'`
    }

    function getOptionsSupported() {
    return array(
    gettext('Allow comments') => array('key' => 'Allow_comments', 'type' => 1, 'desc' => gettext('Check to enable comment section.')),
    gettext('Allow search') => array('key' => 'Allow_search', 'type' => 1, 'desc' => gettext('Check to enable search form.')),

    added

    `gettext('Randomimage') => array('key' => 'Randomimage', 'type' => 2, 'desc' => gettext('Select source for random image')),`
    }

    function handleOption($option, $currentValue) {
    switch ($option) {
    case 'Theme_colors':
    $theme = basename(dirname(__FILE__));
    $themeroot = SERVERPATH . "/themes/$theme/styles";
    echo '<select id="themeselect" name="' . $option . '"' . ">\n";
    generateListFromFiles($currentValue, $themeroot , '.css');
    echo "</select>\n";
    break;

    added

    `case 'Randomimage':
    $albumlist = query_full_array("SELECT folder FROM ".prefix('albums'));
    foreach ($albumlist as $album) $list[] = $album['folder'];
    $list[] = '--Gallery--';//add option for entire gallery to array
    echo '<select id="themeselect" name="' . $option . '"' . ">\n";
    generateListFromArray(array(getOption('Randomimage')), $list, false, false);
    echo "</select>\n";
    break;`
    }
    }

    on thepageyouwant.php add:

    `
    <?php if ((getOption('Randomimage')== '--Gallery--')) {
    makeImageCurrent(getRandomImages(false)); printCustomSizedImageMaxSpace($alt='',600,600,NULL,NULL, false); } // displays a random image from the entire gallery
    else {
    makeImageCurrent(getRandomImagesAlbum(getOption('Randomimage'))); printCustomSizedImageMaxSpace('',600,600,NULL,NULL, false);} ?>
    //displays a random image from any other chosen album
    `
    This is probably not the most elegant way to do it but it works.

    There is still an issue with changing the image description in the frontend. Not sure why.

    Thanks to sbillard for setting me on the right track!
  • fretzl Administrator, Developer
    OK that was not very clear...I'll try again:

    Just in case anyone is interested, here's what I put together:

    On the admin -> options -> theme options page I now have a list with all albums (subalbums included) to choose from + an option to select the entire gallery.

    `In themeoptions.php:`

    in function ThemeOptions()
    I added: `setOptionDefault('Randomimage, 'Gallery');`

    in function getOptionsSupported()
    I added: `gettext('Randomimage') => array('key' => 'Randomimage', 'type' => 2, 'desc' => gettext('Select source for random image')),`

    in function handleOption($option, $currentValue) (which I copied from the Effervescence+ theme)

    I added:

    `
    case 'Randomimage':
    $albumlist = query_full_array("SELECT folder FROM ".prefix('albums'));
    foreach ($albumlist as $album) $list[] = $album['folder'];
    $list[] = '--Gallery--';//add option for entire gallery to array
    echo '\n";
    generateListFromArray(array(getOption('Randomimage')), $list, false, false);
    echo "\n";
    break;
    `
    on thepageyouwant.php add:

    `
    <?php if ((getOption('Randomimage')== '--Gallery--')) {
    makeImageCurrent(getRandomImages(false));
    printCustomSizedImageMaxSpace($alt='',600,600,NULL,NULL, false); }
    // displays a random image from the entire gallery
    else {
    makeImageCurrent(getRandomImagesAlbum(getOption('Randomimage')))
    printCustomSizedImageMaxSpace('',600,600,NULL,NULL, false);} ?>
    //displays a random image from any other chosen album
    `
    This is probably not the most elegant way to do it but it works.

    There is still an issue with changing the image description in the frontend. Not sure why.

    Thanks to sbillard for setting me on the right track!

    P.S Can anyone please delete my previous post?
  • Hi
    I have similar problem. On my index page I have random image with code:
    `
    <?php
    $randomImage = getRandomImage();
    $randomImageURL = getURL($randomImage);
    echo '<a href="'.$randomImageURL.'" title="Zobacz obraz: '.$randomImage->getTitle().'">imagegetCustomImage(480, NULL, 380, NULL, NULL, NULL, NULL).'" alt="'.$randomImage->getTitle().'" id="random" class="reflect rheight33" />';
    ?>
    `
    and I would like to change it to getRandomImagesAlbum becouse I want to choose from what album or albums will be displayed photos. How can I do it I don't know php sorry for that question.
    Regards
  • Use instead of `getRandomImage()` `getRandomImageAlbum()`
  • Ok so I change getRandomImage() to getRandomImageAlbum('Krajobrazy') but page does not working:(
    That's my code now:
    `
    <?php
    $randomImage = getRandomImageAlbum('Krajobrazy');
    $randomImageURL = getURL($randomImage);
    echo '<a href="'.$randomImageURL.'" title="Zobacz obraz: '.$randomImage->getTitle().'">imagegetCustomImage(400, NULL, 300, NULL, NULL, NULL, NULL).'" alt="'.$randomImage->getTitle().'" id="random"
    class="reflect rheight33" />';
    ?>
    `
  • acrylian Administrator, Developer
    That is probably becasue the function is called `getRandomImagesAlbum()`. http://www.zenphoto.org/documentation/functions/_template-functions.php.html#functiongetRandomImagesAlbum
  • Yes !! my mistake now working. thanks!
Sign In or Register to comment.