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 : 3.141.21.106


Current Path : /var/www/html/cnv/kus/src/
Upload File :
Current File : /var/www/html/cnv/kus/src/display_details_in_grid.php

<?php
include_once("/JSON.php"); 
$json = new Services_JSON(); 
function getsearchcondition($cond,$val)
{
	switch($cond)
		{
		case 'eq':
		$cond="=";
		$value="'".$val."'";
		$final_value[0]=$cond;
		$final_value[1]=$value;
		return $final_value;
		break;
		case 'ne':
		$cond="!="; 
		$value="'".$val."'";
		$final_value[0]=$cond;
		$final_value[1]=$value;
		return $final_value;
		break;
		case 'lt':
		$cond="<"; 
		$value="'".$val."'";
		$final_value[0]=$cond;
		$final_value[1]=$value;
		return $final_value;
		break;
		case 'le':
		$cond="<="; 
		$value="'".$val."'";
		$final_value[0]=$cond;
		$final_value[1]=$value;
		return $final_value;
		break;
		case 'gt':
		$cond=">"; 
		$value="'".$val."'";
		$final_value[0]=$cond;
		$final_value[1]=$value;
		return $final_value;
		break;
		case 'ge':
		$cond=">="; 
		$value="'".$val."'";
		$final_value[0]=$cond;
		$final_value[1]=$value;
		return $final_value;
		break;
		case 'cn':
		$cond="like '%"; 
		$value=$val."%'";
		$final_value[0]=$cond;
		$final_value[1]=$value;
		return $final_value;
		break;
		case 'nc':// Doesnot Contain
		$cond="not like '%"; 
		$value=$val."%'";
		$final_value[0]=$cond;
		$final_value[1]=$value;
		return $final_value;
		break;
		
		case 'bw':
		$cond="like '"; 
		$value=trim($val)."%'";
		$final_value[0]=$cond;
		$final_value[1]=$value;
		return $final_value;
		break;
		case 'bn'://Does Not Begin With
		$cond="not like '"; 
		$value=trim($val)."%'";
		$final_value[0]=$cond;
		$final_value[1]=$value;
		return $final_value;
		break;
		
		case 'ew':
		$cond="like '%"; 
		$value=$val."'";
		$final_value[0]=$cond;
		$final_value[1]=$value;
		return $final_value;
		break;
		
		case 'en'://Does Not Ends With
		$cond="not like '%"; 
		$value=$val."'";
		$final_value[0]=$cond;
		$final_value[1]=$value;
		return $final_value;
		break;
		
		case 'in'://Is in
		$cond="in '("; 
		$value=$val."')";
		$final_value[0]=$cond;
		$final_value[1]=$value;
		return $final_value;
		break;
		
		case 'in'://Is not in
		$cond="notin '("; 
		$value=$val."')";
		$final_value[0]=$cond;
		$final_value[1]=$value;
		return $final_value;
		break;
	}
}
function viewUserSchemaDeiatlstest($aobj_context)
  {
  include_once("/JSON.php"); 
 
//echo 	$lstr_ids;		
	$json = new Services_JSON(); 
	$limit = $_GET['rows']; 
	$sidx = $_GET['sidx']; 
	$sord = $_GET['sord']; 
	$page = $_GET['page'];
	if(!$sidx) 
	$sidx =1; 
	$search =$aobj_context->mobj_data["_search"];
	 
	$filter_toolbal_cond="";
	if($search)
	{
	 
	$grid_module_name=$aobj_context->mobj_data["grid_module_name"];
	$grid_module_type=$aobj_context->mobj_data["grid_module_type"];
	if(isset($grid_module_name) && !empty($grid_module_name))
		$filter_toolbal_cond.=" and grid_module_name like '{$grid_module_name}%'";
	if(isset($grid_group_name) && !empty($grid_group_name))
		$filter_toolbal_cond.=" and grid_module_type like '{$grid_module_type}%'";
 
	} 
	 	
	if($search)
	{		
		$count_id="select count(*) as count  from 
		(select s.internal_code as id,s.module_name as grid_module_name,t.value as grid_module_type
			from s_user_schema s inner join s_ref_type t on t.internal_code=s.module_type )a 
			where 1=1  {$filter_toolbal_cond}";
		
	}
	else
	{
	$count_id="select count(*) as count from
			s_user_schema s inner join s_ref_type t on t.internal_code=s.module_type";
	}	
			$result = mysql_query($count_id);
			$row = mysql_fetch_array($result,MYSQL_ASSOC);
			$count = $row['count'];
 
 // calculation of total pages for the query 
	if( $count >0 ) 
	{ 
		$total_pages = ceil($count/$limit); 
	} 
	else 
	{ 
		$total_pages = 0; 
	} 
	if ($page > $total_pages) 
	$page=$total_pages; 
	$start = $limit*$page - $limit; 
	
	if($start <0) 
	$start = 0; 
	// the actual query for the grid data 
	if($search)
	{
			
		$SQL="select * from (select s.internal_code as id,s.module_name as grid_module_name,t.value as grid_module_type
			from s_user_schema s inner join s_ref_type t on t.internal_code=s.module_type  )a 
			where  1=1  {$filter_toolbal_cond}
			ORDER BY $sidx $sord LIMIT $start,$limit";
	 
			
	}
	else
	{
	$SQL = "select s.internal_code as id,s.module_name as grid_module_name,t.value as grid_module_type
			from s_user_schema s inner join s_ref_type t on t.internal_code=s.module_type 
			
			 ORDER BY $sidx $sord LIMIT $start , $limit"; 
			//echo $SQL;  	
	}
	//echo $SQL;
		$result = mysql_query( $SQL ) or die("Could not execute query.".mysql_error());
	
	$responce->page = $page; 
	$responce->total = $total_pages; 
	$responce->records = $count; 

	$i=0; while($row = mysql_fetch_array($result,MYSQL_ASSOC)) 
	{ $responce->rows[$i]['id']=$row[id]; //id
	$responce->rows[$i]['cell']=array($row[grid_module_name],$row[grid_module_type]); 
	$i++; 
	} 
	echo json_encode($responce);
	
	
	// return the formated data 

	
}
 
 function viewUserSchemaDeiatls($aobj_context)
 {
 
	    include_once($aobj_context->main_src.'/src/display_details_in_grid_custom.php');
		$display_obj=new display_details_in_grid($aobj_context);
	 
		  $display_obj->table_cols=array('grid_module_name','grid_display_module_name','grid_module_type','grid_active');
		  $display_obj->display_qry="select s.internal_code as id,
		  s.module_name as grid_module_name,
		  s.display_module_name as grid_display_module_name,
		  t.value as grid_module_type,
		  if(s.active=0,'No','Yes') as grid_active
			from s_user_schema s 
			inner join s_ref_type t on t.internal_code=s.module_type 
								
						 ";	
		// echo $display_obj->display_qry; die();
		$display_obj->DisplayDetailsInGrid();
 }
 
/* function viewUserSchemaDeiatls($aobj_context)
  {
  include_once("/JSON.php"); 
 
//echo 	$lstr_ids;		
	$json = new Services_JSON(); 
	$limit = $_GET['rows']; 
	$sidx = $_GET['sidx']; 
	$sord = $_GET['sord']; 
	$page = $_GET['page'];
	if(!$sidx) 
	$sidx =1; 
	$search =$aobj_context->mobj_data["_search"];
	 
	$filter_toolbal_cond="";
	if($search)
	{
	 
	$grid_module_name=$aobj_context->mobj_data["grid_module_name"];
	$grid_module_type=$aobj_context->mobj_data["grid_module_type"];
	if(isset($grid_module_name) && !empty($grid_module_name))
		$filter_toolbal_cond.=" and grid_module_name like '{$grid_module_name}%'";
	if(isset($grid_module_type) && !empty($grid_module_type))
		$filter_toolbal_cond.=" and grid_module_type like '{$grid_module_type}%'";
 
	} 
	 	
	if($search)
	{		
		$count_id="select count(*) as count  from 
		(select s.internal_code as id,s.module_name as grid_module_name,t.value as grid_module_type
			from s_user_schema s inner join s_ref_type t on t.internal_code=s.module_type )a 
			where 1=1  {$filter_toolbal_cond}";
		
	}
	else
	{
	$count_id="select count(*) as count from
			s_user_schema s inner join s_ref_type t on t.internal_code=s.module_type";
	}	
			$result = mysql_query($count_id);
			$row = mysql_fetch_array($result,MYSQL_ASSOC);
			$count = $row['count'];
 
 // calculation of total pages for the query 
	if( $count >0 ) 
	{ 
		$total_pages = ceil($count/$limit); 
	} 
	else 
	{ 
		$total_pages = 0; 
	} 
	if ($page > $total_pages) 
	$page=$total_pages; 
	 
	$start = $limit*$page - $limit; 
	
	if($start <0) 
	$start = 0; 
	// the actual query for the grid data 
	if($search)
	{
			
		$SQL="select * from (select s.internal_code as id,s.module_name as grid_module_name,t.value as grid_module_type
			from s_user_schema s inner join s_ref_type t on t.internal_code=s.module_type  )a 
			where  1=1  {$filter_toolbal_cond}
			ORDER BY $sidx $sord LIMIT $start,$limit";
	  
			
	}
	else
	{
	$SQL = "select s.internal_code as id,s.module_name as grid_module_name,t.value as grid_module_type
			from s_user_schema s inner join s_ref_type t on t.internal_code=s.module_type 
			
			 ORDER BY $sidx $sord LIMIT $start , $limit"; 
			//echo $SQL;  	
	}
	//echo $SQL;
		$result = mysql_query( $SQL ) or die("Could not execute query.".mysql_error());
	
	$responce->page = $page; 
	$responce->total = $total_pages; 
	$responce->records = $count; 

	$i=0; while($row = mysql_fetch_array($result,MYSQL_ASSOC)) 
	{ $responce->rows[$i]['id']=$row[id]; //id
	$responce->rows[$i]['cell']=array($row[grid_module_name],$row[grid_module_type]); 
	$i++; 
	} 
	echo json_encode($responce);
} */



