#!/usr/bin/perl

#use utf8;
use strict;
use CGI;
use Digest::SHA qw(
    sha1_base64
    sha1_hex
);

use Opals::Context;
use Opals::Template_ajax qw(
    tmpl_read
    tmpl_write
    tmpl_preference
);
use Opals::Locale qw(
    loc_getMsgFile
    loc_write
);
use Opals::Pathfinder qw(
    pf_getPfListByGrp
);
use JSON;

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/getPathfinders.tmpl',
        
    }
);

    my $nItemShow   = $input->{'nItemShow'};
    my $fieldsShow  = $input->{'fieldsShow'};
       $nItemShow   = (!defined $nItemShow  || $nItemShow <=0)?  5 :  $nItemShow;
       $fieldsShow  = (!defined $fieldsShow || $fieldsShow eq '')? 'title,author' : $fieldsShow;

    my($showBrief,$showDetail);
    ($showBrief,$showDetail)=($fieldsShow eq 'title')?('true','false'):('false','true');
    my $pathfinders={
                     showBrief=>$showBrief,
                     showDetail=>$showDetail,
                     pathfinders=>[]
                     };

    my $pfListGrp= pf_getPfListByGrp($dbh,'subject',0,$nItemShow);
     
    foreach my $pfl(@$pfListGrp){
        my $pf={group=>$pfl->{'group'},
               pfList=>$pfl->{'pfRecList'}};
        push @{$pathfinders->{'pathfinders'}},$pf;
        
    }
    my $json = to_json($pathfinders,{pretty=>1});
    print "Content-type: text/plain\n\n";
    print $json;


################################################################################

