<?php
// $Id: functions.php,v 1.1 2004/07/11 10:28:19 Farsus Exp $
// -------------------------------------------------------------------------//
//			                 RUNCMS                                         //
//                                                                          //
//	 reliable - Unique - Nocost &  Simplicity & ease off use                //
//                       < http://www.runcms.org >                          //
// -------------------------------------------------------------------------//
// Original Author: James Knickelbein
// Author Website : http://www.journeymilwaukee.com
// License Type   : GPL: See /manual/LICENSES/GPL.txt
// ------------------------------------------------------------------------- //

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function mainheader() {
echo "<div align='center'><a href='".XOOPS_URL."/modules/mydownloads/index.php'><img src='".XOOPS_URL."/modules/mydownloads/images/logo.gif' border='0' alt='' /></a></div><br />";
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function newdownloadgraphic($time, $status) {

$count     = 7;
$startdate = ( time() - (86400 * $count) );

if ($startdate < $time) {
	if ($status == 1) {
		echo "&nbsp;<img src='".XOOPS_URL."/modules/mydownloads/images/newred.gif' alt='"._MD_NEWTHISWEEK."' />";
		} elseif ($status == 2) {
			echo "&nbsp;<img src='".XOOPS_URL."/modules/mydownloads/images/update.gif' alt='"._MD_UPTHISWEEK."' />";
		}
	}
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function popgraphic($hits) {
global $mydownloadsConfig;

if ($hits >= $mydownloadsConfig['popular']) {
	echo "&nbsp;<img src ='".XOOPS_URL."/modules/mydownloads/images/pop.gif' alt='"._MD_POPULAR."' />";
	}
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
//Reusable Link Sorting Functions
function convertorderbyin($orderby) {

if ($orderby == "titleA")  { $orderby = "title ASC";   }
if ($orderby == "dateA")   { $orderby = "date ASC";    }
if ($orderby == "hitsA")   { $orderby = "hits ASC";    }
if ($orderby == "ratingA") { $orderby = "rating ASC";  }
if ($orderby == "titleD")  { $orderby = "title DESC";  }
if ($orderby == "dateD")   { $orderby = "date DESC";   }
if ($orderby == "hitsD")   { $orderby = "hits DESC";   }
if ($orderby == "ratingD") { $orderby = "rating DESC"; }

return $orderby;
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function convertorderbytrans($orderby) {

if ($orderby == "hits ASC")    { $orderbyTrans = _MD_POPULARITYLTOM; }
if ($orderby == "hits DESC")   { $orderbyTrans = _MD_POPULARITYMTOL; }
if ($orderby == "title ASC")   { $orderbyTrans = _MD_TITLEATOZ;      }
if ($orderby == "title DESC")  { $orderbyTrans = _MD_TITLEZTOA;      }
if ($orderby == "date ASC")    { $orderbyTrans = _MD_DATEOLD;        }
if ($orderby == "date DESC")   { $orderbyTrans = _MD_DATENEW;        }
if ($orderby == "rating ASC")  { $orderbyTrans = _MD_RATINGLTOH;     }
if ($orderby == "rating DESC") { $orderbyTrans = _MD_RATINGHTOL;     }

return $orderbyTrans;
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function convertorderbyout($orderby) {

if ($orderby == "title ASC")   { $orderby = "titleA";  }
if ($orderby == "date ASC")    { $orderby = "dateA";   }
if ($orderby == "hits ASC")    { $orderby = "hitsA";   }
if ($orderby == "rating ASC")  { $orderby = "ratingA"; }
if ($orderby == "title DESC")  { $orderby = "titleD";  }
if ($orderby == "date DESC")   { $orderby = "dateD";   }
if ($orderby == "hits DESC")   { $orderby = "hitsD";   }
if ($orderby == "rating DESC") { $orderby = "ratingD"; }

return $orderby;
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function PrettySize($size) {

$mb = (1024*1024);
if ( $size > $mb ) {
	$mysize = sprintf ("%01.2f", ($size/$mb)) . " MB";
	} elseif ( $size >= 1024 ) {
		$mysize = sprintf ("%01.2f", ($size/1024)) . " KB";
		} else {
			$mysize = sprintf(_MD_NUMBYTES, $size);
		}

return $mysize;
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
//updates rating data in itemtable for a given item
function updaterating($sel_id) {
global $db;

$sql    = "SELECT rating FROM ".$db->prefix("mydownloads_votedata")." WHERE lid=".$sel_id."";
$result = $db->query($sql);

$num_votes   = intval($db->num_rows($result));
$totalrating = 0;

while (list($rating) = @$db->fetch_row($result)) {
	$totalrating += intval($rating);
}

if ($result) {
	$finalrating = ($totalrating/$num_votes);
	$finalrating = number_format($finalrating, 2);
	} else {
		$finalrating = 0;
	}

$sql = "UPDATE ".$db->prefix("mydownloads_downloads")." SET rating=$finalrating, votes=$num_votes WHERE lid = $sel_id";
$db->query($sql) or print($db->error());
}

//---------------------------------------------------------------------------------------//
/**
* returns the total number of items in table that
* are accociated with a given table $table id
*
* @param type $var description
* @return type description
*/
function getTotalItems($sel_id, $status='') {
global $db, $mytree;

$query = 'SELECT SUM(cid='.intval($sel_id).')';
$arr   = array();
$arr   = $mytree->getAllChildId($sel_id);
$size  = count($arr);

for ($i=0; $i<$size; $i++) {
	$query .= ', SUM(cid='.intval($arr[$i]).')';

}

$query .= ' FROM '.$db->prefix('mydownloads_downloads').'';

if ($status != '') {
	$query .= ' WHERE status >= '.intval($status).'';
}

$result = $db->query($query);
$count  = $db->fetch_row($result);

if (!empty($count)) {
	foreach ($count as $key => $value) {
		$out += $value;
	}
}

return $out;
}
?>
