package My_UserProfileFunctions;

use strict;
use Data::Dumper;
use MIME::Base64;

#--------------------------------------------------------------------------------------------------
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_profile_functions_user_profile_types_list
#@author AUTOAPI
#@description List user profile type(s) on the system
#@params api_action
#action_content>user_type_id
#@params_description
#api_action - The name of the function   
#action_content - container for all input fields
#user_type_id - ID for the user type
#@return 
#status
#error_description
#message_level
#user_profile_types
#user_profile_types>user_profile_type
#user_profile_types>user_profile_type>user_profile_type_id
#user_profile_types>user_profile_type>user_type_id
#user_profile_types>user_profile_type>user_profile_type_desc
#user_profile_types>user_profile_type>user_profile_type_default_value
#user_profile_types>user_profile_type>user_profile_type_can_edit
#user_profile_types>user_profile_type>user_profile_type_type
#@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_profile_types - container for all items
#user_profile_type - continer for each item
#user_profile_type_id - ID of the item
#user_type_id - ID of the user type
#user_profile_type_desc - description of the item
#user_profile_type_default_value - Default value for the item
#user_profile_type_can_edit - Can the Item be edited
#user_profile_type_type - Type of item
#@document_class both
sub user_profile_functions_user_profile_types_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;

  if(($rxml=My_GenericFunctions->validate_input_field($this,"user_type_id",$xml->{action_content}->{user_type_id},"FK","tUSER_TYPE")) ne "")
  {
    return($rxml);
  }
	
	$statement = "select ".My_GenericFunctions->return_var_encode("id","tUSER_PROFILE_TYPES")." as 'user_profile_type_id',".My_GenericFunctions->return_var_encode("tUSER_TYPE_id","tUSER_TYPE")." as 'user_type_id', long_desc as 'user_profile_type_desc',default_value as 'user_profile_type_default_value',can_edit as 'user_profile_type_can_edit',profile_type as 'user_profile_type_type',compulsory  from tUSER_PROFILE_TYPES where tUSER_TYPE_id=".My_GenericFunctions->return_var_decode($xml->{action_content}->{user_type_id},"tUSER_TYPE")." and can_view=1";
  $rxml .= My_GenericFunctions->execute_and_return_data($this,$xml,$statement,"user_profile_types","user_profile_type");	
	
  $frxml .= "<\?xml version=\"1.0\"\?>";
  $frxml .= "\n<api>";
  $frxml .= "\n<status>true</status>";
  $frxml .= "$rxml\n";
  $frxml .= "\n</api>";
  return($frxml);    
}


#--------------------------------------------------------------------------------------------------
#@function user_profile_functions_user_profile_list
#@author AUTOAPI
#@description List user profile(s) on the system
#@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_profiles
#user_profiles>user_profile
#user_profiles>user_profile>user_profile_id
#user_profiles>user_profile>user_profile_type_id
#user_profiles>user_profile>user_profile_value
#@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_profiles - container for all items
#user_profile - continer for each item
#user_profile_id - ID of the item
#user_profile_type_id - ID of the profile type
#user_profile_value - Value of the Item
#@document_class both
sub user_profile_functions_user_profile_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_PROFILE")." as 'user_profile_id',".My_GenericFunctions->return_var_encode("tUSER_PROFILE_TYPES_id","tUSER_PROFILE_TYPES")." as 'user_profile_type_id', value as 'user_profile_value' from tUSER_PROFILE where tUSER_id=$tUSER_id and (select can_view from tUSER_PROFILE_TYPES where id=tUSER_PROFILE_TYPES_id)=1";
  $rxml .= My_GenericFunctions->execute_and_return_data($this,$xml,$statement,"user_profiles","user_profile");	
	
  $frxml .= "<\?xml version=\"1.0\"\?>";
  $frxml .= "\n<api>";
  $frxml .= "\n<status>true</status>";
  $frxml .= "$rxml\n";
  $frxml .= "\n</api>";
  return($frxml);    
}


#--------------------------------------------------------------------------------------------------
#@function user_profile_functions_user_profile_edit
#@author AUTOAPI
#@description Edit an user profile on the system
#@params api_action
#action_content>user_profile_id
#action_content>user_profile_value
#@params_description
#api_action - The name of the function   
#user_profile_id - ID of the Item to edit
#user_profile_value - The new value
#@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_profile_functions_user_profile_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_profile_id",$xml->{action_content}->{user_profile_id},"FK","tUSER_PROFILE")) ne "")
  {
    return($rxml);
  }
  if(($rxml=My_GenericFunctions->validate_input_field($this,"user_profile_value",$xml->{action_content}->{user_profile_value},"TEXT","")) ne "")
  {
    return($rxml);
  }
  
  $statement = "select tUSER_PROFILE_TYPES_id from tUSER_PROFILE where id=".My_GenericFunctions->return_var_decode($xml->{action_content}->{user_profile_id},"tUSER_PROFILE")." 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("No Such Profile Value.","warn"));
  }
  
  $statement = "select can_edit,profile_type from tUSER_PROFILE_TYPES where id=(select tUSER_PROFILE_TYPES_id from tUSER_PROFILE where id=".My_GenericFunctions->return_var_decode($xml->{action_content}->{user_profile_id},"tUSER_PROFILE").") ";
  $this->{logger}->info($statement);
  $sth = $this->{dbh}->prepare("$statement");
  $sth->execute;      
  if($sth->rows!=1)
  {
    return(My_GenericFunctions->return_error("No Such Profile Value.","warn"));
  }
  @row = $sth->fetchrow_array;
  if($row[0]!=1)
  {
    return(My_GenericFunctions->return_error("You are not allowed to edit this profile.","warn"));
  }
  if(($rxml=My_GenericFunctions->validate_input_field($this,"user_profile_value",$xml->{action_content}->{user_profile_value},"$row[1]","")) ne "")
  {
    return($rxml);
  }
    
  $statement = "update tUSER_PROFILE set value=\"".$xml->{action_content}->{user_profile_value}."\" where id=".My_GenericFunctions->return_var_decode($xml->{action_content}->{user_profile_id},"tUSER_PROFILE")." 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_profile_functions_user_profile_add
