package Latest;

###############################################################################
###############################################################################
# WebAPP - Automated Perl Portal - www.web-app.org                            #
#-----------------------------------------------------------------------------#
# Latest.pm                                                                   #
# Last Modified for v0.9.9.4                                                  #
# Original (c) Jocelyn Brown (www.bantychick.com)                             #
#                                                                             #
#   ###  WebAPP Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007              #
#   ###  M. Carter Brown                                                      #
#   ###  Jocelyn Brown                                                        #
#   ###  www.web-app.org                                                      #
#                                                                             #
#   ### See individual file headers for more details on copyrights by         #
#   ### contributing developers, per file and per subroutine as noted.        #
#                                                                             #
# This file is part of WebAPP.                                                #
#                                                                             #
# WebAPP 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, or           #
# (at your option) any later version.                                         #
#                                                                             #
# WebAPP is distributed in the hope that it will be useful,                   #
# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
# GNU General Public License for more details.                                #
#                                                                             #
# You should have received a copy of the GNU General Public License           #
# along with WebAPP; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.               #
#                                                                             #
# File: Modified:                                                             #
###############################################################################
###############################################################################

### USAGE
###________________________________________________________________________________
###
###	use Latest;
###	my @tlinks = newtldtl($linksdir, 'linkcats');
###	or
###	my @dtlinks = newtldtl($downloadsdir, 'downloadcats');
###________________________________________________________________________________
###

require Exporter;
@ISA       = qw(Exporter);
@EXPORT    = qw(newtldtl);

##############
sub newtldtl {
##############
# Originally by Floyd. Modified by Jos.

	my ($newfolder, $newtype) = @_;

	my ($c, @tldata, @tlinks);
	my @tlcats = ();

	open(DATABASE, "<$newfolder/$newtype.dat") || print "cannot open $newfolder/$newtype.dat";
	my @lcats = <DATABASE>;
	close(DATABASE);

	foreach (@lcats) {
		my @litem = split(/\|/, $_);
		my $lcat = $litem[1];
		if (open(DATABASE, "<$newfolder/$lcat.dat")) {
			my @lcontent = <DATABASE>;
			close(DATABASE);
			for ($c = 0; $c < @lcontent; $c++) {
				my ($lid, $lname, $ldescription, $ldate, $lcount) = (split(/\|/, $lcontent[$c]))[0,1,3,4,6];
				my @lsort = split(/[\/:]| - /, $ldate);
				my $ltime = "$lsort[2]$lsort[0]$lsort[1]$lsort[3]$lsort[4]$lsort[5]";
				push (@tlcats, join ("\|", $lcat, $lid, $lname, $ltime, $ldate, $lcount, "$ldescription<br><br>"));
			}
		}
	}
	for (0..$#tlcats) {
		my @tlfields = split(/\|/, $tlcats[$_]);
		for $c (0..$#tlfields) {
			$tldata[$_][$c] = $tlfields[$c];
		}
	}
	@tldata = sort { $b->[3] <=> $a->[3] } @tldata;
	for (@tldata) {
		push (@tlinks, join ("\|", @$_));
	}

	return @tlinks;

}

1;