function deleteUserSchemaDeiatls($aobj_context)
{
$main_src_obj=(explode("/",$_SERVER["REQUEST_URI"]));
		$main_src=$main_src_obj[1];		
	$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
			$id = $aobj_context->mobj_data["id"];	 
			
			$lstr_qry = "SELECT system_module_table_name,
					 group_concat(concat(system_module_table_name,'_',system_group_table_name,'_entry')) as entry_group_table,
					 group_concat(concat(system_module_table_name,'_',system_group_table_name,'_group')) as group_table 
					from s_user_schema s
					left join s_user_schema_group sg 
					on s.internal_code=sg.ref_s_user_schema_code and multiple=1
					where s.internal_code={$id}
					group by system_module_table_name"; 
			  $lobj_rs = $aobj_context->mobj_db->GetRow($lstr_qry);
			  $module_table_name=$lobj_rs['system_module_table_name'];
			  
			  $drop_table="drop table  {$module_table_name}";
			  $lobj_rsd = $aobj_context->mobj_db->Execute($drop_table);
			 $lstr_qry = "delete  
			from s_user_schema 
			 where internal_code in (".$id.")"; 
			  $lobj_rsd = $aobj_context->mobj_db->Execute($lstr_qry);
			 $delete_from_ele="delete from s_user_schema_elements where ref_s_user_schema_code in ({$id})";
			 $lobj_rsd = $aobj_context->mobj_db->Execute($delete_from_ele);
			  $entry_group_table=$lobj_rs['entry_group_table'];
			  if(!is_null($entry_group_table))
			  {
				$group_arr=explode(",",$entry_group_table);
				foreach($group_arr as $k=>$v)
				{
				 $drop_entry_table="drop table  {$v}";
				 $lobj_rsd = $aobj_context->mobj_db->Execute($drop_entry_table);
				}
			  }
			  $group_table=$lobj_rs['group_table'];
			 if(!is_null($group_table))
			  {
				$group_arr=explode(",",$group_table);
				foreach($group_arr as $k=>$v)
				{
				 $drop_group_table="drop table  {$v}";
				 $lobj_rsd = $aobj_context->mobj_db->Execute($drop_group_table);
				}
			  }
 
 
	
	
	 $root_file_name=$_SERVER["DOCUMENT_ROOT"]."/".$main_src."/Report_details/report_result_".$lstr_ids.".php";
	 $root_js_name=$_SERVER["DOCUMENT_ROOT"]."/".$main_src."/grid_display_files/module_".$lstr_ids.".js";
	
	   
	   if(file_exists($root_file_name))
	   unlink($root_file_name);
		   if(file_exists($root_js_name))
	   unlink($root_js_name); 
     
        if($lobj_rs) 
		{
		$rdata="Deleted Successfully";
            echo $aobj_context->mobj_output->ToJSONEnvelope($rdata,0,"success"); 
		return 	$rdata;
		}
		 else
		{	$rdata="Deletion Failed";
            echo $aobj_context->mobj_output->ToJSONEnvelope($rdata,-1,"Failure");
			return 	$rdata;
		}
	
}	
function  display_user_schema_group_master_grid($aobj_context)
  {
   include_once($aobj_context->main_src.'/src/display_details_in_grid_custom.php');
		$display_obj=new display_details_in_grid($aobj_context);
	 
		  $display_obj->table_cols=array('grid_schema_name','grid_group_name','grid_multiple','grid_group_sequence');
		  $display_obj->display_qry="select usg.internal_code as id,us.module_name as grid_schema_name,name as grid_group_name,
		multiple as grid_multiple,usg.sequence as grid_group_sequence
			 from s_user_schema us inner join
			s_user_schema_group usg on usg.ref_s_user_schema_code=us.internal_code
								
						 ";	
		// echo $display_obj->display_qry; die();
		$display_obj->DisplayDetailsInGrid();
  }


/* function display_user_schema_group_master_grid($aobj_context)
  {
   include_once("/JSON.php"); 
	$json = new Services_JSON(); 
	$limit = $_GET['rows']; 
	$sidx = $_GET['sidx']; 
	$sord = $_GET['sord']; 
	$page = $_GET['page'];
	$search =$aobj_context->mobj_data["_search"];
	$filter_toolbal_cond="";
	if($search)
	{
	$schema_name=$aobj_context->mobj_data["grid_schema_name"];
	$grid_group_name=$aobj_context->mobj_data["grid_group_name"];
	$grid_group_name=$aobj_context->mobj_data["grid_group_name"];
	if(isset($schema_name) && !empty($schema_name))
		$filter_toolbal_cond.=" and grid_Schema_name like '{$schema_name}%'";
	if(isset($grid_group_name) && !empty($grid_group_name))
		$filter_toolbal_cond.=" and grid_group_name like '{$grid_group_name}%'";
 
	}
	
	
	 	
	if(!$sidx) 
	$sidx =1; 
	if($search)
	{		
		$count_id="select count(*) as count from (
			 select usg.internal_code as id,us.module_name as grid_schema_name,name as 
			 grid_group_name,multiple as grid_multiple
			 from s_user_schema us inner join
			s_user_schema_group usg on usg.ref_s_user_schema_code=us.internal_code)a 
			 where 1=1 {$filter_toolbal_cond}";
		
	}
	else
	{
	$count_id="select count(*) as count
			 from s_user_schema_group";
	}		 
			$result = mysql_query($count_id);
			$row = mysql_fetch_array($result,MYSQL_ASSOC);
			$count = $row['count'];
 
 // calculation of total pages for the query 
	if( $count >0 ) 
	{ 
		$total_pages = ceil($count/$limit); 
	} 
	else 
	{ 
		$total_pages = 0; 
	} 
	if ($page > $total_pages) 
	$page=$total_pages; 
	$start = $limit*$page - $limit; 
	
	if($start <0) 
	$start = 0; 
	// the actual query for the grid data 
	if($search)
	{
			
			$SQL="select * from (select usg.internal_code as id,us.module_name as grid_schema_name,
			name as grid_group_name,multiple as grid_multiple,usg.sequence as grid_group_sequence
			 from s_user_schema us inner join
			s_user_schema_group usg on usg.ref_s_user_schema_code=us.internal_code )
			a where 1=1 {$filter_toolbal_cond}
			ORDER BY $sidx $sord LIMIT $start,$limit";
			//echo $SQL;
	
	}
	else
	{
		$SQL = "select usg.internal_code as id,us.module_name as grid_schema_name,name as grid_group_name,
		multiple as grid_multiple,usg.sequence as grid_group_sequence
			 from s_user_schema us inner join
			s_user_schema_group usg on usg.ref_s_user_schema_code=us.internal_code
			 ORDER BY $sidx $sord LIMIT $start , $limit"; 
	}	//echo 	$SQL ;
		$lobj_rs = $aobj_context->mobj_db->GetAll($SQL); 
		//$result = mysql_query( $SQL ) or die("Couldn t execute query.".mysql_error()); 
	
	$responce->page = $page; 
	$responce->total = $total_pages; 
	$responce->records = $count; 
	$lobj_rs = $aobj_context->mobj_db->GetAll($SQL); 
	 foreach($lobj_rs as $key =>$value)
	 {
	 $responce->rows[$key]['id']=$value[id];
	 $responce->rows[$key]['cell']=array($value[grid_schema_name],$value[grid_group_name],$value[grid_multiple],$value[grid_group_sequence]);
	}
	
	// return the formated data 
	echo $json->encode($responce); 
} */


function delete_user_schema_group_master_setup_details($aobj_context)
{
	$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
			$lstr_ids = $aobj_context->mobj_data["id"];	 

		
//to drop the Group user schema Tables
	$qry="select 
			  concat(system_module_table_name,'_',system_group_table_name,'_entry')  as entry_table_name,
			  concat(system_module_table_name,'_',system_group_table_name,'_group')  as group_table_name 

					from s_user_schema_group msg inner join s_user_schema ms on 
					ms.internal_code=msg.ref_s_user_schema_code
					where msg.internal_code=$lstr_ids";
					$lobj_rs = $aobj_context->mobj_db->GetRow($qry);
			
		$drop_qry="drop table IF EXISTS ". $lobj_rs['group_table_name'];
					$lobj_rs_drop_qry = $aobj_context->mobj_db->Execute($drop_qry);
					
	 	$drop_entry_qry="drop table IF EXISTS ". $lobj_rs['entry_table_name'];
					$lobj_drop_entry_qry = $aobj_context->mobj_db->Execute($drop_entry_qry); 
					
		$lstr_qry = "delete  
			from s_user_schema_group 
			 where internal_code in (".$lstr_ids.")"; 
	//	echo $lstr_qry 
	$lobj_rs = $aobj_context->mobj_db->Execute($lstr_qry);
	
	$delete_eles="delete from s_user_schema_elements where ref_s_user_schema_group_code={$lstr_ids}";
		$lobj_rs = $aobj_context->mobj_db->Execute($delete_eles);
        if($lobj_rs) 
		{
		$rdata="Deleted Successfully";
            echo $aobj_context->mobj_output->ToJSONEnvelope($rdata,0,"success"); 
		return 	$rdata;
		}
		 else
		{	$rdata="Deletion Failed";
            echo $aobj_context->mobj_output->ToJSONEnvelope($rdata,-1,"Failure");
			return 	$rdata;
		}
	
}
function display_user_schema_element_master_grid($aobj_context)
  {
  include_once($aobj_context->main_src.'/src/display_details_in_grid_custom.php');
		$display_obj=new display_details_in_grid($aobj_context);
	 
		  $display_obj->table_cols=array('grid_Schema_name','grid_name','grid_data_type','grid_sequence','grid_group_name','grid_field_position','grid_default_value','grid_optional');
		  $display_obj->display_qry="select uss.internal_code as id,us.module_name as grid_Schema_name,
		  uss.name as grid_name,
			s1.value as grid_data_type,uss.sequence as grid_sequence,usg.name as grid_group_name,field_position as grid_field_position,
			default_value as grid_default_value,
			if(optional=1,'Yes','No') as grid_optional
			from s_user_schema_elements uss
			 inner join s_user_schema us on us.internal_code=uss.ref_s_user_schema_code
			left join s_user_schema_group usg on  usg.internal_code=ref_s_user_schema_group_code
			left join s_ref_type s1 on s1.internal_code=data_type 
								
						 ";	
		// echo $display_obj->display_qry; die();
		$display_obj->DisplayDetailsInGrid();
  }
