<?php
/**
 * pragmaMx  Content Management System
 * Copyright (c) 2005-2007 pragmaMx Dev Team - http://pragmaMx.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3
 * as published by the Free Software Foundation.
 *
 * $Revision: 1.1.2.5 $
 * $Author: tora60 $
 * $Date: 2007/10/21 14:02:13 $
 *
 * this file based on:
 * Coppermine Photo Gallery
 * Copyright (c) 2003-2005 Coppermine Dev Team
 * v1.1 originally written by Gregory DEMAR
 * required version: >= 1.4.3
 */

defined('mxMainFileLoaded') or die('access denied');
// //// Beginn Blockkonfiguration  //////////////////////////
// der Name/Pfad des Gallerymoduls
$cpg_name = 'Gallery';
// Anzahl der Thumbnails
$limit = 4;
// Länge des Bildtitels unter dem Bild
$title_length = 17;
// Liste der Alben, aus denen keine Bilder angezeigt werden sollen. Die ID's durch Komma trennen.
$exluded_albums = '';
// Liste der Alben, aus denen keine Bilder angezeigt werden sollen. Die ID's durch Komma trennen.
$exluded_albums = '';
// Farben des Blocks
global $bgcolor1, $bgcolor2, $bgcolor3, $textcolor1, $textcolor2;
// /  Ende Blockkonfiguration  //////////////////////////////

include('includes/javascript/reflex.js');

if (@file_exists('modules/' . $cpg_name . '/blocks.inc.php')) {
    include('modules/' . $cpg_name . '/blocks.inc.php');
} else {
    if (mxIsAdmin()) {
        $content = 'The module "' . $cpg_name . '" doesn\'t exist or is not correctly installed. Please install this module or change the Variable $cpg_name in file blocks/' . basename(__file__) . '.';
    }
    return;
}
// Abfragebedingung für versteckte Alben erstellen
$exluded_albums = preg_split('#\s*,\s*#', trim($exluded_albums));
foreach($exluded_albums as $ex) {
    $excluded[] = intval($ex);
}
$exluded_albums = '';
if (isset($excluded)) {
    $exluded_albums = ' AND p.aid NOT IN (' . implode(',', $excluded) . ')';
}

$result = sql_query("
SELECT COUNT(p.pid)
FROM $CONFIG[TABLE_PICTURES] AS p INNER JOIN $CONFIG[TABLE_ALBUMS] AS a ON (p.aid = a.aid)
WHERE p.approved='YES' AND " . $vis_groups . " " . $exluded_albums . "
GROUP BY p.pid");

$nbEnr = sql_fetch_row($result);
$pic_count = $nbEnr[0];
// if we have more than 1000 pictures, we limit the number of picture returned
// by the SELECT statement as ORDER BY RAND() is time consuming
if ($pic_count > 1000) {
    $result = sql_query("SELECT COUNT(p.pid) from $CONFIG[TABLE_PICTURES] AS p WHERE approved = 'YES' " . $exluded_albums . "");
    $nbEnr = sql_fetch_row($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 = sql_query("
    SELECT p.pid, p.url_prefix, p.filepath, p.filename, p.aid, p.title
    FROM $CONFIG[TABLE_PICTURES] AS p INNER JOIN $CONFIG[TABLE_ALBUMS] AS a ON (p.aid = a.aid)
    WHERE randpos IN ($random_num_set) AND p.approved='YES' AND " . $vis_groups . " " . $exluded_albums . "
    ORDER BY RAND() DESC
    LIMIT $limit");
} else {
    $result = sql_query("
    SELECT p.pid, p.url_prefix, p.filepath, p.filename, p.aid, p.title
    FROM $CONFIG[TABLE_PICTURES] AS p INNER JOIN $CONFIG[TABLE_ALBUMS] AS a ON (p.aid = a.aid)
    WHERE p.approved='YES' AND " . $vis_groups . " " . $exluded_albums . "
    ORDER BY RAND() DESC
    LIMIT $limit");
}

$output = '';
while ($row = sql_fetch_array($result)) {
    if (empty($row['title'])) {
        if (preg_match('#^(.+)\.[^.]{2,4}$#', $row['filename'], $matches)) {
            $row['title'] = $matches[1];
        }
        $row['title'] = str_replace('_', ' ', $row['title']);
    }
    $output .= '<td align="center" valign="baseline"><a href="modules.php?name=' . $cpg_name . '&amp;act=displayimage&amp;album=' . $row['aid'] . '&amp;pos=-' . $row["pid"] . '"><img class="reflex" src="' . get_pic_url($row, 'thumb') . '" border="0" alt="' . $row['title'] . '" title="' . $row['title'] . '"/><br />' . mxCutString($row['title'], $title_length) . '</a></td>';
}

if ($output) {
$content = '<table width="100%" border="0" cellpadding="3" cellspacing="0" align="center"><tr  style="background-color: ' . $bgcolor1 . ';">' . $output . '</tr>
<tr align="center" style="background-color: ' . $bgcolor1 . ';">
<td colspan="' . $limit . '" valign="baseline">
<a href="modules.php?name=' . $cpg_name . '">' . $CONFIG['gallery_name'] . '</a>
</td>
</tr>
</table>';
}

?>