Author Topic: Smarty-Command: get_random_image  (Read 7024 times)

0 Members and 1 Guest are viewing this topic.

simi

  • Member
  • **
  • Posts: 11
Smarty-Command: get_random_image
« on: January 31, 2009, 03:09:18 PM »
Hello,

A new Smarty-function which returns a random image out of a specified image category.
I use it for a flexible banner system...

function.get_random_image.php     -->  copy to /addon/smarty/plugins/
Code: [Select]
<?php

import
('classes.category.CategoryService');
import('classes.item.ItemService');
import('classes.util.ImageLink');

/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     function.get_random_image.php
 * Type:     function
 * Name:     get_random_image
 * Purpose:  Returns link and item of a random image out of a specified category
 * -------------------------------------------------------------
 */
function smarty_function_get_random_image($params, &$smarty)
{
if(!isset($params['id'])) {
$smarty->trigger_error("get_random_image: missing 'id' attribute");
return;
}

$categoryService = new CategoryItemEnumeration(4,$params['id']);
$itemService = new ItemService(4);

$i rand(1,$categoryService->count());

do {
$temp $categoryService->next();
} while((--$i)>0);

$itemid $temp['itemid'];
$item $itemService->getItem($itemidITEM_LOAD_FULL);

$linkobj = new ImageLink();
$linkobj->setItemID($itemid);
$link LinkHelper::getUrlFromCMSLink($linkobj);

if (isset($params['item'])) {
$smarty->assign($params['item'],$item);
}

if (isset($params['link'])) {
$smarty->assign($params['link'],$link);
}

if ((!isset($params['link']))&&(!isset($params['item'])))
{
unset($categoryService);
unset($itemService);

return $link;
}

unset($categoryService);
unset($itemService);
}

?>

Example 1:
The Function returns the image link. Just specify from which category the image should be selected with the id-parameter. For easy applications you can use it like that:
Code: [Select]
<img src="{get_random_image id="2"}">
Example 2:
You can get back an item-object if you specify the item-parameter. The link-parameter gets the image link.
In this example a link is specified in the Catchwords-Field and is used to link each banner to an other website...
Code: [Select]
{get_random_image id="2" item="banner_item" link="banner_link"}
<a href="{$banner_item->getCatchwords()}">
<img src="{$banner_link}" >
</a>

Regards,
Simon

Dragonslayer

  • Team
  • Bigace Expert
  • *****
  • Posts: 470
    • dragonslayer.se
Re: Smarty-Command: get_random_image
« Reply #1 on: August 24, 2015, 04:03:00 PM »
Thanks this was very usefull