/* function display_user_schema_element_master_grid($aobj_context)
  {
  include_once("/JSON.php"); 
	$json = new Services_JSON(); 
	$limit = $_GET['rows']; 
	$sidx = $_GET['sidx']; 
	$sord = $_GET['sord']; 
	$page = $_GET['page'];
	$search =$aobj_context->mobj_data["_search"];
	$filter_toolbal_cond="";	 	
	if(!$sidx) 
	$sidx =1; 
	$filter_toolbal_cond="";
	if($search)
	{
	$schema_name=$aobj_context->mobj_data["grid_Schema_name"];
	$grid_name=$aobj_context->mobj_data["grid_name"];
	$grid_group_name=$aobj_context->mobj_data["grid_group_name"];
	if(isset($schema_name) && !empty($schema_name))
		$filter_toolbal_cond.=" and grid_Schema_name like '{$schema_name}%'";
	if(isset($grid_name) && !empty($grid_name))
		$filter_toolbal_cond.=" and grid_name like '{$grid_name}%'";
	if(isset($grid_group_name) && !empty($grid_group_name))
		$filter_toolbal_cond.=" and grid_group_name like '{$grid_group_name}%'";	
	}
	if($search)
	{		
		$count_id="select count(*) as count from(
			select uss.internal_code as id,us.module_name as grid_Schema_name,uss.name as grid_name,
			s1.value as grid_data_type,uss.sequence as grid_sequence,usg.name as grid_group_name,description as grid_description,
			default_value as grid_default_value,
			if(optional=1,'Yes','No') as grid_optional
			from s_user_schema_elements uss
			 inner join s_user_schema us on us.internal_code=uss.ref_s_user_schema_code
			left join s_user_schema_group usg on  usg.internal_code=ref_s_user_schema_group_code
			left join s_ref_type s1 on s1.internal_code=data_type)a 
			 where id>0 {$filter_toolbal_cond} ";
	 
	}
	else
	{
	$count_id="select count(*) as count
			 from s_user_schema_elements uss
			 inner join s_user_schema us on us.internal_code=uss.ref_s_user_schema_code
			left join s_user_schema_group usg on  usg.internal_code=ref_s_user_schema_group_code
			left join s_ref_type s1 on s1.internal_code=data_type";
	}
			$result = mysql_query($count_id);
			$row = mysql_fetch_array($result,MYSQL_ASSOC);
			$count = $row['count'];
 
 // calculation of total pages for the query 
	if( $count >0 ) 
	{ 
		$total_pages = ceil($count/$limit); 
	} 
	else 
	{ 
		$total_pages = 0; 
	} 
	if ($page > $total_pages) 
	$page=$total_pages; 
	$start = $limit*$page - $limit; 
	
	if($start <0) 
	$start = 0; 
	// the actual query for the grid data 
	if($search)
	{
			
			$SQL="select * from (select uss.internal_code as id,us.module_name as grid_Schema_name,uss.name as grid_name,
			s1.value as grid_data_type,uss.sequence as grid_sequence,usg.name as grid_group_name,description as grid_description,
			default_value as grid_default_value,
			if(optional=1,'Yes','No') as grid_optional,ref_data.module_name as grid_ref_module_name,s2.value as grid_meta_type
			from s_user_schema_elements uss
			 inner join s_user_schema us on us.internal_code=uss.ref_s_user_schema_code
			left join s_user_schema_group usg on  usg.internal_code=ref_s_user_schema_group_code
			left join s_ref_type s1 on s1.internal_code=data_type
			left join s_user_schema ref_data on ref_data.internal_code=uss.ref_module_code 
			left join  s_ref_type s2 on s2.internal_code=s_ref_type_meta)
			a where   id>0 {$filter_toolbal_cond}
			ORDER BY $sidx $sord LIMIT $start,$limit";
		 
	
	}
	else
	{
	$SQL = "select uss.internal_code as id,us.module_name as grid_Schema_name,uss.name as grid_name,
			s1.value as grid_data_type,uss.sequence as grid_sequence,usg.name as grid_group_name,description as grid_description,
			default_value as grid_default_value,
			if(optional=1,'Yes','No') as grid_optional,ref_data.module_name as grid_ref_module_name,s2.value as grid_meta_type
			from s_user_schema_elements uss
			 inner join s_user_schema us on us.internal_code=uss.ref_s_user_schema_code
			left join s_user_schema_group usg on  usg.internal_code=ref_s_user_schema_group_code
			left join s_ref_type s1 on s1.internal_code=data_type
			left join s_user_schema ref_data on ref_data.internal_code=uss.ref_module_code 
			left join  s_ref_type s2 on s2.internal_code=s_ref_type_meta
			 ORDER BY $sidx $sord LIMIT $start , $limit"; 
	}
			   
	
		$result = mysql_query( $SQL ) or die("Couldn t execute query.".mysql_error()); 
	$responce->page = $page; 
	$responce->total = $total_pages; 
	$responce->records = $count; 
	$lobj_rs = $aobj_context->mobj_db->GetAll($SQL); 

	$i=0; 
	while($row = mysql_fetch_array($result,MYSQL_ASSOC)) 
	{ 
		$responce->rows[$i]['id']=$row[id]; 
		$responce->rows[$i]['cell']=array($row[grid_Schema_name],$row[grid_name],$row[grid_data_type],$row[grid_sequence],
		$row[grid_group_name],$row[grid_description],$row[grid_default_value],$row[grid_optional],$row[grid_ref_module_name],$row[grid_meta_type]); 
		$i++; 
	} 
	// return the formated data 
	echo $json->encode($responce); 
	// return the formated data 
	
} */


function delete_user_schema_element_master_setup_details($aobj_context)
{
	$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
			$lstr_ids = $aobj_context->mobj_data["id"];	
				//to drop field in File
			$lstr_get_schema = "select ref_s_user_schema_code  
			from s_user_schema_elements 
			 where internal_code in (".$lstr_ids.")"; 
	 
	 $lobj_lstr_get_schema = $aobj_context->mobj_db->GetRow($lstr_get_schema);
	 $schema_id= $lobj_lstr_get_schema[ref_s_user_schema_code];
// to drop the multiple group field name if exstis			
 $drop_field="select concat(system_module_table_name,'_',system_group_table_name,'_group')  as  
						table_name,
							db_field as field_name
							from 
							s_user_schema_elements suse 
							inner join s_user_schema_group susg 
							on susg.internal_code=suse.ref_s_user_schema_group_code  
							inner join s_user_schema sus on sus.internal_code=susg.ref_s_user_schema_code
							where suse.internal_code=". $lstr_ids ." and multiple=1";
		$lobj_rs_drop_field = $aobj_context->mobj_db->GetRow($drop_field);
					if(count($lobj_rs_drop_field['table_name'])>0)
							{
							$table_count="show tables like '".$lobj_rs_drop_field['table_name']."'";
							$lobj_rs_table_count = $aobj_context->mobj_db->GetRow($table_count);
								if(count($lobj_rs_table_count)>0)
								{	
								foreach ($lobj_rs_table_count as $key=>$value)
								{
								$alter_qr="alter table ". $value ." drop ".$lobj_rs_drop_field['field_name']   ;
								$lobj_rs_drop = $aobj_context->mobj_db->Execute($alter_qr); 
								$entry_group_table=str_ireplace("group","entry",$value);
								$alter_entry_qr="alter table ". $entry_group_table ." drop ".$lobj_rs_drop_field['field_name']   ;
								$lobj_rs_entry_group_table = $aobj_context->mobj_db->Execute($alter_entry_qr); 
								}
								}
							}
							
		$drop_meta="select system_module_table_name	as table_name,
					db_field as field_name
					from 

					s_user_schema_elements suse 

					inner join s_user_schema sus on sus.internal_code=suse.ref_s_user_schema_code

					inner join s_ref_type s1 on s1.internal_code=suse.s_ref_type_meta

					where suse.internal_code={$lstr_ids} and s1.value='Meta'";
			$lobj_drop_meta = $aobj_context->mobj_db->GetRow($drop_meta);	
			if(!empty($lobj_drop_meta['table_name']))
							{
							$table_count="show tables like '".$lobj_drop_meta['table_name']."'";
							$lobj_rs_table_count = $aobj_context->mobj_db->GetRow($table_count);
								if(count($lobj_rs_table_count)>0)
								{	
								foreach ($lobj_rs_table_count as $key=>$value)
								$alter_qr="alter table ". $value ." drop ".$lobj_drop_meta['field_name']   ;
							$lobj_rs_drop = $aobj_context->mobj_db->Execute($alter_qr); 
								}
							}			
					
		$lstr_qry = "delete  
			from s_user_schema_elements 
			 where internal_code in (".$lstr_ids.")"; 
		//echo $lstr_qry ;
	 $lobj_rs = $aobj_context->mobj_db->Execute($lstr_qry);
	   		
			$aobj_context->mobj_data["schema_id"]=$schema_id;
	 	//require_once(dirname(__FILE__)."\create_file.php");
		include(dirname(__FILE__)."\create_all_grid_files.php");
		populateAllGridFiles($aobj_context);
		 include(dirname(__FILE__)."\create_all_grid_group_files.php");
		populateAllGridGroupFiles($aobj_context); 
		 include(dirname(__FILE__)."\create_html_files.php");
	
		CreateModuleHtmlFiles($aobj_context);
        if($lobj_rs) 
		{
		$rdata="Deleted Successfully";
            echo $aobj_context->mobj_output->ToJSONEnvelope($rdata,0,"success"); 
		return 	$rdata;
		}
		 else
		{	$rdata="Deletion Failed";
            echo $aobj_context->mobj_output->ToJSONEnvelope($rdata,-1,"Failure");
			return 	$rdata;
		}
}
function getUserSchemaDetailsForRefrencedata($aobj_context)
{
include_once("/JSON.php");

//echo $lstr_ids;
$json = new Services_JSON();
$limit = $_GET['rows'];
$sidx = $_GET['sidx'];
$sord = $_GET['sord'];
$page = $_GET['page'];
$module_id = $_GET['ref_module_code'];
if(!$sidx)
$sidx =1;

// to take th e parameters from Search field
if(isset($_GET['searchField']) && $_GET["_search"]=='true')
{
$field_name=$_GET['searchField'];
$final_value=getsearchcondition($_GET['searchOper'],$_GET['searchString']);
$cond=$final_value[0];
$value=$final_value[1];
}
if(isset($_GET['searchField']) && $_GET["_search"]=='true')
{
$count_id="select count(*) as count from (select internal_code as id,
data1 as grid_field1,
data2 as grid_field2

from s_ref_data where ref_s_user_schema_code = $module_id)a where $field_name $cond$value";

}
else
{
$count_id="select count(*) as count
from s_ref_data where ref_s_user_schema_code = $module_id";
}
$result = mysql_query($count_id);
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$count = $row['count'];

