webgui/lib/WebGUI/Utility.pm
2002-03-07 01:40:00 +00:00

112 lines
3.3 KiB
Perl

package WebGUI::Utility;
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2002 Plain Black Software.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#-------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#-------------------------------------------------------------------
use Exporter;
use strict;
use Tie::IxHash;
use WebGUI::International;
use WebGUI::Session;
use WebGUI::SQL;
use WebGUI::URL;
our @ISA = qw(Exporter);
our @EXPORT = qw(&sortByColumn &sortHashDescending &sortHash &isIn &randint &round);
#-------------------------------------------------------------------
sub isIn {
my ($i, @a, @b, @isect, %union, %isect, $e);
foreach $e (@_) {
if ($a[0] eq "") {
$a[0] = $e;
} else {
$b[$i] = $e;
$i++;
}
}
foreach $e (@a, @b) { $union{$e}++ && $isect{$e}++ }
@isect = keys %isect;
if (@isect) {
undef @isect;
return 1;
} else {
return 0;
}
}
#-------------------------------------------------------------------
sub randint {
my ($low, $high) = @_;
$low = 0 unless defined $low;
$high = 1 unless defined $high;
($low, $high) = ($high,$low) if $low > $high;
return $low + int( rand( $high - $low + 1 ) );
}
#-------------------------------------------------------------------
sub round {
return sprintf("%.0f", $_[0]);
}
#-------------------------------------------------------------------
# example: sortByColumn(columnToSort,columnLabel);
sub sortByColumn {
my ($output);
$output = '<a href="'.WebGUI::URL::page('sort='.$_[0].'&sortDirection=');
if ($session{form}{sortDirection} eq "asc") {
$output .= "desc";
} else {
$output .= "asc";
}
$output .= '">'.$_[1].'</a>';
if ($session{form}{sort} eq $_[0]) {
if ($session{form}{sortDirection} eq "desc") {
$output .= ' <img src="'.$session{setting}{lib}.'/desc.gif">';
} else {
$output .= ' <img src="'.$session{setting}{lib}.'/asc.gif">';
}
}
return $output;
}
#-------------------------------------------------------------------
sub sortHash {
my (%hash, %reversedHash, %newHash, $key);
tie %hash, "Tie::IxHash";
tie %reversedHash, "Tie::IxHash";
tie %newHash, "Tie::IxHash";
%hash = @_;
%reversedHash = reverse %hash;
foreach $key (sort {$b cmp $a} keys %reversedHash) {
$newHash{$key}=$reversedHash{$key};
}
%reversedHash = reverse %newHash;
return %reversedHash;
}
#-------------------------------------------------------------------
sub sortHashDescending {
my (%hash, %reversedHash, %newHash, $key);
tie %hash, "Tie::IxHash";
tie %reversedHash, "Tie::IxHash";
tie %newHash, "Tie::IxHash";
%hash = @_;
%reversedHash = reverse %hash;
foreach $key (sort {$a cmp $b} keys %reversedHash) {
$newHash{$key}=$reversedHash{$key};
}
%reversedHash = reverse %newHash;
return %reversedHash;
}
1;