<?php include "../../includes/start_ajax.php";

?>

<div  align=left >
<?php


  echo "<section class='view_toolbar'>";
                    echo "<div class='view_toolbar_left' style='width:50%;'>";
						echo "<span class='fa fa-sign-out'></span> Insert Transaction to DBF file.";
                    echo "</div>";
                    echo "<div class='view_toolbar_right'>";	
    
	
			
						
	
                    echo "</div>";
    echo "</section>";
    
    

?>



<?php 

	


?>

<?php




$this_url="mod/myprofile/index.php";
$display_url="mod/user_own/display.php";
	
	

//Main Configuration ====================================
//Database Field (Columns)
$form_key_id="dbf_id";
$sys_field_id=1;

$form_field_separator[$sys_field_id]="DBF File Path";
//---------------------------------------------------------------------------------------------------------------------------
$form_field[$sys_field_id]="dbf_path";
$form_field_label[$sys_field_id]="File Path";
$form_field_value[$sys_field_id]="";
$form_field_checking[$sys_field_id]="COMPULSORY"; //(COMPULSORY/OPTIONAL/PASSWORD1/PASSWORD2)
$form_field_class[$sys_field_id]="textbox textbox-l"; //(textbox_l/textbox_m/textbox_s/upload/dropdown/textarea/checkbox)
$form_field_type[$sys_field_id]="TEXT"; //(TEXT/PASSWORD/TEXTSUGGEST/DATE_START/DATE_END/DATE/TIME/PASSWORD/DROPDOWN/FILE_PHOTO/PRICE/QUANTITY)
$form_field_dropdown_option[$sys_field_id]=""; //(KEYWORD = include/keyword.php,  SQL = From database )
$form_field_dropdown_option_values[$sys_field_id]=""; //SQL String(select XX as value, YY as text from...) or Keyword 
$form_field_dropdown_option_values_key[$sys_field_id]=""; //SQL row ID
$sys_field_id++;



$form_field_separator[$sys_field_id]="Field Mapping";

//---------------------------------------------------------------------------------------------------------------------------
$form_field[$sys_field_id]="dbf_field_ivh_doc_no";
$form_field_label[$sys_field_id]="Invoice Doc No.";
$form_field_value[$sys_field_id]="";
$form_field_checking[$sys_field_id]="COMPULSORY"; //(COMPULSORY/OPTIONAL/PASSWORD1/PASSWORD2)
$form_field_class[$sys_field_id]="textbox textbox-l"; //(textbox_l/textbox_m/textbox_s/upload/dropdown/textarea/checkbox)
$form_field_type[$sys_field_id]="TEXT"; //(TEXT/PASSWORD/TEXTSUGGEST/DATE_START/DATE_END/DATE/TIME/PASSWORD/DROPDOWN/FILE_PHOTO/PRICE/QUANTITY)
$form_field_dropdown_option[$sys_field_id]=""; //(KEYWORD = include/keyword.php,  SQL = From database )
$form_field_dropdown_option_values[$sys_field_id]=""; //SQL String(select XX as value, YY as text from...) or Keyword 
$form_field_dropdown_option_values_key[$sys_field_id]=""; //SQL row ID
$sys_field_id++;



//table name
$form_table_name="t_".$key_table_prefix."_dbf";