// calculation of total pages for the query
if( $count >0 )
{
$total_pages = ceil($count/$limit);
}
else
{
$total_pages = 0;
}
if ($page > $total_pages)
$page=$total_pages;
$start = $limit*$page - $limit;

if($start <0)
$start = 0;
// the actual query for the grid data
if(isset($_GET['searchField']) && $_GET["_search"]=='true')
{

$SQL="select * from (select internal_code as id,
data1 as grid_field1,
data2 as grid_field2
from s_ref_data where ref_s_user_schema_code = $module_id )a where $field_name $cond$value
ORDER BY $sidx $sord LIMIT $start,$limit";
}
else
{
$SQL = "select internal_code as id,
data1 as grid_field1,
data2 as grid_field2
from s_ref_data where ref_s_user_schema_code = $module_id
ORDER BY $sidx $sord LIMIT $start , $limit";
// echo $SQL;
}
//echo $SQL;
//$result = mysql_query( $SQL ) or die("Couldn t execute query.".mysql_error());
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;
$lobj_rs = $aobj_context->mobj_db->GetAll($SQL);
foreach($lobj_rs as $key =>$value)
{
$responce->rows[$key]['id']=$value[id];
$responce->rows[$key]['cell']=array($value[grid_field1],$value[grid_field2]);
}
/* $i=0;
while($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
$responce->rows[$i]['id']=$row[id];
$responce->rows[$i]['cell']=array($row[grid_module_name],$row[grid_field1],$row[grid_field2],$row[grid_field3],
$row[grid_field4],$row[grid_field5],$row[grid_tree_data] );
$i++;
} */
//var_dump($responce->rows);
echo $json->encode($responce);
}
function viewUserSchemaModuleDetails($aobj_context)
{ 
$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
include_once("/JSON.php");
$json = new Services_JSON();
$search =$aobj_context->mobj_data["_search"];
$filters =$aobj_context->mobj_data["filters"];
$schema_id =$aobj_context->mobj_data["schema_id"];
$table_name =$aobj_context->mobj_data["table_name"];
 
	if(isset($filters))
	{
	$mobj_filters_data = $json->decode($filters);
	$filters_data = get_object_vars($mobj_filters_data);	
	 
	}
;
//echo $lstr_ids;

$limit = $_GET['rows'];
$sidx = $_GET['sidx'];
$sord = $_GET['sord'];
$page = $_GET['page'];

if(!$sidx)
$sidx =1;
 
 

 
$search_db_fields_arr=getSearchFieldIdsArr($aobj_context,$table_name,$schema_id,'include_in_grid');
 
$filter_toolbal_cond="";
if($search)
{
	foreach($aobj_context->mobj_data as $search_k=>$search_v)
	{
		if(!empty($search_db_fields_arr[$search_k]) &&  !is_null($search_db_fields_arr[$search_k]))
		{
		$s_val=trim($search_v);
		$filter_toolbal_cond.=" and {$search_db_fields_arr[$search_k]} like '{$s_val}%' ";	
		}
	}
	if(isset($filters) && !empty($filters_data))
	{
	$filter_toolbal_cond.="   ";
	$oper=$filters_data['groupOp'];
		foreach($filters_data['rules'] as $rk=>$rv)
		{
		$final_value=getsearchcondition($rv->op,$rv->data);
		$cond=$final_value[0];
		$value=$final_value[1];
		$filter_toolbal_cond.=" {$oper}  {$search_db_fields_arr[$rv->field]} {$cond}{$value}";
		}
		 
	$filter_toolbal_cond.="   ";
	 
	}
}
 
 $table_field_name=stripcslashes($_GET['field_name']);
$details_array=$_GET['details_array'];
$search_cond='';
 
$qry=getGeneralViewQry($aobj_context,$table_name,$schema_id,'include_in_grid');	
$cnt_qry=getGeneralViewCountQry($aobj_context,$table_name,$schema_id,'include_in_grid') ;			 
 require_once($aobj_context->main_src."/custom_src/grid_filters.php");
$f_obj=new grid_filters($aobj_context);	
$filter_cond=$f_obj->GetFilterCond();
 
if($search)
{
$count_id="{$cnt_qry}   and {$table_name}.internal_code<>0  {$filter_toolbal_cond} {$filter_cond}";
}
else
{
$count_id=$cnt_qry." ".$filter_cond;;
}
 
/* $result = mysql_query($count_id);
 
$row = mysql_fetch_array($result,mysql_assoc); */

$lobj_count_id =$aobj_context->mobj_db->GetRow($count_id); 
/* echo $count_id;
echo mysql_error();
print_r($lobj_count_id); */
$count = $lobj_count_id['count'];
// calculation of total pages for the query
if( $count >0 )
{
$total_pages = ceil($count/$limit);
}
else
{
$total_pages = 0;
}
if ($page > $total_pages)
$page=$total_pages;
$start = $limit*$page - $limit;

if($start <0)
$start = 0;
// the actual query for the grid data
if($search)
{
$SQL="  {$qry}  and {$table_name}.internal_code<>0  {$filter_toolbal_cond} {$filter_cond}
ORDER BY $sidx $sord LIMIT $start,$limit"; 
}
else
{

$SQL=" {$qry} {$filter_cond} ORDER BY $sidx $sord LIMIT $start,$limit" ;

$SQL1 = " select internal_code as id,$table_field_name from  $table_name  
ORDER BY $sidx $sord LIMIT $start , $limit"; 
  
}
 
 //echo  $SQL; die(); 
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count; 
//echo $SQL; die();
$lobj_rs =$aobj_context->mobj_db->GetAll($SQL); 
  if((mysql_error()!=''))
 $lobj_rs =$aobj_context->mobj_db->GetAll($SQL1);  
 
foreach($lobj_rs as $key =>$value)
{
$val='';
$responce->rows[$key]['id']=$value[id];
$l=0;
 	foreach($value as $key1 =>$value1)
	{	 
		if($key1!='id'){ 
	    $val[$l]=$value[$key1];
		 $l++; 
		}
	} 
$responce->rows[$key]['cell']=($val);
}  
echo $json->encode($responce);
}	
function viewUserSchemaSrefDataDetails($aobj_context)
{
$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
$search_ref_module_db_field=$aobj_context->mobj_data["search_ref_module_db_field"];
$search_ref_module_value=$aobj_context->mobj_data["search_ref_module_value"];
$system_module_table_name=$aobj_context->mobj_data["system_module_table_name"];
$search=$aobj_context->mobj_data["_search"];
$table_name=$aobj_context->mobj_data["table_name"];
$schema_id=$aobj_context->mobj_data["schema_id"];
$search_ref_module_value=str_ireplace("~"," ",$search_ref_module_value);
$clicked_id_system_name=$aobj_context->mobj_data["clicked_id_system_name"];
$base_module_id=$aobj_context->mobj_data["base_module_id"];
$filter_cond="";
 

$search_db_fields_arr=getSearchFieldIdsArr($aobj_context,$table_name,$schema_id,'1');
 
if(!empty($search_ref_module_db_field)  )
{
//$filter_cond=" and {$search_db_fields_arr[$search_ref_module_db_field]} = '{$search_ref_module_value}' ";
$filter_cond=" and  {$table_name}.{$search_ref_module_db_field} = '{$search_ref_module_value}' ";
}
$filter_toolbal_cond="";
if($search)
{
	foreach($aobj_context->mobj_data as $search_k=>$search_v)
	{
		if(!empty($search_db_fields_arr[$search_k]) &&  !is_null($search_db_fields_arr[$search_k]))
		$filter_toolbal_cond.=" and {$search_db_fields_arr[$search_k]} like '{$search_v}%' ";	
	}
}	
 
 include_once("/JSON.php");
//echo $lstr_ids;
$json = new Services_JSON();
$limit = $_GET['rows'];
$sidx = $_GET['sidx'];
$sord = $_GET['sord'];
$page = $_GET['page'];

if(!$sidx)
$sidx =1;

// to take th e parameters from Search field
if(isset($_GET['searchField']) && $_GET["_search"]=='true')
{
$field_name=$_GET['searchField'];
$final_value=getsearchcondition($_GET['searchOper'],$_GET['searchString']);
$cond=$final_value[0];
$value=$final_value[1];
}

$qry=getGeneralViewQry($aobj_context,$table_name,$schema_id,'include_in_popup') ." where 1<>0 {$filter_cond} {$filter_toolbal_cond}" ;
 
$cnt_qry=getGeneralViewCountQry($aobj_context,$table_name,$schema_id,'include_in_popup') ." where 1<>0 {$filter_cond} {$filter_toolbal_cond}" ;
 
if(!empty($system_module_table_name) && !empty($clicked_id_system_name))
{
	$root_file_name=$aobj_context->main_src."/validation_files/validate_".$system_module_table_name.".php";
	$function_name=$clicked_id_system_name;
		if (file_exists($root_file_name)) {
			require_once($root_file_name);
			$data_arr=call_user_func($function_name,$aobj_context,$qry,$cnt_qry,'Pop Up');
			$qry=(!empty($data_arr['qry']))?$data_arr['qry']:$qry;
			$cnt_qry=(!empty($data_arr['cnt_qry']))?$data_arr['cnt_qry']:$cnt_qry;
			}
} 
if(isset($_GET['searchField']) && $_GET["_search"]=='true')
{

$count_id="{$cnt_qry}   and {$search_db_fields_arr[$field_name]} {$cond}{$value}";
}
else
{
$count_id=$cnt_qry;
}
$result = mysql_query($count_id);
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$count = $row['count'];


// calculation of total pages for the query
if( $count >0 )
{
$total_pages = ceil($count/$limit);
}
else
{
$total_pages = 0;
}
if ($page > $total_pages)
$page=$total_pages;
$start = $limit*$page - $limit;
if($start <0)
$start = 0;
// the actual query for the grid data
if(isset($_GET['searchField']) && $_GET["_search"]=='true')
{
$SQL="  {$qry}  and {$search_db_fields_arr[$field_name]} $cond$value
ORDER BY $sidx $sord LIMIT $start,$limit";
}
else
{
$SQL = " {$qry} ORDER BY {$sidx} {$sord} LIMIT {$start} , {$limit}";
}
 //echo $SQL;
 
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count; 
$lobj_rs =$aobj_context->mobj_db->GetAll($SQL); 
 
foreach($lobj_rs as $key =>$value)
{
$val='';
$responce->rows[$key]['id']=$value[id];
$l=0;
 	foreach($value as $key1 =>$value1)
	{	 
		if($key1!='id'){ 
	    $val[$l]=$value[$key1];
		 $l++; 
		}
	} 
$responce->rows[$key]['cell']=($val);
}  
echo $json->encode($responce);
}		
function deleteUserSchemaModuleDetails($aobj_context)
{
$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
			$lstr_ids = $aobj_context->mobj_data["id"];	
         	$table = $aobj_context->mobj_data["table_name"];
			$schema_code_obj=explode("_",$table);		
				
			$get_schema_id="select internal_code from s_user_schema where system_module_table_name='{$table}'";	
			$larray_resultset = $aobj_context->mobj_db->GetRow($get_schema_id);
			$schema_code=$larray_resultset[internal_code];
			$user_schema_code = $schema_code;
			
			$internal_code =  $lstr_ids;
			$error_msg='0';
		$root_file_name=$aobj_context->main_src."/validation_files/validate_".$table.".php";
	
		if (file_exists($root_file_name)) 
		{
			require_once($root_file_name);
				if (function_exists('preDelete'))
				{
				$error_msg=preDelete($aobj_context,$lstr_ids); 
					
				}
		}  
		if($error_msg=='0')
		{
					$sql_query="select internal_code,ref_s_user_schema_code 
					from s_user_schema_elements where ref_module_code=".$user_schema_code;
				//	echo $sql_query;die();
					$larray_resultset =  array();
					
					$larray_resultset = $aobj_context->mobj_db->GetAll($sql_query);
						
					   if(count($larray_resultset)>0)
					   {
						
						 $lstr_dbtablename='';
						 foreach($larray_resultset as $larrayrow)
						 {
							  $lstr_dbtablename = 'module_'.$larrayrow['ref_s_user_schema_code'];
							  $lstr_dbfieldname = 'm_'.$larrayrow['ref_s_user_schema_code'].'_ele_'.$larrayrow['internal_code'];
							  $lstr_query = "select count($lstr_dbfieldname) as cnt from $lstr_dbtablename where $lstr_dbfieldname=".$internal_code;
							  $lint_result_count = $aobj_context->mobj_db->GetRow($lstr_query);
							 
							   $lint_result_count = $lint_result_count['cnt'];
							   if($lint_result_count>0)
							   {  
								 $error_sql = "select module_name from s_user_schema where internal_code=".$larrayrow['ref_s_user_schema_code'];
								 $larray_name = $aobj_context->mobj_db->GetRow($error_sql);
								 $lstr_name=$larray_name['module_name'];
								 $lstr_mixed_bool ="data has been reffered in in module $lstr_name so you cannot delete it";
								 echo $aobj_context->mobj_output->ToJSONEnvelope($lstr_mixed_bool,-1,"Failure");
								 return $lstr_mixed_bool;
							   }
							  
						 }
									   
					   }
					  
				session_start();
				$user_type=$_SESSION['user_type'];
				$user_id=$_SESSION['user_id'];
				$user_dept=$_SESSION['user_dept'];
				$get_rights="select  is_del,is_del_group_records,is_del_dep_records,is_del_all_records
						from user_rights where ref_s_sysdb_code={$user_id}
						and module_id='{$user_schema_code}'";
					 $obj_get_rights = $aobj_context->mobj_db->GetRow($get_rights);	
					 $own=$obj_get_rights[is_del];
					 $group=$obj_get_rights[is_del_group_records];
					 $dep=$obj_get_rights[is_del_dep_records];
					 $all=$obj_get_rights[is_del_all_records];
				if(strtolower($user_type)=='admin' || $all=='1')
				{
				$lobj_rs=deleteModuleDetailsRecord($aobj_context,$table,$lstr_ids,$user_schema_code);
				}	 
				else
				{					
					 if($own=='0')
					 {
						 $data ="You dont have a permission to delete the record";
									 echo $aobj_context->mobj_output->ToJSONEnvelope($data,-1,"Failure");
									 return $data;
					 }
					 else if($own=='1' && $dep=='0') //checks for Own Records Only
					 {
						 $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);
						 
							if($obj_get_own_created_by_id[created_by]==$user_id)
							{
							 $lobj_rs=deleteModuleDetailsRecord($aobj_context,$table,$lstr_ids,$user_schema_code);
							}
							else
							{
							 $data ="You can delete the records of only created by You";
									 echo $aobj_context->mobj_output->ToJSONEnvelope($data,-1,"Failure");
									 return $data;
							}
					 }
					 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}";
							 $obj_u_dep = $aobj_context->mobj_db->GetRow($get_users_of_department);	
							 $dep_users= $obj_u_dep[dep_users];
						
							$get_dep_created_by_id="select created_by from {$table} 
											where internal_code in ({$lstr_ids})
											and created_by in ({$dep_users})";
						 $obj_get_dep_created_by_id = $aobj_context->mobj_db->GetAll($get_dep_created_by_id);
						 if($obj_get_dep_created_by_id)
						 {
							$lobj_rs=deleteModuleDetailsRecord($aobj_context,$table,$lstr_ids,$user_schema_code);
						 }
						 else
							{
							 $data ="You dont have a permission to delete the records created by Department";
									 echo $aobj_context->mobj_output->ToJSONEnvelope($data,-1,"Failure");
									 return $data;
							}
					 }
						
				}
			// delete from grup details
		$lstr_del_group_details_qry="SELECT  parent_group_code,CONCAT(system_module_table_name,'_',system_group_table_name,'_group')
							AS group_table,su.ref_s_user_schema_group_code 
							FROM s_user_schema_elements  su 
							INNER JOIN s_user_schema s ON s.internal_code=su.ref_s_user_schema_code
							INNER JOIN s_user_schema_group gr ON gr.internal_code=su.ref_s_user_schema_group_code
							WHERE su.ref_s_user_schema_code= {$user_schema_code}
							AND su.ref_s_user_schema_group_code>0 
							GROUP BY ref_s_user_schema_group_code";
		$obj_lstr_del_group_details_qry = $aobj_context->mobj_db->GetAll($lstr_del_group_details_qry);
			$cnt=0;
			foreach($obj_lstr_del_group_details_qry as $pk=>$pv)
			{
				if($pv[parent_group_code]>0)
				{
				$child_group_tables[$pv[parent_group_code]][$cnt]=$pv[group_table];
				$cnt++;
				}
			}
	 
				if(count($obj_lstr_del_group_details_qry)>0)
				  {
					foreach($obj_lstr_del_group_details_qry as $key=>$value)
					{
						if($value[parent_group_code]==0)
						{
						$select_int_codes="select group_concat(internal_code) as 
										group_int_codes
									from {$value["group_table"]} 
									where ref_s_ref_data_code in({$lstr_ids})";
						 $obj_select_int_codes = $aobj_context->mobj_db->GetRow($select_int_codes);
						 
						 $child_group_tables_ids[$value[ref_s_user_schema_group_code]]=$obj_select_int_codes[group_int_codes];
						$delete_group="delete from ".$value["group_table"]." 
									where ref_s_ref_data_code in({$lstr_ids})";
						$lobj_delete_group = $aobj_context->mobj_db->Execute($delete_group);
						 
						}
					}
					foreach($child_group_tables as $ck=>$cv)
					{
						$group_id=$ck;
						$s_ref_data_codes=$child_group_tables_ids[$group_id];
						foreach($cv as $ckk)
						{
						$child_group_table_name=" delete from {$ckk} where 
											ref_s_ref_data_code in({$s_ref_data_codes})";
						$lobj_child_group_table_name = $aobj_context->mobj_db->Execute($child_group_table_name);					
					 
						}
					}
				  } 
				
		}
		else
		{
			$rdata=$error_msg;
			echo $aobj_context->mobj_output->ToJSONEnvelope($rdata,-1,"Failure");
			return 	$rdata;
		}
	
}	
function deleteModuleDetailsRecord($aobj_context,$table,$lstr_ids,$user_schema_code)
{
$lstr_del_meta_qry = "delete  
					from {$table} 
					 where internal_code in (".$lstr_ids.")"; 
 $lobj_rs = $aobj_context->mobj_db->Execute($lstr_del_meta_qry);
 //echo $lstr_del_meta_qry;die();
				  
				  
	if($lobj_rs) 
	{
		$rdata="Deleted Successfully";
		echo $aobj_context->mobj_output->ToJSONEnvelope($rdata,0,"Success"); 
		return 	$rdata;
	}
	else
	{	
		$rdata="Deletion Failed";
		echo $aobj_context->mobj_output->ToJSONEnvelope($rdata,-1,"Failure");
		return 	$rdata;
	}
}
function deleteUserSchemaModuleGroupDetails($aobj_context)
{
	$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
			$s_ref_data_code = $aobj_context->mobj_data["s_ref_data_code"];	 
			$table_name = $aobj_context->mobj_data["table_name"];
			$id = $aobj_context->mobj_data["id"];
			$table_name= str_replace("group","entry",$table_name); 
 
		$lstr_del_meta_qry = "delete  
			from {$table_name} 
			 where   internal_code in({$id})"; 
		 
	 $lobj_rs = $aobj_context->mobj_db->Execute($lstr_del_meta_qry);
	   
	  
	  
        if($lobj_rs) 
		{
		$rdata="Deleted Successfully";
            echo $aobj_context->mobj_output->ToJSONEnvelope($rdata,0,"success"); 
		return 	$rdata;
		}
		 else
		{	$rdata="Deletion Failed";
            echo $aobj_context->mobj_output->ToJSONEnvelope($rdata,-1,"Failure");
			return 	$rdata;
		}
	
}	
 
