package My_UserNotificationsFunctions;

use strict;
use Data::Dumper;

#--------------------------------------------------------------------------------------------------
sub new
{
  my $pclass = shift;
  my $pdbhMain = shift;
  my $pGLOBALS = shift;
  my $self = {
    dbh=>$pdbhMain,
    GLOBALS=>$pGLOBALS,
    logger=>Log::Log4perl->get_logger()
  };
  bless $self, $pclass;
  $self->{logger}->info("::DBVersions::Instance Started");
  return $self;
}


#--------------------------------------------------------------------------------------------------
#@function user_notification_functions_user_notification_list
#@author AUTOAPI
#@description List user notification(s) on the ssystem
#@params api_action
#action_content>session_id
#@params_description
#api_action - The name of the function   
#session_id - ID of the session
#@return 
#status
#error_description
#message_level
#user_notifications
#user_notifications>user_notification
#user_notifications>user_notification>user_notification_id
#user_notifications>user_notification>user_notification_value
#user_notifications>user_notification>user_notification_dbdate
#user_notifications>user_notification>user_notification_shown
#@return_description 
#status - Status of the Call (True=Success/False=Faiure)
#error_description - Error description if any
#message_level - The message level, warning or critical
#user_notifications - container for all items
#user_notification - container for each item
#user_notification_id - ID of the item
#user_notification_value - Message of the notification
#user_notification_dbdate - Date the notification was created
#user_notification_shown - has this message been shown before
#@document_class both
sub user_notification_functions_user_notification_list($)
{
  my $this      = shift;
  my ($xml)     = @_;
  my $extXml    = "";
  my $cmd       = "";
  my $rxml      = "";
  my $frxml     = "";
  my $statement = "";
  my $ustatement = "";
  my $sth;
  my $sth2;
  my $sth3;
  my $sth4;
  my @row;
  my @row2;
  my $tUSER_id=$this->{GLOBALS}{tUSER_id};
  my $tUSER_TYPE_value=$this->{GLOBALS}{tUSER_TYPE_value};
  my $tAGENCY_value=$this->{GLOBALS}{tAGENCY_value};
  my $tAGENCY_id=$this->{GLOBALS}{tAGENCY_id};  
	
	$statement = "select ".My_GenericFunctions->return_var_encode("id","tUSER_NOTIFICATION")." as 'user_notification_id', message as 'user_notification_value', dbdate_added as 'user_notification_dbdate',shown as 'user_notification_shown' from tUSER_NOTIFICATION where tUSER_id=$tUSER_id and deleted=0 ";
  $rxml .= My_GenericFunctions->execute_and_return_data($this,$xml,$statement,"user_notifications","user_notification");	
	
  $frxml .= "<\?xml version=\"1.0\"\?>";
  $frxml .= "\n<api>";
  $frxml .= "\n<status>true</status>";
  $frxml .= "$rxml\n";
  $frxml .= "\n</api>";
  return($frxml);    
}