//============================================FORM SUBMITTED======================================================
if ($_SERVER[REQUEST_METHOD] == 'POST') {
$error_msg="";
$password_confirmation=false;
$password_temp="";

	for($i=1;$i<=count($form_field);$i++)
	{
		
		
			if($form_field_type[$i]=="DROPDOWN")
		{
			
			if($form_field_checking[$i]=="COMPULSORY")
			{
				if($_REQUEST[$form_field[$i]]=="0")
					{
						$error_msg=$error_msg."<li>".$form_field_label[$i]."</li>";	  
						$form_field_class[$i]=$form_field_class[$i]."_error";
					}
			}	
			
		}
		elseif($form_field_type[$i]=="QUANTITY")
		{
			if(($form_field_checking[$i]=="COMPULSORY")and($_REQUEST[$form_field[$i]]==""))
			{
				$error_msg=$error_msg."<li>".$form_field_label[$i]."</li>";	  
				$form_field_class[$i]=$form_field_class[$i]."_error";
				
			}
			else
			{
				if(!is_numeric($_REQUEST[$form_field[$i]]))
				{
					$error_msg=$error_msg."<li>".$form_field_label[$i]." must be number value only.</li>";	  
					$form_field_class[$i]=$form_field_class[$i]."_error";
				}
				else
				{
					if(stripos($_REQUEST[$form_field[$i]],'.'))
					{
					 	$error_msg=$error_msg."<li>".$form_field_label[$i]." must be integer value only.</li>";	  
						$form_field_class[$i]=$form_field_class[$i]."_error";
					}
				}
							
				
			}	
			
			
		}
		elseif($form_field_type[$i]=="PRICE")
		{
			if(($form_field_checking[$i]=="COMPULSORY")and($_REQUEST[$form_field[$i]]==""))
			{
				$error_msg=$error_msg."<li>".$form_field_label[$i]."</li>";	  
				$form_field_class[$i]=$form_field_class[$i]."_error";
				
			}
			else
			{
				if(!is_numeric($_REQUEST[$form_field[$i]]))
				{
		
					$error_msg=$error_msg."<li>".$form_field_label[$i]." must be numeric value only.</li>";	  
					$form_field_class[$i]=$form_field_class[$i]."_error";
				}
				
				
			}	
			
			
		}
		else
		{
			if($form_field_checking[$i]=="COMPULSORY")
			{
				if($_REQUEST[$form_field[$i]]=="")
					{
						$error_msg=$error_msg."<li>".$form_field_label[$i]."</li>";	  
						$form_field_class[$i]=$form_field_class[$i]."_error";
					}
			}	
			
			
		}
		
		
		if($form_field_checking[$i]=="PASSWORD1")
		{
			if($_REQUEST[$form_field[$i]]=="")
				{
					 
				}
			else
				{
					$password_temp=$_REQUEST[$form_field[$i]];
				}
		}	
		
		if($form_field_checking[$i]=="PASSWORD2")
		{
			if($_REQUEST[$form_field[$i]]=="")
				{
					if($password_temp!="")
					{
						$error_msg=$error_msg."<li>Password confirmation</li>";	
						$form_field_class[$i]=$form_field_class[$i]."_error";
					}
				}
			else
				{
				 	if($password_temp=="")
				 	{
						$error_msg=$error_msg."<li>Password</li>";	
						$form_field_class[$i-1]=$form_field_class[$i-1]."_error";
					}
					else
					{
						if($password_temp!=$_REQUEST[$form_field[$i]])
					 	{
							$error_msg=$error_msg."<li>Password Confirmation Not Match</li>";
							$form_field_class[$i]=$form_field_class[$i]."_error";
							$form_field_class[$i-1]=$form_field_class[$i-1]."_error";	
						}
					}
				 	
				
				}
		}
	}

	
	if($error_msg=="")
		{
	
		
				
			  	$query="";
			  	$query=$query."UPDATE $form_table_name SET ";
			  
				for($i=1;$i<=count($form_field);$i++)
				{
				 
				 	if($form_field_type[$i]=="PASSWORD")
				 	{
				 	 	if(($form_field_checking[$i]=="PASSWORD1")&&($_REQUEST[$form_field[$i]]!=""))
				 	 	{
							$query=$query."".$form_field[$i]."='".md5($_REQUEST[$form_field[$i]])."',";
						}
					}
					elseif(($form_field_type[$i]!="FILE")AND($form_field_type[$i]!="FILE_PHOTO"))
					{
						$query=$query."".$form_field[$i]."='".toMySQLString($_REQUEST[$form_field[$i]])."',";
					}
					
				}
				if($_REQUEST["relationkey"]!="")
				{
					$query=$query." ".$header_save_field."='".$_REQUEST["relationkey"]."',";
				}
				$query=$query."modified_by='".$CurrentUserID."',";
				$query=$query."MODIFIED_DATE='".date("Y-m-d H:i:s")."' ";
				$query=$query."WHERE $form_key_id=".$CurrentUserID;
				$query=$query.";";	
				
			
			
				$result = mysqli_query($dbconn,$query) or die("Saving failed- Err msg  ". mysqli_error($dbconn).$query);
				
				$message_type="UPDATED";//to be shown at bottom
				mon_log_audit($CurrentUserName,$CurrentUserID,$form_key_id,$CurrentUserID,$message_type);
						
	    	
	    }
    else
	    {
	      	$message_type="FAIL";//to be shown at bottom  	
		}
		
			for($i=1;$i<=count($form_field);$i++)
			{
			 	if($form_field_type[$i]=="PASSWORD")
				{
				 $form_field_value[$i]="";
				}
				else
				{
				 $form_field_value[$i]=$_REQUEST[$form_field[$i]];		
				}
					
			}
			
}






	  		 	$query_get="SELECT * FROM $form_table_name ";
	  		 	$query_get=$query_get." WHERE $form_key_id = '".$CurrentUserID."';";
	  		 	$result_get=mysqli_query($dbconn,$query_get)or die("Get Data failed- Err msg  ". mysqli_error($dbconn));
	  		  	
	  		 	if($line_get=mysqli_fetch_array($result_get,MYSQLI_ASSOC))
	  		 	{
	  		 	 
					for($i=1;$i<=count($form_field);$i++)
						{
						 
						 	if($form_field_type[$i]=="PASSWORD")
							{
							 $form_field_value[$i]="";
							}
							else
							{
							 $form_field_value[$i]=$line_get[$form_field[$i]];	
							}
						 
						
						}
						
				}






			if($message_type=="CREATED"){
				  	displaymessage("Information has been created as at ".date("g:i:s a"),$message_type);
				}
				if($message_type=="UPDATED"){
				  	displaymessage("Information has been updated as at ".date("g:i:s a"),$message_type);
				}
				if($message_type=="FAIL"){
				  	displaymessage($error_msg,$message_type);
				  		for($i=1;$i<=count($form_field);$i++)
						{
						 	if($form_field_type[$i]=="PASSWORD")
							{
							 $form_field_value[$i]="";
							}
							else
							{
							 $form_field_value[$i]=$_REQUEST[$form_field[$i]];	
							}
								
						}
				}


