<?php
/**
* Joomlander.Net "Cool Hits Counter" Module for Joomla! 1.5.x
* License: http://www.gnu.org/copyleft/gpl.html
* Copyright (c) 2006 - 2009 Joomlander.Net
* More info at http://joomlander.net
* Developers: Bruce Scherzinger
* Version: 1.5.1
* License: http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
* This is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
*/

defined( '_JEXEC' ) or die( 'Restricted access' );

$content = "";

// *** Get Params
$source = $params->get( 'source' );
$output = $params->get( 'output' );
$pretext = $params->get( 'pretext' );
$posttext = $params->get( 'posttext' );
$hitsonly = $params->get( 'hitsonly' );
$position = $params->get( 'position' );
$style = $params->get( 'style' );
$mindigits = intval( $params->get( 'mindigits' ) );
$increase = intval( $params->get( 'increase' ) );
$moduleclass_sfx = $params->get( 'moduleclass_sfx' );

// *** Retrieve DB statisitcs : [1 : Agents/Browsers]
if ( $source == 'joomlageneric' ) {
	$query = "SELECT sum(hits) FROM #__content WHERE state='1'";
} else if ( $source == 'joomlastats') {
	$query = "SELECT sum(id) FROM jos_jstats_visits";
}
$database =& JFactory::getDBO();
$database->setQuery( $query );
$hits = $database->loadResult();

// *** Correct hit count as needed
if ($hits == NULL) { $hits = 0; }
$hits = $hits + $increase;   // Increase the counter if needed... (use old value, of simulate high traffic...)

// *** Split graphic selection into an array - check for randomization
if ( $output == 'image' ) {
    $choice = preg_split('/ /', $style, -1, PREG_SPLIT_OFFSET_CAPTURE);
    if ( count($choice) > 1 )
        // Randomly select one of the provided choices (separated by spaces)
        $style = trim($choice[rand(1,count($choice)-1)][0]);
    else
        // Only one choice
        $style = $choice[0][0];
}

// *** Display hits using graphics
$hitcount = $hits;
$readout = "";
while ($hits > 0 || $mindigits > 0) {
    $digit = $hits % 10;
	if ( $output == 'image' ) {
	    $readout = "<img src='modules".DS."mod_coolhits".DS."$style/$digit.png' alt='' />".$readout;
	} else if ( $output == 'text' ) {
		$readout = $digit.$readout;	
	}
    $hits = intval ($hits/10);
    $mindigits --;
}
$content .= $readout;

// *** Display pre- and post- text if requested
if (!$hitsonly) {
    if ($pretext)  $content = $pretext.$content;
    if ($posttext) $content .= $posttext;
}

// *** Apply style with or without suffix
$content = "<div class='module".$moduleclass_sfx."' style='text-align:$position'>".$content."</div>"."\n";

?>