function displayUserSetUpDetails($aobj_context)
  {
  include_once("/JSON.php"); 
 
//echo 	$lstr_ids;		
	$json = new Services_JSON(); 
	$limit = $_GET['rows']; 
	$sidx = $_GET['sidx']; 
	$sord = $_GET['sord']; 
	$page = $_GET['page'];
	if(!$sidx) 
	$sidx =1; 
	
	// to take th e parameters from Search field
	if(isset($_GET['searchField']) && $_GET["_search"]=='true')
	{
		$field_name=$_GET['searchField'];
		$final_value=getsearchcondition($_GET['searchOper'],$_GET['searchString']);
		$cond=$final_value[0];
		$value=$final_value[1];
	}	
	if(isset($_GET['searchField']) && $_GET["_search"]=='true')
	{		
		$count_id="select count(*) as count  from 
		(select s.internal_code as id,
			s.sysusr as grid_sysusr,
			s.sysusrkey as grid_sysusrkey,
			t.value as  grid_user_type,
			ifnull(s.real_name,'') as grid_real_name,
			ifnull(s.email_id,'') as grid_email_id,
			ifnull(s1.sysusr,'') as grid_user_dept,
			ifnull(s2.sysusr,'') as grid_user_group,
			s.can_edit_help_text as grid_can_edit_help_text
			from s_sysdb s inner join s_ref_type t on t.internal_code=s.user_type
			left join  s_sysdb s1 on s.user_dept=s1.internal_code
			left join  s_sysdb s2 on s.user_group=s2.internal_code
			  )a 
			where $field_name $cond$value";
		
	}
	else
	{
	$count_id="	select count(*) as count
				from s_sysdb s inner join s_ref_type t on t.internal_code=s.user_type
				left join  s_sysdb s1 on s.user_dept=s1.internal_code
				left join  s_sysdb s2 on s.user_group=s2.internal_code";
	}	
			$result = mysql_query($count_id);
			 
			$row = mysql_fetch_array($result,MYSQL_ASSOC);
			$count = $row['count'];
			 
 
 // calculation of total pages for the query 
	if( $count >0 ) 
	{ 
		$total_pages = ceil($count/$limit); 
	} 
	else 
	{ 
		$total_pages = 0; 
	} 
	if ($page > $total_pages) 
	$page=$total_pages; 
	$start = $limit*$page - $limit; 
	
	if($start <0) 
	$start = 0; 
	// the actual query for the grid data 
	if(isset($_GET['searchField']) && $_GET["_search"]=='true')
	{
			
		$SQL="select * from (select s.internal_code as id,
					s.sysusr as grid_sysusr,
					s.sysusrkey as grid_sysusrkey,
					t.value as  grid_user_type,
					ifnull(s.real_name,'') as grid_real_name,
					ifnull(s.email_id,'') as grid_email_id,
					ifnull(s1.sysusr,'') as grid_user_dept,
					ifnull(s2.sysusr,'') as grid_user_group,
					s.can_edit_help_text as grid_can_edit_help_text
					from s_sysdb s inner join s_ref_type t on t.internal_code=s.user_type
					left join  s_sysdb s1 on s.user_dept=s1.internal_code
					left join  s_sysdb s2 on s.user_group=s2.internal_code
					 )a where  $field_name $cond$value
			ORDER BY $sidx $sord LIMIT $start,$limit";
			
			
	}
	else
	{
	$SQL = "select s.internal_code as id,
			s.sysusr as grid_sysusr,
			s.sysusrkey as grid_sysusrkey,
			t.value as  grid_user_type,
			ifnull(s.real_name,'') as grid_real_name,
			ifnull(s.email_id,'') as grid_email_id,
			ifnull(s1.sysusr,'') as grid_user_dept,
			ifnull(s2.sysusr,'') as grid_user_group,
			s.can_edit_help_text as grid_can_edit_help_text
			from s_sysdb s inner join s_ref_type t on t.internal_code=s.user_type
			left join  s_sysdb s1 on s.user_dept=s1.internal_code
			left join  s_sysdb s2 on s.user_group=s2.internal_code
			 
			
			 ORDER BY $sidx $sord LIMIT $start , $limit"; 
			//echo $SQL;  	
	}
	  
	
	
	$responce->page = $page; 
	$responce->total = $total_pages; 
	$responce->records = $count; 
	$lobj_rs = $aobj_context->mobj_db->GetAll($SQL); 
	 foreach($lobj_rs as $key =>$value)
	 {
	 $responce->rows[$key]['id']=$value[id];
	 $responce->rows[$key]['cell']=array($value[grid_sysusr],$value[grid_sysusrkey],$value[grid_user_type],
						 $value[grid_real_name],$value[grid_email_id],$value[grid_user_dept],$value[grid_user_group],
						 $value[grid_can_edit_help_text]);
	}
	
	
	
	
	// return the formated data 
	echo $json->encode($responce); 
}