#--------------------------------------------------------------------------------------------------
#@function user_notification_functions_user_notification_edit
#@author AUTOAPI
#@description Edit an user notification on the ssystem
#@params api_action
#action_content>session_id
#action_content>user_notification_id
#action_content>user_notification_shown
#@params_description
#api_action - The name of the function   
#session_id - Session ID of the user
#user_notification_id - ID of the Item to edit
#user_notification_shown - HAs the item been shown
#@return 
#status
#error_description
#message_level
#@return_description 
#status - Status of the Call (True=Success/False=Faiure)
#error_description - Error description if any
#message_level - The message level, warning or critical
#@document_class both
sub user_notification_functions_user_notification_edit($)
{
  my $this      = shift;
  my ($xml)     = @_;
  my $extXml    = "";
  my $cmd       = "";
  my $rxml      = "";
  my $frxml     = "";
  my $statement = "";
  my $ustatement = "";
  my $sth;
  my $sth2;
  my $sth3;
  my $sth4;
  my @row;
  my @row2;
  my $tUSER_id=$this->{GLOBALS}{tUSER_id};
  my $tUSER_TYPE_value=$this->{GLOBALS}{tUSER_TYPE_value};
  my $tAGENCY_value=$this->{GLOBALS}{tAGENCY_value};
  my $tAGENCY_id=$this->{GLOBALS}{tAGENCY_id};  
  
  if(($rxml=My_GenericFunctions->validate_input_field($this,"user_notification_id",$xml->{action_content}->{user_notification_id},"FK","tUSER_NOTIFICATION")) ne "")
  {
    return($rxml);
  }
  if(($rxml=My_GenericFunctions->validate_input_field($this,"user_notification_shown",$xml->{action_content}->{user_notification_shown},"BOOL","")) ne "")
  {
    return($rxml);
  }
  
  $statement = "select id from tUSER_NOTIFICATION where id=".My_GenericFunctions->return_var_decode($xml->{action_content}->{user_notification_id},"tUSER_NOTIFICATION")." and tUSER_id=$tUSER_id and deleted=0";
  $this->{logger}->info($statement);
  $sth = $this->{dbh}->prepare("$statement");
  $sth->execute;      
  if($sth->rows!=1)
  {
    return(My_GenericFunctions->return_error("No Such Notification.","warn"));
  }
  

  $statement = "update tUSER_NOTIFICATION set shown=\"".$xml->{action_content}->{user_notification_shown}."\" where id=".My_GenericFunctions->return_var_decode($xml->{action_content}->{user_notification_id},"tUSER_NOTIFICATION")." and tUSER_id=$tUSER_id";
  $this->{logger}->info($statement);
  $sth = $this->{dbh}->prepare("$statement");
  $sth->execute;      
  if($sth->rows!=1)
  {
    return(My_GenericFunctions->return_error("Failed to Update item.","warn"));
  }
  
	
  $frxml .= "<\?xml version=\"1.0\"\?>";
  $frxml .= "\n<api>";
  $frxml .= "\n<status>true</status>";
  $frxml .= "$rxml\n";
  $frxml .= "\n</api>";
  return($frxml);    
}

#--------------------------------------------------------------------------------------------------
#@function user_notification_functions_user_notification_delete
#@author AUTOAPI
#@description Delete an user notification on the ssystem
#@params api_action
#action_content>session_id
#action_content>user_notification_id
#@params_description
#api_action - The name of the function   
#session_id - session id of the user
#user_notification_id - ID of the Item to edit
#@return 
#status
#error_description
#message_level
#@return_description 
#status - Status of the Call (True=Success/False=Faiure)
#error_description - Error description if any
#message_level - The message level, warning or critical
#@document_class both
sub user_notification_functions_user_notification_delete($)
{
  my $this      = shift;
  my ($xml)     = @_;
  my $extXml    = "";
  my $cmd       = "";
  my $rxml      = "";
  my $frxml     = "";
  my $statement = "";
  my $ustatement = "";
  my $sth;
  my $sth2;
  my $sth3;
  my $sth4;
  my @row;
  my @row2;
  my $tUSER_id=$this->{GLOBALS}{tUSER_id};
  my $tUSER_TYPE_value=$this->{GLOBALS}{tUSER_TYPE_value};
  my $tAGENCY_value=$this->{GLOBALS}{tAGENCY_value};
  my $tAGENCY_id=$this->{GLOBALS}{tAGENCY_id};  
  
  if(($rxml=My_GenericFunctions->validate_input_field($this,"user_notification_id",$xml->{action_content}->{user_notification_id},"FK","tUSER_NOTIFICATION")) ne "")
  {
    return($rxml);
  }

  $statement = "select id from tUSER_NOTIFICATION where id=".My_GenericFunctions->return_var_decode($xml->{action_content}->{user_notification_id},"tUSER_NOTIFICATION")." and tUSER_id=$tUSER_id and deleted=0";
  $this->{logger}->info($statement);
  $sth = $this->{dbh}->prepare("$statement");
  $sth->execute;      
  if($sth->rows!=1)
  {
    return(My_GenericFunctions->return_error("No Such Notification.","warn"));
  }
  

  $statement = "update tUSER_NOTIFICATION set deleted=1 where id=".My_GenericFunctions->return_var_decode($xml->{action_content}->{user_notification_id},"tUSER_NOTIFICATION")." and tUSER_id=$tUSER_id";
  $this->{logger}->info($statement);
  $sth = $this->{dbh}->prepare("$statement");
  $sth->execute;      
  if($sth->rows!=1)
  {
    return(My_GenericFunctions->return_error("Failed to Update item.","warn"));
  }
  
	
  $frxml .= "<\?xml version=\"1.0\"\?>";
  $frxml .= "\n<api>";
  $frxml .= "\n<status>true</status>";
  $frxml .= "$rxml\n";
  $frxml .= "\n</api>";
  return($frxml);    
}

1;
