#!/usr/bin/perl

use strict;
use CGI;

use Opals::Context;
use Opals::Template_ajax qw(
    tmpl_read
    tmpl_write
);
use Opals::Locale qw(
    loc_getMsgFile
    loc_write
);


 
my $dbh = Opals::Context->dbh();
END { $dbh->disconnect(); }
use JSON;
my $input=getLeafInfo();
my $cgi = CGI->new;
my ($permission, $cookie, $template) = tmpl_read(
    {
        dbh             => $dbh,
        cgi             => $cgi,
        tmplFile        => 'ajax/portlet/saveOWLLeafDB.tmpl',
    }
);


    my $rank      = $input->{"rank"};
    my $title       = $input->{"title"};
       $title       =~ s/[\r\n]//g;
    my $url         = $input->{"url"};
       $url         =~ s/[\r\n]//g;
    my $description = $input->{"description"};

    my $sth=$dbh->prepare("update opl_extDatabase set name=?, url=?, description=? where page='OWL' && rank=?");
    my $rv =$sth->execute($title,$url,$description,$rank);  
    
    $sth->finish;  
    my $status = ($rv eq '0E0') ? 0 : 1;        
    $template->param(
        status => $status,
        leafId => $rank
    );

print "Content-type: text/plain\n\n";
print   to_json({status => $status,leafId => $rank});


#-------------------------------------------------------------------------------
sub getLeafInfo{
  my $in={};
  if ($ENV{'REQUEST_METHOD'} eq "POST") {
        my $json ="";
        while (<STDIN>) {
            $json .= $_;
        }
        #open debug ,">/tmp/bb";print debug $json;close debug;
        $in = decode_json($json);
   }
   return $in;
}