function deleteUserSetUpDetails($aobj_context)
{
	$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
			$lstr_ids = $aobj_context->mobj_data["id"];	 

		$lstr_qry = "delete  
			from s_sysdb 
			 where internal_code in (".$lstr_ids.")"; 
		
	
	 $lobj_rs = $aobj_context->mobj_db->Execute($lstr_qry);
	 
     
        if($lobj_rs) 
		{
		$rdata="Deleted Successfully";
            echo $aobj_context->mobj_output->ToJSONEnvelope($rdata,0,"success"); 
		return 	$rdata;
		}
		 else
		{	$rdata="Deletion Failed";
            echo $aobj_context->mobj_output->ToJSONEnvelope($rdata,-1,"Failure");
			return 	$rdata;
		}
	
}	
function viewTransactionSeriesGrid($aobj_context)
  {
  include_once($aobj_context->main_src.'/src/display_details_in_grid_custom.php');
		$display_obj=new display_details_in_grid($aobj_context);
		 
		$display_obj->table_cols=array('grid_ref_module_code','grid_ref_module_element_code',
									'grid_series_name','grid_prefix','grid_starting_no',
											'grid_starting_no','grid_ending_no',
											'grid_last_no'
											);
		  $display_obj->display_qry="select tsp.internal_code as id,
									us.module_name as grid_ref_module_code,
									uss.name as grid_ref_module_element_code,
									series_name as grid_series_name,
									prefix as grid_prefix,
									starting_no as grid_starting_no,
									ending_no as grid_ending_no,
									last_no as grid_last_no,
									DATE_FORMAT(starting_date,'%d/%m/%Y') as grid_starting_date,
									DATE_FORMAT(ending_date,'%d/%m/%Y')  as grid_ending_date
									from transaction_series_prefix tsp 
									inner join s_user_schema us on us.internal_code=ref_module_code
									inner join s_user_schema_elements uss on uss.internal_code=ref_module_element_code
									";//
		 
		$display_obj->DisplayDetailsInGrid();
	
}	
function deleteTransactionSeries($aobj_context)
{
	$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
			$lstr_ids = $aobj_context->mobj_data["id"];	 

		$lstr_qry = "delete  
			from transaction_series_prefix 
			 where internal_code in (".$lstr_ids.")"; 
		$lobj_rs = $aobj_context->mobj_db->Execute($lstr_qry); 	 
		  if($lobj_rs) 
		{
		$rdata="Deleted Successfully";
            echo $aobj_context->mobj_output->ToJSONEnvelope($rdata,0,"success"); 
		return 	$rdata;
		}
		 else
		{	$rdata="Deletion Failed";
            echo $aobj_context->mobj_output->ToJSONEnvelope($rdata,-1,"Failure");
			return 	$rdata;
		}
}
function viewUserSchemaModuleGroupDetails($aobj_context)
{
$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
$search =$aobj_context->mobj_data["_search"];
$schema_id =$aobj_context->mobj_data["schema_id"];
$group_id =$aobj_context->mobj_data["group_id"];
$table_name =$aobj_context->mobj_data["table_name"];
$table_name=str_replace("group","entry",$table_name);

include_once("/JSON.php");
 
//echo $lstr_ids;
$json = new Services_JSON();
$limit = $_GET['rows'];
$sidx = $_GET['sidx'];
$sord = $_GET['sord'];
$page = $_GET['page'];
$s_ref_data_code=$_GET['s_ref_data_code'];
if(!$sidx)
$sidx =1;
 
 
$login_cnt=$_GET['login_cnt'];

 

// this field we are not using for main query display selection since we need to get the data from REference tables, we are mannualy doing the quey below
$table_field_name=stripcslashes($_GET['field_name']);
$details_array=$_GET['details_array'];
$group_qry=getGroupDetailsQry($aobj_context,$table_name,$schema_id,$group_id,'Normal');
 
// to take th e parameters from Search field
$filter_toolbal_cond=""; 		 
	if($search)
	{
		$field_arr=explode(",",$table_field_name);
		 foreach($aobj_context->mobj_data as $search_k=>$search_v)
			{
				if(in_array($search_k,$field_arr))
				$filter_toolbal_cond.=" and {$search_k} like '{$search_v}%' ";	
			}
	 	
	$count_id="select count(*) as count from 
					( {$group_qry} 
							 where ref_s_ref_data_code= $s_ref_data_code  and login_cnt={$login_cnt})a 
							 where id>0 {$filter_toolbal_cond}";
 
	}
else
{
$count_id="select count(*) as count from $table_name where ref_s_ref_data_code=$s_ref_data_code  and login_cnt={$login_cnt}";
}
$result = mysql_query($count_id);
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$count = $row['count'];

// calculation of total pages for the query
if( $count >0 )
{
$total_pages = ceil($count/$limit);
}
else
{
$total_pages = 0;
}
if ($page > $total_pages)
$page=$total_pages;
$start = $limit*$page - $limit;

if($start <0)
$start = 0;

// the actual query for the grid data
if($search)
{

$SQL="select * from ( {$group_qry} 
							 where 
							 (ref_s_ref_data_code= $s_ref_data_code || ref_s_ref_data_code=0)
							 and login_cnt={$login_cnt} )a  where id>0 {$filter_toolbal_cond}
ORDER BY $sidx $sord LIMIT $start,$limit";
 
 
}
else
{

							$SQL="select $table_name.internal_code as id,$fields from 	$table_name  $join_qry  
							 where (ref_s_ref_data_code= $s_ref_data_code || ref_s_ref_data_code=0)
							  and login_cnt={$login_cnt} ORDER BY $sidx $sord LIMIT $start , $limit";
						 	  
							$SQL=" {$group_qry} 
							 where (ref_s_ref_data_code= $s_ref_data_code || ref_s_ref_data_code=0)
							  and login_cnt={$login_cnt} ORDER BY $sidx $sord LIMIT $start , $limit";	
							    

		 				 
  
 $SQL1 = " select internal_code as id,$table_field_name from  $table_name  
 where    (ref_s_ref_data_code= $s_ref_data_code || ref_s_ref_data_code=0)  and login_cnt={$login_cnt}
ORDER BY $sidx $sord LIMIT $start , $limit"; 
}
 
     
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count; 
$lobj_rs =$aobj_context->mobj_db->GetAll($SQL); 
  if((mysql_error()!=''))
 $lobj_rs =$aobj_context->mobj_db->GetAll($SQL1);  
 
