0xV3NOMx
Linux ip-172-26-7-228 5.4.0-1103-aws #111~18.04.1-Ubuntu SMP Tue May 23 20:04:10 UTC 2023 x86_64



Your IP : 18.226.186.153


Current Path : /var/www/oasis/src_old/
Upload File :
Current File : /var/www/oasis/src_old/dynamicmastersdetails.php

<?php
$main_src_obj=(explode("/",$_SERVER["REQUEST_URI"]));
$main_src=$main_src_obj[1];
$root_file_name=$_SERVER["DOCUMENT_ROOT"]."/".$main_src."/validation_files/validate_".$_SESSION['g_file_id'].".php";
ini_set('max_execution_time', 1900);
function getModuleColumnName($aobj_context)
    {
	    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC); 
		$module_name=$aobj_context->mobj_data["module_name"];
		$qry="  select concat('module_',schema_id) as table_name,GROUP_CONCAT(name SEPARATOR '-ele-') as name,GROUP_CONCAT(field_name) as field_name from 
				(select * from 
				(select s.internal_code as schema_id,su.name,ifnull(multiple,0) as multiple,ifnull(ug.name,'0None') as group_name,concat('m_',s.internal_code,'_ele_',su.internal_code) as field_name from s_user_schema s inner join s_user_schema_elements su on 
				su.ref_s_user_schema_code=s.internal_code
				inner join s_ref_type t on t.internal_code=su.s_ref_type_meta
				left join s_user_schema_group ug on ug.internal_code=su.ref_s_user_schema_group_code
				 where module_name='$module_name'     and t.value='Meta' order by ug.sequence,su.sequence,su.internal_code ) l where   multiple=0 limit 2) a group by schema_id"; 
		$lobj_rs = $aobj_context->mobj_db->GetRow($qry);
		
		require_once("getmoduledetails.php");
		$links=getViewLinks($aobj_context,$module_name);
		$arr['details']=$lobj_rs;
		$arr['links']=$links;
		//echo $qry;
		print_r($aobj_context->mobj_output->ToJSONEnvelope($arr,0,"success"));
		return $lobj_rs;
	}
	function AssignRefModuleLovValues($aobj_context)
    {
		session_start();
		$user_type=$_SESSION['user_type'];
		$user_dept=$_SESSION['user_dept'];
	    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC); 
		$schema_id=$aobj_context->mobj_data["schema_id"];
		$qry="select su.internal_code,su.ref_module_code,s.module_name,
				concat('m_',ref_module_code,'_ele_',ref_module_code_ref_field) as const_field,
				ifnull(ref_module_code_ref_field,0) as ref_module_code_ref_field,
				ref_module_code_ref_field_const,
				ifnull(su.populate_lov,1) as populate_lov from s_user_schema_elements su
				inner join s_user_schema s on s.internal_code=su.ref_module_code
				where su.is_ref_module_code_lov=1 
				and su.ref_s_user_schema_code={$schema_id}
				and su.ref_module_code<>0"; 
		$lobj_rs = $aobj_context->mobj_db->GetAll($qry);
		 $indx=0;
		for($mod=0;$mod<count($lobj_rs);$mod++)
		{
			$ref_module_code=$lobj_rs[$mod][ref_module_code];
			$const_cond="";
			$const_field=$lobj_rs[$mod][const_field];
			$ref_module_code_ref_field=$lobj_rs[$mod][ref_module_code_ref_field];
			$ref_module_code_ref_field_const=$lobj_rs[$mod][ref_module_code_ref_field_const];
			if($ref_module_code_ref_field>0 && !empty($ref_module_code_ref_field_const))
			{
			$const_cond=" where {$const_field}='{$ref_module_code_ref_field_const}'";
			 
			}
			$ele_code=$lobj_rs[$mod][internal_code];
			$module_name=$lobj_rs[$mod][module_name];
			
			$select_meta_data="select  field_id,table_name from module_meta_columns
					where module_id={$ref_module_code} ";
			$lobj_select_meta_data = $aobj_context->mobj_db->GetRow($select_meta_data);	
			$field_id=$lobj_select_meta_data[field_id];
			$table_name=$lobj_select_meta_data[table_name];
			$item_type_codes="";
			 
			if($lobj_rs[$mod]['populate_lov']=='1')
			{
				$select_data="select {$ele_code} as ele_code,internal_code, concat({$field_id}) as  value
							from {$table_name} {$const_cond}";
				$lobj_select_data = $aobj_context->mobj_db->GetAll($select_data);
				
				if(!empty($lobj_select_data)&& count($lobj_select_data)>0)
				{
					$arr[$indx]=	$lobj_select_data; $indx++;
				}
				else
				{
					$lobj_select_data_lov[0]['ele_code']=$ele_code;
					$lobj_select_data_lov[0]['internal_code']=0;
					$lobj_select_data_lov[0]['value']="";
					$arr[$indx]=	$lobj_select_data_lov; $indx++;
				}
			}
			else
			{		
				$lobj_select_data_lov[0]['ele_code']=$ele_code;
				$lobj_select_data_lov[0]['internal_code']=0;
				$lobj_select_data_lov[0]['value']="";
				$arr[$indx]=	$lobj_select_data_lov; $indx++;				
			}			
			//echo $select_data;
			
		}
		 
		print_r($aobj_context->mobj_output->ToJSONEnvelope($arr,0,"success"));
	 
	}
	function getsRefDataValuesForDetails($aobj_context)
    {
	    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC); 
		$internal_code=$aobj_context->mobj_data["internal_code"];
		$schema_id=$aobj_context->mobj_data["schema_id"];
		$text_box_id=$aobj_context->mobj_data["text_box_id"];
		$qry="  select concat('module_',schema_id) as table_name,GROUP_CONCAT(name SEPARATOR '-ele-') as name,GROUP_CONCAT(field_name) as field_name from 
				(select * from 
				(select s.internal_code as schema_id,su.name,ifnull(multiple,0) as multiple,ifnull(ug.name,'0None') as group_name,concat('m_',s.internal_code,'_ele_',su.internal_code) as field_name from s_user_schema s inner join s_user_schema_elements su on 
				su.ref_s_user_schema_code=s.internal_code
				inner join s_ref_type t on t.internal_code=su.s_ref_type_meta
				left join s_user_schema_group ug on ug.internal_code=su.ref_s_user_schema_group_code
				 where s.internal_code='$schema_id'    and t.value='Meta' order by ug.sequence,su.sequence,su.internal_code ) l where   multiple=0 limit 2) a group by schema_id"; 
		$lobj_rs = $aobj_context->mobj_db->GetRow($qry);
		$field_split=explode(",",$lobj_rs[field_name]);
		$get_value="select concat({$field_split[0]},'-',{$field_split[1]}) as value from {$lobj_rs[table_name]}
				where internal_code={$internal_code}";
			$lobj_get_value[val] = $aobj_context->mobj_db->GetRow($get_value);	
			$lobj_get_value[text_box_id]=$text_box_id;
		print_r($aobj_context->mobj_output->ToJSONEnvelope($lobj_get_value,0,"success"));
			        return $lobj_get_value;
	}
	function getRefModuleColumnName($aobj_context)
    {
	    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC); 
		$internal_code=$aobj_context->mobj_data["internal_code"];
		$html_field_id=$aobj_context->mobj_data["html_field_id"];
		$old_qry="  select concat('module_',schema_id) as table_name,GROUP_CONCAT(name SEPARATOR '-ele-') as name,GROUP_CONCAT(field_name) as field_name from 
				(select * from 
				(select s.internal_code as schema_id,su.name,ifnull(multiple,0) as multiple,ifnull(ug.name,'0None') as group_name,concat('m_',s.internal_code,'_ele_',su.internal_code) as field_name from s_user_schema s inner join s_user_schema_elements su on 
				su.ref_s_user_schema_code=s.internal_code
				inner join s_ref_type t on t.internal_code=su.s_ref_type_meta
				left join s_user_schema_group ug on ug.internal_code=su.ref_s_user_schema_group_code
				 where s.internal_code='$internal_code'    and t.value='Meta' order by ug.sequence,su.sequence,su.internal_code ) l where   multiple=0 limit 2) a group by schema_id"; 
		//echo $qry;
				$qry="select concat('module_',schema_id  ) as table_name,
				schema_id as table_id,
				GROUP_CONCAT(name  order by seq SEPARATOR '-ele-' ) as name,
				GROUP_CONCAT(name,'->',field_name) as pop_field_name,
				GROUP_CONCAT(field_name order by seq) as field_name from 
				(select ref_s_user_schema_code  as schema_id,name,db_field as field_name,
				ifnull(sequence,0) as seq
				from s_user_schema_elements 
				where include_in_popup=1 
				and ref_s_user_schema_code={$internal_code} 
				order by ref_s_user_schema_group_code,ifnull(sequence,0),internal_code
				 ) a group by schema_id";
		$lobj_rs = $aobj_context->mobj_db->GetRow($qry);
		// To Get The Filter Consition
		  $get_ref_module_filter="select s.ref_module_code,
				s.ref_module_code_ref_field,s_ref.db_field,
				s.ref_module_code_ref_field_on ,s.ref_module_code_ref_field_const
				from s_user_schema_elements s 
				inner join s_user_schema_elements s_ref on s_ref.internal_code=s.ref_module_code_ref_field
				where s.internal_code={$html_field_id}";
			$lobj_get_ref_module_filter = $aobj_context->mobj_db->GetRow($get_ref_module_filter);
			$pop_up_value_split=explode(",",$lobj_rs['pop_field_name']);	 
			foreach($pop_up_value_split as $k=>$v)
			{
				$comma_split=explode("->",$v);	
				$pop_up_data[$comma_split[1]]=$comma_split[0];
			}
			$pop_up_data['created_by']='Created By'; 
		$arr['pop_up_data']=$pop_up_data;	 
		$arr['ref_data']=$lobj_rs;	 
		$arr['filter_data']=$lobj_get_ref_module_filter;	 
		if(!empty($lobj_rs))
		{
		print_r($aobj_context->mobj_output->ToJSONEnvelope($arr,0,"success"));
			        return $lobj_rs;
		}
		else
		{
		print_r($aobj_context->mobj_output->ToJSONEnvelope($arr,-1,"success"));
			        return $lobj_rs;
		}
	}
