#!/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 JSON;
my $dbh = Opals::Context->dbh();
END { $dbh->disconnect(); }

my $cgi = CGI->new;
my $input = $cgi->Vars();
my $op = $input->{'op'};
my $id = $input->{'id'};
#my @dateExpList = ($cgi->param('dateExp'));
#my @titleList   = ($cgi->param('title'));
#my @descList    = ($cgi->param('desc'));

my $newsEventCont = $input->{'msgpost'};
$newsEventCont =~ s/"loadfile\?/"\/bin\/util\/loadfile?/g; 
my $expDate = $input->{'dateExp'};
my $status = $input->{'status'};
#$newsEventCont =~ s/\'/\\'/g; 
my ($permission, $cookieList, $template) = tmpl_read(
        {
            dbh             => $dbh,
            cgi             => $cgi,
            tmplFile        => 'util/newsEvent.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('newsEvent') );
# See User.pm for the list of permissions
if ($permission && $permission->{'pref_edit'}) {

   if($op eq  'save'){
       saveNewsEvents($dbh,$newsEventCont,$expDate,$id,$status);
   }
   elsif($op eq  'delete'){
       delNewsEvents($dbh,$id);
   }

   my ($newsEventList) = listnewsEvent($dbh);
   my $newsEventListJson={};
   foreach my $ne (@$newsEventList){
       $newsEventListJson->{$ne->{'idNE'}}={
                                            dateExp =>$ne->{'expDate'},
                                            status  =>$ne->{'status'},
                                            content  =>$ne->{'description'}
                                            };
   }
   $template->param(
         newsEventList  => $newsEventList,
         newsEventListJson  => to_json($newsEventListJson,{pretty=>1}),
         newsEvent      => 1,
         today          => $dateToday,
   );

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

#######################################
sub listnewsEvent_bk{
        my ($dbh) = @_;
        
    my $sth = $dbh->prepare(<<_STH_);
    select * from  opl_newsEvents order by idNE desc 
_STH_
    $sth->execute();
 
        my @newsEvent;
        while (my $rec = $sth->fetchrow_hashref) {
             push @newsEvent, $rec;
        }
        $sth->finish;
        return \@newsEvent;
       $sth->finish;
}
#---------------------------------------
sub listnewsEvent{
    my ($dbh) = @_;
        
    my $sth = $dbh->prepare(<<_STH_);
     select * from  opl_newsEvents where expDate>=? OR expDate is null OR expDate='0000-00-00' order by expDate desc 
_STH_
    $sth->execute($dateToday);
 
        my @newsEvent;
        my $href;
        my $p;
        $p=0;
        my @url;
        while (my $rec = $sth->fetchrow_hashref) {
             #if ($rec->{'description'} =~ m/(http:\/\/[\w\/\.\_]+)/g) {
             #Ha while ($rec->{'description'} =~ m/(http:\/\/[\w\/\.\_\-\~]+)/g) {
=item            
             while ($rec->{'description'} =~ m/(http:\/\/[\w\/\.\_\#\-\~=\?\+\;]+)/g) {
                 $href = $1;
                 $href =~ s/\.$//;
                 $url[$p] = $href;
                 $p++;
            }
            for(my $i=0;$i<scalar @url; $i++  ){
                my $str_sought=$url[$i];
                $str_sought =~s/\?/\\\?/g;                
                $rec->{'description'} =~ s/$str_sought/<a href="$url[$i]">$url[$i]<\/a>/g;

            }
            #$rec->{'description'} =~ s/$href/<a href="$href">$href<\/a>/g;
            $rec->{'description'} =~ s/\n/<br>/g;
            if($rec->{'expDate'} && $rec->{'expDate'} ne ''){
                $rec->{'description'} = ' Expiration: <i> ' . $rec->{'expDate'}  . '<i><br>' . $rec->{'description'};
            }
            if($rec->{'title'} && $rec->{'title'} ne ''){
                $rec->{'description'} = '<b>' . $rec->{'title'}  . '</b><br>' . $rec->{'description'} ;
            }
=cut
            push @newsEvent, $rec;
        }
        $sth->finish;
        return \@newsEvent;
       $sth->finish;
}
#######################################
sub saveNewsEvents{
    my ($dbh,$newsEventCont,$expDate,$id,$status) = @_;

    my $sth = $dbh->prepare(<<_STH_);
    delete from  opl_newsEvents where expDate>=?
_STH_
    #$sth->execute($dateToday);

    if(defined $id && $id>0){
    $sth = $dbh->prepare(<<_STH_);
update  opl_newsEvents
set     description  = ?,
        expDate      = ?,
        status       =?
where   idNE = ?
_STH_
    $sth->execute($newsEventCont,$expDate,$status,$id);

    }
    else{
     $sth = $dbh->prepare(<<_STH_);
insert  into opl_newsEvents
set     description  = ?,
        expDate      = ?,
        status       =?

_STH_
        $sth->execute($newsEventCont,$expDate,$status);
    }
        $sth->finish;
}

#######################################
sub delNewsEvents{
    my ($dbh,$id) = @_;

    my $sth = $dbh->prepare(<<_STH_);
    delete from  opl_newsEvents where idNE =?
_STH_
    $sth->execute($id);
    $sth->finish;
}

    
    