foreach($lobj_rs as $key =>$value)
{
$val='';
$responce->rows[$key]['id']=$value[id];
$l=0;
 	foreach($value as $key1 =>$value1)
	{	 
		if($key1!='id'){ 
	    $val[$l]=$value[$key1];
		 $l++; 
		}
	} 
$responce->rows[$key]['cell']=($val);
}  
echo $json->encode($responce);
}
function viewUserSchemaModuleGridInlineGroupDetails($aobj_context)
{
$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
$geditinternalcode =$aobj_context->mobj_data["geditinternalcode"];
/* if($geditinternalcode==0)
{
return '{"page":0,"total":0,"records":"0"}';
} */

$search =$aobj_context->mobj_data["_search"];
$schema_id =$aobj_context->mobj_data["schema_id"];
$group_id =$aobj_context->mobj_data["group_id"];
$table_name =$aobj_context->mobj_data["table_name"];
$table_name=str_replace("group","entry",$table_name);

include_once("/JSON.php");
 
//echo $lstr_ids;
$json = new Services_JSON();
$limit = $_GET['rows'];
$sidx = $_GET['sidx'];
$sord = $_GET['sord'];
$page = $_GET['page'];
$s_ref_data_code=$_GET['s_ref_data_code'];
if(!$sidx)
$sidx =1;
 
 
$login_cnt=$_GET['login_cnt'];

 

// this field we are not using for main query display selection since we need to get the data from REference tables, we are mannualy doing the quey below
$table_field_name=stripcslashes($_GET['field_name']);
$details_array=$_GET['details_array'];
$group_qry=getGroupDetailsQry($aobj_context,$table_name,$schema_id,$group_id,'Inline');
  
// to take th e parameters from Search field
$filter_toolbal_cond=""; 		 
	if($search)
	{
	 
		$field_arr=explode(",",$table_field_name);
		//print_r($field_arr);
		 foreach($aobj_context->mobj_data as $search_k=>$search_v)
			{
				if(in_array($search_k,$field_arr))
				$filter_toolbal_cond.=" and {$search_k} like '{$search_v}%' ";	
			}
	 	
	$count_id="select count(*) as count from 
					( {$group_qry} 
							 where ref_s_ref_data_code= $s_ref_data_code  and login_cnt={$login_cnt})a 
							 where id>0 {$filter_toolbal_cond}";
 
	}
else
{
$count_id="select count(*) as count from $table_name where ref_s_ref_data_code=$s_ref_data_code  and login_cnt={$login_cnt}";
}
$result = mysql_query($count_id);
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$count = $row['count'];

// calculation of total pages for the query
if( $count >0 )
{
$total_pages = ceil($count/$limit);
}
else
{
$total_pages = 0;
}
if ($page > $total_pages)
$page=$total_pages;
$start = $limit*$page - $limit;

if($start <0)
$start = 0;
 
// the actual query for the grid data
if($search)
{

$SQL="select * from ( {$group_qry} 
							 where 
							 (ref_s_ref_data_code= $s_ref_data_code || ref_s_ref_data_code=0)
							 and login_cnt={$login_cnt} )a  where id>0 {$filter_toolbal_cond}
ORDER BY $sidx $sord LIMIT $start,$limit";
 
 
}
else
{

							$SQL="select $table_name.internal_code as id,$fields from 	$table_name  $join_qry  
							 where (ref_s_ref_data_code= $s_ref_data_code || ref_s_ref_data_code=0)
							  and login_cnt={$login_cnt} ORDER BY $sidx $sord LIMIT $start , $limit";
						 	  
							$SQL=" {$group_qry} 
							 where (ref_s_ref_data_code= $s_ref_data_code || ref_s_ref_data_code=0)
							  and login_cnt={$login_cnt} ORDER BY $sidx $sord LIMIT $start , $limit";	
							    

		 				 
  
 $SQL1 = " select internal_code as id,$table_field_name from  $table_name  
 where    (ref_s_ref_data_code= $s_ref_data_code || ref_s_ref_data_code=0)  and login_cnt={$login_cnt}
ORDER BY $sidx $sord LIMIT $start , $limit"; 
}

    
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count; 
 
$lobj_rs =$aobj_context->mobj_db->GetAll($SQL); 
  if((mysql_error()!=''))
 $lobj_rs =$aobj_context->mobj_db->GetAll($SQL1);  
 
foreach($lobj_rs as $key =>$value)
{
$val='';
$responce->rows[$key]['id']=$value[id];
$l=0;
 	foreach($value as $key1 =>$value1)
	{	 
		if($key1!='id'){ 
	    $val[$l]=$value[$key1];
		 $l++; 
		}
	} 
$responce->rows[$key]['cell']=($val);
}  
echo $json->encode($responce);
}

