#!/usr/bin/perl

#use utf8;
use strict;
use CGI;

use Opals::Context;
use Opals::Template qw(
    tmpl_read
    tmpl_write
    tmpl_redirect
    tmpl_preference

);
use Opals::User qw(
    user_logout
);
use Opals::Portal qw(
    portal_getLayout
    portal_getNewItemList
    portal_getSearchForm
    portal_getRSS
    portal_getWebLink
    portal_getLibHours
    portal_getNewsEvent
    portal_getPathfinders
    portal_getWeather
    portal_getReaderReview
    portal_getLibCalendar
    portal_getProtalHTML
);

use Opals::Date qw(
    date_text
    date_parse
    date_nearestWorkday
);
use Opals::Locale qw(
    loc_getMsgFile
    loc_write
);

use Opals::Session qw(
    SessionHdl_clearVar    
);
my $dbh = Opals::Context->dbh();
END { $dbh->disconnect(); }
# test incremental backup

my $cgi = CGI->new;
my $input = $cgi->Vars();
my ($permission, $cookie, $template) = tmpl_read(
        {
            dbh             => $dbh,
            cgi             => $cgi,
            tmplFile        => 'home.tmpl',
        }
);
if ($permission && $permission->{'pref_edit'}) {
     $template->param(permEditCal=>$permission->{'pref_edit'});
}



my $sessionID = $cgi->cookie('globalSessionID');

if($sessionID ne '') {SessionHdl_clearVar($dbh,$sessionID);}
if (defined $input->{'logout'}) {
    $cookie = user_logout($cgi, $template);
}
my $sth=$dbh->prepare("select path from opl_menuItem where headerkey='home'");
$sth->execute();

if(my $rec =$sth->fetchrow_hashref){
    if($rec->{'path'} ne "/bin/home"){
        tmpl_redirect($cgi,$rec->{'path'} ."?logout=1");
    }
}

  my($layout, $portletList) = portal_getLayout($dbh);
  my $aboutUs=undef;
  foreach my $r(@$layout){
      foreach my $c(@{$r->{'colList'}}){
          foreach my $p(@{$c->{'portlet'}}){
              if($p->{'type'} ne 'aboutUs' ){
                  $p->{'content'}=getPortletContent($dbh,$p);
                  if($p->{'type'} eq 'calendar'){
                      $template->param(calSetup=>1);
                  }
              } 
              
              else{
                   $aboutUs=getPortletContent($dbh,$p);
                   $p->{'content'}=undef;
              }
          }
      }
  }

  $template->param( aboutUs         =>$aboutUs,
                    layout          =>$layout,
                    portletList     => $portletList,
                    bgcolor         =>'#2EB8E6',
                                      );    
my $stdMsgMap  =loc_getMsgFile('search/standard.msg');
loc_write($template,$stdMsgMap);

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

##################################################################
sub getPortletContent{
    my($dbh,$p)=@_;    

    my $type            = $p->{'type'};
    my $url             = $p->{'url'};
    my $nItemShow       = $p->{'nItemShow'};
    my $fieldsShow      = $p->{'fieldsShow'};
    my $showBookCover   = $p->{'showBookCover'};
    my $showDetail      = $p->{'showDetail'};
    my $zipCode         = $p->{'zipCode'};
    my $unit            = $p->{'unit'};
    my $showHolidays    = $p->{'showHolidays'};

    my $portletContent="";
    if($type eq 'rss' && defined $url && $url ne ''){
        $url =~ s/%3F/\?/gi;
        $url =~ s/%26/&/g;
        $portletContent=portal_getRSS($dbh,$url,$nItemShow,$fieldsShow);
    }
    elsif($type eq 'libraryHours'){
        $portletContent=portal_getLibHours($dbh);
    }
    elsif($type eq 'newItemList'){
        $portletContent=portal_getNewItemList($dbh,$nItemShow,$fieldsShow,$showBookCover);
    }
    elsif($type eq 'webLinks'){
        $portletContent=portal_getWebLink($dbh,$nItemShow,$fieldsShow);
    }
    elsif($type eq 'newsEvents'){ 
        $portletContent=portal_getNewsEvent($dbh,$nItemShow,$fieldsShow);
    }
    elsif($type eq 'pathfinders'){ 
        $portletContent=portal_getPathfinders($dbh,$nItemShow,$fieldsShow);
    }
    elsif($type eq 'weather'){
        $portletContent=portal_getWeather($dbh,$zipCode,$unit);
    }
    elsif($type eq 'reviewItemList'){
        $portletContent=portal_getReaderReview($dbh,$nItemShow,$fieldsShow,$showBookCover);
    }
    elsif($type eq 'calendar'){
        $portletContent=portal_getLibCalendar($dbh,$showHolidays);
    }
    elsif($type eq 'html'){
        $portletContent=portal_getProtalHTML($dbh,$p->{'html'},$p->{'fieldsShow'});
    }
    elsif($type eq 'aboutUs'){
        $portletContent= $p->{'content'};
    }
    else{
        $portletContent=undef;
    }
     
    return $portletContent;

    
}
