#!/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 $op = $input->{'op'};
my @titleList   = ($cgi->param('title'));
my @descList    = ($cgi->param('desc'));
my @urlList     = ($cgi->param('url'));
my @subjectList = ($cgi->param('subject'));
my @gradeList   = ($cgi->param('grade'));
my $debug;
my ($permission, $cookieList, $template) = tmpl_read(
        {
            dbh             => $dbh,
            cgi             => $cgi,
            tmplFile        => 'util/weblink.tmpl',
            reqPermission   => 'pref_edit',
        }
);

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


$template->param(hlpUrl     => Opals::Constant->getHlpUrl('weblink') );
# See User.pm for the list of permissions
if ($permission && $permission->{'pref_edit'}) {

   if($op eq  'save'){
       saveweblink($dbh);
   }
   my ($weblinkLst) = listweblink($dbh);
   $template->param(
         weblinkLst  => $weblinkLst,
         weblink     =>1,
         debug        =>$debug
   );

#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 listweblink{
        my ($dbh) = @_;
        
    my $sth = $dbh->prepare(<<_STH_);
    select * from  opl_webLinks  order by title 
_STH_
    $sth->execute();
 
        my @weblink;
        my ($title,$url);
        while (my $rec = $sth->fetchrow_hashref) {
            $title =  $rec->{'title'};
            $title =~ s/[\r\n]//g;
            $rec->{'title'} = $title;
           
            $url =  $rec->{'url'};
            $url =~ s/[\r\n]//g;
            #$url =~ s/\//\\\//g;
            $rec->{'url'} = $url;

            my $desc =  $rec->{'description'};
            $desc =~ s/</&lt;/g ;
            $desc =~ s/>/&gt;/g ;
            $rec->{'description'} = $desc;

            push @weblink, $rec;
        }
        $sth->finish;
        return \@weblink;
       $sth->finish;
}

#######################################
sub saveweblink{
    my ($dbh) = @_;

    my $sth = $dbh->prepare(<<_STH_);
    delete from  opl_webLinks 
_STH_
    $sth->execute();

         $sth = $dbh->prepare(<<_STH_);
insert into opl_webLinks
set     title = ?,
        description  = ?,
        url  = ?,
        subject  = ?,
        grade=?
_STH_

        for(my $i =0; $i < scalar(@urlList);$i++) {
            $titleList[$i]      =~ s/[\r\n]//g;
            #$descList[$i]       =~ s/[\r\n]//g;
            $urlList[$i]        =~ s/[\r\n]//g;
            $subjectList[$i]    =~ s/[\r\n]//g;
            $gradeList[$i]      =~ s/[\r\n]//g;
            $sth->execute($titleList[$i],$descList[$i],$urlList[$i],$subjectList[$i],$gradeList[$i]);
        }
        $sth->finish;
}

    



