#!/usr/bin/perl
use Opals::Context;
use Time::localtime;
use Opals::Template qw(
    tmpl_read
    tmpl_write
    tmpl_preference
);
use Opals::Date qw(
    date_parse
    date_text
    date_profileList
);
use Opals::Locale qw(
    loc_getMsgFile
    loc_write
);

my $dbh = Opals::Context->dbh();
END { $dbh->disconnect(); }

my $cgi = CGI->new;
my $input = $cgi->Vars();
my ($permission, $cookieList, $template) = tmpl_read(
        {
            dbh             => $dbh,
            cgi             => $cgi,
            tmplFile        => 'viewWeblink.tmpl',
        }
);

    my $tm = localtime;
    my $dateToday = sprintf("%04d-%02d-%02d", $tm->year+1900, ($tm->mon)+1, $tm->mday);



   my ($webLinksList) = listwebLinks($dbh);
   $template->param(
         webLinksList  => $webLinksList,
         hlpUrl     => Opals::Constant->getHlpUrl('viewWeblink')
   );

#Tue, Jan 12, 2010 @ 10:31:41 EST
my $msgValMap ={};
my $msgMap            =loc_getMsgFile('util/weblink.msg',$msgValMap);
loc_write($template,$msgMap);


tmpl_write($dbh, $cgi, $cookie, $template); 


#######################################
sub listwebLinks{
    my ($dbh) = @_;
        
    my $sth = $dbh->prepare(<<_STH_);
    select * from   opl_webLinks order by title 
_STH_


    $sth->execute();
 
        my @webLinks;
        my $url;
        while (my $rec = $sth->fetchrow_hashref) {
            $url = $rec->{'url'};
            if($url !~ m/^mailto:/i && $url !~ m/http:\/\/|https:\/\//i){
                $url = "http://" . $url;
                $rec->{'url'} = $url;
            }
            $rec->{'description'} =~ s/\n/<br>/g;
            $rec->{'title'} =~ s/'/\'/g;

            $rec->{'subject'}     =~ s/,/, /g;
            $rec->{'grade'}       =~ s/,/, /g;
             push @webLinks, $rec;
        }
        $sth->finish;
        return \@webLinks;
       $sth->finish;
}