function viewUserSchemaModuleGridGroupDetails($aobj_context)
{
$geditinternalcode =$aobj_context->mobj_data["geditinternalcode"];




$search =$aobj_context->mobj_data["_search"];
$field_name =$aobj_context->mobj_data["field_name"];
$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
include_once("/JSON.php");

//echo $lstr_ids;
$json = new Services_JSON();
$limit = $_GET['rows'];
$sidx = $_GET['sidx'];
$sord = $_GET['sord'];
$page = $_GET['page'];
$s_ref_data_code=$_GET['s_ref_data_code'];
$group_id=$_GET['group_id'];
if(!$sidx)
$sidx =1;
$s_indx=explode("grid_",$sidx);
if(!empty($s_indx[1]))
$sidx =$s_indx[1];
 
$login_cnt=$_GET['login_cnt'];
$table_name=$_GET['table_name'];
 
$get_user_defined_group_qry="select grid_edit_query,grid_edit_search_fields from 
							s_user_schema_group where internal_code={$group_id}";
$obj_group_qry =$aobj_context->mobj_db->GetRow($get_user_defined_group_qry); 
 
$qry=$obj_group_qry['grid_edit_query'];
$grid_edit_search_fields=explode(",",$obj_group_qry['grid_edit_search_fields']);
 $function_exists=strpos($qry,'select');
if ($function_exists === false) 
{
require_once($aobj_context->main_src."/custom_src/GetMultiGroupGridEditQry.php");
 
$qry= call_user_func($qry,$aobj_context);
}
else
{					
 eval("\$qry = \"$qry\";");
}

$exclude_arr=array("_search","a","field_name","filters","geditinternalcode","group_id","login_cnt","nd",
					"page","rows","s_ref_data_code","sidx","sord","table_name","Qx0",
					"username","login_time","user_type","can_edit_help_text","host_url","real_name","super_wiser",
					"pswd","image_path","user_dept","user_group","jt_db", 
					"user_id","jt_db like","client_type","jquery-ui-theme_name","PHPSESSID");
					 
$filter_toolbal_cond=""; 		 
	if($search || $search=='true')
	{
	 //print_r($grid_edit_search_fields);
		$field_arr=explode(",",$field_name);
		//print_r($field_arr);
		 foreach($aobj_context->mobj_data as $search_k=>$search_v)
			{
				$search_v=trim($search_v);
				if(!in_array($search_k,$exclude_arr) && in_array($search_k,$grid_edit_search_fields))
				$filter_toolbal_cond.=" and {$search_k} like '{$search_v}%' ";	
			}
	}	
 // echo $filter_toolbal_cond;die();	
if($search=='true')
{
$count_id="select count(*) as count from ({$qry})a  where 1=1 {$filter_toolbal_cond} ";
}
else 
{
$count_id="select count(*) as count from ({$qry})a ";
}
 
 
$result = mysql_query($count_id);
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$count = $row['count'];

// calculation of total pages for the query
if( $count >0 )
{
$total_pages = ceil($count/$limit);
}
else
{
$total_pages = 0;
}
if ($page > $total_pages)
$page=$total_pages;
$start = $limit*$page - $limit;

if($start <0)
$start = 0;

// the actual query for the grid data

if($search=='true')
{

$SQL=" select * from ({$qry}) a  where 1=1 {$filter_toolbal_cond} ORDER BY {$sidx} {$sord} LIMIT {$start},{$limit}";

 
}
else
{
$SQL=" {$qry} ORDER BY {$sidx} {$sord} LIMIT {$start},{$limit}";
}
 


//echo $SQL; die();

$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count; 
$lobj_rs =$aobj_context->mobj_db->GetAll($SQL); 
  if((mysql_error()!=''))
 $lobj_rs =$aobj_context->mobj_db->GetAll($SQL1);  
 
foreach($lobj_rs as $key =>$value)
{
$val='';
$responce->rows[$key]['id']=$value[id];
$l=0;
 	foreach($value as $key1 =>$value1)
	{	 
		if($key1!='id'){ 
	    $val[$l]=$value[$key1];
		 $l++; 
		}
	} 
$responce->rows[$key]['cell']=($val);
}  
echo $json->encode($responce);
}
function getGeneralViewQry($aobj_context,$table_name,$schema_id,$type)
{
 
$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 concat(' left join ',s.system_module_table_name, 
			' as ',s.system_module_table_name ,'_',sus.internal_code,
			' on ',s.system_module_table_name,'_',sus.internal_code,'.internal_code =',
			sch.system_module_table_name ,'.', db_field) as join_table_name
			from s_user_schema_elements  sus
			inner join s_user_schema s on sus.ref_module_code=s.internal_code
			inner join s_user_schema sch on sus.ref_s_user_schema_code=sch.internal_code
			where ref_s_user_schema_code={$schema_id} and sus.{$type}=1
			and is_multiple_group=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  from 
							(select table_name,name,field_name as grid_field_name,
							if(data_type!='Reference Data',
							if(data_type='Date',concat('date_format(',table_name,'.',field_name,',\"%d/%m/%Y\") as ', field_name) ,
							concat(table_name,'.',field_name,' as ', field_name)),
							concat(\"concat(\",f1,f2,\") as \",field_name)) as field_name 
						 
							,ref_module_code from 
							(select  s.internal_code, su.internal_code as ele_id,
							ref_module_code, s.system_module_table_name as table_name ,
							field_id,
							if(ifnull(field_id1,'')!='',concat(table_name,'_',su.internal_code,'.',field_id1
							),'') as f1,
							if(ifnull(field_id2,'')!='',concat(',\'-\',',table_name,'_',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 ,t1.value as data_type
							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 t1 on t1.internal_code=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  su.{$type}=1
							and su.is_multiple_group=0
							order by ifnull(ug.sequence,0),su.sequence,su.internal_code ) l 
							where   multiple=0 )   a ";		
				 $lobj_get_display_cols = $aobj_context->mobj_db->GetRow($get_display_cols);	
  
		$qry = " select {$table_name}.internal_code as id,{$lobj_get_display_cols[field_name]}   from 
				{$table_name}  {$join_qry}  
				";
		$view_cond="";	
 
		if($type=='include_in_grid')
		{		
		$qry = " select {$table_name}.internal_code as id,{$lobj_get_display_cols[field_name]},
				concat(ifnull(sys_db.sysusr,'No User'),'-',date_format({$table_name}.created_date,'%d.%m.%y')) as created_by  from 
				{$table_name}  {$join_qry}
				left join s_sysdb sys_db on sys_db.internal_code={$table_name}.created_by
				";
	 
		$view_cond=getViewCondition($aobj_context,$schema_id,$table_name);
		$qry.=" {$view_cond}";	
 		}	
 
return 	$qry;			
}
function getViewCondition($aobj_context,$schema_id,$table_name)
{

	
	session_start();
	$user_type=$_SESSION['user_type'];
	$user_id=$_SESSION['user_id'];
	$user_group=$_SESSION['user_group'];
	$user_dept=$_SESSION['user_dept'];
	$get_rights="select  is_view,is_view_group_records,is_view_dep_records,is_view_all_records
			from user_rights where ref_s_sysdb_code in({$user_id},{$user_group})
			and module_id='{$schema_id}'";
		 $obj_get_rights = $aobj_context->mobj_db->GetRow($get_rights);	
		 
		 $own=$obj_get_rights[is_view];
		 $group=$obj_get_rights[is_view_group_records];
		 $dep=$obj_get_rights[is_view_dep_records];
		 $all=$obj_get_rights[is_view_all_records];
	$view_cond=" where 1<>1";
	if(strtolower($user_type)=='admin' || $all=='1')
	{
	$view_cond=" where 1=1 ";
	}	 
	else
	{					
		 if($own=='0')
		 {
			 $view_cond=" where {$table_name}.created_by=0 ";
		 }
		 else if($own=='1' && $dep=='0') //checks for Own Records Only
		 {
			$view_cond=" where {$table_name}.created_by={$user_id} ";							 
		 }
		 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}";
				 $obj_u_dep = $aobj_context->mobj_db->GetRow($get_users_of_department);	
				 $dep_users= $obj_u_dep[dep_users];
			$view_cond=" where {$table_name}.created_by in ({$dep_users}) ";
		}else if($group=='1')//checks for Department Records Only
		 {
				$get_users_of_groups="select GROUP_CONCAT(internal_code) as grp_users
					from s_sysdb where user_group={$group}";
				 $obj_u_grp = $aobj_context->mobj_db->GetRow($get_users_of_groups);	
				 $grp_users= $obj_u_dep[grp_users];
			$view_cond=" where {$table_name}.created_by in ({$grp_users}) ";
		}
	}
	 
return $view_cond;			 
}
function getGeneralViewCountQry($aobj_context,$table_name,$schema_id,$type)
{
		$select_ref_module_code="select 
			GROUP_CONCAT(join_table_name SEPARATOR ' ') as qry from 
			(select concat(' left join ',s.system_module_table_name, 
			' as ',s.system_module_table_name ,'_',sus.internal_code,
			' on ',s.system_module_table_name,'_',sus.internal_code,'.internal_code =',
			sch.system_module_table_name ,'.', db_field) as join_table_name
			from s_user_schema_elements  sus
			inner join s_user_schema s on sus.ref_module_code=s.internal_code
			inner join s_user_schema sch on sus.ref_s_user_schema_code=sch.internal_code
			where ref_s_user_schema_code={$schema_id} and  include_in_grid={$type}
			and is_multiple_group=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]:'';
		$qry = " select count(1) as count from 
				{$table_name}  {$join_qry}";
				
		if($type=='include_in_grid')
		{
		$view_cond=getViewCondition($aobj_context,$schema_id,$table_name);
		$qry.=" {$view_cond}";		
		}		
return 	$qry;			
}

function getGroupDetailsQry($aobj_context,$table_name,$schema_id,$group_id,$type)
{	
	$select_ref_module_code="select 
			GROUP_CONCAT(join_table_name SEPARATOR ' ') as qry from 
			(select concat(' left join ',s.system_module_table_name,' as ',
			s.system_module_table_name ,'_',sus.internal_code,' on ',
			s.system_module_table_name,'_',sus.internal_code,'.internal_code =', 
			sch.system_module_table_name,'_',ug.system_group_table_name ,'_entry .', db_field)
			as join_table_name	from s_user_schema_elements  sus
			inner join s_user_schema s on sus.ref_module_code=s.internal_code
			inner join s_user_schema sch on sus.ref_s_user_schema_code=sch.internal_code
			left join s_user_schema_group ug on ug.internal_code=sus.ref_s_user_schema_group_code
			where sus.ref_s_user_schema_code={$schema_id} and  include_in_grid=1
			and (is_multiple_group=1 or parent_group_code<>0)
			and (ref_s_user_schema_group_code={$group_id} ) 
			and is_multiple_group=1 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]:'';
	if($type=="Normal")
	$concat_field_name='concat(",f1,f2,")';
	else
	$concat_field_name='concat(",f1,f2,",\'~\',",f3,")';
	$get_display_cols="select GROUP_CONCAT(field_name) as field_name  from 
							(select table_name,name,field_name as grid_field_name,
							if(ref_module_code=0,
							concat(table_name,'.',field_name,' as ', field_name),
							concat(\"{$concat_field_name} as \",field_name)) as field_name,
							
							if(ref_module_code=0,concat('module_',internal_code,'.',field_name ),
							concat(\"concat(\",f1,f2,\")\" )) as search_field_name 
							,ref_module_code from 
							(select  s.internal_code, su.internal_code as ele_id,
							ref_module_code, concat(s.system_module_table_name,'_',ug.system_group_table_name,'_entry') as table_name ,
							field_id,
							if(ifnull(field_id1,'')!='',concat(table_name,'_',su.internal_code,'.',field_id1
							),'') as f1,
							if(ifnull(field_id1,'')!='',concat(table_name,'_',su.internal_code,'.internal_code'
							),'') as f3,
							if(ifnull(field_id2,'')!='',concat(',\'-\',',table_name,'_',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 
							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
							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 su.include_in_grid=1 and ( su.is_multiple_group=1  or parent_group_code<>0)
								 
								and (ug.internal_code={$group_id})
								order by ifnull(ug.sequence,0),su.sequence,su.internal_code) l 
							where   multiple=1 )   a ";							 
		$lobj_get_display_cols = $aobj_context->mobj_db->GetRow($get_display_cols);
		 

		$qry = " select {$table_name}.internal_code as id,{$lobj_get_display_cols[field_name]}  from 
				{$table_name}  {$join_qry}"; 
 
return 	$qry;			
}
function getSearchFieldIdsArr($aobj_context,$table_name,$schema_id,$type)
{
 
 
		$db_fields_arr=array();
		 
		$get_display_cols="(select table_name,name,field_name as grid_field_name,
							if(ref_module_code=0,
							concat(table_name,'.',field_name),
							concat(\"concat(\",f1,f2,\")   \" )) as field_name,
							if(ref_module_code=0,concat('module_',internal_code,'.',field_name ),
							concat(\"concat(\",f1,f2,\")\" )) as search_field_name 
							,ref_module_code from 
							(select  s.internal_code, su.internal_code as ele_id,
							ref_module_code, s.system_module_table_name as table_name ,
							field_id,
							if(ifnull(field_id1,'')!='',concat(table_name,'_',su.internal_code,'.',field_id1
							),'') as f1,
							if(ifnull(field_id2,'')!='',concat(',\'-\',',table_name,'_',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 
							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
							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   {$type}=1
							and su.is_multiple_group=0
							order by ifnull(ug.sequence,0),su.sequence,su.internal_code ) l 
							where   multiple=0 )				 
								   ";
		$lobj_get_display_cols = $aobj_context->mobj_db->GetAll($get_display_cols);
	  
		foreach($lobj_get_display_cols as $key=>$val)
			{
				$db_fields_arr[$val['grid_field_name']]=$val['field_name'];
			}
		 
return($db_fields_arr);
}
?>