#!/usr/bin/perl

#use utf8;
use strict;
use CGI;

use Opals::Context;use POSIX;

use Opals::User qw(
    user_currentUser
    user_getInformationById
);

use Opals::Template qw(
    tmpl_read
    tmpl_write
);

use Opals::Locale qw(
    loc_getMsgFile
    loc_write
);

use Opals::Constant;
use JSON;
my $dbh = Opals::Context->dbh();
END { $dbh->disconnect(); }

my $USERFIELDMAP={
                uid=>0,
                status=>0,
                firstname=>1,
                middlename=>1,
                lastname=>1,
                nickname=>1,
                userbarcode=>0,
                username=>0,
                expiryDate=>0,
                email=>1,
                phone=>1,
                workPhone=>1,
                cellphone=>1,
                addrLine1=>1,
                addrLine2=>1,
                city=>1,
                zip=>1,
                state=>1,
                country=>1,
                neighborhood=>1,
                buildingcode=>0,
                homeroom=>0,
                teacher=>0,
                grade=>0,
                yeargraduation=>0,
                pref_lang=>1,
                pref_contact=>1,
                program=>0,
                studies=>0,
                permanent=>0,
                fulltime=>0,
                keepLoanHistory=>1,
                noticeByEmail=>1
};

my $cgi = CGI->new;
my $input = $cgi->Vars();

my ($permission, $cookie, $template) = tmpl_read(
        {
            dbh             => $dbh,
            cgi             => $cgi,
            tmplFile        => 'user/myinfo.tmpl',
        }
);
if(defined $input->{'userInfo'} && $input->{'userInfo'} ne ''){
    my $userObj=decode_json($input->{'userInfo'});
    saveUserInfo($dbh, $userObj);
}

my $userInfo = getUserInfo($dbh);
if (!defined $userInfo){
    $template->param(InvalidUser => 1);
}
else{ 
     $template->param(userInfo=>to_json($userInfo, {pretty => 1}) );
}

#Thu, Jan 07, 2010 @ 13:50:35 EST
my $msgValMap ={};
my $msgMap            =loc_getMsgFile('user/userInfo.msg',$msgValMap);
loc_write($template,$msgMap);
    
tmpl_write($dbh, $cgi, $cookie, $template);


#-------------------------------------------------------------
sub getUserInfo{
    my($dbh)=@_;
    my $user=undef;
    my ($errCode, $myCookie, $curentUser) = user_currentUser($dbh, $cgi);
    if(defined  $curentUser){
        $user={};
        my ($userInfo, $guardian) = user_getInformationById($dbh, $curentUser->{'uid'});
          foreach my $f(keys %$USERFIELDMAP){
            $user->{$f}=(defined $userInfo->{$f})?$userInfo->{$f}:"";
        }
        $user->{'birthday'} = '' if ($user->{'birthday'} eq '0000-00-00');
    }
    return $user;
}
#-------------------------------------------------------------
sub saveUserInfo{
    my($dbh,$userInfo)=@_;
    my ($errCode, $myCookie, $curentUser) = user_currentUser($dbh, $cgi);
    if(defined  $curentUser &&  $curentUser->{'uid'} eq $userInfo->{'uid'}){
        my @newData  = ();
        my @fields   =();
        my $uid=$userInfo->{'uid'};
        my  $sql = "update opl_user set " ;
        foreach my $f(keys %$USERFIELDMAP){
            if(defined $userInfo->{$f} && $USERFIELDMAP->{$f} ==1){
                push  @fields,"$f=?";
                push @newData,$userInfo->{$f};
            }
        }

        $sql .= join (",", @fields) . " where uid =$uid";
        $dbh->do($sql,undef,@newData);
    }
}

