<?php
/**
 * $Source: /home/cvs/nukescripts/blocks/block-CPG_scroll_Random_pictures.php,v $
 * $Revision: 1.0.0 $
 * $Author: cyril $
 * $Date: 2007/04/19 17:01:24 $
 *
 */
/************************************************************************/
/* Maximus CMS: Web Portal System                                       */
/* ==============================                                       */
/*                                                                      */
/* Copyright (c) 2002 - 2007                                            */
/* http://www.php-maximus.org                                           */
/* dev@php-maximus.org                                                  */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/
/* Based on PHP-NUKE: Web Portal System                                 */
/************************************************************************/
/* Portage Coppermine Photo Gallery                                     */
/************************************************************************/
defined('MAXIMUS') or die ('Access denied block CPG Last Pictures scroll directly...');

global $prefix, $db, $CONFIG, $lang_block;


$cpg_block = true;
include_once('modules/Coppermine/include/constants.php');
include_once('modules/Coppermine/include/load.inc.php');


$numberpic = secure( $CONFIG['thumbcols'], 'num', '1' ); //nombre d'images

$limit = $numberpic;
$maxlength = 20; // longueur du titre maximale

$content = '';
$content .= '<p><marquee behavior="scroll" direction="up" height="150" scrollamount="1" scrolldelay="1" onmouseover=\'this.stop()\' onmouseout=\'this.start()\'><center>';

$result = $GLOBALS['db']->sql_query("SELECT COUNT(*) FROM ".TABLE_CPG_PICTURES." as p INNER JOIN ".TABLE_CPG_ALBUMS." AS a ON (p.aid = a.aid) WHERE approved='YES' GROUP BY pid");
$nbEnr = $GLOBALS['db']->sql_fetchrow($result);
$pic_count = $nbEnr[0];

if ($pic_count > 1000) {
    $result = $GLOBALS['db']->sql_query("SELECT COUNT(*) from ".TABLE_CPG_PICTURES." WHERE approved = 'YES'");
    $nbEnr = $GLOBALS['db']->sql_fetchrow($result);
    $total_count = $nbEnr[0];
    $granularity = floor($total_count / 1000);
    $cor_gran = ceil($total_count / $pic_count);
    srand(time());
    for ($i = 1; $i <= $cor_gran; $i++) $random_num_set = rand(0, $granularity) . ', ';
    $random_num_set = substr($random_num_set, 0, -2);
    $result = $GLOBALS['db']->sql_query("SELECT pid, filepath, filename, p.aid, p.title FROM ".TABLE_CPG_PICTURES." AS p INNER JOIN ".TABLE_CPG_ALBUMS." AS a ON (p.aid = a.aid ) WHERE randpos IN ($random_num_set) AND approved='YES' GROUP BY pid ORDER BY RAND() DESC LIMIT $limit");
} else {
    $result = $GLOBALS['db']->sql_query("SELECT pid, filepath, filename, p.aid, p.title FROM ".TABLE_CPG_PICTURES." AS p INNER JOIN ".TABLE_CPG_ALBUMS." AS a ON (p.aid = a.aid ) WHERE approved='YES' GROUP BY pid ORDER BY RAND() DESC LIMIT $limit");
}
$rowset = array();
while ($row = $GLOBALS['db']->sql_fetchrow($result)) {
        $thumb_title  = secure( $row['filename'], 'strip', '1');
	$row['title'] = secure( $row['title'], 'strip', '1');
        if ($row['title'] != '') {
            $thumb_title = $row['title'];
        } else {
            $thumb_title = substr($row['filename'], 0, -4);
        }

    $content .= '<a href="modules.php?name=Coppermine&amp;op=displayimage&amp;album='.$row['aid'].'&amp;pos=' . $row["pid"] . '"><img src="' .get_pic_url($row, 'thumb') . '" border="0" alt="' . $thumb_title . '" title="' . $thumb_title . '"><br />' . truncate_stringblocks($thumb_title,$maxlength) . '</a><br />';
}
$content .= '</center></marquee></p><p align="center"><a href="modules.php?name=Coppermine">'.$lang_block["photogallery"].'</a></p>';
