View source
<?php
function brilliant_gallery_views_tables() {
$tables['brilliant_gallery'] = array(
'name' => 'brilliant_gallery',
'fields' => array(
'node' => array(
'name' => t('Brilliant Gallery: A random image'),
'handler' => array(
'brilliant_gallery_views_handler_image_img' => t('Random image'),
),
'notafield' => true,
'sortable' => false,
'help' => t('Display one random image from the respective gallery.'),
),
),
);
return $tables;
}
function brilliant_gallery_views_handler_image_img($fieldinfo, $fielddata, $value, $data) {
$tmp = $data->nid;
$query = "SELECT SQL_CACHE `field_gallery_value` FROM `content_type_page` WHERE `nid` = '" . $tmp . "' ORDER BY `vid` DESC LIMIT 1";
$string = db_result(db_query($query), 0);
$string = str_replace(array(
'[bg|',
']',
), '', $string);
if (strpos($string, '|') !== false) {
$string = substr($string, 0, strpos($string, '|'));
}
if ($string == '') {
return;
}
$absolpath = realpath(file_directory_path() . '/' . variable_get('brilliant_gallery_folder', '') . '/' . $string);
$poct = -1;
$retval = array();
$handle = opendir($absolpath);
while ($file = readdir($handle)) {
$poct += 1;
$testending = strtolower(substr($file, -4, 4));
if (strtolower($testending) != '.jpg' and strtolower($testending) != 'jpeg' and strtolower($testending) != '.gif' and strtolower($testending) != '.png') {
continue;
}
$retval[$poct] = $file;
}
closedir($handle);
$randimg = mt_rand(0, count($retval));
$result = $absolpath . '/' . $retval[$randimg];
$temp = getimagesize($result);
$imgh = 100;
$imgw = round($temp[0] / $temp[1] * $imgh);
$langcode = '';
if (function_exists('i18n_get_lang')) {
$langcode = i18n_get_lang();
}
$modulepath = url(drupal_get_path('module', 'brilliant_gallery'), NULL, NULL, TRUE);
if ($langcode != '') {
$modulepath = str_replace('/' . $langcode . '/', '/', $modulepath);
}
$modulepath = str_replace("?q=", "", $modulepath);
$result = '<a href="' . $modulepath . '/image.php?imgp=' . base64_encode($absolpath . '/' . $retval[$randimg]) . '&imgw=' . $imgw * 6 . '&imgh=' . $imgh * 6 . '"';
$setname = mt_rand(1, 9999999);
$overbrowser = variable_get('brilliant_gallery_overbrowser', 'thickbox');
switch ($overbrowser) {
case 'thickbox':
$result .= ' class="thickbox"';
$result .= ' rel="img_' . $setname . '"';
break;
case 'lightbox':
$result .= ' rel="lightbox[' . $setname . ']"';
break;
case 'greyboxr':
$result .= ' class="greybox"';
break;
default:
break;
}
if ($showcaption != '') {
$result .= ' title="' . $caption . '"';
}
$result .= '>';
$result .= '<img style="display: block;border:0;align:right" src="' . $modulepath . '/image.php?imgp=' . base64_encode($absolpath . '/' . $retval[$randimg]) . '&imgw=' . $imgw . '&imgh=' . $imgh . '" />';
$result .= '</a>';
return $result;
}