#!/usr/bin/perl

use strict;
use CGI;

use Opals::Context;
use Opals::Template_ajax qw(
    tmpl_read
    tmpl_write
    tmpl_preference
);
use Opals::Portal qw(
    portal_getNewItemList
    portal_getSearchForm
    portal_getRSS
    portal_getWebLink
    portal_getLibHours
    portal_getNewsEvent
    portal_getPathfinders
    portal_getWeather
    portal_getReaderReview
    portal_getLibCalendar
    portal_getProtalHTML
    portal_getPfShowcase
);
my $dbh = Opals::Context->dbh();
END { $dbh->disconnect(); }

my $cgi = CGI->new;
my $input = $cgi->Vars();
my ($permission, $cookie, $template) = tmpl_read(
    {
        dbh             => $dbh,
        cgi             => $cgi,
        tmplFile        => 'ajax/portlet/getPortletContent.tmpl',
    }
);

my $pref        = tmpl_preference($dbh);

my $type            = $input->{'type'};
my $url             = $input->{'url'};
my $nItemShow       = $input->{'nItemShow'};
my $fieldsShow      = $input->{'fieldsShow'};
my $showBookCover   = $input->{'showBookCover'};
my $showDetail      = $input->{'showDetail'};
my $portalEdit      = $input->{'portalEdit'};
my $zipCode         = $input->{'zipCode'};
my $unit            = $input->{'unit'};
my $showHolidays    = $input->{'showHolidays'};
my $pHtml           = $input->{'html'};
my $pfIdList        = $input->{'pfIdList'};
my $title           = $input->{'title'};

    my $portletContent="";
    if($type eq 'rss' && defined $url && $url ne ''){
        $portletContent=portal_getRSS($dbh,$url,$nItemShow,$fieldsShow,'portalEdit');
    }
    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 'search'){
        $portletContent=portal_getSearchForm($dbh);
    }
    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,$pHtml,$fieldsShow);
    }
    elsif($type eq 'pfShowcase'){ 
        $portletContent=portal_getPfShowcase($dbh,$pfIdList,$fieldsShow);
    }
    elsif($type eq 'libShowcase'){ 
        $portletContent=portal_getNewItemList($dbh,$nItemShow,$fieldsShow,$showBookCover);
    }
    else{
        $portletContent="undefined";
    }
     
    $template->param(portletContent=>$portletContent);


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