?>

<form name="<?php echo $form_key_id; ?>_form_<?php echo $CurrentUserID;?>_<?php echo $_REQUEST["relationkey"];?>" >

	
<?php
	//IMPORTANT! - Ensure the id is exist after creation!
	echo "<input type='hidden' name='".$form_key_id."' value='".$CurrentUserID."' >";
	echo "<input type='hidden' name='offset' value='$offset' >";
	echo "<input type='hidden' name='action' value='".$_REQUEST["action"]."' >";
	echo "<input type='hidden' name='relationkey' value='".$_REQUEST["relationkey"]."' >";
	echo "<input type='hidden' name='relationkey2' value='".$_REQUEST["relationkey2"]."' >";
	echo "<input type='hidden' name='relationkey3' value='".$_REQUEST["relationkey3"]."' >";
	echo "<input type='hidden' name='search_word' value='".$_REQUEST["search_word"]."' >";
	echo "<input type='hidden' name='search_field' value='".$_REQUEST["search_field"]."' >";
?>


<?php



for($i=1;$i<=count($form_field);$i++)
		{
		 
			if($form_field_separator[$i]!="")
			{
				echo "<div class='form_field_separator' >";
				
				
				echo "<font size=3><b>".$form_field_separator[$i]."</b></font>";
				
				echo "</div>";
			}
			
			
			
			echo "<div class='form_field_row' >";
			
			
			
				//------------------------label--------------------------------------------------
			
				
				
				echo "<div class='form_field_label' >";
				
				if($form_field_checking[$i]=="COMPULSORY")
				{
					
					echo "<b>".$form_field_label[$i]."</b>";
					//echo " <font class='compulsory_note' >*</font> ";
					
					
				}
				else
				{
					
					echo $form_field_label[$i];
					
					
				}
				echo "</div>";
				
			
			
				//-------------------------input-------------------------------------------------
			
			
				echo "<div class='form_field_input' >";
			
					
					if($form_field_type[$i]=="TEXT")
					{
						echo "<input type=\"text\" class=\"".$form_field_class[$i]."\" name=\"".$form_field[$i]."\" id=\"id_".$form_field[$i]."\" value=\"".$form_field_value[$i]."\" placeholder=\"".$form_field_hints[$i]."\" >";
					}
					
					if($form_field_type[$i]=="PRICE")
					{
						echo "<input type=\"text\" class=\"".$form_field_class[$i]."\" name=\"".$form_field[$i]."\" id=\"id_".$form_field[$i]."\" value=\"".$form_field_value[$i]."\" placeholder=\"".$form_field_hints[$i]."\" >";
					}
					
					if($form_field_type[$i]=="QUANTITY")
					{
						echo "<input type=\"text\" class=\"".$form_field_class[$i]."\" name=\"".$form_field[$i]."\" id=\"id_".$form_field[$i]."\" value=\"".$form_field_value[$i]."\" placeholder=\"".$form_field_hints[$i]."\" >";
					}
					
					if($form_field_type[$i]=="DATE_START")
					{
						echo "<input type=\"text\" class=\"".$form_field_class[$i]."\" name=\"".$form_field[$i]."\" id=\"id_".$form_field[$i]."\" value=\"".$form_field_value[$i]."\" onfocus=blur() >";
						echo "<a href=\"javascript:void(0)\" onclick=\"if(self.gfPop)gfPop.fStartPop(document.".$form_key_id."_form_".$_REQUEST[$form_key_id]."_".$_REQUEST["relationkey"].".".$form_field[$i].",document.".$form_key_id."_form_".$_REQUEST[$form_key_id]."_".$_REQUEST["relationkey"].".".$form_field[$i+1].");"."return false;\" HIDEFOCUS>";
						echo " <span class='fa fa-calendar fa-lg' style='color:#000;'></span>";
						echo "</a>"; 
					}
				
					if($form_field_type[$i]=="DATE_END")
					{
						echo "<input type=\"text\" class=\"".$form_field_class[$i]."\" name=\"".$form_field[$i]."\" id=\"id_".$form_field[$i]."\" value=\"".$form_field_value[$i]."\" onfocus=blur() >";
						echo "<a href=\"javascript:void(0)\" onclick=\"if(self.gfPop)gfPop.fEndPop(document.".$form_key_id."_form_".$_REQUEST[$form_key_id]."_".$_REQUEST["relationkey"].".".$form_field[$i-1].",document.".$form_key_id."_form_".$_REQUEST[$form_key_id]."_".$_REQUEST["relationkey"].".".$form_field[$i].");"."return false;\" HIDEFOCUS>";
						echo " <span class='fa fa-calendar fa-lg' style='color:#000;'></span>";
						echo "</a>"; 
					}
				
				
					if($form_field_type[$i]=="DATE")
					{
						echo "<input type=\"text\" class=\"".$form_field_class[$i]."\" name=\"".$form_field[$i]."\" id=\"id_".$form_field[$i]."\" value=\"".$form_field_value[$i]."\" onfocus=blur() > ";
						echo "<a href=\"javascript:void(0)\" onclick=\"if(self.gfPop)gfPop.fPopCalendar(document.".$form_key_id."_form_".$_REQUEST[$form_key_id]."_".$_REQUEST["relationkey"].".".$form_field[$i].");"."return false;\" HIDEFOCUS>";
						echo " <span class='fa fa-calendar fa-lg' style='color:#000;'></span>";
						echo "</a>"; 
					}
				
				
					if($form_field_type[$i]=="TIME")
					{
						echo "<input type=\"text\" class=\"".$form_field_class[$i]."\" name=\"".$form_field[$i]."\" id=\"id_".$form_field[$i]."\" value=\"".$form_field_value[$i]."\" onfocus=blur() >";
						echo "<a href=\"javascript:void(0)\" onclick=\"if(self.gfPop2)gfPop2.fPopCalendar2(document.".$form_key_id."_form_".$_REQUEST[$form_key_id]."_".$_REQUEST["relationkey"].".".$form_field[$i].");"."return false;\" HIDEFOCUS>";
						echo " <span class='fa fa-clock-o fa-lg' style='color:#000;'></span>";
						echo "</a>"; 
					
					}
					
					
					
					
					if($form_field_type[$i]=="PASSWORD")
					{
						echo "<input type=\"password\" class=\"".$form_field_class[$i]."\" name=\"".$form_field[$i]."\" id=\"id_".$form_field[$i]."\" value=\"".$form_field_value[$i]."\" placeholder=\"Leave blank if no change.\">";
						//echo "<br><font color=FF0000 size=1>(Leave blank if not reset)</font>";
					}
					if($form_field_type[$i]=="TEXTAREA")
					{
						echo "<textarea  class=\"".$form_field_class[$i]."\" name=\"".$form_field[$i]."\"  id=\"id_".$form_field[$i]."\" placeholder=\"".$form_field_hints[$i]."\" >".$form_field_value[$i]."</textarea>";
					}
					if($form_field_type[$i]=="DROPDOWN")
					{
						echo "<select class=\"".$form_field_class[$i]."\" name=\"".$form_field[$i]."\"  id=\"id_".$form_field[$i]."\" >";
						
						
						echo "<option value='0' >".$form_field_hints[$i]."</option>";
						if($form_field_dropdown_option[$i]=="KEYWORD")
						{
						 
						//for($j=1;$j<=count($mon_keyword_list[$form_field_dropdown_option_values[$i]]);$j++)
						//	{
							 
							 
							 
							foreach($mon_keyword_list[$form_field_dropdown_option_values[$i]] as $mon_key=>$mon_value) 
							{
								if($form_field_value[$i]==$mon_key)
									{
										echo "<option value='$mon_key' selected >".$mon_value."</option>";
									}
								else
									{
										echo "<option value='$mon_key' >".$mon_value."</option>";
									}
							}
							
						}
						if($form_field_dropdown_option[$i]=="SQL")
						{
						 
							$query_select=$form_field_dropdown_option_values[$i];
							$result_select=mysqli_query($dbconn,$query_select) or die("Retrieval failed- Err msg  ". mysqli_error($dbconn));
							while($line_select=mysqli_fetch_array($result_select,MYSQLI_ASSOC))
							{
								if($form_field_value[$i]==$line_select["value"])
									{
										echo "<option value='".$line_select["value"]."' selected >".$line_select["text"]."</option>";
									}
								else
									{
										echo "<option value='".$line_select["value"]."' >".$line_select["text"]."</option>";
									}
							}
							
						}
						
						
						
						echo "</select>";
					}
					
					if($form_field_type[$i]=="FILE")
					{
						if($_REQUEST[$form_key_id]!="")
						{
							echo "<iframe  class=\"upload_iframe\" src=\"mod/fuploader/index.php?mod=".$form_key_id."&mod_id=".$_REQUEST[$form_key_id]."&mod_field=".$form_field[$i]."\" frameborder=0 ></iframe>";
						}
						else
						{
							echo "<font size=2 color=FF0000 >You can only upload file after saving this record</font>";	
						}
					}
					
					if($form_field_type[$i]=="FILE_PHOTO")
					{
						if($_REQUEST[$form_key_id]!="")
						{
							echo "<iframe  class=\"upload_iframe\" src=\"mod/uploader/index.php?mod=".$form_key_id."&mod_id=".$_REQUEST[$form_key_id]."&mod_field=".$form_field[$i]."\" frameborder=0 ></iframe>";
							
							
						}
						else
						{
							echo "<font size=2 color=FF0000 >You can only upload file after saving this record</font>";	
						}
					}
					
					
					
					
					
			
				echo "</div>";
				
			echo "</div>";	
			
	 
	 
	 }
	 
	 
	 
	
?>

	<?php
	
	echo "<div class='form_field_row' >";
		echo "<div class='form_field_label' >";
			
		echo "</div>";
		echo "<div class='form_field_input' >";
			echo "<div class='fa-stack fa-2x'  style='cursor:pointer;float:left;'  onclick=\"mon_submit_form('".$form_key_id."_form_".$CurrentUserID."_".$_REQUEST["relationkey"]."','".$this_url."')\" >";
				echo "<i class=\"fa fa-square fa-stack-2x color_blue\"  ></i><i class=\"fa fa-save fa-stack-1x color_white\" ></i>";
			echo "</div>";
		echo "</div>";
	
	echo "</div>";
			
	?>



</form>
</div>

<?php include "../../includes/end_ajax.php";

?>