#@author AUTOAPI
#@description Add an user profile item on the system
#@params api_action
#action_content>user_profile_type_id
#action_content>user_profile_value
#@params_description
#api_action - The name of the function   
#user_profile_type_id - ID of the profile type to add
#user_profile_value - The new value
#@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_profile_functions_user_profile_add($)
{
  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_profile_type_id",$xml->{action_content}->{user_profile_type_id},"FK","tUSER_PROFILE_TYPES")) ne "")
  {
    return($rxml);
  }
  if(($rxml=My_GenericFunctions->validate_input_field($this,"user_profile_value",$xml->{action_content}->{user_profile_value},"TEXT","")) ne "")
  {
    return($rxml);
  }
  
  $statement = "select ID from tUSER_PROFILE where tUSER_PROFILE_TYPES_id=".My_GenericFunctions->return_var_decode($xml->{action_content}->{user_profile_type_id},"tUSER_PROFILE_TYPES")." and tUSER_id=$tUSER_id";
  $this->{logger}->info($statement);
  $sth = $this->{dbh}->prepare("$statement");
  $sth->execute;      
  if($sth->rows!=0)
  {
    return(My_GenericFunctions->return_error("Item Already Exists, Please update rather.","warn"));
  }
  
  $statement = "select can_edit,profile_type from tUSER_PROFILE_TYPES where id=".My_GenericFunctions->return_var_decode($xml->{action_content}->{user_profile_type_id},"tUSER_PROFILE_TYPES")." ";
  $this->{logger}->info($statement);
  $sth = $this->{dbh}->prepare("$statement");
  $sth->execute;      
  if($sth->rows!=1)
  {
    return(My_GenericFunctions->return_error("No Such Profile Value.","warn"));
  }
  @row = $sth->fetchrow_array;
  if($row[0]!=1)
  {
    return(My_GenericFunctions->return_error("You are not allowed to edit this profile.","warn"));
  }
  if(($rxml=My_GenericFunctions->validate_input_field($this,"user_profile_value",$xml->{action_content}->{user_profile_value},"$row[1]","")) ne "")
  {
    return($rxml);
  }
    
  $statement = "insert into  tUSER_PROFILE (tUSER_id,tUSER_PROFILE_TYPES_id,value) values ($tUSER_id,".My_GenericFunctions->return_var_decode($xml->{action_content}->{user_profile_type_id},"tUSER_PROFILE_TYPES")." ,\"".$xml->{action_content}->{user_profile_value}."\")";
  $this->{logger}->info($statement);
  $sth = $this->{dbh}->prepare("$statement");
  $sth->execute;      
  if($sth->rows!=1)
  {
    return(My_GenericFunctions->return_error("Failed to Add 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_profile_functions_user_profile_picture_add
#@author AUTOAPI
#@description Add a picture to an user profile item on the system
#@params api_action
#action_content>session_id
#action_content>data
#@params_description
#api_action - The name of the function   
#session_id - the session id for the user
#data - the base64 data for the picture
#@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_profile_functions_user_profile_picture_add($)
{
  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_profile_type_id",$xml->{action_content}->{user_profile_type_id},"FK","tUSER_PROFILE_TYPES")) ne "")
  #{
  #  return($rxml);
  #}
  
  if(($rxml=My_GenericFunctions->validate_input_field($this,"data",$xml->{action_content}->{data},"TEXT","")) ne "")
  {
    return($rxml);
  }
  
  my $decoded = decode_base64($xml->{action_content}->{data});  
  if($decoded eq "")
  {
    return(My_GenericFunctions->return_error("Invalid Image data.","warn"));
  }
   
  my $SystemGlobals=My_GenericFunctions->GetSystemGlobals($this);	

  $this->{logger}->info("tmp file /tmp/$$");  
  open(OUTFILE, ">/tmp/$$");
  print OUTFILE $decoded;
  close(OUTFILE);
  
  $cmd = "/usr/bin/file --mime-type /tmp/$$";
  $this->{logger}->info("$cmd");  
  my $ret = `$cmd`;   
  $ret = substr($ret,index($ret,":")+1);
  if(index($ret,"image")==-1)
  {
  	unlink("/tmp/$$");
    return(My_GenericFunctions->return_error("Data not of a image type $ret","warn"));
  }
  $ret = substr($ret,index($ret,"/")+1);
  chomp($ret);
  
  $statement = "insert into tUSER_PROFILE_PICTURES (tUSER_id,dbdate_added,ext) values ($tUSER_id,now(),'$ret')";
  $this->{logger}->info($statement);
  $sth = $this->{dbh}->prepare("$statement");
  $sth->execute;      
  if($sth->rows!=1)
  {
  	unlink("/tmp/$$");
    return(My_GenericFunctions->return_error("Failed to Add item.","warn"));
  }

	$statement = "select ".My_GenericFunctions->return_var_encode("LAST_INSERT_ID()","tUSER_PROFILE_PICTURES")."";
  $this->{logger}->info($statement);
  $sth = $this->{dbh}->prepare("$statement");
  $sth->execute;      
	@row = $sth->fetchrow_array;
	
  $this->{logger}->info("rename /tmp/$$  ",$SystemGlobals->{image_path}."/$row[0].$ret");  
	rename("/tmp/$$",$SystemGlobals->{image_path}."/$row[0].$ret");
	
  $frxml .= "<\?xml version=\"1.0\"\?>";
  $frxml .= "\n<api>";
  $frxml .= "\n<status>true</status>";
  $frxml .= "$rxml\n";
  $frxml .= "\n</api>";
  return($frxml);    
}



#--------------------------------------------------------------------------------------------------
#@function user_profile_functions_user_profile_picture_list
#@author AUTOAPI
#@description List user profile pictture(s) on the system
#@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_profile_pictures
#user_profile_pictures>user_profile_picture
#user_profile_pictures>user_profile_picture>user_profile_picture_id
#user_profile_pictures>user_profile_picture>user_profile_picture_url
#@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_profile_pictures - container for all pictures
#user_profile_picture - container for each picture
#user_profile_picture_id - ID for the picture
#user_profile_picture_url - URL for the picture
#@document_class both
sub user_profile_functions_user_profile_picture_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_PROFILE_PICTURES")." as 'user_profile_picture_id', concat('./images/',".My_GenericFunctions->return_var_encode("id","tUSER_PROFILE_PICTURES").",'.',ext) as 'user_profile_picture_url' from tUSER_PROFILE_PICTURES where tUSER_id=$tUSER_id";
  $rxml .= My_GenericFunctions->execute_and_return_data($this,$xml,$statement,"user_profile_pictures","user_profile_picture");	
	
  $frxml .= "<\?xml version=\"1.0\"\?>";
  $frxml .= "\n<api>";
  $frxml .= "\n<status>true</status>";
  $frxml .= "$rxml\n";
  $frxml .= "\n</api>";
  return($frxml);    
}



#--------------------------------------------------------------------------------------------------
#@function user_profile_functions_user_profile_picture_delete
#@author AUTOAPI
#@description Delete a user profile pictture(s) on the system
#@params api_action
#action_content>session_id
#action_content>user_profile_picture_id
#@params_description
#api_action - The name of the function   
#session_id - ID of the session
#user_profile_picture_id - ID of the picture to delete
#@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_profile_functions_user_profile_picture_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_profile_picture_id",$xml->{action_content}->{user_profile_picture_id},"FK","tUSER_PROFILE_PICTURES")) ne "")
  {
    return($rxml);
  }

  $statement = "select id,ext from tUSER_PROFILE_PICTURES where tUSER_id=$tUSER_id and id=".My_GenericFunctions->return_var_decode($xml->{action_content}->{user_profile_picture_id},"tUSER_PROFILE_PICTURES")."";
  $this->{logger}->info($statement);
  $sth = $this->{dbh}->prepare("$statement");
  $sth->execute;      
  if($sth->rows!=1)
  {
    return(My_GenericFunctions->return_error("Failed to Delete item.","warn"));
  }
  @row = $sth->fetchrow_array;
    
  my $SystemGlobals=My_GenericFunctions->GetSystemGlobals($this);	

  unlink($SystemGlobals->{image_path}."/".$xml->{action_content}->{user_profile_picture_id}.".$row[1]");
	
  $statement = "delete from tUSER_PROFILE_PICTURES where tUSER_id=$tUSER_id and id=".My_GenericFunctions->return_var_decode($xml->{action_content}->{user_profile_picture_id},"tUSER_PROFILE_PICTURES")."";
  $this->{logger}->info($statement);
  $sth = $this->{dbh}->prepare("$statement");
  $sth->execute;      
  if($sth->rows!=1)
  {
    return(My_GenericFunctions->return_error("Failed to Delete item.","warn"));
  }
	
  $frxml .= "<\?xml version=\"1.0\"\?>";
  $frxml .= "\n<api>";
  $frxml .= "\n<status>true</status>";
  $frxml .= "$rxml\n";
  $frxml .= "\n</api>";
  return($frxml);    
}

1;