function pupulateLeftNav($aobj_context)
{
		  /* require("login.php");
		   $CMayaContextuser=NEW CMayaContext();
		echo  "gg".$CMayaContextuser->getUser(); */
        $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
		$type=$aobj_context->mobj_data["type"];
		$get_user_right="select ss.internal_code as user_id,user_type,value  from  s_sysdb ss 
		inner join  s_ref_type sr on sr.internal_code=user_type where ss.internal_code=".$_SESSION['user_id'];
		   $lobj_get_user_right = $aobj_context->mobj_db->GetRow($get_user_right);
		if($lobj_get_user_right[value]=='Admin')
		{
				$lstr_qry = "select s.internal_code,module_name,t.value,is_menu_only,if(is_menu_only=1,menu_function,'') as menu_function 
				,IFNULL(main_menu,'0') as main_menu_id,
				IFNULL(mm.value,concat('z',module_name)) as main_menu_val from s_user_schema s inner join s_ref_type t
				on t.internal_code=module_type
				left join s_ref_type mm on mm.internal_code=main_menu
				where module_name is not null and t.value='{$type}'
				and s.active=1
				group by s.internal_code order by 
				 ifnull(s.sequence,0),main_menu_val,module_name";
		}
		else
		{
        $lstr_qry = "select s.internal_code,module_name,t.value,is_menu_only,if(is_menu_only=1,menu_function,'') as menu_function 
				,IFNULL(main_menu,'0') as main_menu_id,
				IFNULL(mm.value,concat('z',module_name)) as main_menu_val from s_user_schema s inner join s_ref_type t
					on t.internal_code=module_type
					left join s_ref_type mm on mm.internal_code=main_menu
				inner join user_rights ur on ur.module_id=s.internal_code
					where module_name is not null and t.value='{$type}'
					and (is_view=1 or is_add=1 or is_edit=1 or is_del=1)
				and ref_s_sysdb_code=".$_SESSION['user_id']." and s.active=1
				group by s.internal_code
				order by  ifnull(s.sequence,0),main_menu_val,module_name";
		}
		 
        $lobj_rs = $aobj_context->mobj_db->GetAll($lstr_qry);
		//echo $lstr_qry;
		$arr[0]=$type;
		$arr[1]=$lobj_rs;
        if(count($lobj_rs) != 0)
            echo $aobj_context->mobj_output->ToJSONEnvelope($arr,0,"success"); 
        else
            echo $aobj_context->mobj_output->ToJSONEnvelope($arr,-1,"Failure");
        return;
}
function AddUserSchemaGeneralDetails($aobj_context)
{
	session_start();
	$user_id=$_SESSION['user_id'];
	include_once("/JSON.php"); 
    $json = new Services_JSON();
    $obj_error_class = $aobj_context->main_src."/custom_src/parseini.php"; 
	
	require_once($aobj_context->main_src."/custom_src/parseini.php");
	$obj_error_class=new parseIni($aobj_context);
	
	$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);  
		 
		$data_cols=stripcslashes($aobj_context->mobj_data["data_cols"]);
		$data_cols=  (substr($data_cols,0,strlen($data_cols) - 1));
		$data_values=stripcslashes($aobj_context->mobj_data["data_values"]);
		$data_values=  (substr($data_values,0,strlen($data_values) - 1));
		$schema_id=$aobj_context->mobj_data["ref_s_user_schema_code"];
		$validation=$aobj_context->mobj_data["validation"];
		$login_cnt=$aobj_context->mobj_data["login_counter"];
		$is_update_visible_field=$aobj_context->mobj_data["is_update_visible_field"];
		$validation=  (substr($validation,0,strlen($validation) - 1));
		$root_file_name=$aobj_context->main_src."/validation_files/validate_".$schema_id.".php";
		if (file_exists($root_file_name)) {
			require_once($root_file_name);
			}
			
 // gets the details information
	$internal_code=$aobj_context->mobj_data["internal_code"];
	$table_name=$aobj_context->mobj_data["table_name"];
	$auto_increment_value=$aobj_context->mobj_data["auto_increment_value"];
	$lstr_param = stripslashes($aobj_context->mobj_data["schemaData"]);
	$mobj_jsondata = $json->decode($lstr_param);
	$larr_schema_data = get_object_vars($mobj_jsondata);
	$schema_module_table="module_".$schema_id;
	 
	$lstr_save_schema_details_arr =  stripcslashes($aobj_context->mobj_data["save_schema_details_arr"]);
	$mobj_jsondata = $json->decode($lstr_save_schema_details_arr);
	$larr_save_data = get_object_vars($mobj_jsondata); 
	if(empty($larr_save_data))
	{
	$lstr_save_schema_details_arr =  ($aobj_context->mobj_data["save_schema_details_arr"]);
	$mobj_jsondata = $json->decode($lstr_save_schema_details_arr);
	$larr_save_data = get_object_vars($mobj_jsondata); 
	}
	
	$lstr_auto_inc_jsn_text = stripslashes($aobj_context->mobj_data["auto_inc_jsn_text"]);
	$mobj_ai_jsondata = $json->decode($lstr_auto_inc_jsn_text);
	$auto_inc_jsn_text = get_object_vars($mobj_ai_jsondata);
	 foreach($auto_inc_jsn_text as $ak=>$av)
	 {
		foreach($av as $akk=>$avv)
		$auto_inc_arr[$akk]=$avv;
	 }
 
	$insert_cols="";
	$insert_col_values="";
	$update_col_values="";
	foreach($larr_save_data as $pk=>$pv)
	{
	$insert_cols.=$pv->field_id.",";
	
	$field_split_arr=explode("_ele_",$pv->field_id);
	$int_code=$field_split_arr[1];
	if(isset($auto_inc_arr) && (in_array($int_code,$auto_inc_arr) || $auto_inc_arr[$int_code]))
	{
	$prefix=$auto_inc_arr[$int_code];
	$ref_module_element_code=$int_code;
	$ref_module_code=$schema_id;
	$aut_inc_val=getSerieslastNoInFile($aobj_context,$prefix,$ref_module_code,$ref_module_element_code);
	$insert_col_values.="'{$aut_inc_val}',";
	}
	else	
	$insert_col_values.="'".addslashes($pv->field_id_val)."',";
	
	$update_cond.=$pv->field_id."='".addslashes($pv->field_id_val)."',";
	}

	$insert_cols=substr($insert_cols,0,strlen($insert_cols)-1);
	$insert_col_values=substr($insert_col_values,0,strlen($insert_col_values)-1);
	$update_cond.=" modified_by={$user_id}, modified_date=now() ";
	$update_qry=" update {$schema_module_table} set {$update_cond} where internal_code={$internal_code}";
	 
		//echo $validation; die(); 
		
		$_SESSION['error_flag']=1;
		 
		 
	 if (function_exists('validate'))
		{
			validate($aobj_context); 
		}
	
 	
	if($internal_code==0 && $_SESSION['error_flag']==1)
	{

		  //inserts bthe values in first Module_ table
		$insert_qry="insert into module_".$schema_id."
								( ref_s_user_schema_code, ".$insert_cols.",created_by,
								created_date,
								modified_by,
								modified_date) values 
								(".$schema_id.",".$insert_col_values.",".$_SESSION['user_id'].",now(),".$_SESSION['user_id'].",now()
								)";	
			 				
		$insert_qry_old="insert into module_".$schema_id."
								( ref_s_user_schema_code, ".$data_cols.",created_by,
								created_date,
								modified_by,
								modified_date) values 
								(".$schema_id.",".$data_values.",".$_SESSION['user_id'].",now(),".$_SESSION['user_id'].",now()
								)";
		//echo $insert_qry;
		// die();
		 $lobj_rs = $aobj_context->mobj_db->Execute($insert_qry); 
		 
		 if(mysql_error()!='')
		 { 
		    $validation_data_cols = explode(",", $data_cols);
		    $validation_data_values = explode(",",str_ireplace('"','',$data_values));
	        
			for($spc = 0 ;$spc < count($validation_data_values) ; $spc++)	
			 {
			     $trim_validation_data_values[$spc] = trim($validation_data_values[$spc]);
			 }
			
			$validation_data_values =  array_flip($trim_validation_data_values);
		  
		    $pos=strpos(mysql_error(),'Duplicate');
							
		 		   
           $dupvalid =  strstr(mysql_error(), "'");
           $dupvalid_array =  explode("' for",$dupvalid);
		   $dupvalid = substr($dupvalid_array[0],1);
		   	   
		   
		   $get_search_id = $validation_data_values[$dupvalid];
		    
		   
		   $get_system_id_array =  explode('ele_',$validation_data_cols[$get_search_id]);
		   $mid = $schema_id;
		   $sysid = $get_system_id_array[1];
		   
		   $getmod_sys_names = "select s.module_name,ss.system_name from s_user_schema s
									inner join s_user_schema_elements ss on ss.ref_s_user_schema_code = s.internal_code
									where s.internal_code = {$mid} and ss.internal_code = {$sysid} ";
		   $lobj_getmod_sys_names = $aobj_context->mobj_db->GetRow($getmod_sys_names);
		   $obj_error_class->valid_module_name =  $lobj_getmod_sys_names[module_name];
		   $obj_error_class->valid_system_name =  $lobj_getmod_sys_names[system_name];
		   $valid_message = $obj_error_class->getErrorMessage();
		   		   		
		   		   	           		  
			if($pos>=0);
			 {
	            if(empty($valid_message))
                {				
				   $rdata['msg']="Check for Unique values".mysql_error();
				}
				else 
				{
				   
                   $rdata['msg']=$valid_message;	
                }
                
				   
				echo $aobj_context->mobj_output->ToJSONEnvelope($rdata,-1,"Failure");
				return 	$rdata;
			 }
		 }
		 // to update the transaction_series_prefix table with last number
		  if(!empty($auto_increment_value))
		 {
		 $loop_count=explode("~",$auto_increment_value);
		 for($i=0;$i<count($loop_count)-1;$i++)
		 {
		 $last_no=0;
		 $ref_module_element_code_arr=explode("ele_value",$loop_count[$i]);
		 $ref_module_element_code=$ref_module_element_code_arr[0];
		 $index_val=strripos($ref_module_element_code_arr[1],"-");
		 $last_no=(substr($ref_module_element_code_arr[1],$index_val+1));
		 $prefix=(substr($ref_module_element_code_arr[1],0,$index_val));
		
		 $update_last_no="update transaction_series_prefix set last_no={$last_no} 
		 where ref_module_element_code={$ref_module_element_code} and ref_module_code={$schema_id}
		 and {$last_no}>last_no and prefix = '{$prefix}' ";
		
		  $update_last_no = $aobj_context->mobj_db->Execute($update_last_no);
		 }
		 }
	             
		$last_inserted_id = $aobj_context->mobj_db->Insert_ID();
  ########################### To Inser tMultiple Group Details ####################################### To Inser tMultiple Group Details
		 $select_distinct_group_names="select internal_code,name,
								concat('user_schema_',ref_s_user_schema_code,'_entry_',internal_code) as entry_table_name,
								concat('user_schema_',ref_s_user_schema_code,'_group_',internal_code) as group_table_name
								,ifnull(parent_group_code,0) as parent_group_code
								from s_user_schema_group 
								where ref_s_user_schema_code={$schema_id}
								and multiple=1 and ifnull(parent_group_code,0)=0 
								order by ifnull(sequence,0)";
		 $lobj_select_distinct_group_names = $aobj_context->mobj_db->GetAll($select_distinct_group_names);
 	 
		foreach( $lobj_select_distinct_group_names as $gk=>$gv)
		{
			$group_id=$gv['internal_code'];
			$entry_table_name=$gv['entry_table_name'];
			$group_table_name=$gv['group_table_name'];
			
			$get_child_groups="select internal_code,
							concat('user_schema_',ref_s_user_schema_code,'_entry_',internal_code) as child_entry_table,
							concat('user_schema_',ref_s_user_schema_code,'_group_',internal_code) as child_group_table_name
							from s_user_schema_group
							where parent_group_code={$group_id}
							and multiple=1";
			 $lobj_get_child_groups =  $aobj_context->mobj_db->GetAll($get_child_groups);
			 
			$desc_table="desc {$entry_table_name}";
			 $lobj_desc_table = $aobj_context->mobj_db->GetAll($desc_table);
			 $elements='';
			 foreach($lobj_desc_table as $d=>$dv)
			 {
			 	if($dv['Field']!='login_cnt' && $dv['Field']!='internal_code' &&  $dv['Field']!='ref_s_ref_data_code'&&  $dv['Field']!='group_int_code')
				{
				 $elements.=$dv['Field'].',';
				}
			 }
			$elements=substr($elements,0,strlen($elements)-1);
			 ########################### to Map the CHILD Group Table  Values STARTS #######################################
		 	if($lobj_get_child_groups)
			{
				$get_limit_cnt="select * from {$entry_table_name}
								where login_cnt={$login_cnt}";
				 $lobj_get_limit_cnt = $aobj_context->mobj_db->getAll($get_limit_cnt);
				for($limit=0;$limit<count($lobj_get_limit_cnt);$limit++)
				{
				$insert_into_group_table="insert into {$group_table_name} 
									(internal_code,ref_s_ref_data_code,{$elements})
									select 0,{$last_inserted_id},{$elements} from {$entry_table_name}
									where login_cnt={$login_cnt}  and
									internal_code={$lobj_get_limit_cnt[$limit][internal_code]}";
					$group_insrt=$aobj_context->mobj_db->Execute($insert_into_group_table);
					 
			 		$last_inserted_parent_group_id = $aobj_context->mobj_db->Insert_ID();
						for($j=0;$j<count($lobj_get_child_groups);$j++)
						{
								$child_entry_table=$lobj_get_child_groups[$j][child_entry_table];
								$child_group_table_name=$lobj_get_child_groups[$j][child_group_table_name];
								$desc_table="desc {$child_entry_table}";
								 $lobj_desc_table = $aobj_context->mobj_db->GetAll($desc_table);
								 $child_elements='';
								 foreach($lobj_desc_table as $d=>$dv)
								 {
									if($dv['Field']!='login_cnt' && $dv['Field']!='internal_code' &&  $dv['Field']!='ref_s_ref_data_code' &&  $dv['Field']!='group_int_code')
									{
									 $child_elements.=$dv['Field'].',';
									}
								 }
							$child_elements=substr($child_elements,0,strlen($child_elements)-1);
								$insert_into_child_group_table="insert into {$child_group_table_name} 
								(internal_code,ref_s_ref_data_code,{$child_elements})
								select 0,{$last_inserted_parent_group_id},{$child_elements} 
								from {$child_entry_table}
								where login_cnt={$login_cnt}
								and ref_s_ref_data_code= {$lobj_get_limit_cnt[$limit][internal_code] }";
						$group_insrt=$aobj_context->mobj_db->Execute($insert_into_child_group_table);
												
					 
						}
				}
				for($d=0;$d<count($lobj_get_child_groups);$d++)
					{
					$del_child_entry_table=$lobj_get_child_groups[$d][child_entry_table];
					$delete_from_child_group_table="delete from {$del_child_entry_table} where login_cnt={$login_cnt}";
					$delete_from_child_group_table =  $aobj_context->mobj_db->Execute($delete_from_child_group_table);
					}
			}	
			
			 ########################### to Map the CHILD Group Table  Values ENDS #######################################
			 
			else
			{
			$insert_into_group_table="insert into {$group_table_name} 
									(internal_code,ref_s_ref_data_code,{$elements})
									select 0,{$last_inserted_id},{$elements} from {$entry_table_name}
									where login_cnt={$login_cnt}";
			$group_insrt=$aobj_context->mobj_db->Execute($insert_into_group_table);
			 
			UpldateGroupImagesFromEntryToGroupTable($aobj_context,$login_cnt,$last_inserted_id,$schema_id,$group_id,$group_table_name,$entry_table_name);
			}
			$delete_from_group_table="delete from {$entry_table_name} where login_cnt={$login_cnt}";
			$lobj_delete_from_group_table =  $aobj_context->mobj_db->Execute($delete_from_group_table);
		}
	 
			
			
	  if (function_exists('postSave'))
		{
			$post_data =  postSave($aobj_context,$last_inserted_id,$internal_code); 
		}
	     //echo $lstr_qry;
	        if(count($lobj_rs) != 0)
			{
			 
			
				for( $i = 0; $i < count($larr_schema_data); $i++ )
				{
				$get_module_code="select ref_module_code from s_user_schema_elements where internal_code=".addslashes($larr_schema_data[$i]->ref_s_user_schema_elements_code);
				$lobj_ele_code = $aobj_context->mobj_db->GetRow($get_module_code);
				 
					$insert_qry="insert into {$table_name} (
			       ref_s_user_schema_code,
			       ref_s_module_code,
			       ref_s_user_schema_group_code,
			       ref_s_user_schema_elements_code,
			       value,ref_ref_s_user_schema_code,
				   created_by,
					created_date,
					modified_by,
					modified_date
				   
			       )
			        values ( '".addslashes($larr_schema_data[$i]->ref_s_user_schema_code)  ."','"
			                   .  $last_inserted_id  ."','"
			                   .   addslashes($larr_schema_data[$i]->ref_s_user_schema_group_code)  ."','"
			                   .   addslashes($larr_schema_data[$i]->ref_s_user_schema_elements_code)  ."','"
			                   .   addslashes($larr_schema_data[$i]->value)  ."',".$lobj_ele_code[ref_module_code].",
								 ".$_SESSION['user_id'].",now(),".$_SESSION['user_id'].",now()  
								)";
						   $lobj_rs = $aobj_context->mobj_db->Execute($insert_qry); 
	 				   //echo 	$insert_qry;
				}
			}
					 
				if($lobj_rs)
				{
				 
		        $rdata['msg'] = (!empty($post_data))?$post_data:"Inserted  Successfully.";
		        $rdata['last_id'] = $last_inserted_id;
			    print_r($aobj_context->mobj_output->ToJSONEnvelope($rdata,0,"success"));
		        return;
			 }
	 
			 else
			{
		 
				$rdata['msg']="Insertion Failed";
	            echo $aobj_context->mobj_output->ToJSONEnvelope($rdata,-1,"Failure");
				return 	$rdata;
			}
	} 
		
    
	else if($internal_code>0 && $_SESSION['error_flag']==1)
	{
	  //inserts bthe values in first Module_ table
	   
	  $table_name="module_{$schema_id}";
	  /* $old_data_arr="";
	  $get_old_data="select * from module_{$schema_id} where internal_code={$internal_code}";
	  $lobj_get_old_data = $aobj_context->mobj_db->GetRow($get_old_data); 
	  foreach( $lobj_get_old_data as $k=>$v)
		$old_data_arr[$k]=$v;
	  
	  $del_qry="delete from  {$table_name} where internal_code={$internal_code}";
	   $lobj_rs = $aobj_context->mobj_db->Execute($del_qry); //deleting same and inserting same id as internal_code
		$insert_qry="insert into {$table_name} (internal_code,ref_s_user_schema_code, ".$data_cols.",
								created_by,
								created_date,
								modified_by,
								modified_date) values (".$internal_code.",".$schema_id.",".$data_values.",".$_SESSION['user_id'].",now(),".$_SESSION['user_id'].",now()
								)";
		
		 
		 $lobj_rs = $aobj_context->mobj_db->Execute($insert_qry) */;
		 $lobj_rs = $aobj_context->mobj_db->Execute($update_qry);
			
		  if(mysql_error()!='')
		 { 
		 
		    $validation_data_cols = explode(",", $data_cols);
		    $validation_data_values = explode(",",str_ireplace('"','',$data_values));
	        
			for($spc = 0 ;$spc < count($validation_data_values) ; $spc++)	
			 {
			     $trim_validation_data_values[$spc] = trim($validation_data_values[$spc]);
			 }
			
			$validation_data_values =  array_flip($trim_validation_data_values);
		  
		    $pos=strpos(mysql_error(),'Duplicate');
							
		 		   
           $dupvalid =  strstr(mysql_error(), "'");
           $dupvalid_array =  explode("' for",$dupvalid);
		   $dupvalid = substr($dupvalid_array[0],1);
		   	   
		   
		   $get_search_id = $validation_data_values[$dupvalid];
		    
		   
		   $get_system_id_array =  explode('ele_',$validation_data_cols[$get_search_id]);
		   $mid = $schema_id;
		   $sysid = $get_system_id_array[1];
		   
		   $getmod_sys_names = "select s.module_name,ss.system_name from s_user_schema s
									inner join s_user_schema_elements ss on ss.ref_s_user_schema_code = s.internal_code
									where s.internal_code = {$mid} and ss.internal_code = {$sysid} ";
		   $lobj_getmod_sys_names = $aobj_context->mobj_db->GetRow($getmod_sys_names);
		   $obj_error_class->valid_module_name =  $lobj_getmod_sys_names[module_name];
		   $obj_error_class->valid_system_name =  $lobj_getmod_sys_names[system_name];
		   
		   $valid_message = $obj_error_class->getErrorMessage();
						
			if($pos>=0);
			 {
				 if(empty($valid_message))
                {				
				   $rdata['msg']="Check for Unique values";
				}
				else 
				{
				   
                   $rdata['msg']=$valid_message;	
                }			
				
				echo $aobj_context->mobj_output->ToJSONEnvelope($rdata,-1,"Failure");
				return 	$rdata;
			 }
		 }
		  $last_inserted_id = $internal_code;
		  
		  // Update the Old Value to Table
		/* $update_created_by_values="update {$table_name} set 
						created_by='{$old_data_arr[created_by]}',
						created_date='{$old_data_arr[created_date]}' where internal_code={$internal_code}";
			 $lobj_update = $aobj_context->mobj_db->Execute($update_created_by_values);			 
		 
		 // To update the Non visible field Values
		if($is_update_visible_field)
		{
			$select_db_field="select db_field from s_user_schema_elements where 
				ref_s_user_schema_code={$schema_id}
				  and is_visible=0";
			$lobj_select_db_field = $aobj_context->mobj_db->GetAll($select_db_field); 
			$update_cond="";
			foreach($lobj_select_db_field  as $ku=>$kv)
			{
			$update_cond.="{$kv[db_field]}='{$old_data_arr[$kv[db_field]]}',";
			}
		 $update_cond=substr($update_cond,0,strlen($update_cond)-1);
		 $update_old_val="update module_{$schema_id} set 
					{$update_cond} where internal_code={$internal_code}";
		$lobj_update_old_val = $aobj_context->mobj_db->Execute($update_old_val); 			
		 }*/
		 if(!empty($auto_increment_value))
		 {
		 $loop_count=explode("~",$auto_increment_value);
		 for($i=0;$i<count($loop_count)-1;$i++)
		 {
		 $last_no=0;
		 $ref_module_element_code_arr=explode("ele_value",$loop_count[$i]);
		 $ref_module_element_code=$ref_module_element_code_arr[0];
		 $index_val=strripos($ref_module_element_code_arr[1],"-");
		 $last_no=(substr($ref_module_element_code_arr[1],$index_val+1));
		 $prefix=(substr($ref_module_element_code_arr[1],0,$index_val));
		
		 $update_last_no="update transaction_series_prefix set last_no={$last_no} 
		 where ref_module_element_code={$ref_module_element_code} and ref_module_code={$schema_id}
		 and {$last_no}>last_no and prefix = '{$prefix}' ";
		 
		 
		 //echo $update_last_no;
		  $update_last_no = $aobj_context->mobj_db->Execute($update_last_no);
		 }
		 }
	 				
			 if($lobj_rs)
		    {
			$del_qry="delete from $table_name where ref_s_module_code=".$internal_code;
			 $lobj_del = $aobj_context->mobj_db->Execute($del_qry); 
			
			for( $i = 0; $i < count($larr_schema_data); $i++ )
					{
					$get_module_code="select ref_module_code from s_user_schema_elements where
					internal_code=".addslashes($larr_schema_data[$i]->ref_s_user_schema_elements_code);
					$lobj_ele_code = $aobj_context->mobj_db->GetRow($get_module_code);
					 
						$insert_qry="insert into {$table_name} (
				       ref_s_user_schema_code,
				       ref_s_module_code,
				       ref_s_user_schema_group_code,
				       ref_s_user_schema_elements_code,
				       value,ref_ref_s_user_schema_code,
					   created_by,
					created_date,
					modified_by,
					modified_date
					   
				       )
				        values ( '".addslashes($larr_schema_data[$i]->ref_s_user_schema_code)  ."','"
				                   .  $internal_code  ."','"
				                   .   addslashes($larr_schema_data[$i]->ref_s_user_schema_group_code)  ."','"
				                   .   addslashes($larr_schema_data[$i]->ref_s_user_schema_elements_code)  ."','"
				                   .   addslashes($larr_schema_data[$i]->value)  ."',".$lobj_ele_code[ref_module_code].",
								 ".$_SESSION['user_id'].",now(),".$_SESSION['user_id'].",now()  
								)";
							   $lobj_rs1 = $aobj_context->mobj_db->Execute($insert_qry);
				 	   
					}
			 
			   
			
		     }
			#######################To Inser tMultiple Group Details STARTS#############################
		 $select_distinct_group_names="select internal_code,name,
								concat('user_schema_',ref_s_user_schema_code,'_entry_',internal_code) as entry_table_name,
								concat('user_schema_',ref_s_user_schema_code,'_group_',internal_code) as group_table_name
								from s_user_schema_group 
								where ref_s_user_schema_code={$schema_id} 
								and ifnull(parent_group_code,0)=0 
								and multiple=1";
		 $lobj_select_distinct_group_names = $aobj_context->mobj_db->GetAll($select_distinct_group_names);
 	 
		foreach( $lobj_select_distinct_group_names as $gk=>$gv)
		{
			$group_id=$gv['internal_code'];
			$entry_table_name=$gv['entry_table_name'];
			$group_table_name=$gv['group_table_name'];
			 $get_delete_p_ids_of_child="select group_concat(internal_code) as code
									from {$group_table_name} 
									where ref_s_ref_data_code={$internal_code} ";
			$lobj_get_delete_ids_of_child =  $aobj_context->mobj_db->GetRow($get_delete_p_ids_of_child);
			$p_ref_code=$lobj_get_delete_ids_of_child[code];
			 // Delete From Parent Group
			$delete_from_group_table="delete from {$group_table_name} 
				where ref_s_ref_data_code={$internal_code}";
			$lobj_delete_from_group_table =  $aobj_context->mobj_db->Execute($delete_from_group_table);
			 
			 $get_child_groups="select internal_code,
							concat('user_schema_',ref_s_user_schema_code,'_entry_',internal_code) as child_entry_table,
							concat('user_schema_',ref_s_user_schema_code,'_group_',internal_code) as child_group_table_name
							from s_user_schema_group
							where parent_group_code={$group_id}";
			 $lobj_get_child_groups =  $aobj_context->mobj_db->GetAll($get_child_groups);
			 
			$desc_table="desc {$entry_table_name}";
			 $lobj_desc_table = $aobj_context->mobj_db->GetAll($desc_table);
			 $elements='';
			 foreach($lobj_desc_table as $d=>$dv)
			 {
			 	if($dv['Field']!='login_cnt' && $dv['Field']!='internal_code' &&  $dv['Field']!='ref_s_ref_data_code'&&  $dv['Field']!='group_int_code')
				{
				 $elements.=$dv['Field'].',';
				}
			 }
			$elements=substr($elements,0,strlen($elements)-1);
			 ########################### to Map the CHILD Group Table  Values STARTS #######################################
		 	
			if($lobj_get_child_groups)
			{
				$get_limit_cnt="select * from {$entry_table_name}
								where login_cnt={$login_cnt}";
				 $lobj_get_limit_cnt = $aobj_context->mobj_db->getAll($get_limit_cnt);
				   for($s=0;$s<count($lobj_get_child_groups);$s++)
					{
					$child_group_table_name=$lobj_get_child_groups[$s][child_group_table_name];
					$delete_from_child_group_table="delete from {$child_group_table_name} 
											where   
											 ref_s_ref_data_code in({$p_ref_code})";
					 $obj_delete_from_child_group_table =  $aobj_context->mobj_db->Execute($delete_from_child_group_table);
					  
					}  
					 
				for($limit=0;$limit<count($lobj_get_limit_cnt);$limit++)
				{
				$insert_into_group_table="insert into {$group_table_name} 
									(internal_code,ref_s_ref_data_code,{$elements})
									select group_int_code,{$internal_code},{$elements} from {$entry_table_name}
									where login_cnt={$login_cnt}  and
									internal_code={$lobj_get_limit_cnt[$limit][internal_code]}";
				$group_insrt=$aobj_context->mobj_db->Execute($insert_into_group_table);
				
				
				//$last_inserted_parent_group_id = $aobj_context->mobj_db->Insert_ID();
				
				$get_last_insert_id_qry= "select group_int_code from {$entry_table_name}
									where login_cnt={$login_cnt}  and
									internal_code={$lobj_get_limit_cnt[$limit][internal_code]}";
				 $obj_get_last_insert_id_qry=$aobj_context->mobj_db->GetRow($get_last_insert_id_qry);
				 $last_inserted_parent_group_id= $obj_get_last_insert_id_qry[group_int_code];
				 if(empty($last_inserted_parent_group_id))
				 {
					$get_max_int_code="select max(internal_code) as max_code from {$group_table_name} where  created_by=".$_SESSION['user_id'];
					$obj_get_last_insert_id_qry=$aobj_context->mobj_db->GetRow($get_max_int_code);	
					$last_inserted_parent_group_id= $obj_get_last_insert_id_qry[max_code];
				 }
				 
				 
					for($j=0;$j<count($lobj_get_child_groups);$j++)
					{
									$child_entry_table=$lobj_get_child_groups[$j][child_entry_table];
									$child_group_table_name=$lobj_get_child_groups[$j][child_group_table_name];
									$desc_table="desc {$child_entry_table}";
									 $lobj_desc_table = $aobj_context->mobj_db->GetAll($desc_table);
									 $child_elements='';
									 foreach($lobj_desc_table as $d=>$dv)
									 {
										if($dv['Field']!='login_cnt' && $dv['Field']!='internal_code' &&  $dv['Field']!='ref_s_ref_data_code'&&  $dv['Field']!='group_int_code')
										{
										 $child_elements.=$dv['Field'].',';
										}
									 }
							$child_elements=substr($child_elements,0,strlen($child_elements)-1);
							  
							$insert_into_child_group_table="insert into {$child_group_table_name} 
									(internal_code,ref_s_ref_data_code,{$child_elements})
									select group_int_code,{$last_inserted_parent_group_id},{$child_elements} 
									from {$child_entry_table}
									where login_cnt={$login_cnt}
									and ref_s_ref_data_code= {$lobj_get_limit_cnt[$limit][internal_code] }";
							$group_insrt=$aobj_context->mobj_db->Execute($insert_into_child_group_table);	
					  
					}
					//die();
				 	}	
				for($d=0;$d<count($lobj_get_child_groups);$d++)
					{
					$del_child_entry_table=$lobj_get_child_groups[$d][child_entry_table];
					$delete_from_child_group_table="delete from {$del_child_entry_table} 
					where login_cnt={$login_cnt}";
					$delete_from_child_group_table =  $aobj_context->mobj_db->Execute($delete_from_child_group_table);
					}
			}	
			 
		
			 ########################### to Map the CHILD Group Table  Values ENDS #######################################
			else
			{
			$insert_into_group_table="insert into {$group_table_name} 
									(internal_code,ref_s_ref_data_code,{$elements})
									select group_int_code,{$internal_code},{$elements} from {$entry_table_name}
									where login_cnt={$login_cnt}";
			$group_insrt=$aobj_context->mobj_db->Execute($insert_into_group_table);
			UpldateGroupImagesFromEntryToGroupTable($aobj_context,$login_cnt,$last_inserted_id,$schema_id,$group_id,$group_table_name,$entry_table_name);
			}
			$delete_from_entry_group_table="delete from {$entry_table_name} where login_cnt={$login_cnt}";
			$lobj_delete_from_entry_group_table =  $aobj_context->mobj_db->Execute($delete_from_entry_group_table);
		}
			#######################To Inser tMultiple Group Details ENDS#############################
			  if (function_exists('postSave'))
					{
				 		$post_data =postSave($aobj_context,$internal_code,$internal_code); 
					}
			if($lobj_rs)
			 {
			 $_SESSION['g_file_id'] ='';
			   $rdata['last_id'] = $internal_code;
		        $rdata['msg']= $rdata['msg'] = (!empty($post_data))?$post_data:"Updated  Successfully.";
				 
		        print_r($aobj_context->mobj_output->ToJSONEnvelope($rdata,0,"success"));
		        return;
			 }
		    else
		    {
			    $_SESSION['g_file_id'] ='';
		        $rdata['msg'] = "Udatation Failed.";
		        print_r($aobj_context->mobj_output->ToJSONEnvelope($rdata,-1,"Failure"));      
		     } 
	}		 
}
function GetSchemaModuleDetailsForUpdate($aobj_context)
  {
	include_once("format.php");
    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);  
	$internal_code = $aobj_context->mobj_data["internal_code"]; 	
	$table_name = $aobj_context->mobj_data["table_name"]; 	
	$login_cnt = $aobj_context->mobj_data["login_cnt"]; 	
	$field_name = $aobj_context->mobj_data["field_name"]; 
	$field_name_spl1	=  explode(',',$field_name);
	$schema_id_explode=explode("_",$table_name);
	$schema_id=$schema_id_explode[1];
		$get_data_types="select s.internal_code,value from s_user_schema_elements s inner join s_ref_type t 
					on t.internal_code=data_type where s.ref_s_user_schema_code={$schema_id}";
		$lobj_get_data_types = $aobj_context->mobj_db->GetAll($get_data_types); 				
		$data_type = array();
	  for($k=0;$k<count($lobj_get_data_types);$k++)
		{
		$data_type[$lobj_get_data_types[$k]['internal_code']]=$lobj_get_data_types[$k][value];
		}
		$get_meta_data_qry="select * from {$table_name} where internal_code={$internal_code}";
	    $lobj_meta_data_qry = $aobj_context->mobj_db->GetRow($get_meta_data_qry); 
		 $created_by_id=$lobj_meta_data_qry[created_by];
		$l=0;
		$meta_arr='';
		foreach($lobj_meta_data_qry as $key=>$value)
		{		
			if($key!='internal_code' && $key !='ref_s_user_schema_code' && $key !='created_by' && $key !='created_date'&& $key !='modified_by' && $key !='modified_date')
			{
				$ele_code=explode('_ele_',$key);
				//to get the ref_data details
				$s_ref_data="select ref_module_code from 
				s_user_schema_elements where internal_code={$ele_code[1]} 
				and ref_module_code>0";
				$s_ref_data_obj=$aobj_context->mobj_db->GetRow($s_ref_data); 
				 
			 if(!empty($s_ref_data_obj))
			 {
			 $get_col_names_old=" select concat('module_',schema_id) as table_name,GROUP_CONCAT(name SEPARATOR '-ele-') as name,GROUP_CONCAT(field_name) as field_name from 
				(select * from 
				(select s.internal_code as schema_id,su.name,ifnull(ug.name,'0None') as group_name,concat('m_',s.internal_code,'_ele_',su.internal_code) as field_name,
				ifnull(multiple,0) as multiple from s_user_schema s inner join s_user_schema_elements su on 
				su.ref_s_user_schema_code=s.internal_code
				inner join s_ref_type t on t.internal_code=su.s_ref_type_meta
				left join s_user_schema_group ug on ug.internal_code=su.ref_s_user_schema_group_code
				 where s.internal_code={$s_ref_data_obj[ref_module_code]} 
				 and t.value='Meta' order by ug.sequence,su.sequence,su.internal_code ) l where    multiple=0 limit 2) a group by schema_id";
				 
				 $get_col_names="select table_name,field_name AS name,
				 case when field_id2=''
				then field_id1 else concat(field_id1,',',field_id2)  end as field_name
				from module_meta_columns where module_id={$s_ref_data_obj[ref_module_code]}";
				 
				  $s_get_col_names_obj=$aobj_context->mobj_db->GetRow($get_col_names); 
				 
				$pos = strpos($s_get_col_names_obj['field_name'], ',');
				if($pos===false)
				{
				$ref_data_qry="select {$s_get_col_names_obj['field_name']}  as s_ref_value from {$s_get_col_names_obj[table_name]} where internal_code={$value}";
				}
				else {
				$field_name_spl	=  explode(',',$s_get_col_names_obj['field_name']);
				$ref_data_qry="select concat({$field_name_spl[0]},'-',{$field_name_spl[1]}) as
						s_ref_value from {$s_get_col_names_obj[table_name]} where internal_code={$value}";
				}
			 $s_ref_data_qry_obj=$aobj_context->mobj_db->GetRow($ref_data_qry); 
			 
				if(!empty($s_ref_data_qry_obj))
				{
				$field="txt_ref_schema_".$ele_code[1];
//				$field_value=$value;
				$pos = strpos($value,"-");
				if( $pos === false  )
				{
					$field_value=$value;
				}
				else
				{ 
					$arr = explode('-',$value);
					if(count($arr) == 3)
					{
						if( strlen($arr[0]) ==  4 && strlen($arr[1]) == 2 && strlen($arr[2]) == 2)
						{	 
						$field_value=$arr[2] . "/".$arr[1]."/".$arr[0];
						}
						else
						$field_value=$value;
					} 
					//$str.="<td ".$style."'>".$value1."</td>";
					else
						$field_value=$value;					
				}
				$meta_arr[$l][id]=$field; 
				$meta_arr[$l][data]=$s_ref_data_qry_obj['s_ref_value']; 
				$meta_arr[$l][flag]=1; 
				$meta_arr[$l][hidden_id]="details_".$ele_code[1]; 
				$meta_arr[$l][hidden_value]=$field_value;  
				$l++;
				}
			 }
			 else
				 {
				$field="details_".$ele_code[1];
				if($data_type[$ele_code[1]]=='Date')
					$field_value=ui_date_Format($value);
				else if($data_type[$ele_code[1]]=='Month')
					$field_value=ui_month_format($value);
				else 
				$field_value=$value;	
				$meta_arr[$l][id]=$field; 
				$meta_arr[$l][data]=$field_value; 
				$meta_arr[$l][flag]=0; 
				$meta_arr[$l][hidden_id]=""; 
				$meta_arr[$l][hidden_value]=""; 
				$l++;
				}
			}		
		}
		$edit_right=getEditRights($aobj_context,$schema_id, $created_by_id);
		
					 
		//var_dump($meta_arr);
		//take all the s_ref data table name where type=Users
 
	  $details_qry="select
				 suse.ref_module_code,
				 if(ref_ref_s_user_schema_code>0,concat('txt_ref_schema_',ref_s_user_schema_elements_code),
				concat('details_',ref_s_user_schema_elements_code)) as id, 
				ref_s_user_schema_elements_code,
				value,
				if(ref_ref_s_user_schema_code>0,1,0) as flag
				from {$table_name}_details m 
				inner join  s_user_schema_elements suse on suse.internal_code=m.ref_s_user_schema_elements_code
				where ref_s_module_code=  {$internal_code}";
		 
	$lobj_details=  $aobj_context->mobj_db->GetAll($details_qry);  
	 
	//ref display starts,'
	$s_id=explode('_',$table_name);
	$schema_id_ref=$s_id[1];
	$s_ref_display=GetRefDisplayArray($aobj_context,$schema_id_ref,0,$internal_code);
		 
			//####### FOR GROUP ENTRY STARTS
		//$login_cnt	
		$select_distinct_group_names="select internal_code,name,
								concat('user_schema_',ref_s_user_schema_code,'_entry_',internal_code) as entry_table_name,
								concat('user_schema_',ref_s_user_schema_code,'_group_',internal_code) as group_table_name
								from s_user_schema_group 
								where ref_s_user_schema_code={$schema_id}
								and multiple=1 
								and ifnull(parent_group_code,0)=0";
		 $lobj_select_distinct_group_names = $aobj_context->mobj_db->GetAll($select_distinct_group_names);
 	 $b=0;
	  
		foreach( $lobj_select_distinct_group_names as $gk=>$gv)
		{
			$entry_table_name=$gv['entry_table_name'];
			$group_table_name=$gv['group_table_name'];
			$group_id=$gv['internal_code'];
			$delete_entry_table_name="delete from {$entry_table_name} where login_cnt={$login_cnt}";
			$lobj_delete_from_group_table =  $aobj_context->mobj_db->Execute($delete_entry_table_name);
			$get_child_groups="select internal_code,
						concat('user_schema_',ref_s_user_schema_code,'_entry_',internal_code) as child_entry_table,
						concat('user_schema_',ref_s_user_schema_code,'_group_',internal_code) as child_group_table 
						from s_user_schema_group
					where parent_group_code={$group_id} and multiple=1 ";
					 $lobj_get_child_groups =  $aobj_context->mobj_db->GetAll($get_child_groups);
					 $desc_table="desc {$entry_table_name}";
						$lobj_desc_table = $aobj_context->mobj_db->GetAll($desc_table);
						$elements='';
						foreach($lobj_desc_table as $d=>$dv)
						{
							if($dv['Field']!='login_cnt' && $dv['Field']!='internal_code'&& $dv['Field']!='group_int_code')
							{
							$elements.=$dv['Field'].',';
							}
						}
						$elements=substr($elements,0,strlen($elements)-1);
					 
			
			 ### Child Data####
				
					 if($lobj_get_child_groups)
					 {
						 for($d=0;$d<count($lobj_get_child_groups);$d++)
						{
						$del_child_entry_table=$lobj_get_child_groups[$d][child_entry_table];
						$delete_from_child_group_table="delete from {$del_child_entry_table} where login_cnt={$login_cnt}";
						$delete_from_child_group_table =  $aobj_context->mobj_db->Execute($delete_from_child_group_table);
						}
					 	$get_parent_data="select * from {$group_table_name}
								where ref_s_ref_data_code={$internal_code}";
								$lobj_get_parent_data = $aobj_context->mobj_db->GetAll($get_parent_data);
						for($limit=0;$limit<count($lobj_get_parent_data);$limit++)
						{
							$insert_into_parent_entry_table="insert into {$entry_table_name} 
							(internal_code,login_cnt,group_int_code,{$elements})
							select 0,{$login_cnt},internal_code,{$elements} from {$group_table_name}
							where ref_s_ref_data_code={$internal_code}
							and internal_code={$lobj_get_parent_data[$limit][internal_code]} ";
							$group_insrt=$aobj_context->mobj_db->Execute($insert_into_parent_entry_table);
							 
							$last_parent_id = $aobj_context->mobj_db->Insert_ID();
									
							for($j=0;$j<count($lobj_get_child_groups);$j++)
							{
									$child_group_id=$lobj_get_child_groups[$j][internal_code];
									$child_entry_table=$lobj_get_child_groups[$j][child_entry_table];
									$child_group_table=$lobj_get_child_groups[$j][child_group_table];
									$desc_table="desc {$child_entry_table}";
									$lobj_desc_table = $aobj_context->mobj_db->GetAll($desc_table);
									$child_elements='';
									foreach($lobj_desc_table as $d=>$dv)
									{
										if($dv['Field']!='login_cnt' && $dv['Field']!='internal_code' && $dv['Field']!='group_int_code' &&    $dv['Field']!='ref_s_ref_data_code')
										{
										$child_elements.=$dv['Field'].',';
										}
									}
									$child_elements=substr($child_elements,0,strlen($child_elements)-1);
									$insert_into_child_entry_table="insert into {$child_entry_table} 
									(internal_code,login_cnt,group_int_code,ref_s_ref_data_code,{$child_elements})
									select 0,{$login_cnt},internal_code,{$last_parent_id},{$child_elements} 
									from {$child_group_table}
									where ref_s_ref_data_code={$lobj_get_parent_data[$limit][internal_code]}
									 ";
									$group_insrt=$aobj_context->mobj_db->Execute($insert_into_child_entry_table);
									 
									
									
								
							}
						}
						$group_details[$b]['table_name']= $group_table_name;
								$group_details[$b]['s_ref_data_code']= $internal_code;
								$group_details[$b]['group_id']= $group_id;
								$group_details[$b]['main_group_grid']= "main_group_grid_".$group_id;
								$b++;	
					 }
					 else
					 {
					 
						$insert_into_entry_table="insert into {$entry_table_name} 
						(internal_code,login_cnt,group_int_code,{$elements})
						select 0,{$login_cnt},internal_code,{$elements} from {$group_table_name}
						where ref_s_ref_data_code={$internal_code}";
						$group_insrt=$aobj_context->mobj_db->Execute($insert_into_entry_table);
						$group_details[$b]['table_name']= $group_table_name;
						$group_details[$b]['s_ref_data_code']= $internal_code;
						$group_details[$b]['group_id']= $group_id;
						$group_details[$b]['main_group_grid']= "main_group_grid_".$group_id;
						 $b++;
					 }
			
		
		}
		  //print_r($group_details);
	// ###### GROUP ENTRY ENDS
	 
	$data['internal_code']=$internal_code;
	$data['meta_arr']=$meta_arr;
	$data['details']=$lobj_details;
    $data['s_ref_display']=$s_ref_display;     
    $data['group_details']=$group_details;     
    $data['edit_right']=$edit_right;     
  
    if( ($meta_arr))
      echo $aobj_context->mobj_output->ToJSONEnvelope($data,0,"success"); 
    else
      echo $aobj_context->mobj_output->ToJSONEnvelope($data,-1,"Failure");
    return;
  }
function getEditRights($aobj_context,$schema_id, $created_by_id)
{
			$edit_right=1;
			session_start();
				$user_type=$_SESSION['user_type'];
				$user_id=$_SESSION['user_id'];
				$user_dept=$_SESSION['user_dept'];
				if(strtolower($user_type)!="admin")
				{
				$get_rights="select   is_edit,is_edit_group_records,is_edit_dep_records,is_edit_all_records
						from user_rights where ref_s_sysdb_code='{$user_id}'
						and module_id='{$schema_id}'";
					 
					 $obj_get_rights = $aobj_context->mobj_db->GetRow($get_rights);	
					 $own=$obj_get_rights[is_edit];
					 $group=$obj_get_rights[is_edit_group_records];
					 $dep=$obj_get_rights[is_edit_dep_records];
					 $all=$obj_get_rights[is_edit_all_records];
					if(strtolower($user_type)=='admin' || $all=='1')
					{
					$edit_right=1;
					}
					else if($dep=='1')//checks for Department Records Only
					 {
							$get_users_of_department="select GROUP_CONCAT(ref_user_code) as dep_users
								from dept_user_mapping where ref_dept_code={$user_dept}
								and ref_user_code={$created_by_id}";
							 $obj_u_dep = $aobj_context->mobj_db->GetRow($get_users_of_department);	
							  if($obj_u_dep)
							  	$edit_right=1;else $edit_right=0;						  
					}
					else if($own=='0')
					{
					$edit_right=0;
					}
					else if($own=='1' && $dep=='0') //checks for Own Records Only
					 {
						if($created_by_id==$user_id)
						$edit_right=1;
						else $edit_right=0;
						 $get_own_created_by_id="select created_by from {$table} 
											where internal_code in ({$lstr_ids})";
						 $obj_get_own_created_by_id = $aobj_context->mobj_db->GetRow($get_own_created_by_id);
					}
						 
				}
return $edit_right;				
}
function getFieldName($aobj_context)
{
		$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
		$internal_code = $aobj_context->mobj_data["internal_code"];
		$table_id = $aobj_context->mobj_data["table_id"];
		$table = "module_{$table_id}";
		$field_name = $aobj_context->mobj_data["field_name"];
		$schema_id = $aobj_context->mobj_data["schema_id"];
		$inputboxid = $aobj_context->mobj_data["inputboxid"];
		//to get the ref_display_names Starts
		$get_ref_display_table="select s.value,su.internal_code as field_id,
			ifnull(ref.ref_module_code,0) as ref_module_code,mmc.field_id as mmc_field_id,
			if(s.value='Meta',concat('module_',ref.ref_s_user_schema_code),
			concat('module_',ref.ref_s_user_schema_code,'_details')) as table_name,
			if(s.value='Meta',concat('m_',ref.ref_s_user_schema_code,'_ele_',su.source_field),
			su.source_field) as field_name
			,su.source_field from s_user_schema_elements su 
			inner join s_user_schema_elements ref on ref.internal_code=su.source_field
			inner join s_ref_type s on s.internal_code=ref.s_ref_type_meta
			left join module_meta_columns mmc on mmc.module_id=ref.ref_module_code
			where su.ref_s_user_schema_code={$schema_id} and su.linked_field={$inputboxid}";
			
		$lobj_get_ref_display_table = $aobj_context->mobj_db->GetAll($get_ref_display_table);
		//echo $get_ref_display_table;
		$s_ref_display[]='';
		for($k=0;$k<count($lobj_get_ref_display_table);$k++)
		{		
			if($lobj_get_ref_display_table[$k][value]=='Meta')
			{
				if($lobj_get_ref_display_table[$k][ref_module_code]=='0')
				{
				$get_ref_display_val="select internal_code,".$lobj_get_ref_display_table[$k][field_name]." as value,
				concat('details_','".$lobj_get_ref_display_table[$k][field_id]."') as field_id from ".$lobj_get_ref_display_table[$k][table_name]." where internal_code={$internal_code}";
				$lobj_get_ref_display_val = $aobj_context->mobj_db->GetRow($get_ref_display_val);
				$s_ref_display[$k]['value']=$lobj_get_ref_display_val['value'];
				$s_ref_display[$k]['field_id']=$lobj_get_ref_display_val['field_id'];
				}
				else
				{
				$base_table=$lobj_get_ref_display_table[$k][table_name];
				$join_table="module_".$lobj_get_ref_display_table[$k][ref_module_code];
				$get_ref_display_val="select 
					b.internal_code,
					concat({$lobj_get_ref_display_table[$k][mmc_field_id]}) as value,
				concat('details_','{$lobj_get_ref_display_table[$k][field_id]}') as field_id 
				from {$base_table} b
				left join {$join_table} j on b.{$lobj_get_ref_display_table[$k][field_name]}=j.internal_code
				where b.internal_code={$internal_code}";
				$lobj_get_ref_display_val = $aobj_context->mobj_db->GetRow($get_ref_display_val);
				//echo $get_ref_display_val;
				$s_ref_display[$k]['value']=$lobj_get_ref_display_val['value'];
				$s_ref_display[$k]['field_id']=$lobj_get_ref_display_val['field_id'];
				}
			 
		 	}
		}// ref_display_names Ends
		
		// OLD LOGIC
		/* $pos = strpos($field_name, ',');
		if($pos===false)
		{
		$get_data="select internal_code,{$field_name}  as name 
		from {$table}  where internal_code='".$internal_code."'";
		}
		else
		{
		$field_value= explode(',',$field_name);					 
		$get_data="select internal_code,concat({$field_value[0]},'-',{$field_value[1]})  as name 
		from {$table}  where internal_code='".$internal_code."'";
		} */	
		$get_key_fields="select field_id from module_meta_columns where table_name='{$table}'";
		$lobj_get_key_fields = $aobj_context->mobj_db->GetRow($get_key_fields);
		$get_data="select internal_code,concat({$lobj_get_key_fields[field_id]})  as name 
		from {$table}  where internal_code='{$internal_code}'";
		$lobj_rs = $aobj_context->mobj_db->GetRow($get_data);
	 //echo $get_data;
		$arr[0]=$lobj_rs;
		$arr[1]=$s_ref_display;
		$select_on_exit_event="select ifnull(on_exit,0) as on_exit
					from s_user_schema_elements where internal_code={$inputboxid}";
		$lobj_select_on_exit_event = $aobj_context->mobj_db->GetRow($select_on_exit_event);
		$arr['on_exit_event']=$lobj_select_on_exit_event;
		 
		if(count($lobj_rs) > 0)
		echo $aobj_context->mobj_output->ToJSONEnvelope($arr,0,"success");
		else
		echo $aobj_context->mobj_output->ToJSONEnvelope($arr,-1,"Failure");
		return;
}
function getGroupRefDetails($aobj_context)
{
$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
		$name = $aobj_context->mobj_data["name"];
		$ele_id = $aobj_context->mobj_data["ele_id"];
		$field_id = $aobj_context->mobj_data["field_id"];
		  
		$get_schema_id="select  ref_module_code,ifnull(on_exit,'') as on_exit 
		from s_user_schema_elements where internal_code = {$ele_id}";
	 	$lobj_rs = $aobj_context->mobj_db->GetRow($get_schema_id);
		$on_exit_function=$lobj_rs['on_exit'];
		$get_key_field="select table_name,field_id,field_id1,field_id2 from module_meta_columns
							where module_id={$lobj_rs[ref_module_code]}";
			$lobj_get_key_field = $aobj_context->mobj_db->GetRow($get_key_field);				
			 $search_field_id=	$lobj_get_key_field[field_id];
			 $table_name=	$lobj_get_key_field[table_name];
			 $field_id1=	$lobj_get_key_field[field_id1];
			 $field_id2=	$lobj_get_key_field[field_id2];
			 if(!empty($field_id2))
			 $field_2_cond=" or {$field_id1} like '%{$field_id2}%' ";
			
			$get_row_value="select '{$field_id}'  as field_id,internal_code,'{$on_exit_function}' as on_exit
				  from {$table_name}
			where (concat({$search_field_id}) like '%{$name}%' {$field_2_cond}) limit 1";
			$lobj_get_row_value = $aobj_context->mobj_db->GetRow($get_row_value);
 			 echo $get_row_value;
		if(count($lobj_get_row_value) > 0)
		echo $aobj_context->mobj_output->ToJSONEnvelope($lobj_get_row_value,0,"success");
		else
		echo $aobj_context->mobj_output->ToJSONEnvelope($lobj_get_row_value,-1,"Failure");
		return;
}
function getReferenceNameForLovForIntCode($aobj_context)
{
$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
		$ele_id = $aobj_context->mobj_data["ele_id"];
		$int_code = $aobj_context->mobj_data["int_code"];
		$get_schema_id="select  ref_module_code 
		from s_user_schema_elements where internal_code = {$ele_id}";
	 	$lobj_rs = $aobj_context->mobj_db->GetRow($get_schema_id);
		$get_key_field="select table_name,field_id,field_id1,field_id2 from module_meta_columns
							where module_id={$lobj_rs[ref_module_code]}";
			$lobj_get_key_field = $aobj_context->mobj_db->GetRow($get_key_field);				
			 $search_field_id=	$lobj_get_key_field[field_id];
			 $table_name=	$lobj_get_key_field[table_name];
			 $field_id1=	$lobj_get_key_field[field_id1];
			 $field_id2=	$lobj_get_key_field[field_id2];
			
			$get_row_value="select concat({$search_field_id}) as value,internal_code
				  from {$table_name}
			where internal_code={$int_code} limit 1";
			$lobj_get_row_value = $aobj_context->mobj_db->GetRow($get_row_value);
			// echo $get_row_value;
			
		if(count($lobj_get_row_value) > 0)
		echo $aobj_context->mobj_output->ToJSONEnvelope($lobj_get_row_value,0,"success");
		else
		echo $aobj_context->mobj_output->ToJSONEnvelope($lobj_get_row_value,-1,"Failure");
		return;
}
function getEmpMultipleGroupDetails($aobj_context)
{
	$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
			$table_name = $aobj_context->mobj_data["table_name"];	
			$ref_s_ref_data_code = $aobj_context->mobj_data["ref_s_ref_data_code"];
			$str_desc = "desc $table_name";
			$obj_desc=$aobj_context->mobj_db->GetAll($str_desc);
			//popudating insert command
			$insert_qry="insert into $table_name(ref_s_user_schema_code,ref_s_user_schema_group_code,ref_s_ref_data_code,";
			$id='';
			// starts from -4 since we are eeminating first 4 fields
			$i=-5;
						foreach ($obj_desc as $key=>$value)
						{
							if($value['Field']!='internal_code' && $value['Field']!='ref_s_user_schema_code' &&
							$value['Field']!='ref_s_user_schema_group_code' && $value['Field']!='ref_s_ref_data_code' &&
							$value['Field']!='login_cnt' &&
							$value['Field']!='created_by' && $value['Field']!='created_date' &&
							$value['Field']!='modified_by' && $value['Field']!='modified_date')
							{
							$ele_id=explode('ele_',$value['Field']);
							 
							$id.=$ele_id[1].",";
							$insert_qry.=$value['Field'].",";
							}
							$i++;
						}
				 
					$insert_qry.="created_by,created_date,modified_by,modified_date) values (";
					
					$schema_code=explode('user_schema_',$table_name);
					$all_code=explode('_group_',$schema_code[1]);
					$insert_qry.=$all_code[0].",".$all_code[1].",".$ref_s_ref_data_code.",";
					$id= substr($id,0,strlen($id) - 1);
					$get_name="select su.internal_code,name,value as Type,is_read_only,default_value,optional,su.ref_module_code,list_of_values,linked_field,source_field,suffix,width*9 as width from 
						s_user_schema_elements su left join  s_ref_type s on s.internal_code=su.data_type where su.internal_code in (".$id.") order by su.internal_code";
			  // echo $get_name;
			 $lobj_get_name =  $aobj_context->mobj_db->GetAll($get_name);
			//sends Table Name for div id
			$arr['table_name'] = $table_name;
			$arr['insert_qry'] = $insert_qry;
			$arr['elements'] = $lobj_get_name;
			$arr['ref_s_ref_data_code'] = $ref_s_ref_data_code;
			if($lobj_rs) 
		{
		
            echo $aobj_context->mobj_output->ToJSONEnvelope($arr,0,"success"); 
		
		}
		 else
		{	
            echo $aobj_context->mobj_output->ToJSONEnvelope($arr,-1,"Failure");
		
		}
}
function getGroupDetailsRowData($aobj_context)
{
	$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
	$internal_code = $aobj_context->mobj_data["internal_code"];	 
	$group_table_name = $aobj_context->mobj_data["table_name"];
	 $group_table_name=str_ireplace("group","entry",$group_table_name);
	session_start();
	$login_cnt=	$_SESSION['login_cnt'];
	$split_data=explode("user_schema_",$group_table_name);
	$table_data=explode('_entry_',$split_data[1]);
	$schema_id=$table_data[0];
	$group_id=$table_data[1]; 
	 /* $aobj_context->mobj_db->execute("SET @@session.group_concat_max_len=10000");
							$select_ref_module_code="select 
								GROUP_CONCAT(join_table_name SEPARATOR ' ') as qry from 
								(select  parent_group_code,ref_s_user_schema_group_code, @a:=@a+1,ref_module_code,
								concat(' left join module_',suse.ref_module_code, ' as module_',suse.ref_module_code,'_',suse.internal_code,' on module_',suse.ref_module_code,'_',suse.internal_code,'.internal_code =',suse.db_field) as join_table_name
								from s_user_schema_elements suse
								left join s_user_schema_group ug on ug.internal_code=suse.ref_s_user_schema_group_code
								where suse.ref_s_user_schema_code={$schema_id}  
								and (is_multiple_group=1 or parent_group_code<>0) 
								and (ref_s_user_schema_group_code={$group_id} or parent_group_code={$group_id}) 
								and ref_module_code>0)j";
							$obj_select_ref_module_code =  $aobj_context->mobj_db->GetRow($select_ref_module_code);
							 $join_qry=(count($obj_select_ref_module_code)>0)?$obj_select_ref_module_code[qry]:'';
							// to form the fields   dynamically 
							$select_field_ids="select 
								db_field as field_id,
								su.name,t.value as data_type,
								su.ref_module_code from 
								s_user_schema_elements	su
								inner join s_ref_type t on t.internal_code=su.data_type
								left join s_user_schema_group ug on ug.internal_code=su.ref_s_user_schema_group_code
								where su.ref_s_user_schema_code={$schema_id} and
								( su.is_multiple_group=1  or parent_group_code<>0)
								and (ug.internal_code={$group_id} or parent_group_code={$group_id}) 
								order by su.internal_code";
								 
							$lobj_select_field_ids =  $aobj_context->mobj_db->GetAll($select_field_ids);
 							foreach($lobj_select_field_ids as $key =>$value)
							{
								if($value[data_type]=='Reference Data')
								{
								$aobj_context->mobj_db->execute("SET @@session.group_concat_max_len=10000");
								$qry=" select concat('module_',schema_id) as table_name,GROUP_CONCAT(name SEPARATOR '-ele-') as name,
									GROUP_CONCAT(field_name separator ',\'-\',') as field_name from 
									(select * from 
									(select s.internal_code as schema_id,su.name,ifnull(multiple,0) as multiple,ifnull(ug.name,'0None') as group_name,concat('m_',s.internal_code,'_ele_',su.internal_code) as field_name from s_user_schema s inner join s_user_schema_elements su on 
									su.ref_s_user_schema_code=s.internal_code
									inner join s_ref_type t on t.internal_code=su.s_ref_type_meta
									left join s_user_schema_group ug on ug.internal_code=su.ref_s_user_schema_group_code
									where s.internal_code=$value[ref_module_code] and t.value='Meta' order by ug.sequence,su.sequence,su.internal_code ) l where   multiple=0 limit 2) a group by schema_id"; 
								 						 
									$lobj_rs = $aobj_context->mobj_db->GetRow($qry);
								$fields.="concat(".$lobj_rs[field_name].") as $value[field_id]".",";
								}
								else if($value[data_type]=='Date')
								{								 
								$fields.='DATE_FORMAT('.$value[field_id].",'%d/%m/%Y') as ".$value[field_id] .",";
								}
								else
								{
								$fields.=$value[field_id].",";
								}
							}
							$fields=substr($fields,0,strlen($fields)-1);
							$get_group_details="select '{$group_table_name}' as table_name, 
								$group_table_name.internal_code,$fields from 	$group_table_name  $join_qry  
							 where $group_table_name.internal_code= $internal_code  " */;
							 
							 $get_group_details_arr=getGroupDetailsQryForIntCode($aobj_context,$schema_id,$group_id,$internal_code);
							 $get_group_details=$get_group_details_arr['qry'];
							 $on_exit_arr=$get_group_details_arr['on_exit_arr'];
							$lobj_group_details=  $aobj_context->mobj_db->GetRow($get_group_details);
							 
							$get_child_groups="select internal_code as group_id,
							concat('user_schema_',ref_s_user_schema_code,'_group_',internal_code) as group_table_name,
							concat('user_schema_',ref_s_user_schema_code,'_entry_',internal_code) as entry_table_name,
							concat('main_group_grid_',internal_code) as main_group_grid_div 
							from s_user_schema_group
							where parent_group_code={$group_id}
							and multiple=1";
							$lobj_get_child_groups=  $aobj_context->mobj_db->GetAll($get_child_groups);
							 for($h=0;$h<count($lobj_get_child_groups);$h++)
							 {
							 $entry_table=$lobj_get_child_groups[$h][entry_table_name];
							 $delete_entry_table_with_0="delete from {$entry_table}
											where ref_s_ref_data_code=0
											and login_cnt={$login_cnt}";
							$lobj_delete_entry_table_with_0=  $aobj_context->mobj_db->Execute($delete_entry_table_with_0);				
							 }
			######### REF Display#########################
		$s_ref_display=GetRefDisplayArray($aobj_context,$schema_id,$group_id,$internal_code);
		 
		 	 $arr["parent_group"]=$lobj_group_details;
		 	 $arr["on_exit_arr"]=$on_exit_arr;
			 $arr["child_group"]=$lobj_get_child_groups;
			 $arr["s_ref_display"]=$s_ref_display;
			 
			echo $aobj_context->mobj_output->ToJSONEnvelope($arr,0,"success"); 
}
function getGroupDetailsQryForIntCode($aobj_context,$schema_id,$group_id,$internal_code)
{
$table_name="user_schema_{$schema_id}_entry_{$group_id}";
$aobj_context->mobj_db->execute("SET @@session.group_concat_max_len=10000");
$aobj_context->mobj_db->execute("set @a:=0");
 		$select_ref_module_code="select 
			GROUP_CONCAT(join_table_name SEPARATOR ' ') as qry from 
			(select  parent_group_code,ref_s_user_schema_group_code, @a:=@a+1,ref_module_code,
			concat(' left join module_',suse.ref_module_code, ' as module_',suse.ref_module_code,'_',suse.internal_code,' on module_',suse.ref_module_code,'_',suse.internal_code,'.internal_code =',suse.db_field) as join_table_name
			from s_user_schema_elements suse
			left join s_user_schema_group ug on ug.internal_code=suse.ref_s_user_schema_group_code
			where suse.ref_s_user_schema_code={$schema_id} 
			and (is_multiple_group=1 or parent_group_code<>0) 
			and ((ref_s_user_schema_group_code={$group_id} or (parent_group_code={$group_id} and multiple=0)) and is_grid_edit=0) 
			and ref_module_code>0)j";
		$obj_select_ref_module_code =  $aobj_context->mobj_db->GetRow($select_ref_module_code);
				
		$join_qry=(count($obj_select_ref_module_code)>0)?$obj_select_ref_module_code[qry]:'';
				
		$get_display_cols="select GROUP_CONCAT(field_name) as field_name,GROUP_CONCAT(on_exit_event) as on_exit_event  from 
								(select table_name,name,field_name as grid_field_name,
								if(data_type='date',concat('date_format(',field_name,',\"%d/%m/%Y\"',\") as \",field_name)
								,if((ref_module_code=0 or is_ref_module_code_lov=1),
								field_name ,concat('concat(',f1,f2,') as ',field_name,
								',',field_name,' as ref_',field_name
								)))	as field_name,
								if(ref_module_code<>0 and trim(on_exit)!=''  ,concat(ele_id,'->',on_exit),'') as on_exit_event,							 
								 ele_id,on_exit
								,ref_module_code from 
								(select  s.internal_code, su.internal_code as ele_id,ifnull(is_ref_module_code_lov,0) as is_ref_module_code_lov,
								ref_module_code,concat('module_',s.internal_code) as table_name ,
								field_id,typ.value as data_type,	
								if(ifnull(field_id1,'')!='',concat('module_',ref_module_code,'_',su.internal_code,'.',field_id1),'') as f1,
								if(ifnull(field_id2,'')!='',concat(',\'-\',module_',ref_module_code,'_',su.internal_code,'.',field_id2),'') as f2,
								LCASE(REPLACE(REPLACE(REPLACE(REPLACE(su.system_name,' ','_'),'.','_'),'&','_'),'/','_')) as name
								,ifnull(multiple,0) as multiple,ifnull(ug.name,'0None') as group_name,
								db_field as field_name ,on_exit
								from s_user_schema s inner join s_user_schema_elements su on 
								su.ref_s_user_schema_code=s.internal_code
								inner join s_ref_type t on t.internal_code=su.s_ref_type_meta
								inner join s_ref_type as typ on typ.internal_code=su.data_type
								left join s_user_schema_group ug on ug.internal_code=su.ref_s_user_schema_group_code
								left join module_meta_columns mmc on mmc.module_id=su.ref_module_code
								where s.internal_code={$schema_id} and t.value='Meta'  
								 
								and (ug.internal_code={$group_id} or (parent_group_code={$group_id} and multiple=0)) and is_grid_edit = '0'
								order by ifnull(ug.sequence,0),su.sequence,su.internal_code ) l 
								 
								 ) a ";
		$lobj_get_display_cols = $aobj_context->mobj_db->GetRow($get_display_cols);
		 $on_exit_event_arr=$lobj_get_display_cols[on_exit_event];
		 $on_exit_arr= array();		 
		 if(!empty($on_exit_event_arr))
		 {
			$comma_split=explode(",",$on_exit_event_arr);			 
			foreach($comma_split as $k=>$v)
			{
				$arr_exp=explode("->",$v);
				if(!empty($arr_exp[1]))
				$on_exit_arr[$arr_exp[0]]=$arr_exp[1]; 
			}
		 }
		$qry = " select '{$table_name}' as table_name,{$table_name}.internal_code,{$lobj_get_display_cols[field_name]}  from 
				{$table_name}  {$join_qry}
				where  {$table_name}.internal_code={$internal_code}";
		$arr['qry']=$qry; 	 	  
		//echo $qry;
		$arr['on_exit_arr']= $on_exit_arr;	 	  
		return 	$arr;	
	
}

function GetRefDisplayArray($aobj_context,$schema_id_ref,$group_id,$internal_code)
{
$get_ref_display_table_fields="select  ref.name,su. ref_s_user_schema_group_code,
								s.value as ref_value,s1.value as basevalue,
								concat('details_',su.internal_code) as field_id,
								if(s.value='Meta',concat('module_',ref.ref_s_user_schema_code),
								concat('module_',ref.ref_s_user_schema_code,'_details')) as table_name,
								ref.ref_module_code,mmc.field_id as ref_module_field,mmc.table_name as ref_module_table_name,
								if(s.value='Meta',concat('m_',ref.ref_s_user_schema_code,'_ele_',su.source_field),
								su.source_field) as field_name,
								if(s1.value='Meta',
								if(su. ref_s_user_schema_group_code=0,
								concat('module_',linked.ref_s_user_schema_code),
								concat('user_schema_',linked.ref_s_user_schema_code,'_entry_',
								linked.ref_s_user_schema_group_code)),
								concat('module_',linked.ref_s_user_schema_code,'_details')
								) as base_table_name,
								if(s1.value='Meta',
								if(su. ref_s_user_schema_group_code=0,
								concat('m_',linked.ref_s_user_schema_code,'_ele_',linked.internal_code),
								concat('u_',linked.ref_s_user_schema_code,'_g_',linked.ref_s_user_schema_group_code,'_ele_',
								linked.internal_code)),
								linked.internal_code) 
								as base_table_field_name 
								,su.source_field from s_user_schema_elements su 
								inner join s_user_schema_elements ref on ref.internal_code=su.source_field
								inner join s_user_schema_elements linked on linked.internal_code=su.linked_field
								inner join s_ref_type s on s.internal_code=ref.s_ref_type_meta
								inner join s_ref_type s1 on s1.internal_code=linked.s_ref_type_meta
								left join module_meta_columns mmc on mmc.module_id=ref.ref_module_code
								where su.ref_s_user_schema_code={$schema_id_ref}
								and su. ref_s_user_schema_group_code={$group_id}";
			 
			$lobj_get_ref_display_table_fields=  $aobj_context->mobj_db->GetAll($get_ref_display_table_fields);
			$k=0;
			$s_ref_display[]='';
			foreach($lobj_get_ref_display_table_fields as $k=>$v)
			{
				//if ref module is of type meta seacch the value in meta table by passing the id as below
				if($v[ref_value]=='Meta')
				{
				$obj_get_base_id='';
					if($v[basevalue]=='Meta')
					{
					//need to get the id stored for this element and use that id as internal_code in next query
						$get_base_id="select ".$v[base_table_field_name]." as id
						from " .$v[base_table_name]. " where 
						internal_code={$internal_code}";
					 
					$obj_get_base_id=$aobj_context->mobj_db->Getrow($get_base_id);
						
						$get_ref_meta_value="select ".$v[field_name]. " as value from ".$v[table_name]." where 
						internal_code=".$obj_get_base_id[id];
						$obj_get_ref_meta_value=$aobj_context->mobj_db->Getrow($get_ref_meta_value);
						if($v[ref_module_code]>0)
						{
						$get_ref_meta_module_value="select  {$v[ref_module_field]} as value from 
									{$v[ref_module_table_name]} where 
								internal_code={$obj_get_ref_meta_value[value]}";
						$obj_get_ref_meta_value=$aobj_context->mobj_db->Getrow($get_ref_meta_module_value);
						}
					 						
					$s_ref_display[$k]['value']=$obj_get_ref_meta_value['value'];
					$s_ref_display[$k]['field_id']=$v[field_id];
					$k++;
					}
					else if($v[basevalue]=='User')
					{
					//need to get the id stored for this element and use that id as internal_code in next query
					$get_base_id="select value as id from " .$v[base_table_name]. " where 
						ref_s_module_code={$internal_code} and ref_s_user_schema_elements_code=".$v[base_table_field_name];
						
					$obj_get_base_id=$aobj_context->mobj_db->Getrow($get_base_id);	
					$get_ref_meta_value="select ".$v[field_name]. " as value from ".$v[table_name]." where 
					internal_code=".$obj_get_base_id[id];
					 //echo $get_ref_meta_value;
					$obj_get_ref_meta_value=$aobj_context->mobj_db->Getrow($get_ref_meta_value);
					$s_ref_display[$k]['value']=$obj_get_ref_meta_value['value'];
					$s_ref_display[$k]['field_id']=$v[field_id];
					$k++;
					}
				}
				else if($v[ref_value]=='User')
				{
				 
				$obj_get_base_id='';
					if($v[basevalue]=='Meta')
					{
					//need to get the id stored for this element and use that id as internal_code in next query
					$get_base_id="select ".$v[base_table_field_name]." as id from " .$v[base_table_name]. " where 
						internal_code={$internal_code}";
					$obj_get_base_id=$aobj_context->mobj_db->Getrow($get_base_id);	
					
					$get_ref_usr_value="select value  from ".$v[table_name]." where 
					ref_s_module_code=  {$internal_code} and ref_s_user_schema_elements_code =".$obj_get_base_id[id];
					$obj_get_ref_usr_value=$aobj_context->mobj_db->Getrow($get_ref_usr_value);
					$s_ref_display[$k]['value']=$obj_get_ref_meta_value['value'];
					$s_ref_display[$k]['field_id']=$v[field_id];
					$k++;
					}
					else if($v[basevalue]=='User')
					{
					//need to get the id stored for this element and use that id as internal_code in next query
					$get_base_id="select value as id from " .$v[base_table_name]. " where 
						ref_s_module_code={$internal_code} and ref_s_user_schema_elements_code=".$v[base_table_field_name];
						 
				 	$obj_get_base_id=$aobj_context->mobj_db->Getrow($get_base_id);
					 
						$get_ref_usr_value="select value  from ".$v[table_name]." where 
						ref_s_module_code=  {$obj_get_base_id[id]} and ref_s_user_schema_elements_code =".$v[field_name];
						 
						$obj_get_ref_usr_value=$aobj_context->mobj_db->Getrow($get_ref_usr_value);
						 
						$s_ref_display[$k]['value']=$obj_get_ref_usr_value['value'];
						$s_ref_display[$k]['field_id']=$v[field_id];
						$k++;
					}
				
				 
				} 
			}
		 
	return 	$s_ref_display;	
}
function deleteChildTableZeroValues($aobj_context)
{
$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
$group_id = $aobj_context->mobj_data["group_id"];	 
$get_child_groups="select internal_code as group_id,
							concat('user_schema_',ref_s_user_schema_code,'_group_',internal_code) as group_table_name,
							concat('user_schema_',ref_s_user_schema_code,'_entry_',internal_code) as entry_table_name,
							concat('main_group_grid_',internal_code) as main_group_grid_div 
							 
							from s_user_schema_group
							where parent_group_code={$group_id}";
							$lobj_get_child_groups=  $aobj_context->mobj_db->GetAll($get_child_groups);
							 $b=0;
							 for($h=0;$h<count($lobj_get_child_groups);$h++)
							 {
							 $entry_table=$lobj_get_child_groups[$h][entry_table_name];
							 $group_id=$lobj_get_child_groups[$h][group_id];
							 $group_table_name=$lobj_get_child_groups[$h][group_table_name];
							 $delete_entry_table_with_0="delete from {$entry_table}
											where ref_s_ref_data_code=0
											and login_cnt={$login_cnt}";
								$lobj_delete_entry_table_with_0=  $aobj_context->mobj_db->Execute($delete_entry_table_with_0);				
							 $group_details[$b]['table_name']= $group_table_name;
							$group_details[$b]['s_ref_data_code']= 0;
							$group_details[$b]['group_id']= $group_id;
							$group_details[$b]['main_group_grid']= "main_group_grid_".$group_id;
							$b++;	
							 }
 $arr["group_details"]=$group_details;
 echo $aobj_context->mobj_output->ToJSONEnvelope($arr,0,"success");							 
}
 function addSchemaGroupDetails($aobj_context)
{
	$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
	session_start();
	include_once("/JSON.php"); 
	$json = new Services_JSON();
	 
			$del_id = $aobj_context->mobj_data["del_int_code"];	 
			$is_update_visible_field = $aobj_context->mobj_data["is_update_visible_field"];	 
			$insert_qry = $aobj_context->mobj_data["insert_qry"];
			$login_ctr = $aobj_context->mobj_data["login_ctr"];	 
			$validation = $aobj_context->mobj_data["validation"];	 
			$ref_s_ref_data_code = $aobj_context->mobj_data["ref_s_ref_data_code"];	 
			$tab_name = $aobj_context->mobj_data["table_name"];	 
			$user_id=$_SESSION['user_id'];
			$values = stripcslashes($aobj_context->mobj_data["values"]);
			$values.=$_SESSION['user_id'].",now(),".$_SESSION['user_id'].",now()";	
			$_SESSION['error_flag']=1;
			$group_int_code=0;
			$group_details_split=explode("_",$tab_name);
			$schema_id=$group_details_split[2];
			$group_id=$group_details_split[4];
		$root_file_name=$aobj_context->main_src."/validation_files/validate_".$schema_id.".php";
			if (file_exists($root_file_name)) {
			require_once($root_file_name);
			}	
	$lstr_save_schema_details_arr = stripslashes($aobj_context->mobj_data["save_schema_group_details_arr_jsn_text"]);
	$mobj_jsondata = $json->decode($lstr_save_schema_details_arr);
	$larr_save_data = get_object_vars($mobj_jsondata); 
	foreach($larr_save_data as $pk=>$pv)
	{
 
		$insert_cols.=$pv->group_field_id.",";
		$insert_col_values.="'".addslashes($pv->group_field_id_val)."',";
		$update_cond.=$pv->group_field_id."='".addslashes($pv->group_field_id_val)."',";
	}
	$insert_cols=substr($insert_cols,0,strlen($insert_cols)-1);
	$insert_col_values=substr($insert_col_values,0,strlen($insert_col_values)-1);
	$update_cond.=" modified_by={$user_id}, modified_date=now() ";
	$update_qry=" update {$tab_name} set {$update_cond} where internal_code={$del_id}";
	 
	$group_insert_qry="insert into {$tab_name}
					(login_cnt,
					group_int_code,
					ref_s_user_schema_code,
					ref_s_user_schema_group_code,
					ref_s_ref_data_code,{$insert_cols} 
					,created_by,created_date,modified_by,modified_date)
					values(
					{$login_ctr},{$group_int_code},{$schema_id},{$group_id},{$ref_s_ref_data_code},
					{$insert_col_values},
					{$user_id},
					now(),
					{$user_id},
					now() 
					)";
 		
	if (function_exists('validateGroupDetails'))
	{
	     validateGroupDetails($aobj_context); 
	}
	if($_SESSION['error_flag']==1)
	{
		if($del_id=='0')
		{
		$insert_obj=$aobj_context->mobj_db->Execute($group_insert_qry); 
		//echo mysql_error();
		$last_inserted_id = $aobj_context->mobj_db->Insert_ID();
		}
		else
		{
		$insert_obj=$aobj_context->mobj_db->Execute($update_qry);
       // mysql_error();
		//echo $update_qry;
//die();		
		$last_inserted_id=$del_id;
		}
		 
		 
		#TO insert the Parent Group Internal Code in Child group Code Starts
		$get_group_id=explode("_",$tab_name);
		$group_id=$get_group_id[4];
		 $get_child_groups="select internal_code,
				concat('user_schema_',ref_s_user_schema_code,'_entry_',internal_code) as child_table
				from s_user_schema_group
				where parent_group_code={$group_id} and multiple=1";
		 $lobj_get_child_groups =  $aobj_context->mobj_db->GetAll($get_child_groups);
		 if($lobj_get_child_groups)
		 {
			for($j=0;$j<count($lobj_get_child_groups);$j++)
				{
					$child_group_table=$lobj_get_child_groups[$j][child_table];
					$update_ref_data_code="update {$child_group_table}
											set ref_s_ref_data_code={$last_inserted_id}
											where ref_s_ref_data_code=0
											and login_cnt={$login_ctr}";
					$insert_update_ref_data_code=$aobj_context->mobj_db->Execute($update_ref_data_code); 
			 				
				}
		 }
		
		if(mysql_error()!='')
		 { $pos=strpos(mysql_error(),'Duplicate');
			 if($pos>=0)
			 {
				$arr[0]='Check for Unique values'.mysql_error();
				echo $aobj_context->mobj_output->ToJSONEnvelope($arr,-1,"Failure");
				 return;
			 }  
		 }
		 
		 if (function_exists('postsavegroupdetails'))
			{
			     postsavegroupdetails($aobj_context,$last_inserted_id); 
			}
		 
		 if($insert_obj) 
		{
			if($del_id>0)
			$arr[0]='Group Details updated Successfully';
			else
			$arr[0]=' Group Details Inserted Successfully';
			$arr[1]=$tab_name;
			$arr['last_insert_id']=$last_inserted_id;
		    echo $aobj_context->mobj_output->ToJSONEnvelope($arr,0,"success"); 
		}
		else
		{	
		$arr[0]='Inserted Failed';
		$arr[1]=$tab_name;
		$arr['last_insert_id']=$last_inserted_id;
		echo $aobj_context->mobj_output->ToJSONEnvelope($arr,-1,"Failure");
		}
	}		
}
function deleteSchemaGroupDetails($aobj_context)
{
$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
			
			$table_name = $aobj_context->mobj_data["table_name"];	 
			$ids = $aobj_context->mobj_data["ids"];
			$del_qry="delete from $table_name  where internal_code in ($ids)";
 $delete_obj=$aobj_context->mobj_db->Execute($del_qry); 
 if($delete_obj) 
		{
		$arr[0]='Deleted Successfully';
		$arr[1]=$table_name;
	
		
            echo $aobj_context->mobj_output->ToJSONEnvelope($arr,0,"success"); 
		
		}
		 else
		{	
		$arr[0]='Deletion Failed';
		$arr[1]=$table_name;
		 
            echo $aobj_context->mobj_output->ToJSONEnvelope($arr,-1,"Failure");
		
		}
}
function uploadGroupCsvFileIntoDB($aobj_context)
{
$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
	$table_name = $aobj_context->mobj_data["table_name"];
	$s_ref_data_code = $aobj_context->mobj_data["s_ref_data_code"];
	$schema_details=explode("_",$table_name);
	 
	$ref_s_user_schema_code=$schema_details[2];
	$ref_s_user_schema_group_code=$schema_details[4];
	$main_src_obj=(explode("/",$_SERVER["REQUEST_URI"]));
	$main_src=$main_src_obj[1];
	$root_file_name=$_SERVER["DOCUMENT_ROOT"]."/".$main_src."/imported_files/";
	$root_file_name = $root_file_name . $_SESSION['user_id']."_".basename( $_FILES['uploadedfile']['name']); 
$failed=0;
 
			$str_desc = "desc $table_name";
			 
			$obj_desc=$aobj_context->mobj_db->GetAll($str_desc);
				foreach ($obj_desc as $key=>$value)
				{
					if($value['Field']!='internal_code' && $value['Field']!='ref_s_user_schema_code' &&
					$value['Field']!='ref_s_user_schema_group_code' && $value['Field']!='ref_s_ref_data_code')
					{
					$ele_id=explode('ele_',$value['Field']);
					$id.=$ele_id[1].",";
					$insert_qry.=$value['Field'].",";
					}
					$i++;
				}
		 			 $id= substr($id,0,strlen($id) - 1);
						$get_name="select su.internal_code,
						concat('u_',ref_s_user_schema_code,'_g_',ref_s_user_schema_group_code,'_ele_',su.internal_code) as field_id,name,
						if(su.ref_module_code!=0,concat('module_',su.ref_module_code),'') as ref_table_name,value as Type,is_read_only,default_value,optional,su.ref_module_code,list_of_values,linked_field,source_field,suffix,width*9 as width from 
						s_user_schema_elements su left join  s_ref_type s on s.internal_code=su.data_type where su.internal_code in (".$id.") order by su.internal_code";
			  
			 $lobj_get_name =  $aobj_context->mobj_db->GetAll($get_name);
			 
 
        if(isset($_FILES['uploadedfile']['name']) && $_FILES['uploadedfile']['name'] != '')         
        { 
		 	 
		  if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'],$root_file_name))
		  {
		   $handle = fopen($root_file_name,"r"); 
		    $ldata1  = fgetcsv($handle,1024,",");
	 		$i=0;
			foreach ($lobj_get_name as $key=>$value)
			{
				$cond.="'".$value[name]."' == '".$ldata1[$i]. "'"."&&"; 
				if(strtolower($value[name]) == strtolower($ldata1[$i]))
				{
				$field_ids.=$value[field_id].",";
				$i++;
				$failed=0;
				}
				else
				{
				
				$error_data.= $value[name] ." and ".$ldata1[$i]."are not matchng";
				$error_data.="\n";
				header("Content-type:application/txt");
				header("Content-Disposition:attachment;filename=error_log");  
				echo $error_data;
				return;
				$failed=1;
				break;
				}
			}
				$field_ids=substr($field_ids,0,strlen($field_ids)-1);
				
				$first_ins_qry="\$insert_qry=\"insert into {$table_name} (ref_s_user_schema_code,
						ref_s_user_schema_group_code,
						ref_s_ref_data_code,{$field_ids})
						values
						({\$ref_s_user_schema_code},{\$ref_s_user_schema_group_code},{\$s_ref_data_code},";
			if($failed==0)
			{
			$data_str.="<?php while ((\$data = fgetcsv(\$handle,1024,\",\")) !== FALSE) {	
	                if(!empty(\$data))
	                {";
			$k=0;
			 
			 foreach ($lobj_get_name as $key=>$value)
			{
		
			if($value[ref_module_code]!=0)
			{
			$qry="  select concat('module_',schema_id) as table_name,GROUP_CONCAT(name SEPARATOR '-ele-') as name,GROUP_CONCAT(field_name) as field_name from 
				(select * from 
				(select s.internal_code as schema_id,su.name,ifnull(multiple,0) as multiple,ifnull(ug.name,'0None') as group_name,concat('m_',s.internal_code,'_ele_',su.internal_code) as field_name from s_user_schema s inner join s_user_schema_elements su on 
				su.ref_s_user_schema_code=s.internal_code
				inner join s_ref_type t on t.internal_code=su.s_ref_type_meta
				left join s_user_schema_group ug on ug.internal_code=su.ref_s_user_schema_group_code
				 where s.internal_code= $value[ref_module_code] and t.value='Meta' order by ug.sequence,su.sequence,su.internal_code ) l where   multiple=0 limit 2) a group by schema_id"; 
	 
			$lobj_rs = $aobj_context->mobj_db->GetRow($qry);
			
			$cond=explode(",",$lobj_rs[field_name]);
			
			 $data_str.="\$where_cond1=\"{$cond[0]} like '%{\$data[$k]}%'\";";
			//$where_cond1= $data[".$k."] ;
			
			 
			if(!empty($cond[1]))
			$data_str.="\$where_cond2=\" or  {$cond[1]} like '%{\$data[$k]}%'\";";
			
			$data_str.="\$select_ref_data_{$value[ref_module_code]}=\"select internal_code from {$value[ref_table_name]} where {\$where_cond1}  {\$where_cond2}\";";
			 $data_str.="\$obj_select_ref_data_{$value[ref_module_code]} =  \$aobj_context->mobj_db->GetRow(\$select_ref_data_{$value[ref_module_code]});";
			 $data_str.="$".$value[field_id]."  = \$obj_select_ref_data_{$value[ref_module_code]}[internal_code];";
			$insert_value.="'{\$".$value[field_id]."}',";
			$k++;
			}
			else
			{
			$data_str.="$".$value[field_id]."  = addslashes(\$data[{$k}]);";
			$insert_value.="'{\$".$value[field_id]."}',";
			$k++;
			}
			}
					$insert_value=substr($insert_value,0,strlen($insert_value)-1);
				$data_str.=$first_ins_qry.$insert_value.")\";";
 							$data_str.="echo \$insert_qry;";
 							$data_str.="	\$obj_qry =  \$aobj_context->mobj_db->Execute(\$insert_qry);";
			$data_str.=" }}  ?>";
			 
			 
				$tmp_root_file_name=$_SERVER["DOCUMENT_ROOT"]."/".$main_src."/imported_files/upload_report_qry_".$_SESSION['user_id']."_.php";
		 
		file_put_contents($tmp_root_file_name,$data_str);
		 
require($tmp_root_file_name);
			 

			}
			 unlink($tmp_root_file_name);
			unlink($root_file_name); 
		 /*   echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
			" has been uploaded"; */
			 //echo "<script type='text/javascript'> window.close();</script>";
		/* 	header("Content-type:application/csv");
			header("Content-Disposition:attachment;filename=".basename($root_file_name)); */
			//readfile($root_file_name);
			//echo $root_file_name;
		  }
          else
		  echo "faied";
		}	 
		
     //echo "<script type='text/javascript'> getEmpMultipleGroupDetails('{$table_name}');</script>";
}
function uploadGroupXlsFileIntoDB($aobj_context)
{
$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
	$table_name = $aobj_context->mobj_data["table_name"];
	$s_ref_data_code = $aobj_context->mobj_data["s_ref_data_code"];
	$schema_details=explode("_",$table_name);
	 
	$ref_s_user_schema_code=$schema_details[2];
	$ref_s_user_schema_group_code=$schema_details[4];
	$main_src_obj=(explode("/",$_SERVER["REQUEST_URI"]));
	$main_src=$main_src_obj[1];
	$root_file_name=$_SERVER["DOCUMENT_ROOT"]."/".$main_src."/imported_files/";
	$root_file_name = $root_file_name . $_SESSION['user_id']."_".basename( $_FILES['uploadedfile']['name']); 
$failed=0;
  
/* $array1 = array("a" => "green", "b" => "brown");
$array2 = array("a" => "green", "b"=>"yellow");
$result = array_diff_assoc($array1, $array2);
print_r($result); */
 
			$str_desc = "desc $table_name";
			 
			$obj_desc=$aobj_context->mobj_db->GetAll($str_desc);
				foreach ($obj_desc as $key=>$value)
				{
					if($value['Field']!='internal_code' && $value['Field']!='ref_s_user_schema_code' &&
					$value['Field']!='ref_s_user_schema_group_code' && $value['Field']!='ref_s_ref_data_code'&&
					$value['Field']!='created_by' && $value['Field']!='created_date'&&
					$value['Field']!='modified_by' && $value['Field']!='modified_date')
					{
					$ele_id=explode('ele_',$value['Field']);
					$id.=$ele_id[1].",";
					$insert_qry.=$value['Field'].",";
					}
					$i++;
				}
		 			 $id= substr($id,0,strlen($id) - 1);
						$get_name="select su.internal_code,
						concat('u_',ref_s_user_schema_code,'_g_',ref_s_user_schema_group_code,'_ele_',su.internal_code) as field_id,name,
						if(su.ref_module_code!=0,concat('module_',su.ref_module_code),'') as ref_table_name,value as Type,is_read_only,default_value,optional,su.ref_module_code,list_of_values,linked_field,source_field,suffix,width*9 as width,is_unique from 
						s_user_schema_elements su left join  s_ref_type s on s.internal_code=su.data_type where su.internal_code in (".$id.") order by su.internal_code";
			 
			 $lobj_get_name =  $aobj_context->mobj_db->GetAll($get_name);
			 
 
        if(isset($_FILES['uploadedfile']['name']) && $_FILES['uploadedfile']['name'] != '')         
        {
	 
		if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'],$root_file_name))
		  {
		  
		 $reader_path=$_SERVER["DOCUMENT_ROOT"]."/".$main_src."/Excel_reader/reader.php";
		require_once $reader_path;
		$data = new Spreadsheet_Excel_Reader();
		$data->setOutputEncoding('CP1251');
		$data->read($root_file_name);
		error_reporting(E_ALL ^ E_NOTICE);
	 
		 
		for ($i = 1; $i <=1; $i++) 
				{
					for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
						$xls_header_columns[$j]=trim(strtolower($data->sheets[0]['cells'][$i][$j]));
					}					
				}
					$l=1;	 
		foreach ($lobj_get_name as $key=>$value)
			{
			$db_header_columns[$l]=strtolower($value[name]);
			$field_ids.=$value[field_id].",";
			$l++;
			}
			$result = array_diff($xls_header_columns, $db_header_columns);
			 
			 if(!empty($result))
			{
			$error_data='';
			foreach($result as $key=>$value)
			{
			
			$error_data.="\"".strtoupper($value)."\" column are not matching with DB Column";
			$error_data.="\n";
			}
			 
			header("Content-type:application/txt");
			header("Content-Disposition:attachment;filename=error_log");  
			//readfile($root_file_name);
			echo $error_data;
			return; 
			}  
		 $field_ids=substr($field_ids,0,strlen($field_ids)-1);
				$first_ins_qry="\$insert_qry=\"insert into {$table_name} (ref_s_user_schema_code,
						ref_s_user_schema_group_code,
						ref_s_ref_data_code,{$field_ids},created_by,created_date,modified_by,modified_date)
						values
						({\$ref_s_user_schema_code},{\$ref_s_user_schema_group_code},{\$s_ref_data_code},";
				//$update_qry="update {$table_name} set "		
				$data_str="<?php \n";
				$data_str.="\$s_ref_error_flag=0; \n";
				$data_str.="\$original_value=''; \n";
				$data_str.="\$down_load_value=''; \n";
				$data_str.="\$header_value='Following Records are not inserted'.\"\\n\"; \n";
			 	$data_str.="for (\$i = 2; \$i <= \$data->sheets[0]['numRows']; \$i++) 
				{
				\$values='';
				\$original_value='';
				\$s_ref_error_flag=0;
					for (\$j = 1; \$j <= \$data->sheets[0]['numCols']; \$j++) {
					if(\$i==2)
					{
					\$header_value.=\$data->sheets[0]['cells'][1][\$j].\",\";
					}";
			
				$cell_row=1;
			 foreach ($lobj_get_name as $key=>$value)
			{
			if($value[is_unique]==1)
			{
			$update_cond.="{$value[field_id]} = '{\$".$value[field_id]."}' and ";
			}
			if($value[ref_module_code]!=0)
			{
			$qry="  select concat('module_',schema_id) as table_name,GROUP_CONCAT(name SEPARATOR '-ele-') as name,GROUP_CONCAT(field_name) as field_name from 
				(select * from 
				(select s.internal_code as schema_id,su.name,ifnull(multiple,0) as multiple,ifnull(ug.name,'0None') as group_name,concat('m_',s.internal_code,'_ele_',su.internal_code) as field_name from s_user_schema s inner join s_user_schema_elements su on 
				su.ref_s_user_schema_code=s.internal_code
				inner join s_ref_type t on t.internal_code=su.s_ref_type_meta
				left join s_user_schema_group ug on ug.internal_code=su.ref_s_user_schema_group_code
				 where s.internal_code= $value[ref_module_code] and t.value='Meta' order by ug.sequence,su.sequence,su.internal_code ) l where   multiple=0 limit 2) a group by schema_id"; 
	 
			$lobj_rs = $aobj_context->mobj_db->GetRow($qry);
			
			$cond=explode(",",$lobj_rs[field_name]);
			 $data_str.="\n if(TRIM(strtolower(\$data->sheets[0]['cells'][1][\$j]))==strtolower('{$value[name]}')) \n { \n";
			 $data_str.="\$original_value.=(\$data->sheets[0]['cells'][\$i][\$j]).\",\"; \n";
			 $data_str.="\$where_cond1=''; \n";
			 $data_str.="\$where_cond2=''; \n";
			 $data_str.="\$where_cond_1_2=''; \n";
			 $data_str.="\$where_cond1=\"{$cond[0]} like '\".trim(\$data->sheets[0]['cells'][\$i][\$j]).\"'\"; \n";
			//$where_cond1= $data[".$k."] ;
			
			 
			if(!empty($cond[1]))
			{
			$data_str.="\$where_cond2=\" or  {$cond[1]} like '\".trim(\$data->sheets[0]['cells'][\$i][\$j]).\"'\"; \n";
			$data_str.="\$where_cond_1_2=\" or  concat({$cond[0]},' - ',{$cond[1]}) like '{\$data->sheets[0]['cells'][\$i][\$j]}'\"; \n";
			}
			$data_str.="\$select_ref_data_{$value[ref_module_code]}=\"select internal_code from {$value[ref_table_name]} where ( {\$where_cond1}  {\$where_cond2} {\$where_cond_1_2})\"; \n";
			 $data_str.="\$obj_select_ref_data_{$value[ref_module_code]} =  \$aobj_context->mobj_db->GetRow(\$select_ref_data_{$value[ref_module_code]}); \n";
			$data_str.=" if(empty(\$obj_select_ref_data_{$value[ref_module_code]})) \n
					{ \n
					\$s_ref_error_flag=1; \n
					} \n";
			 $data_str.="$".$value[field_id]."  = \$obj_select_ref_data_{$value[ref_module_code]}[internal_code]; \n } \n ";
			$insert_value.="'{\$".$value[field_id]."}',";
			$update_cond_value.=$value[field_id]."= '{\$".$value[field_id]."}' , ";
			$k++;
			}
			else if($value[list_of_values]!='')
			{
			
			$data_str.="if(TRIM(strtolower(\$data->sheets[0]['cells'][1][\$j]))==strtolower('{$value[name]}')) { \n ";
			$data_str.="\$original_value.=(\$data->sheets[0]['cells'][\$i][\$j]).\",\"; \n";
			 $data_str.="\$pos1 = stripos(\"{$value[list_of_values]}\", \$data->sheets[0]['cells'][\$i][\$j]);
				if(\$pos1===false)
				{ \n
					\$s_ref_error_flag=1; \n
					} \n else \n";
			
			 $data_str.="$".$value[field_id]."  = addslashes(\$data->sheets[0]['cells'][\$i][\$j]); \n } \n";
			$insert_value.="'{\$".$value[field_id]."}',";
			$update_cond_value.=$value[field_id]."= '{\$".$value[field_id]."}' , ";
			$k++;
			}
			else if($value[type]=='Date')
			{ 
			 $data_str.="if(TRIM(strtolower(\$data->sheets[0]['cells'][1][\$j]))==strtolower('{$value[name]}')) { \n ";
			 $data_str.="\$original_value.=(\$data->sheets[0]['cells'][\$i][\$j]).\",\"; \n";
			 $data_str.="\$date_obj  =explode(\"/\",addslashes(\$data->sheets[0]['cells'][\$i][\$j])); \n  \n";
			$data_str.="$".$value[field_id]."  = \$date_obj[2].\"-\".\$date_obj[1].\"-\".\$date_obj[0]; }";
					
			
			//$data_str.="$".$value[field_id]."  = addslashes(\$data->sheets[0]['cells'][\$i][\$j]); \n } \n";
			$insert_value.="'{\$".$value[field_id]."}',";
			$update_cond_value.=$value[field_id]."= '{\$".$value[field_id]."}' , ";
			$k++;
			}
			else
			{ 
			 $data_str.="if(TRIM(strtolower(\$data->sheets[0]['cells'][1][\$j]))==strtolower('{$value[name]}')) { \n ";
			 $data_str.="\$original_value.=(\$data->sheets[0]['cells'][\$i][\$j]).\",\"; \n";
			 
			
			$data_str.="$".$value[field_id]."  = trim(addslashes(\$data->sheets[0]['cells'][\$i][\$j]))==''?'0':trim(addslashes(\$data->sheets[0]['cells'][\$i][\$j])); \n } \n";
			$insert_value.="'{\$".$value[field_id]."}',";
			$update_cond_value.=$value[field_id]."= '{\$".$value[field_id]."}' , ";
			$k++;
			}
			}
			$who_fields_update=" ,modified_by=".$_SESSION['user_id'].",modified_date=now()";
			 $update_cond_value=substr($update_cond_value,0,strlen($update_cond_value)-2) ;
			$final_update_qry.=" update {$table_name}  set ".$update_cond_value.$who_fields_update."\n"; 
			$pos=strripos($update_cond,"and"); 
			  $final_update_qry.=" where ref_s_ref_data_code={$s_ref_data_code} and ".substr($update_cond,0,$pos-1); 
				$insert_value=substr($insert_value,0,strlen($insert_value)-1);
				$data_str.="\n }";
				$data_str.="if(\$s_ref_error_flag==0) \n
					{ \n";
				$who_fields=",".$_SESSION['user_id'].",now(),".$_SESSION['user_id'].",now() ";	
				$data_str.=$first_ins_qry.$insert_value.$who_fields.")\"; \n";
 							//$data_str.="echo \$insert_qry;\n";
 							$data_str.="\$obj_qry =  \$aobj_context->mobj_db->Execute(\$insert_qry); \n 
							if(mysql_error()!='') \n
						{ \n
						\$pos=strpos(mysql_error(),'Duplicate');
						if(\$pos===false) 
						 {
						  \$down_load_value.=\$original_value.\",\".mysql_error().\"\\n\";
						 }
						 else
						
						 \$update_qry=\" {$final_update_qry} \"; \n 
						  \$obj_qry =  \$aobj_context->mobj_db->Execute(\$update_qry); \n 
						} 
						\n
						}";
				$data_str.="			
						else
					{
					\$original_value.=\"\\n\";
					\$down_load_value.=\$original_value \n;
					} \n";	
					
			$data_str.=" } "; 
			$data_str.=" if(!empty(\$down_load_value)) \n
			{ \n
			header(\"Content-type:application/csv\"); \n
			header(\"Content-Disposition:attachment;filename=error_log.csv\");   \n
			echo \$header_value.\"\\n\".\$down_load_value; \n
			return; \n
			}
			else \n
			echo \"<script type='text/javascript'> alert('File has been uploaded. Please click on Group Name to Refresh');</script>\";
		
			?>";
			 
			 
				$tmp_root_file_name=$_SERVER["DOCUMENT_ROOT"]."/".$main_src."/imported_files/upload_group_details_".$_SESSION['user_id']."_.php";
		 //echo $tmp_root_file_name;
		    file_put_contents($tmp_root_file_name,$data_str);
		   require_once($tmp_root_file_name);
		    unlink($tmp_root_file_name);
		    unlink($root_file_name);
		    unset($_FILES);
			}
		}
 //echo "<script type='text/javascript'> alert('done');</script>";
}
function getSerieslastNoInFile($aobj_context,$prefix,$ref_module_code,$ref_module_element_code)
{
	$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
	$get_details="select concat(prefix,'-',RIGHT(lpad((last_no+1),LENGTH(ending_no),'0'),LENGTH(ending_no))) as last_no from 
	transaction_series_prefix where ref_module_code={$ref_module_code}
	and ref_module_element_code={$ref_module_element_code} and 
	internal_code='{$prefix}' limit 1";
	$get_details_obj=$aobj_context->mobj_db->getRow($get_details);
	 
	return $get_details_obj["last_no"];
}

function getSerieslastNo($aobj_context)
{
	$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
	$prefix = $aobj_context->mobj_data["prefix"];
	$ref_module_code = $aobj_context->mobj_data["ref_module_code"];
	$ref_module_element_code = $aobj_context->mobj_data["ref_module_element_code"];
	$edit_selected_val = $aobj_context->mobj_data["edit_selected_val"];
	$geditinternalcode = $aobj_context->mobj_data["geditinternalcode"];
	if($geditinternalcode=='0' or $edit_selected_val!=$prefix or empty($edit_selected_val))
	{
	$get_details="select concat(prefix,'-',RIGHT(lpad((last_no+1),LENGTH(ending_no),'0'),LENGTH(ending_no)))  as last_no from 
	transaction_series_prefix where ref_module_code={$ref_module_code}
	and ref_module_element_code={$ref_module_element_code} and 
	internal_code='{$prefix}' limit 1";
	}
	else  
	{
	$get_details="select concat(prefix,'-',RIGHT(lpad((last_no),LENGTH(ending_no),'0'),LENGTH(ending_no)))  as last_no from 
	transaction_series_prefix where ref_module_code={$ref_module_code}
	and ref_module_element_code={$ref_module_element_code} and 
	internal_code='{$prefix}' limit 1";
	}
	$get_details_obj=$aobj_context->mobj_db->getRow($get_details);
	  
		if($get_details_obj) 
		{
		$arr[0]=$get_details_obj["last_no"];
		$arr[1]=$ref_module_element_code;
	
		
            echo $aobj_context->mobj_output->ToJSONEnvelope($arr,0,"success"); 
		
		}
		 else
		{	
		$arr[0]='';
		$arr[1]=$ref_module_element_code;
		 
            echo $aobj_context->mobj_output->ToJSONEnvelope($arr,-1,"Failure");
		
		}
}
function getAutoIncrementDefaultPrefix($aobj_context)
{
$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
 
	$ref_module_code = $aobj_context->mobj_data["ref_module_code"];
	$ref_module_element_code = $aobj_context->mobj_data["ref_module_element_code"];
	
	$get_details="select {$ref_module_element_code} as ele_id,internal_code,prefix as value
				from transaction_series_prefix 
				where ref_module_element_code={$ref_module_element_code} and ref_module_code={$ref_module_code}
				and starting_date<=current_date() and ending_date>=current_date()";
	$get_details_obj=$aobj_context->mobj_db->getAll($get_details);
	   
		if(!empty($get_details_obj))
            echo $aobj_context->mobj_output->ToJSONEnvelope($get_details_obj,0,"success"); 
			else
			  echo $aobj_context->mobj_output->ToJSONEnvelope($get_details_obj,-1,"failure"); 
		
		 
}
function getAutoIncrementDocRootPrefix($aobj_context)
{
$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
 
	$ref_module_code = $aobj_context->mobj_data["ref_module_code"];
	$ref_module_element_code = $aobj_context->mobj_data["ref_module_element_code"];
	$date = $aobj_context->mobj_data["date"];
	
	$get_details="select {$ref_module_element_code} as ele_id,internal_code,prefix as value
				from transaction_series_prefix 
				where ref_module_element_code={$ref_module_element_code} and ref_module_code={$ref_module_code}
				and starting_date<='{$date}' and ending_date>='{$date}' order by internal_code";
	$get_details_obj=$aobj_context->mobj_db->getAll($get_details);
	   
		if(!empty($get_details_obj))
            echo $aobj_context->mobj_output->ToJSONEnvelope($get_details_obj,0,"success"); 
			else
			  echo $aobj_context->mobj_output->ToJSONEnvelope($get_details_obj,-1,"failure"); 
}
function ClearEntryTableNameForLoginCnt($aobj_context)
{
$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
$login_cnt = $aobj_context->mobj_data["login_cnt"];
$entry_table = $aobj_context->mobj_data["entry_table"];
$delete_qry="delete from {$entry_table} where login_cnt={$login_cnt}";
$delete_qry_obj=$aobj_context->mobj_db->Execute($delete_qry);
 echo $aobj_context->mobj_output->ToJSONEnvelope($delete_qry_obj,0,"success"); 
}
function CreateGroupEntryTable($aobj_context)
{
$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
$get_schema_names="select internal_code,module_name 
							from s_user_schema   
							";
		 $lobj_get_schema_names = $aobj_context->mobj_db->GetAll($get_schema_names);
 	 
	foreach($lobj_get_schema_names as $k=>$v)	 
	{
		$schema_id=$v['internal_code'];
		$select_distinct_group_names="select internal_code,name,
								concat('user_schema_',ref_s_user_schema_code,'_group_',internal_code) as group_table_name,
								concat('user_schema_',ref_s_user_schema_code,'_entry_',internal_code) as entry_table_name
								from s_user_schema_group 
							where ref_s_user_schema_code={$schema_id}
							and multiple=1";
		 $lobj_select_distinct_group_names = $aobj_context->mobj_db->GetAll($select_distinct_group_names);	
		
		foreach($lobj_select_distinct_group_names as $gk=>$gv)
		{
			$group_table_name =$gv["group_table_name"];	
			$entry_table_name =$gv["entry_table_name"];	
			$str_desc = "show create table $group_table_name";
			$obj_desc=$aobj_context->mobj_db->GetAll($str_desc);
		   $create_table=$obj_desc[0]['Create Table'];
		   $create_table=str_ireplace("`ref_s_user_schema_code` bigint(20)","`login_cnt` int(11) default NULL ,group_int_code bigint(11),`ref_s_user_schema_code  bigint(20)`",$create_table);
		    $create_table=str_ireplace("`","",$create_table);
		
			 
			$create_table=str_ireplace($group_table_name,$entry_table_name,$create_table);
 
			$lobj_create_table =  $aobj_context->mobj_db->Execute("drop table {$entry_table_name}");
			$lobj_create_table =  $aobj_context->mobj_db->Execute($create_table);
			
		 	$alter_auto_inc="alter table  {$entry_table_name} AUTO_INCREMENT=1";
			$lobj_alter_auto_inc = $aobj_context->mobj_db->Execute($alter_auto_inc); 
			
			$add_indx="ALTER   TABLE {$entry_table_name} add     INDEX   login_cnt(login_cnt)"; 
				$lobj_add_indx = $aobj_context->mobj_db->Execute($add_indx); 
		}
	}
	echo $aobj_context->mobj_output->ToJSONEnvelope($delete_qry_obj,0,"success"); 
}
function UpdateMultiGroupTableWithGridEdit($aobj_context)
{
$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
$internal_code = $aobj_context->mobj_data["internal_code"];
$table_name = $aobj_context->mobj_data["table_name"];
$val = $aobj_context->mobj_data["val"];
$system_name = $aobj_context->mobj_data["system_name"];
$schema_ids=explode("_",$table_name);
$schema_id=$schema_ids[2]; 
$group_id=$schema_ids[4];

		$get_db_field="select internal_code,db_field,name,system_name 
						from s_user_schema_elements 
						where ref_s_user_schema_code={$schema_id}
						and ref_s_user_schema_group_code={$group_id}
						and system_name='{$system_name}'";
		$lobj_get_db_field = $aobj_context->mobj_db->GetRow($get_db_field); 				
		$db_filed=$lobj_get_db_field['db_field'];
		$update_entry_table="update {$table_name} set {$db_filed}='{$val}'
							where internal_code={$internal_code}";
			 			
		$lobj_update_entry_table = $aobj_context->mobj_db->Execute($update_entry_table); 
		if(!empty($lobj_update_entry_table))
            echo $aobj_context->mobj_output->ToJSONEnvelope($arr,0,"success"); 
			else
			 echo $aobj_context->mobj_output->ToJSONEnvelope($arr,-1,"failure"); 		

}

function UpdateGandhiWithCellEdit($aobj_context)
{
$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
$internal_code = $aobj_context->mobj_data["internal_code"];
$table_name = $aobj_context->mobj_data["table_name"];
$val = $aobj_context->mobj_data["val"];
$system_name = $aobj_context->mobj_data["system_name"];
 

		 
		$update_gandhi_grid_table="update {$table_name} set {$system_name}='{$val}'
								where internal_code={$internal_code}";
			 			
		$lobj_update_gandhi_grid_table = $aobj_context->mobj_db->Execute($update_gandhi_grid_table); 
		if(!empty($lobj_update_entry_table))
            echo $aobj_context->mobj_output->ToJSONEnvelope($arr,0,"success"); 
			else
			 echo $aobj_context->mobj_output->ToJSONEnvelope($arr,-1,"failure"); 		

}


function uploadHeaderImageDocuments($aobj_context)
{
$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
$internal_code = $aobj_context->mobj_data["internal_code"];
$module_name = $aobj_context->mobj_data["module_name"];
$module_name=replace($module_name);
$ref_s_user_schema_code = $aobj_context->mobj_data["ref_s_user_schema_code"];
mkdir($aobj_context->main_src."/user_images/m_{$ref_s_user_schema_code}");
$get_type="select ue.internal_code,
		ue.name,s.value
		from s_user_schema_elements ue
		inner join s_user_schema u on u.internal_code=ue.ref_s_user_schema_code
		inner join s_ref_type s on s.internal_code=ue.data_type
		where ref_s_user_schema_code={$ref_s_user_schema_code}
		and value='Image' and is_multiple_group=0";
$lobj_get_type = $aobj_context->mobj_db->GetAll($get_type);
$non_group_ids_arr=getIdsArray($lobj_get_type);	
$db_field_path="/user_images/m_{$ref_s_user_schema_code}/";
foreach($_FILES as $k=>$v)
	{ 
		$text_id_split=explode("_",$k);
		$text_id=$text_id_split[2];
		$file_ext=basename($v['name']);
		$file_ext_arr=explode(".",$file_ext);
		$file_ext=$file_ext_arr[count($file_ext_arr)-1];
		 
		$root_file_name=$internal_code."_".$text_id.".".$file_ext;
		$update_field_value=$db_field_path.$root_file_name;
		$storage_file_name=$aobj_context->main_src."/user_images/m_{$ref_s_user_schema_code}/{$root_file_name}";
		if(in_array($text_id,$non_group_ids_arr))
		{
		  if(isset($v['name']) && $v['name'] != '')         
			{ 
				if(move_uploaded_file($v['tmp_name'],$storage_file_name))
				{
					$field_id="m_{$ref_s_user_schema_code}_ele_{$text_id}";
					$update_db="update module_{$ref_s_user_schema_code}
								set {$field_id}='{$update_field_value}'
								where internal_code={$internal_code}";
					$lobj_update_db = $aobj_context->mobj_db->Execute($update_db); 
				 
				
				}
			}
		}
 	}
}
function uploadGroupImageDocuments($aobj_context)
{
$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
$internal_code = $aobj_context->mobj_data["internal_code"];
$module_name = $aobj_context->mobj_data["module_name"];
$group_name = $aobj_context->mobj_data["group_name"];
$module_name=replace($module_name);
$group_name=replace($group_name);
$ref_s_user_schema_code = $aobj_context->mobj_data["ref_s_user_schema_code"];
$group_id = $aobj_context->mobj_data["group_id"];
$get_type="select ue.internal_code
		from s_user_schema_elements ue
		inner join s_user_schema u on u.internal_code=ue.ref_s_user_schema_code
		inner join s_ref_type s on s.internal_code=ue.data_type
		where ref_s_user_schema_code={$ref_s_user_schema_code}
		and value='Image' and is_multiple_group=1";
$lobj_get_type = $aobj_context->mobj_db->GetAll($get_type); 
$group_ids_arr=getIdsArray($lobj_get_type);
 
mkdir($aobj_context->main_src."/user_images/m_{$ref_s_user_schema_code}");
mkdir($aobj_context->main_src."/user_images/m_{$ref_s_user_schema_code}/tmp");
echo "<pre>";
print_r($_FILES);  
$main_src_obj=(explode("/",$_SERVER["REQUEST_URI"]));
$db_field_path="/user_images/m_{$ref_s_user_schema_code}/tmp/";
 
 
	foreach($_FILES as $k=>$v)
	{ 
		$text_id_split=explode("_",$k);
		$text_id=$text_id_split[2];
		$file_ext=basename($v['name']);
		$file_ext_arr=explode(".",$file_ext);
		$file_ext=$file_ext_arr[count($file_ext_arr)-1];
		$root_file_name=$group_id."_".$internal_code."_".$text_id.".".$file_ext;
		$update_field_value=$db_field_path.$root_file_name;
		$storage_file_name=$aobj_context->main_src."/user_images/m_{$ref_s_user_schema_code}/tmp/{$root_file_name}";
		if(in_array($text_id,$group_ids_arr))
		{	
			if(isset($v['name']) && $v['name'] != '')         
			{ 
				if(move_uploaded_file($v['tmp_name'],$storage_file_name))
				{	 
					$field_id="u_{$ref_s_user_schema_code}_g_{$group_id}_ele_{$text_id}";
					$entry_table="user_schema_{$ref_s_user_schema_code}_entry_{$group_id}";
					$update_db="update {$entry_table}
								set {$field_id}='{$update_field_value}'
								where internal_code={$internal_code}";
					$lobj_update_db = $aobj_context->mobj_db->Execute($update_db); 
				}
			}
		}
 
	}
}
function UpldateGroupImagesFromEntryToGroupTable($aobj_context,$login_cnt,$last_inserted_id,$schema_id,$group_id,$group_table_name,$entry_table_name)
{
 
	$entry_table_name="user_schema_{$schema_id}_entry_{$group_id}";
	$group_table_name="user_schema_{$schema_id}_group_{$group_id}";
	$main_src_path=$aobj_context->main_src;
	$select_group_image_ids="select ue.internal_code,
			concat('u_',ref_s_user_schema_code,'_g_',ref_s_user_schema_group_code,'_ele_',ue.internal_code) as group_field
			,ue.name,s.value
			from s_user_schema_elements ue
			inner join s_user_schema u on u.internal_code=ue.ref_s_user_schema_code
			inner join s_ref_type s on s.internal_code=ue.data_type
			where ref_s_user_schema_code={$schema_id}
			and value='Image' 
			and is_multiple_group=1";
	$lobj_select_group_image_ids = $aobj_context->mobj_db->GetAll($select_group_image_ids);
	$group_fields="";	
	foreach($lobj_select_group_image_ids as $k=>$v)
	{
		$group_fields.=$v[group_field].',';
	}
	$group_fields=substr($group_fields,0,strlen($group_fields)-1);
	
	$get_dat_from_group_table="select internal_code,{$group_fields} from 
					{$group_table_name} where ref_s_ref_data_code={$last_inserted_id}";
	$lobj_get_dat_from_group_table = $aobj_context->mobj_db->GetAll($get_dat_from_group_table);	
	
	foreach($lobj_get_dat_from_group_table as $key=>$val)
	{
		foreach($val as $k=>$v)
			if($k!="internal_code")
			{
				$field_id_arr=explode("ele_",$k);
				$field_id=$field_id_arr[1];
				$tmp_image_path=$main_src_path.$v;
				$file_ext=basename($v);
				$file_ext_arr=explode(".",$file_ext);
				$file_ext=$file_ext_arr[count($file_ext_arr)-1];
				$original_path="{$main_src_path}/user_images/m_{$schema_id}/{$group_id}_{$val[internal_code]}_{$field_id}.{$file_ext}";
				$update_field_value="/user_images/m_{$schema_id}/{$group_id}_{$val[internal_code]}_{$field_id}.{$file_ext}";
				$tmp_pos= strpos($tmp_image_path,"tmp");
				if ($tmp_pos === false)  continue;
				copy($tmp_image_path, $original_path);
				unlink($tmp_image_path);
				$update_group_table_field="update {$group_table_name} set 
					{$k}=\"{$update_field_value}\"
						where internal_code={$val[internal_code]}";
				$lobj_update_group_table_field = $aobj_context->mobj_db->Execute($update_group_table_field);		
				
			}
	}
}
function getModuleImage($aobj_context)
{
$internal_code = $aobj_context->mobj_data["internal_code"];
$schema_id = $aobj_context->mobj_data["schema_id"];
$ele_id = $aobj_context->mobj_data["ele_id"];
$field_id="m_{$schema_id}_ele_{$ele_id}";
$table_name="module_{$schema_id}";
$doc_path=$aobj_context->main_src;
$main_src_obj=(explode("/",$_SERVER["REQUEST_URI"]));
$pdf_main_src=$main_src_obj[1]; 
$image_path=  "http://".$_SERVER["SERVER_NAME"] ."/{$pdf_main_src}";

$get_data="select {$field_id} as path_name from {$table_name} where internal_code={$internal_code}" ;
$lobj_get_data = $aobj_context->mobj_db->GetRow($get_data);	
	if(!empty($lobj_get_data))
	{
	$type_arr=$lobj_get_data['path_name'];
		if(getDisplayType($type_arr)=="Image")
		{
		$arr['type']="Image";
		$arr['path']=$image_path.$type_arr;
		}
		else
		{
		$arr['type']="Doc";
		$arr['path']=$doc_path.$type_arr;
		}
	 echo $aobj_context->mobj_output->ToJSONEnvelope($arr,0,"success"); 
	}
    else
	echo $aobj_context->mobj_output->ToJSONEnvelope($arr,-1,"failure"); 
}

function getGroupImage($aobj_context)
{
$internal_code = $aobj_context->mobj_data["internal_code"];
$schema_id = $aobj_context->mobj_data["schema_id"];
$ele_id = $aobj_context->mobj_data["ele_id"];
$group_id = $aobj_context->mobj_data["group_id"];
$field_id="u_{$schema_id}_g_{$group_id}_ele_{$ele_id}";
$group_table_name="user_schema_{$schema_id}_group_{$group_id}";
$entry_table_name="user_schema_{$schema_id}_entry_{$group_id}";
$doc_path=$aobj_context->main_src;
$main_src_obj=(explode("/",$_SERVER["REQUEST_URI"]));
$pdf_main_src=$main_src_obj[1]; 
$image_path=  "http://".$_SERVER["SERVER_NAME"] ."/{$pdf_main_src}";

	$select_group_table_int_code="select group_int_code from {$entry_table_name} where
							internal_code={$internal_code}";
	$lobj_select_group_table_int_code = $aobj_context->mobj_db->GetRow($select_group_table_int_code);	
	$group_int_code=$lobj_select_group_table_int_code['group_int_code'];	
	
	$get_data="select {$field_id} as path_name from {$group_table_name} where internal_code={$group_int_code}" ;
	$lobj_get_data = $aobj_context->mobj_db->GetRow($get_data);	
	if(!empty($lobj_get_data))
	{
	$type_arr=$lobj_get_data['path_name'];
		if(getDisplayType($type_arr)=="Image")
		{
		$arr['type']="Image";
		$arr['path']=$image_path.$type_arr;
		}
		else
		{
		$arr['type']="Doc";
		$arr['path']=$doc_path.$type_arr;
		}
	 echo $aobj_context->mobj_output->ToJSONEnvelope($arr,0,"success"); 
	}
    else
	echo $aobj_context->mobj_output->ToJSONEnvelope($arr,-1,"failure"); 
}

function displayUserUploadedFiles($aobj_context)
{
		$path = $aobj_context->mobj_data["path"];
		$len = filesize($path);
		$file_ext=basename($path);
		$file_name=basename($path);
		$file_ext_arr=explode(".",$file_ext);
		$file_ext=strtolower($file_ext_arr[count($file_ext_arr)-1]);
		switch( $file_ext ) {
         case "pdf": $ctype="application/pdf"; break;
		 case "exe": $ctype="application/octet-stream"; break;
		 case "zip": $ctype="application/zip"; break;
		 case "doc": $ctype="application/msword"; break;
		 case "txt": $ctype="application/txt"; break;
		 case "php": $ctype="application/php"; break;
		 case "htm": $ctype="application/htm"; break;
		 case "html": $ctype="application/html"; break;
		 case "xls": $ctype="application/vnd.ms-excel"; break;
		 case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
		 case "gif": $ctype="image/gif"; break;
		 case "png": $ctype="image/png"; break;
		 case "jpeg":
		 case "jpg": $ctype="image/jpg"; break;
		 case "mp3": $ctype="audio/mpeg"; break;
		 case "wav": $ctype="audio/x-wav"; break;
		 case "mpeg":
		 case "mpg":
		 case "mpe": $ctype="video/mpeg"; break;
		 case "mov": $ctype="video/quicktime"; break;
		 case "avi": $ctype="video/x-msvideo"; break;
		 //The following are for extensions that shouldn't be downloaded (sensitive stuff, like php files)
		 case "phpa":
		 case "htma":
		 case "htmla":
		 die("<b>Cannot be used for ". $file_extension ." files!</b>"); break;
		 default: $ctype="application/force-download";
		}
	header("Pragma: public");
	header("Expires: 0");
	header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
	header("Cache-Control: public"); 
	header("Content-Description: File Transfer");
	header("Content-Type: $ctype");
	$header="Content-Disposition: attachment; filename=".$file_name.";";
	header($header );
	header("Content-Transfer-Encoding: binary");
	header("Content-Length: ".$len);
	readfile($path);
}
function getIdsArray($obj)
{
$ids_arr='';
$i=0;
	foreach($obj as $k=>$v)
	{
	  $ids_arr[$i]=$v[internal_code];
	  $i++;
	}
return $ids_arr;
}
function replace($name)
{
	$spl_char = array("*","/","~","!","@","#","$","%","^","&",":",";","?","/","\\","_","-","'"," ",".");
	$name = strtolower(str_replace($spl_char,"_",$name));
	return str_ireplace("__","_",$name);
}
function getDisplayType($path)
{
$image_type_arr=array("jpg","jpeg","gif","png");
		$file_ext=$path;
		$file_ext_arr=explode(".",$file_ext);
		$file_ext=strtolower($file_ext_arr[count($file_ext_arr)-1]);
		if(in_array($file_ext,$image_type_arr))
		return "Image";
		else
		return "Doc";
}
function AssignParentModuleIdToSession($aobj_context)
{
   $_SESSION['g_file_id']=$aobj_context->mobj_data["module_id"]; 
   $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
   
  
	print_r($aobj_context->mobj_output->ToJSONEnvelope($rdata,0,"success"));
	return;
	 
}
?>