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.189.189.19


Current Path : /proc/thread-self/root/var/www/html/rcub/src/
Upload File :
Current File : //proc/thread-self/root/var/www/html/rcub/src/systemsetup.php

<?php
function getModuleType($aobj_context)
{
    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
    $sql_query = "select internal_code,value as type 
				from s_ref_type where type='module_type' and active=1 order by 2";
    $lobj_rs = $aobj_context->mobj_db->GetAll($sql_query);

    $arr[0] = $lobj_rs;
    $sql_query_menu = "select internal_code,value as type 
			from s_ref_type where type='sub_menu' and active=1  order by 2";
    $lobj_rs_menu = $aobj_context->mobj_db->GetAll($sql_query_menu);

    $arr[1] = $lobj_rs_menu;
    include_once("getmoduledetails.php");
    $arr['links_data'] = getLinks($aobj_context, "General Master Setup");
    if (count($lobj_rs) != 0)
        print_r($aobj_context->mobj_output->ToJSONEnvelope($arr, 0, "success"));
    else
        print_r($aobj_context->mobj_output->ToJSONEnvelope($lobj_rs, -1, "Failure"));
    return;
}

function getAutoIncrementSourceFields($aobj_context)
{
    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
    $schema_code = $aobj_context->mobj_data["schema_code"];

    $sql_query = "select su.internal_code,su.name as value
			from s_user_schema_elements su 
			inner join s_ref_type st on st.internal_code=data_type
			inner join s_ref_type st1 on st1.internal_code=data_type
			where su.ref_s_user_schema_code={$schema_code} and st1.value='Date'";

    $lobj_rs = $aobj_context->mobj_db->GetAll($sql_query);

    if (count($lobj_rs) != 0)
        print_r($aobj_context->mobj_output->ToJSONEnvelope($lobj_rs, 0, "success"));
    else
        print_r($aobj_context->mobj_output->ToJSONEnvelope($lobj_rs, -1, "Failure"));
    return;
}

function pupulateSystemMenu($aobj_context)
{
    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
    $get_all_menus = "select * from system_menu where active=1 order by sequence";
    $lobj_get_all_menus = $aobj_context->mobj_db->GetAll($get_all_menus);
    $html = "";
    session_start();
    $user_id = $_SESSION['user_id'];
    $user_name = $_SESSION['usr'];
    $college_code = $_SESSION['collcode'];
    $fsnglent = $_SESSION['FSNGLENT'];
    $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);

    foreach ($lobj_get_all_menus as $menu_key => $menu_val) {
        $type = $menu_val['id'];
        $id = $menu_val['id'];
        if ($fsnglent == 'T') {
            $markscol = " and s.display_module_name NOT IN ('I.A. Mark Entry - 1','I.A. Mark Entry - 2')";
        } else
            $markscol = "and s.display_module_name NOT IN ('I.A. Mark Entry')"; //

        if ($lobj_get_user_right['user_type'] == 52) {
            $sql_query = "select internal_code,menu_name,menu_function,id  
				from system_menu  	
				where active=1  and system_menu='T' order by ifnull(sequence,1),menu_name";

            $lstr_qry = "select s.internal_code,ifnull(display_module_name,module_name) as module_name,
				ifnull(display_module_name,module_name)  as  display_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,
				REPLACE(module_name,' ','~') as r_module_name,
				IFNULL(mm.value,concat('z',display_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 display_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,ifnull(display_module_name,module_name)";
        } else if (($lobj_get_user_right['user_type'] == 14 || $lobj_get_user_right['user_type'] == 40) && $lobj_get_user_right['user_type'] != 52) {
            $sql_query = "select internal_code,menu_name,menu_function,id  from system_menu  	
				where active=1  and system_menu='F' order by ifnull(sequence,1),menu_name";

            $lstr_qry = "select s.internal_code,ifnull(display_module_name,module_name) as module_name,
				ifnull(display_module_name,module_name)  as  display_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,
				REPLACE(module_name,' ','~') as r_module_name,
				IFNULL(mm.value,concat('z',display_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 display_module_name is not null and t.value='{$type}'
				and s.active=1  {$markscol}
				group by s.internal_code order by 
				ifnull(s.sequence,0),main_menu_val,ifnull(display_module_name,module_name)";
        } else if ($lobj_get_user_right['value'] == 'University' && $lobj_get_user_right['user_type'] != 52) {
            $sql_query = "select internal_code,menu_name,menu_function,id  from system_menu  	
				where active=1  and system_menu='U' order by ifnull(sequence,1),menu_name";

            $lstr_qry = "select s.internal_code,ifnull(display_module_name,module_name) as module_name,
				ifnull(display_module_name,module_name)  as  display_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,
				REPLACE(module_name,' ','~') as r_module_name,
				IFNULL(mm.value,concat('z',display_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 display_module_name is not null and t.value='{$type}'
				and s.active=1 {$markscol}
				group by s.internal_code order by 
				ifnull(s.sequence,0),main_menu_val,ifnull(display_module_name,module_name)";
        } else if ($lobj_get_user_right['user_type'] == 17) {
            $sql_query = "select s.internal_code,s.menu_name,s.menu_function,s.id  
				from system_menu s  inner join 	s_ref_type st on st.value=s.id 
				and st.type='module_type' inner join
				s_user_schema us on us.module_type=st.internal_code
				inner join 	user_rights ur on ur.module_id=us.internal_code
				where s.active=1  and ur.is_view=1  and system_menu='F'
				and ref_s_sysdb_code={$user_id}  and college_code='{$college_code}'
				group by s.internal_code
				order by ifnull(s.sequence,1),s.menu_name";

            $lstr_qry = "select s.internal_code,ifnull(display_module_name,module_name) as module_name,  
				ifnull(display_module_name,module_name)  as  display_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,
				REPLACE(module_name,' ','~') as r_module_name,
				IFNULL(mm.value,concat('z',display_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={$user_id} and college_code='{$college_code}' 
				and s.active=1 {$markscol}
				group by s.internal_code
				order by  ifnull(s.sequence,0),main_menu_val,ifnull(display_module_name,module_name)";
        } else if (strtolower($user_name) == 'vkuadmin' || $college_code == 'rcub') {
            $sql_query = "select internal_code,menu_name,menu_function,id  from system_menu where active=1 and id='Admission_Admin'  and system_menu='T' order by ifnull(sequence,1),menu_name";


            $lstr_qry = "select s.internal_code,ifnull(display_module_name,module_name) as module_name,
					ifnull(display_module_name,module_name)  as  display_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,
					REPLACE(module_name,' ','~') as r_module_name,
					IFNULL(mm.value,concat('z',display_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 display_module_name is not null and t.value='{$type}'
					and s.active=1  and module_type ='66'
					group by s.internal_code order by 
					ifnull(s.sequence,0),main_menu_val,ifnull(display_module_name,module_name)";
        } else if (strtolower($college_code) == 'rcub') {
            $sql_query = "select internal_code,menu_name,menu_function,id from system_menu where active=1 and id='Admission_Admin' and system_menu='T' order by ifnull(sequence,1),menu_name;";


            $lstr_qry = "select s.internal_code,ifnull(display_module_name,module_name) as module_name,
					ifnull(display_module_name,module_name)  as  display_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,
					REPLACE(module_name,' ','~') as r_module_name,
					IFNULL(mm.value,concat('z',display_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 display_module_name is not null and t.value='{$type}'
					and s.active=1 and module_type ='66'
					group by s.internal_code order by 
					ifnull(s.sequence,0),main_menu_val,ifnull(display_module_name,module_name);";
            //var_dump($lstr_qry);
            //var_dump($sql_query);

        }
        $lobj_rs = $aobj_context->mobj_db->GetAll($lstr_qry);
        $lobj_rs_data = $aobj_context->mobj_db->GetAll($sql_query);
        $html .= "\n\n\n\n\n\n\n\n\n\n\n<div id='tarka_main_menu_{$id}' class='hidden'>  \n";
        $html .= "<ul>  \n";
        $main_menu_id = $lobj_rs[0]['main_menu_id'];
        $main_menu_val = $lobj_rs[0]['main_menu_val'];
        $i = 0;

        for ($m = 0; $m < count($lobj_rs); $m++) {
            $r_module_name = $lobj_rs[$m]['r_module_name'];
            $dis_module_name = $lobj_rs[$m]['display_module_name'];
            $int_code = $lobj_rs[$m]['internal_code'];
            $call_mod = "";
            $call_mod = ($lobj_rs[$m]['is_menu_only'] == '0') ? " getModulesDetails('{$r_module_name}',{$int_code},'Add','{$dis_module_name}') " : $lobj_rs[$m]['menu_function'];

            if ($i == 0) {
                if ($main_menu_id > 0) {
                    $html .= "<li><a href='#' >{$lobj_rs[$m]['main_menu_val']}</a>  \n";
                    $html .= "<ul> \n";
                    $html .= "<li><a href='#' onclick=\"{$call_mod};\" >{$lobj_rs[$m]['module_name']} </a> </li>\n";
                } else {
                    $html .= "<li><a href='#' onclick=\"{$call_mod};\" >{$lobj_rs[$m]['module_name']} </a> </li> \n";
                }
                $main_menu_id =   $lobj_rs[$m]['main_menu_id'];
                $main_menu_val =  $lobj_rs[$m]['main_menu_val'];
                $i++;
                continue;
            } else {
                if ($main_menu_id == $lobj_rs[$m]['main_menu_id'] && $lobj_rs[$m]['main_menu_id'] > 0) {
                    $html .= "<li><a href='#' onclick=\"{$call_mod};\" >{$lobj_rs[$m]['module_name']} </a> </li>\n";
                    $main_menu_id =   $lobj_rs[$m]['main_menu_id'];
                    $main_menu_val =  $lobj_rs[$m]['main_menu_val'];
                } else if ($main_menu_id <> $lobj_rs[$m]['main_menu_id'] && $lobj_rs[$m]['main_menu_id'] > 0) {
                    if ($lobj_rs[$m - 1]['main_menu_id'] > 0) {
                        $html .= "</ul> \n";
                        $html .= "</li> \n";
                    }
                    $html .= "\t <li><a href='#'>{$lobj_rs[$m]['main_menu_val']}</a>   \n";
                    $html .= "<ul> \n";
                    $html .= "<li><a href='#' onclick=\"{$call_mod};\" >{$lobj_rs[$m]['module_name']}</a> </li>\n";
                } else {
                    if ($lobj_rs[$m - 1]['main_menu_id'] > 0) {
                        $html .= "</ul> \n";
                        $html .= "</li> \n";
                    }
                    $html .= " \t <li><a href='#' onclick=\"{$call_mod};\" >{$lobj_rs[$m]['module_name']}</a> </li>\n";
                }
                $main_menu_id =   $lobj_rs[$m]['main_menu_id'];
                $main_menu_val =  $lobj_rs[$m]['main_menu_val'];
            }
        }
        if ($lobj_rs[$m - 1]['main_menu_id'] > 0) {
            $html .= "</ul> \n";
            $html .= "</li> \n";
        }
        $html .= "</ul>  \n";
        $html .= "</div>  \n";
    }

    $arr['login_cnt'] = $_SESSION['login_cnt'];
    $arr['details'] = $lobj_rs_data;
    $arr['html'] = $html;

    if (count($lobj_rs_data) != 0)
        print_r($aobj_context->mobj_output->ToJSONEnvelope($arr, 0, "success"));
    else
        print_r($aobj_context->mobj_output->ToJSONEnvelope($arr, -1, "Failure"));
    return;
}

function GetUserSchemaParentGroup($aobj_context)
{
    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
    $schema_name = ($aobj_context->mobj_data["schema_name"]);
    $get_details = "select  internal_code,name as user_schema 
				from s_user_schema_group 
				where ref_s_user_schema_code={$schema_name} order by name";
    $lobj_details = $aobj_context->mobj_db->getAll($get_details);

    $arr['parent_group'] = $lobj_details;
    if ($lobj_details)
        echo $aobj_context->mobj_output->ToJSONEnvelope($arr, 0, "success");
    else
        echo $aobj_context->mobj_output->ToJSONEnvelope($arr, -1, "Failure");
}

function insertUserSchemaMaster($aobj_context)
{

    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
    $internal_code = $aobj_context->mobj_data["internal_code"];
    $active = $aobj_context->mobj_data["active"];
    $create_table = $aobj_context->mobj_data["create_table"];
    $module_name = addslashes(trim($aobj_context->mobj_data["module_name"]));

    $user_schema_js_path = addslashes(trim($aobj_context->mobj_data["user_schema_js_path"]));
    $user_schema_grid_default_rows = addslashes(trim($aobj_context->mobj_data["user_schema_grid_default_rows"]));
    $user_schema_display_module_name = addslashes(trim($aobj_context->mobj_data["user_schema_display_module_name"]));
    $grid_custom_links = addslashes(trim($aobj_context->mobj_data["grid_custom_links"]));
    $is_menu_only = addslashes(trim($aobj_context->mobj_data["is_menu_only"]));

    $module_function_name = addslashes(trim($aobj_context->mobj_data["module_function_name"]));
    $module_type = $aobj_context->mobj_data["module_type"];
    $main_menu = $aobj_context->mobj_data["main_menu"];
    $module_grid_type = $aobj_context->mobj_data["module_grid_type"];
    $grid_user_schema_coloum_names = $aobj_context->mobj_data["grid_user_schema_coloum_names"];

    $grid_user_schema_grid_query = $aobj_context->mobj_data["grid_user_schema_grid_query"];
    $grid_user_schema_coloum_models = $aobj_context->mobj_data["grid_user_schema_coloum_models"];
    $is_save = addslashes(trim($aobj_context->mobj_data["is_save"]));
    $is_clear = addslashes(trim($aobj_context->mobj_data["is_clear"]));
    $is_download = addslashes(trim($aobj_context->mobj_data["is_download"]));

    $is_view = addslashes(trim($aobj_context->mobj_data["is_view"]));
    $is_generate = addslashes(trim($aobj_context->mobj_data["is_generate"]));
    $custom_links = (trim($aobj_context->mobj_data["custom_links"]));
    $display_type = $aobj_context->mobj_data["display_type"];
    $get_module_type = "select value from s_ref_type where internal_code=$module_type";
    $lobj_module_type = $aobj_context->mobj_db->GetRow($get_module_type);
    $mod_type = $lobj_module_type['value'];

    if ($internal_code == 0) {
        $get_data = "select count(*) as module_count from s_user_schema 
			where module_name='" . $module_name . "'
			and  module_type='" . $module_type . "'";
        $lobj_rs = $aobj_context->mobj_db->GetRow($get_data);

        if ($lobj_rs['module_count'] == 0) {
            $lstr_qry = "insert into s_user_schema( 
					module_name,
					display_module_name,
					module_type,
					is_menu_only,
					menu_function,
					active,main_menu,display_type,
					include_save,include_clear,
					include_download,include_view,
					include_generate,grid_type,custom_links,s_col_names,s_col_model,s_grid_query,s_grid_custom_links,grid_default_rows,js_path)
					values ( '{$module_name}','{$user_schema_display_module_name}','{$module_type}',
								'{$is_menu_only}',
								'{$module_function_name}',
								'{$active}',
								'{$main_menu}',
								'{$display_type}',
								'{$is_save}',
								'{$is_clear}',
								'{$is_download}',
								'{$is_view}',
								'{$is_generate}',
								'{$module_grid_type}',
								\"{$custom_links}\",
								\"{$grid_user_schema_coloum_names}\",
								\"{$grid_user_schema_coloum_models}\",
								\"{$grid_user_schema_grid_query}\",
								\"{$grid_custom_links}\",
								\"{$user_schema_grid_default_rows}\",
								'{$user_schema_js_path}')";

            $lobj_rs = $aobj_context->mobj_db->Execute($lstr_qry);

            $last_inserted_id  = $aobj_context->mobj_db->Insert_ID();
            $get_admin_id = "select * from s_sysdb where sysusr='admin'";
            $lobj_get_admin_id = $aobj_context->mobj_db->getRow($get_admin_id);
            $insert_new_user = "insert into user_rights(
							ref_s_sysdb_code,
							module_type,
							module_id,
							is_view,
							is_add,
							is_edit,
							is_del) values
							(
							'{$lobj_get_admin_id[internal_code]}',
							'{$module_type}',
							'{$last_inserted_id}',
							'1',
							'1',
							'1',
							'1'	)";
            $lobj_insert_user = $aobj_context->mobj_db->Execute($insert_new_user);

            if ($mod_type == 'Reports') {
                $main_src_obj = (explode("/", $_SERVER["REQUEST_URI"]));
                $main_src = $main_src_obj[1];
                $report_storage_path = $_SERVER["DOCUMENT_ROOT"] . "/" . $main_src . "/Report_details/report_result_" . $last_inserted_id . ".php";
                $report_format_path = $_SERVER["DOCUMENT_ROOT"] . "/" . $main_src . "/src/general_report_format.php";
                file_put_contents($report_storage_path, file_get_contents($report_format_path));
            }
            if ($create_table == 1 && $mod_type != 'Reports' && $is_menu_only == '0') {
                $create_table = "CREATE TABLE module_" . $last_inserted_id .
                    "(
						internal_code bigint(20) unsigned NOT NULL auto_increment,
						ref_s_user_schema_code bigint(20),
						created_by  int(11) default NULL,
						created_date  datetime default NULL,
						modified_by  int(11) default NULL,
						modified_date  datetime default NULL,
						PRIMARY KEY  (internal_code)
					) engine INNODB ";

                $lobj_rs = $aobj_context->mobj_db->Execute($create_table);
                $create_details_table = "CREATE TABLE module_" . $last_inserted_id . "_details" .
                    "(
									  internal_code bigint(20) unsigned NOT NULL auto_increment,
									  ref_s_user_schema_code bigint(20) ,
									   ref_s_module_code int(11) default NULL,
									  ref_s_user_schema_group_code int(11) default NULL,
									  ref_s_user_schema_elements_code int(11) default NULL,
									  value text collate latin1_general_ci,
									  ref_ref_s_user_schema_code int(11) default NULL,
									   created_by  int(11) default NULL,
									   created_date  datetime default NULL,
									   modified_by  int(11) default NULL,
									   modified_date  datetime default NULL,
									  PRIMARY KEY  (internal_code),
									  KEY NewIndex1 (ref_s_user_schema_code),
									  KEY NewIndex2 (ref_s_module_code),
									  KEY NewIndex3 (ref_s_user_schema_group_code),
									  KEY NewIndex4 (ref_s_user_schema_elements_code)
									   
									) engine INNODB";

                $lobj_rs = $aobj_context->mobj_db->Execute($create_details_table);
            }
            if ($lobj_rs) {
                $rdata = "Module Inserted Successfully.";
                print_r($aobj_context->mobj_output->ToJSONEnvelope($rdata, 0, "success"));
                return $rdata;
            } else {
                $rdata = "insertion Failed.";
                print_r($aobj_context->mobj_output->ToJSONEnvelope($rdata, -1, "Failure"));
                return $rdata;
            }
        } else {
            $rdata = "Module Name Already Exstis!";
            print_r($aobj_context->mobj_output->ToJSONEnvelope($rdata, 0, "success"));
            return $rdata;
        }
    } else {
        $lstr_qry = "update s_user_schema
				set module_name='" . $module_name . "',
				module_type='" . $module_type . "',display_module_name='" . $user_schema_display_module_name . "', 
				main_menu= '{$main_menu}', display_type= '" . $display_type . "',
				active= {$active} ,is_menu_only='{$is_menu_only}',
				menu_function= '{$module_function_name}',
				include_save= '" . $is_save . "',include_clear= '" . $is_clear . "',
				include_download= '" . $is_download . "',include_view= '" . $is_view . "',
				include_generate = '" . $is_generate . "',
				custom_links= \"{$custom_links}\",
				grid_type= '{$module_grid_type}',
				s_col_names= \"{$grid_user_schema_coloum_names}\",
				s_col_model= \"{$grid_user_schema_coloum_models}\",
				s_grid_query= \"{$grid_user_schema_grid_query}\" ,
				s_grid_custom_links= \"{$grid_custom_links}\", 
				grid_default_rows = \"{$user_schema_grid_default_rows}\",
				js_path= '{$user_schema_js_path}'
				where internal_code ={$internal_code}";
        $lobj_rs = $aobj_context->mobj_db->Execute($lstr_qry);

        if ($lobj_rs) {
            $rdata = "Updated  Successfully.";
            print_r($aobj_context->mobj_output->ToJSONEnvelope($rdata, 0, "success"));
            return;
        } else {
            $rdata = "Udatation Failed.";
            print_r($aobj_context->mobj_output->ToJSONEnvelope($rdata, -1, "Failure"));
        }
    }
}

function GetSchemaDetailsForUpdate($aobj_context)
{
    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
    $internal_code = $aobj_context->mobj_data["internal_code"];
    $get_data = "select internal_code, module_name, display_module_name, 
					module_type,is_menu_only,ifnull(menu_function,'') as menu_function,main_menu
					,display_type, active,include_save,include_clear,include_download,include_view,
					s_col_names,s_col_model,s_grid_query,grid_type,
					include_generate,include_close,custom_links,
					s_grid_custom_links,js_path,grid_default_rows 
					from s_user_schema where internal_code='" . $internal_code . "'";
    $lobj_rs = $aobj_context->mobj_db->GetRow($get_data);
    if (count($lobj_rs) > 0)
        echo $aobj_context->mobj_output->ToJSONEnvelope($lobj_rs, 0, "success");
    else
        echo $aobj_context->mobj_output->ToJSONEnvelope($lobj_rs, -1, "Failure");
    return;
}

function getModuleNameForGroups($aobj_context)
{
    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
    $get_module_name = "select internal_code,module_name as user_schema 
		from s_user_schema where is_menu_only=0 and active =1 order by module_name";

    $lobj_rs = $aobj_context->mobj_db->GetAll($get_module_name);

    if (count($lobj_rs) != 0)
        print_r($aobj_context->mobj_output->ToJSONEnvelope($lobj_rs, 0, "success"));
    else
        print_r($aobj_context->mobj_output->ToJSONEnvelope($lobj_rs, -1, "Failure"));
    return;
}

function forInsertuserschemagroup($aobj_context)
{
    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
    $internal_code = $aobj_context->mobj_data["internal_code"];
    $group_order_by = $aobj_context->mobj_data["user_schema_group_order_by"];
    $grid_default_rows = $aobj_context->mobj_data["user_schema_group_grid_default_rows"];
    $grid_default_rows = (empty($grid_default_rows)) ? 0 : $grid_default_rows;

    if ($internal_code == 0) {
        $get_data = "select count(*) as group_schema_count from s_user_schema_group 
			where name='" . $aobj_context->mobj_data["user_schema_group_name"] . "'" . " 
			and ref_s_user_schema_code=" . $aobj_context->mobj_data["group_user_schema"];
        $lobj_rs = $aobj_context->mobj_db->GetRow($get_data);

        if ($lobj_rs['group_schema_count'] == 0) {
            $lstr_qry = "insert into s_user_schema_group(  
					ref_s_user_schema_code,
					name,parent_group_code,sequence,multiple,is_grid_edit,inline_div,col_name,col_model,grid_edit_query,custom_links,on_click_event,group_order_by,group_default_rows) 
					values ( '" .   addslashes($aobj_context->mobj_data["group_user_schema"]) . "','"
                .   addslashes($aobj_context->mobj_data["user_schema_group_name"]) . "','"
                .   addslashes($aobj_context->mobj_data["user_schema_parent_group"]) . "','"
                .   addslashes($aobj_context->mobj_data["sequence"]) . "','"
                .   addslashes($aobj_context->mobj_data["is_multiple_user_schema_group"]) . "','"
                .   addslashes($aobj_context->mobj_data["is_grid_edit_user_schema_group"]) . "','"
                .   addslashes($aobj_context->mobj_data["is_inline_div_user_schema_group"]) . "',\""
                .   addslashes($aobj_context->mobj_data["user_schema_coloum_names"]) . "\",\""
                .   addslashes($aobj_context->mobj_data["user_schema_coloum_models"]) . "\",\""
                .   addslashes($aobj_context->mobj_data["user_schema_grid_query"]) . "\",\""
                .   addslashes($aobj_context->mobj_data["user_schema_group_custom_links"]) . "\",\""
                .   addslashes($aobj_context->mobj_data["user_schema_group_onclick"]) . "\",
							  '{$group_order_by}',{$grid_default_rows})";

            $lobj_rs = $aobj_context->mobj_db->Execute($lstr_qry);
            $last_inserted_id  = $aobj_context->mobj_db->Insert_ID();

            if ($aobj_context->mobj_data["is_multiple_user_schema_group"] == 1) {
                $qry = "select CONCAT('user_schema_',ms.INTERNAL_CODE,'_group_',msg.internal_code) as  group_table_name,
					CONCAT('user_schema_',ms.INTERNAL_CODE,'_entry_',msg.internal_code) as  entry_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=$last_inserted_id";
                $lobj_rs = $aobj_context->mobj_db->GetRow($qry);

                $create_table = "CREATE TABLE " . $lobj_rs['group_table_name'] .
                    "(
					internal_code bigint(20) unsigned NOT NULL auto_increment,
					ref_s_user_schema_code bigint(20),
					ref_s_user_schema_group_code bigint(20),
					ref_s_ref_data_code bigint(20),
					created_by  int(11) default NULL,
					created_date  datetime default NULL,
					modified_by  int(11) default NULL,
					modified_date  datetime default NULL,
					PRIMARY KEY  (internal_code),
					KEY `ref_s_ref_data_code` (`ref_s_ref_data_code`) 
					)  engine INNODB";

                $lobj_create_table = $aobj_context->mobj_db->Execute($create_table);

                $create_entry_table = "CREATE TABLE {$lobj_rs['entry_table_name']}
					(
					internal_code bigint(20) unsigned NOT NULL auto_increment,
					login_cnt int(11), 
					group_int_code int(11) default 0, 
					ref_s_user_schema_code bigint(20),
					ref_s_user_schema_group_code bigint(20),
					ref_s_ref_data_code bigint(20),
					created_by  int(11) default NULL,
					created_date  datetime default NULL,
					modified_by  int(11) default NULL,
					modified_date  datetime default NULL,
					PRIMARY KEY  (internal_code),
					KEY `login_cnt` (`login_cnt`),
					KEY `group_int_code` (`group_int_code`),
					KEY `ref_s_ref_data_code` (`ref_s_ref_data_code`) 
					)  engine INNODB";

                $lobj_create_entry_table = $aobj_context->mobj_db->Execute($create_entry_table);
            }


            $get_module_type = "select module_type from s_user_schema where internal_code=" . $aobj_context->mobj_data["group_user_schema"];
            $lobj_get_module_type = $aobj_context->mobj_db->getRow($get_module_type);

            $module_type = $lobj_get_module_type["module_type"];
            $module_id = $aobj_context->mobj_data["group_user_schema"] . "_" . $last_inserted_id;
            $insert_rights = "insert into user_rights(
				ref_s_sysdb_code,
				module_type,
				module_id,
				is_view,
				is_add,
				is_edit,
				is_del) values
				(1,{$module_type},'{$module_id}',1,1,1,1)";

            $lobj_insert_rights = $aobj_context->mobj_db->Execute($insert_rights);
            if ($lobj_rs) {
                $rdata = "Group Name inserted successfully.";
                print_r($aobj_context->mobj_output->ToJSONEnvelope($rdata, 0, "success"));
                return $rdata;
            } else {
                $rdata = "insertion Failed.";
                print_r($aobj_context->mobj_output->ToJSONEnvelope($rdata, -1, "Failure"));
                return $rdata;
            }
        } else {
            $rdata = "Group Name Already Exstis!";
            print_r($aobj_context->mobj_output->ToJSONEnvelope($rdata, 0, "success"));
            return $rdata;
        }
    } else {
        $group_table = "select internal_code,ref_s_user_schema_code,multiple, 
					concat('user_schema_',ref_s_user_schema_code,'_group_',internal_code)
					as table_name from s_user_schema_group where internal_code=" . $internal_code;
        $lobj_rs_row = $aobj_context->mobj_db->GetRow($group_table);
        if ($aobj_context->mobj_data["is_multiple_user_schema_group"] == 0) {
            $drop_non_multiple = "drop table if EXISTS user_schema_" . $aobj_context->mobj_data["group_user_schema"] . "_group_" . $aobj_context->mobj_data["internal_code"];
            $drop_obj = $aobj_context->mobj_db->Execute($drop_non_multiple);
        } else if ($lobj_rs_row["multiple"] == 0 && $aobj_context->mobj_data["is_multiple_user_schema_group"] == 1) {
            $create_table = "CREATE TABLE user_schema_" . $aobj_context->mobj_data["group_user_schema"] . "_group_" . $internal_code .
                "(
									internal_code bigint(20) unsigned NOT NULL auto_increment,
									ref_s_user_schema_code bigint(20),
									ref_s_user_schema_group_code bigint(20),
									ref_s_ref_data_code bigint(20),
									created_by  int(11) default NULL,
									created_date  datetime default NULL,
									modified_by  int(11) default NULL,
									modified_date  datetime default NULL,
									PRIMARY KEY  (internal_code)
								)";
            $lobj_create_table = $aobj_context->mobj_db->Execute($create_table);
            $create_entry_table = "CREATE TABLE " . $lobj_rs['entry_table_name'] .
                "(
										internal_code bigint(20) unsigned NOT NULL auto_increment,
										login_cnt int(11), 
										group_int_code int(11) default 0,
										ref_s_user_schema_code bigint(20),
										ref_s_user_schema_group_code bigint(20),
										ref_s_ref_data_code bigint(20),
										created_by  int(11) default NULL,
										created_date  datetime default NULL,
										modified_by  int(11) default NULL,
										modified_date  datetime default NULL,
										PRIMARY KEY  (internal_code),
										KEY `login_cnt` (`login_cnt`),
										KEY `group_int_code` (`group_int_code`)
									)  ";
            $lobj_create_entry_table = $aobj_context->mobj_db->Execute($create_entry_table);
        }
        $lstr_qry = "update s_user_schema_group
				set ref_s_user_schema_code='" . trim($aobj_context->mobj_data["group_user_schema"]) . "'," .
            "name='" . $aobj_context->mobj_data["user_schema_group_name"] . "'," .
            "parent_group_code='" . $aobj_context->mobj_data["user_schema_parent_group"] . "'," .
            "multiple='" . $aobj_context->mobj_data["is_multiple_user_schema_group"] . "',
				sequence=" . trim($aobj_context->mobj_data["sequence"]) . ",
				is_grid_edit=" . trim($aobj_context->mobj_data["is_grid_edit_user_schema_group"]) . ",
				inline_div=" . trim($aobj_context->mobj_data["is_inline_div_user_schema_group"]) . ",
				col_name=\"" . trim($aobj_context->mobj_data["user_schema_coloum_names"]) . "\",
				col_model=\"" . trim($aobj_context->mobj_data["user_schema_coloum_models"]) . "\",
				grid_edit_query=\"" . trim($aobj_context->mobj_data["user_schema_grid_query"]) . "\",
				custom_links=\"" . trim($aobj_context->mobj_data["user_schema_group_custom_links"]) . "\",
				on_click_event=\"" . trim($aobj_context->mobj_data["user_schema_group_onclick"]) . "\",
				group_order_by='{$group_order_by}',
				group_default_rows={$grid_default_rows}
				where internal_code =" . $aobj_context->mobj_data["internal_code"];

        $lobj_rs = $aobj_context->mobj_db->Execute($lstr_qry);
        if ($lobj_rs) {
            $rdata = "Updated  Successfully.";
            print_r($aobj_context->mobj_output->ToJSONEnvelope($rdata, 0, "success"));
            return;
        } else {
            $rdata = "Udatation Failed.";
            print_r($aobj_context->mobj_output->ToJSONEnvelope($rdata, -1, "Failure"));
        }
    }
}

function GetUserSchemaGroupDetailsForUpdate($aobj_context)
{
    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
    $internal_code = $aobj_context->mobj_data["internal_code"];

    $qry = "select * from s_user_schema_group where internal_code=$internal_code";
    $lobj_rs = $aobj_context->mobj_db->GetRow($qry);

    if (count($lobj_rs) != 0)
        echo $aobj_context->mobj_output->ToJSONEnvelope($lobj_rs, 0, "success");
    else
        echo $aobj_context->mobj_output->ToJSONEnvelope($lobj_rs, -1, "Failure");
    return;
}

function getUserSchemaElementType($aobj_context)
{
    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);

    $sql_query = "select internal_code, type,value  
				from s_ref_type  	
				where type in('data_type','element_type') 
				order by type,value";

    $lobj_rs = $aobj_context->mobj_db->GetAll($sql_query);
    if (count($lobj_rs) != 0)
        print_r($aobj_context->mobj_output->ToJSONEnvelope($lobj_rs, 0, "success"));
    else
        print_r($aobj_context->mobj_output->ToJSONEnvelope($lobj_rs, -1, "Failure"));
    return;
}

function getUserSchemaElementReferenceData($aobj_context)
{
    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
    $module_name = $aobj_context->mobj_data["module_name"];
    $qry = " select internal_code,module_name  
		from s_user_schema where is_menu_only=0 and active =1 order by module_name";
    $lobj_rs = $aobj_context->mobj_db->GetAll($qry);
    //echo $qry;
    print_r($aobj_context->mobj_output->ToJSONEnvelope($lobj_rs, 0, "success"));
    return $lobj_rs;
}

function getUserSchemaGroup($aobj_context)
{
    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);

    $sql_query = "select internal_code,name as user_schema_group  
					from s_user_schema_group where 
					ref_s_user_schema_code=" . $aobj_context->mobj_data["elemenmt_user_schema"] . " order by name";
    $lobj_rs = $aobj_context->mobj_db->GetAll($sql_query);

    $select_max_seq = "select round(ifnull(max(sequence),0)+1) as sequence 
						from s_user_schema_elements 					
						where ref_s_user_schema_code=" . $aobj_context->mobj_data["elemenmt_user_schema"];
    $lobj_select_max_seq = $aobj_context->mobj_db->GetRow($select_max_seq);
    $sequence =    $lobj_select_max_seq['sequence'];
    $arr[0] = $lobj_rs;
    $arr[1] = $sequence;
    if (count($lobj_rs) != 0)
        print_r($aobj_context->mobj_output->ToJSONEnvelope($arr, 0, "success"));
    else
        print_r($aobj_context->mobj_output->ToJSONEnvelope($arr, -1, "Failure"));
    return;
}

function GetUserSchemaElementDetailsForUpdate($aobj_context)
{
    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
    $internal_code = $aobj_context->mobj_data["internal_code"];

    $qry = "select * from s_user_schema_elements where internal_code=$internal_code";
    $lobj_rs = $aobj_context->mobj_db->GetRow($qry);

    if (count($lobj_rs) != 0)
        echo $aobj_context->mobj_output->ToJSONEnvelope($lobj_rs, 0, "success");
    else
        echo $aobj_context->mobj_output->ToJSONEnvelope($lobj_rs, -1, "Failure");
    return;
}
function getLinkedModules($aobj_context)
{
    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
    $schema_code = $aobj_context->mobj_data["schema_code"];

    $qry = "select internal_code,name as value from s_user_schema_elements where ref_module_code>0 
			and ref_s_user_schema_code=$schema_code";

    $lobj_rs = $aobj_context->mobj_db->GetAll($qry);

    if (count($lobj_rs) != 0)
        echo $aobj_context->mobj_output->ToJSONEnvelope($lobj_rs, 0, "success");
    else
        echo $aobj_context->mobj_output->ToJSONEnvelope($lobj_rs, -1, "Failure");
    return;
}

function getSourceFields($aobj_context)
{
    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
    $element_code = $aobj_context->mobj_data["element_code"];

    $qry = "select internal_code,name as value from s_user_schema_elements where 
					ref_s_user_schema_code=(select ref_module_code 
					from s_user_schema_elements where internal_code=$element_code )";

    $lobj_rs = $aobj_context->mobj_db->GetAll($qry);
    if (count($lobj_rs) != 0)
        echo $aobj_context->mobj_output->ToJSONEnvelope($lobj_rs, 0, "success");
    else
        echo $aobj_context->mobj_output->ToJSONEnvelope($lobj_rs, -1, "Failure");
    return;
}

function insertUserSchemaElements($aobj_context)
{
    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
    $internal_code = $aobj_context->mobj_data["internal_code"];
    $name = $aobj_context->mobj_data["user_schema_element_name"];
    $group_id = $aobj_context->mobj_data["elemenmt_user_schema_group"];
    $system_name = $aobj_context->mobj_data["user_schema_system_name"];

    $schema_id = $aobj_context->mobj_data["elemenmt_user_schema"];
    $include_in_pop_up = $aobj_context->mobj_data["include_in_pop_up"];
    $on_enter = stripslashes($aobj_context->mobj_data["on_enter"]);
    $on_exit = stripslashes($aobj_context->mobj_data["on_exit"]);

    $ref_module_code_ref_field_const = ($aobj_context->mobj_data["ref_module_code_ref_field_const"]);
    $ref_module_code_ref_field_on = ($aobj_context->mobj_data["ref_module_code_ref_field_on"]);
    $ref_module_code_ref_field = ($aobj_context->mobj_data["ref_module_code_ref_field"]);
    $include_in_grid = ($aobj_context->mobj_data["include_in_grid"]);

    $is_key_field = ($aobj_context->mobj_data["is_key_field"]);
    $is_lov = ($aobj_context->mobj_data["is_lov"]);
    $is_populate_lov = ($aobj_context->mobj_data["is_populate_lov"]);
    $text_area_rows = ($aobj_context->mobj_data["user_schema_element_text_area_rows"]);
    $text_area_rows = empty($text_area_rows) ? 2 : $text_area_rows;
    $grid_width = ($aobj_context->mobj_data["user_schema_element_grid_width"]);
    if (empty($grid_width))
        $grid_width = 0;
    $is_unique = $aobj_context->mobj_data["is_unique"];

    $select_max_seq = "select round(ifnull(max(sequence),0)+1)  as sequence 
						from s_user_schema_elements 					
						where ref_s_user_schema_code=" . $aobj_context->mobj_data["elemenmt_user_schema"] . "
						and ref_s_user_schema_group_code=" . $aobj_context->mobj_data["elemenmt_user_schema_group"];

    $lobj_select_max_seq = $aobj_context->mobj_db->GetRow($select_max_seq);
    $max_seq = $lobj_select_max_seq["sequence"];

    $get_multiple_group = "select multiple from s_user_schema_group where internal_code=" . $aobj_context->mobj_data["elemenmt_user_schema_group"];
    $lobj_multiple_group = $aobj_context->mobj_db->GetRow($get_multiple_group);
    if (!empty($lobj_multiple_group))
        $is_multiple_group = $lobj_multiple_group['multiple'];
    else
        $is_multiple_group = 0;
    $get_group_multiple = "select count(*) as count from s_user_schema_group 
						where internal_code=" . $aobj_context->mobj_data["elemenmt_user_schema_group"] . " 
						and (multiple=1 or parent_group_code<>0) ";
    $lobj_get_group_multiple = $aobj_context->mobj_db->GetRow($get_group_multiple);
    if ($internal_code == 0) {
        $get_data = "select count(*) as element_count from s_user_schema_elements 
						where ref_s_user_schema_code={$schema_id}  
						and system_name='{$system_name}'";

        $lobj_rs = $aobj_context->mobj_db->GetRow($get_data);

        if ($lobj_rs['element_count'] == 0) {
            $select_id = "select value from s_ref_type 
						where internal_code=" . addslashes($aobj_context->mobj_data["meta_type"]);
            $lobj_select_id = $aobj_context->mobj_db->GetRow($select_id);

            $insert_linked_table = "select ref_module_code from s_user_schema_elements 
					where internal_code={$schema_id}";
            $lobj_inked_table = $aobj_context->mobj_db->GetRow($insert_linked_table);
            if (!empty($lobj_inked_table))
                $lobj_inked_table_id = $lobj_inked_table['ref_module_code'];
            else
                $lobj_inked_table_id = '0';


            $lstr_qry = "insert into s_user_schema_elements(  
					ref_s_user_schema_code,
					name,
					system_name,
					data_type,
					sequence,
					field_position,
					ref_s_user_schema_group_code,
					description,
					default_value,
					optional,
					decimals,
					list_of_values,
					ref_module_code,
					 ref_module_code_ref_field,
					 ref_module_code_ref_field_on,
					 ref_module_code_ref_field_const,
					s_ref_type_meta,
					is_multiple_group,
					linked_field,
					linked_field_table,
					source_field,
					suffix,
					width,
					padding,
					is_visible,
					is_ref_module_code_lov,
					populate_lov,
					conditional_display,
					is_read_only,
					is_unique,
					include_in_popup,
					include_in_grid,
					is_key_field,
					on_enter,
					on_exit,
					grid_width,
					text_area_rows				
					) 
					 values ( '" .   addslashes($aobj_context->mobj_data["elemenmt_user_schema"]) . "','"
                .   addslashes($aobj_context->mobj_data["user_schema_element_name"]) . "','"
                .   addslashes($aobj_context->mobj_data["user_schema_system_name"]) . "','"
                .   addslashes($aobj_context->mobj_data["user_schema_element_type"]) . "','"
                .   addslashes($aobj_context->mobj_data["user_schema_element_sequence"]) . "','"
                .   addslashes($aobj_context->mobj_data["user_schema_element_field_position"]) . "','"
                .   addslashes($aobj_context->mobj_data["elemenmt_user_schema_group"]) . "','"
                .   addslashes($aobj_context->mobj_data["user_schema_element_description"]) . "','"
                .   addslashes($aobj_context->mobj_data["user_schema_element_default_value"]) . "',"
                .   addslashes($aobj_context->mobj_data["is_user_schema_element_optional"]) . ","
                .   addslashes($aobj_context->mobj_data["decimal"]) . ",'"
                .   addslashes($aobj_context->mobj_data["lov"]) . "',"
                .   addslashes($aobj_context->mobj_data["ref_module_code"]) . ",
									'{$ref_module_code_ref_field}',{$ref_module_code_ref_field_on},'{$ref_module_code_ref_field_const}',"
                .   addslashes($aobj_context->mobj_data["meta_type"]) . ","
                .     $is_multiple_group . ",'"
                .     addslashes($aobj_context->mobj_data["linked_field"]) . "','"
                .     $lobj_inked_table_id . "','"
                .     addslashes($aobj_context->mobj_data["source_fields"]) . "','"
                .     addslashes($aobj_context->mobj_data["suffix"]) . "','"
                .     addslashes($aobj_context->mobj_data["width"]) . "','"
                .     addslashes($aobj_context->mobj_data["padding"]) . "','"
                .     addslashes($aobj_context->mobj_data["is_visible"]) . "','"
                .     addslashes($aobj_context->mobj_data["is_lov"]) . "','"
                .     addslashes($aobj_context->mobj_data["is_populate_lov"]) . "','"
                .     addslashes($aobj_context->mobj_data["user_schema_element_conditional_display"]) . "','"
                .     addslashes($aobj_context->mobj_data["is_read_only"])
                . "',{$is_unique},'{$include_in_pop_up}','{$include_in_grid}',
								'{$is_key_field}','{$on_enter}','{$on_exit}','{$grid_width}',{$text_area_rows})";
            $lobj_rs = $aobj_context->mobj_db->Execute($lstr_qry);

            UpdateDbField($aobj_context, $schema_id);
            $select_max_seq = "select round(ifnull(max(sequence),0)+1)  as sequence from s_user_schema_elements 					
						where ref_s_user_schema_code=" . $aobj_context->mobj_data["elemenmt_user_schema"] . "
						and ref_s_user_schema_group_code=" . $aobj_context->mobj_data["elemenmt_user_schema_group"];
            $lobj_select_max_seq = $aobj_context->mobj_db->GetRow($select_max_seq);
            $max_seq = $lobj_select_max_seq["sequence"];
            $last_inserted_id  = $aobj_context->mobj_db->Insert_ID();

            //to check whether elements belongs to multiple group
            $decml = $aobj_context->mobj_data["decimal"] + 12;
            $is_visible = $aobj_context->mobj_data["is_visible"];

            $is_group = "select 
							if(multiple=1,CONCAT('user_schema_',sus.INTERNAL_CODE,'_group_',susg.internal_code),
							CONCAT('user_schema_',sus.INTERNAL_CODE,'_group_',susg.parent_group_code)) 
								as table_name,
								case when s.value='Text' then 'varchar(500)'
								when s.value='List Of Values' then 'varchar(50)'
								when s.value='Reference Data' then 'int(11)'
								when s.value ='Number' and {$decml}>12 then 'decimal(" . $decml . "," . $aobj_context->mobj_data["decimal"] . ")'
								when s.value ='Number' and {$decml}=12 then 'int(11)'
								when s.value ='Date' then 'date'
								when s.value='Month' then 'int(6)'
								when s.value='Image' then 'varchar(250)'
								else 'text' end as   data_type
								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
								inner join s_ref_type s1 on s1.internal_code=suse.s_ref_type_meta
								and s1.value='Meta'
								left join s_ref_type s on s.internal_code=suse.data_type
								where suse.internal_code=" . $last_inserted_id . " 
								and ((multiple=1) or (multiple=0 and parent_group_code<>0) )";

            $lobj_rs_group = $aobj_context->mobj_db->GetRow($is_group);


            // if count of Group is greater than one the alter the table 
            if (count($lobj_rs_group['table_name']) > 0) {
                $table_count = "show tables like '" . $lobj_rs_group['table_name'] . "'";
                $lobj_rs_table_count = $aobj_context->mobj_db->GetRow($table_count);

                if (count($lobj_rs_table_count) > 0) {
                    $ref_s_user_schema_group_code = $aobj_context->mobj_data["elemenmt_user_schema_group"];
                    // it will insert into corresponding filds
                    $getafterId = "select max(suse.internal_code) as max_id
									from s_user_schema_elements suse
									inner join s_user_schema_group sg on sg.internal_code=suse.ref_s_user_schema_group_code
									inner join s_ref_type t on t.internal_code=suse.s_ref_type_meta
									where suse.ref_s_user_schema_code={$aobj_context->mobj_data["elemenmt_user_schema"]}
									and suse.internal_code<{$last_inserted_id}  and sg.internal_code={$group_id}
									and t.value='meta'
									and (sg.internal_code={$ref_s_user_schema_group_code}
									or sg.parent_group_code={$ref_s_user_schema_group_code})";
                    $lobj_getafterId = $aobj_context->mobj_db->GetRow($getafterId);

                    $after = (!is_null($lobj_getafterId['max_id'])) ? "u_" . $aobj_context->mobj_data["elemenmt_user_schema"] . "_g_" . $aobj_context->mobj_data["elemenmt_user_schema_group"] . "_ele_" . $lobj_getafterId["max_id"] : " ref_s_ref_data_code";
                    foreach ($lobj_rs_table_count as $key => $value) {
                        $select_db_field = "select db_field from s_user_schema_elements 
										where internal_code={$last_inserted_id}
										";
                        $lobj_select_db_field = $aobj_context->mobj_db->GetRow($select_db_field);

                        $field_id = $lobj_select_db_field['db_field'];
                        $alter_qr = "alter table " . $value . "  add  {$field_id} " . $lobj_rs_group['data_type'] . " after " . $after;
                        $lobj_rs = $aobj_context->mobj_db->Execute($alter_qr);


                        // for Inserting Entry Group Table STARTS
                        $group_entry_table = str_ireplace("group", "entry", $value);
                        $alter_e_qr = "alter table " . $group_entry_table . "  add  {$field_id} " . $lobj_rs_group['data_type'] . " after " . $after;
                        $lobj_alter_e_qr = $aobj_context->mobj_db->Execute($alter_e_qr);
                        // for Inserting Entry Group Table ENDS
                        if ($is_unique == '1') {
                            $create_idx = "CREATE UNIQUE INDEX indx_{$field_id}  ON {$value}(ref_s_ref_data_code,{$field_id}) ";
                            //echo $create_idx;
                            $lobj_create_idx = $aobj_context->mobj_db->Execute($create_idx);
                        }
                    }
                }
            }
            //to check element belong to meta
            if ($lobj_select_id['value'] == 'Meta' && $lobj_get_group_multiple['count'] == 0) {
                $is_group = "select CONCAT('module_',sus.INTERNAL_CODE) 
								as table_name,
								case when s.value='Text' then 'varchar(500)'
								when s.value='List Of Values' then 'varchar(50)'
								when s.value='Reference Data' then 'int(11)'
								when s.value ='Number' and {$decml}>12 then 'decimal(" . $decml . "," . $aobj_context->mobj_data["decimal"] . ")'
								when s.value ='Number' and {$decml}=12 then 'int(11)'
								when s.value ='Date' then 'date'
								when s.value='Month' then 'int(6)'
								when s.value='Image' then 'varchar(250)'
								else 'text' end as data_type
								from 
								s_user_schema_elements suse 
								inner join s_user_schema sus on sus.internal_code=suse.ref_s_user_schema_code
								left join s_ref_type s on s.internal_code=suse.data_type
								inner join s_ref_type s1 on s1.internal_code=suse.s_ref_type_meta
								where suse.internal_code=" . $last_inserted_id . " and s1.value='Meta'";

                $lobj_rs_group = $aobj_context->mobj_db->GetRow($is_group);

                //echo 'count'.count($lobj_rs_group['table_name']);
                // if count of Group is greater than one the alter the table 
                if (count($lobj_rs_group['table_name']) > 0) {
                    // it will insert into corresponding filds
                    $getafterId = "select max(s.internal_code) as max_id
									from s_user_schema_elements s inner join s_ref_type t on t.internal_code=s_ref_type_meta where ref_s_user_schema_code={$aobj_context->mobj_data["elemenmt_user_schema"]}
									and s.internal_code<{$last_inserted_id}  and is_multiple_group=0 and t.value='meta'";
                    $lobj_getafterId = $aobj_context->mobj_db->GetRow($getafterId);
                    $after = (!is_null($lobj_getafterId['max_id'])) ? "m_" . $aobj_context->mobj_data["elemenmt_user_schema"] . "_ele_" . $lobj_getafterId["max_id"] : " ref_s_user_schema_code";
                    $table_count = "show tables like '" . $lobj_rs_group['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) {
                            $meta_field_id = "m_" . $aobj_context->mobj_data["elemenmt_user_schema"] . "_ele_" . $last_inserted_id;
                            $alter_qr = "alter table " . $value . "  add  {$meta_field_id} " . $lobj_rs_group['data_type'] . " after " . $after;
                            $lobj_rs = $aobj_context->mobj_db->Execute($alter_qr);

                            if ($is_unique == '1') {
                                $create_idx = "CREATE UNIQUE INDEX indx_{$meta_field_id}  ON {$value}({$meta_field_id}) ";
                                $lobj_create_idx = $aobj_context->mobj_db->Execute($create_idx);
                            }
                        }
                    }
                }
            }

            //to create the file for each group
            //include(dirname(__FILE__)."\create_file.php");
            $aobj_context->mobj_data["schema_id"] = $schema_id;
            include(dirname(__FILE__) . "/create_all_grid_files.php");
            populateAllGridFiles($aobj_context);
            include(dirname(__FILE__) . "/create_module_meta_columns.php");
            createModuleMetaColumns($aobj_context);
            //include(dirname(__FILE__)."\create_group_file.php");
            include_once(dirname(__FILE__) . "/create_all_grid_group_files.php");
            populateAllGridGroupFiles($aobj_context);
            include(dirname(__FILE__) . "/create_html_files.php");
            CreateModuleHtmlFiles($aobj_context);
            include(dirname(__FILE__) . "/create_all_popup_files.php");
            populateAllPopupGridFiles($aobj_context);
            include(dirname(__FILE__) . "/create_all_views.php");
            CreateView($aobj_context);
            include(dirname(__FILE__) . "/update_defined_flag.php");
            UpdateDefinedFlag($aobj_context);
            fclose($handle);
            if ($lobj_rs) {
                $rdata[0] = "Element Name inserted successfully.";
                $rdata[1] = $max_seq;
                print_r($aobj_context->mobj_output->ToJSONEnvelope($rdata, 0, "success"));
                return $rdata;
            } else {
                $rdata[0] = "insertion Failed.";
                $rdata[1] = $max_seq;
                print_r($aobj_context->mobj_output->ToJSONEnvelope($rdata, -1, "Failure"));
                return $rdata;
            }
        } else {
            $rdata[0] = "System Name Already Exstis!";
            $rdata[1] = $max_seq;
            print_r($aobj_context->mobj_output->ToJSONEnvelope($rdata, 0, "success"));
            return $rdata;
        }
    } else { //to updATE THE s_group_table
        $decml = $aobj_context->mobj_data["decimal"] + 12;
        $update_field = "select CONCAT('user_schema_',sus.INTERNAL_CODE,'_group_',susg.internal_code) 
					as table_name,concat('u_',sus.internal_code,'_g_',susg.internal_code,'_ele_',suse.internal_code) as field_name,
					case when s.value='Text' then 'varchar(500)'
					when s.value='List Of Values' then 'varchar(50)'
					when s.value='Reference Data' then 'int(11)'
					when s.value ='Number' and {$decml}>12 then 'decimal(" . $decml . "," . $aobj_context->mobj_data["decimal"] . ")'
					when s.value ='Number' and {$decml}=12 then 'int(11)'
					when s.value ='Date' then 'date'
					when s.value='Month' then 'int(6)'
					when s.value='Image' then 'varchar(250)'
					else 'text' end as data_type,s.internal_code as data_type_code,susg.internal_code as group_code
					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
					left join s_ref_type s on s.internal_code=suse.data_type
					where suse.internal_code=" . $aobj_context->mobj_data["internal_code"] . " and multiple=1";

        $lobj_rs_alter_field = $aobj_context->mobj_db->GetRow($update_field);
        // to make is unique to set for group elemens if unique=1
        if ($is_unique == '1') {
            $alt_drop_indx = "ALTER   TABLE " . $lobj_rs_alter_field["table_name"] . " drop INDEX  indx_" . $lobj_rs_alter_field["field_name"];
            $lobj_alt_drop_indx = $aobj_context->mobj_db->Execute($alt_drop_indx);

            $alt_add_indx = "ALTER   TABLE " . $lobj_rs_alter_field["table_name"] . " add   UNIQUE INDEX  indx_" . $lobj_rs_alter_field["field_name"] . "(ref_s_ref_data_code," . $lobj_rs_alter_field["field_name"] . ")";
            $lobj_alt_add_indx = $aobj_context->mobj_db->Execute($alt_add_indx);
        }
        // to check updated group name is same as db group name and data type has been changed
        if ($lobj_rs_alter_field["group_code"] == $aobj_context->mobj_data["elemenmt_user_schema_group"] && $lobj_rs_alter_field["data_type_code"] != $aobj_context->mobj_data["user_schema_element_type"]) {
            $table_count = "show tables like '" . $lobj_rs_alter_field['table_name'] . "'";
            $lobj_rs_table_count = $aobj_context->mobj_db->GetRow($table_count);
            if (count($lobj_rs_table_count) > 0) {
                $desc_qry = "desc " . $lobj_rs_alter_field['table_name'];
                //echo $desc_qry;
                $obj_desc = $aobj_context->mobj_db->GetAll($desc_qry);
                foreach ($obj_desc as $key => $value)
                    //to check whether table has secific fied name
                    if ($value['Field'] == $lobj_rs_alter_field["field_name"]) {
                        $get_data_type = "select case when s.value='Text' then 'varchar(500)'
								when s.value='List Of Values' then 'varchar(50)'
								when s.value='Reference Data' then 'int(11)'
								when s.value ='Number' and {$decml}>12 then 'decimal(" . $decml . "," . $aobj_context->mobj_data["decimal"] . ")'
								when s.value ='Number' and {$decml}=12 then 'int(11)'
								when s.value ='Date' then 'date'
								when s.value='Month' then 'int(6)'
								when s.value='Image' then 'varchar(250)'
								else 'text' end as data_type from s_ref_type s  where internal_code=" . $aobj_context->mobj_data["user_schema_element_type"];
                        //echo $get_data_type;
                        $get_data_type_for_update = $aobj_context->mobj_db->GetRow($get_data_type);
                        $update_field_row = "alter table " . $lobj_rs_alter_field["table_name"] . " change " . $lobj_rs_alter_field["field_name"] . "  " . $lobj_rs_alter_field["field_name"] . " " . $get_data_type_for_update["data_type"];
                        $lobj_rs = $aobj_context->mobj_db->Execute($update_field_row);
                        // for Inserting Entry Group Table STARTS
                        $entry_group_table = str_ireplace("group", "entry", $lobj_rs_alter_field["table_name"]);
                        $update_entry_field_row = "alter table {$entry_group_table} change " . $lobj_rs_alter_field["field_name"] . "  " . $lobj_rs_alter_field["field_name"] . " " . $get_data_type_for_update["data_type"];
                        $lobj_update_entry_field_row = $aobj_context->mobj_db->Execute($update_entry_field_row);
                        // for Inserting Entry Group Table ENDS
                        $alt_drop_indx = "ALTER   TABLE " . $lobj_rs_alter_field["table_name"] . " drop INDEX  indx_" . $lobj_rs_alter_field["field_name"];
                        $lobj_alt_drop_indx = $aobj_context->mobj_db->Execute($alt_drop_indx);
                        if ($is_unique == '1') {
                            $alt_add_indx = "ALTER   TABLE " . $lobj_rs_alter_field["table_name"] . " add   UNIQUE INDEX  indx_" . $lobj_rs_alter_field["field_name"] . "(ref_s_ref_data_code," . $lobj_rs_alter_field["field_name"] . ")";
                            $lobj_alt_add_indx = $aobj_context->mobj_db->Execute($alt_add_indx);
                        }
                    }
            }
        }
        // If users changes the Exsting Multi Group to None
        else if ($aobj_context->mobj_data["elemenmt_user_schema_group"] == 0 && $lobj_rs_alter_field["group_code"] > 0) {
            if (count($lobj_rs_alter_field['table_name']) > 0) {
                $table_count = "show tables like '" . $lobj_rs_alter_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_alter_field['field_name'];
                        $lobj_rs_drop = $aobj_context->mobj_db->Execute($alter_qr);

                        $alt_drop_indx = "ALTER   TABLE " . $value . " drop INDEX  indx_" . $lobj_rs_alter_field["field_name"];
                        $lobj_alt_drop_indx = $aobj_context->mobj_db->Execute($alt_drop_indx);
                        if ($is_unique == '1') {
                            $alt_add_indx = "ALTER   TABLE " . $value . " add   UNIQUE INDEX  indx_" . $lobj_rs_alter_field["field_name"] . "(ref_s_ref_data_code," . $lobj_rs_alter_field["field_name"] . ")";
                            $lobj_alt_add_indx = $aobj_context->mobj_db->Execute($alt_add_indx);
                        }
                    }
                }
            }
        } //if user changs one group name to other grup name need to delete the exsting group field and insert new field in selected group
        else if ($aobj_context->mobj_data["elemenmt_user_schema_group"] != $lobj_rs_alter_field["group_code"]  && $aobj_context->mobj_data["elemenmt_user_schema_group"] != 0) {
            if (count($lobj_rs_alter_field['table_name']) > 0) {
                $table_count = "show tables like '" . $lobj_rs_alter_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_alter_field['field_name'];
                    $lobj_rs_drop = $aobj_context->mobj_db->Execute($alter_qr);
                    $alt_drop_indx = "ALTER   TABLE " . $value . " drop INDEX  indx_" . $lobj_rs_alter_field["field_name"];
                    $lobj_alt_drop_indx = $aobj_context->mobj_db->Execute($alt_drop_indx);
                    if ($is_unique == '1') {
                        $alt_add_indx = "ALTER   TABLE " . $value . " add   UNIQUE INDEX  indx_" . $lobj_rs_alter_field["field_name"] . "(ref_s_ref_data_code," . $lobj_rs_alter_field["field_name"] . ")";
                        $lobj_alt_add_indx = $aobj_context->mobj_db->Execute($alt_add_indx);
                    }
                }
            }
            //to get new grup name
            $new_group_qry = "select concat('user_schema_',ref_s_user_schema_code,'_group_',internal_code) as table_name
								from  s_user_schema_group where multiple=1 and internal_code=" . $aobj_context->mobj_data["elemenmt_user_schema_group"];
            $get_new_group_qry = $aobj_context->mobj_db->GetRow($new_group_qry);
            //to get new data type for that element
            $get_data_type = "select case when s.value='Text' then 'varchar(500)'
								when s.value='List Of Values' then 'varchar(50)'
								when s.value='Reference Data' then 'int(11)'
								when s.value ='Number' and {$decml}>12 then 'decimal(" . $decml . "," . $aobj_context->mobj_data["decimal"] . ")'
								when s.value ='Number' and {$decml}=12 then 'int(11)'
								when s.value ='Date' then 'date'
								when s.value='Month' then 'int(6)'
								when s.value='Image' then 'varchar(250)'
								else 'text' end as data_type from s_ref_type s  where internal_code=" . $aobj_context->mobj_data["user_schema_element_type"];
            //echo $get_data_type;
            $get_data_type_for_update = $aobj_context->mobj_db->GetRow($get_data_type);

            $getafterId = "select max(internal_code) as max_id
									from s_user_schema_elements where ref_s_user_schema_code={$aobj_context->mobj_data["elemenmt_user_schema"]}
									and internal_code<" . $aobj_context->mobj_data["internal_code"] . " and is_multiple_group=1 and ref_s_user_schema_group_code=" . $aobj_context->mobj_data["elemenmt_user_schema_group"];
            $lobj_getafterId = $aobj_context->mobj_db->GetRow($getafterId);
            $after = (!is_null($lobj_getafterId['max_id'])) ? "u_" . $aobj_context->mobj_data["elemenmt_user_schema"] . "_g_" . $aobj_context->mobj_data["elemenmt_user_schema_group"] . "_ele_" . $lobj_getafterId["max_id"] : " ref_s_ref_data_code";

            //to alter the new field name
            $update_field_row = "alter table " . $get_new_group_qry["table_name"] . " add u_" . $aobj_context->mobj_data["elemenmt_user_schema"] . "_g_" . $aobj_context->mobj_data["elemenmt_user_schema_group"] . "_ele_" . $aobj_context->mobj_data["internal_code"] . " " . $get_data_type_for_update['data_type'] . " after " . $after;
            // echo $update_field_row;
            $alter_lobj_rs = $aobj_context->mobj_db->Execute($update_field_row);

            $alt_drop_indx = "ALTER   TABLE " . $get_new_group_qry["table_name"] . " drop INDEX  indx_u_" . $aobj_context->mobj_data["elemenmt_user_schema"] . "_g_" . $aobj_context->mobj_data["elemenmt_user_schema_group"] . "_ele_" . $aobj_context->mobj_data["internal_code"];
            $lobj_alt_drop_indx = $aobj_context->mobj_db->Execute($alt_drop_indx);
            if ($is_unique == '1') {
                $f_id = $aobj_context->mobj_data["elemenmt_user_schema"] . "_g_" . $aobj_context->mobj_data["elemenmt_user_schema_group"] . "_ele_" . $aobj_context->mobj_data["internal_code"];
                $alt_add_indx = "ALTER   TABLE " . $get_new_group_qry["table_name"] . " add   UNIQUE INDEX  indx_{$f_id} (ref_s_ref_data_code,{$f_id}) ";
                $lobj_alt_add_indx = $aobj_context->mobj_db->Execute($alt_add_indx);
            }
        }

        $select_id = "select value from s_ref_type where internal_code=" . addslashes($aobj_context->mobj_data["meta_type"]);
        $lobj_select_id = $aobj_context->mobj_db->GetRow($select_id);
        //if users updates Type from Meta to Users then drop the Exsting Meta Field from Corresponing Table
        if ($lobj_select_id['value'] == 'User') {
            $is_group = "select CONCAT('module_',sus.INTERNAL_CODE) 
								as table_name,CONCAT('m_',sus.INTERNAL_CODE,'_ele_'," . $aobj_context->mobj_data["internal_code"] . ") as field_name,
								case when s.value='Text' then 'varchar(500)'
									when s.value='List Of Values' then 'varchar(50)'
									when s.value='Reference Data' then 'int(11)'
									when s.value ='Number' and {$decml}>12 then 'decimal(" . $decml . "," . $aobj_context->mobj_data["decimal"] . ")'
									when s.value ='Number' and {$decml}=12 then 'int(11)'
									when s.value ='Date' then 'date'
									when s.value='Month' then 'int(6)'
									when s.value='Image' then 'varchar(250)'
									else 'text'   end as data_type
								from 
								s_user_schema_elements suse 
								inner join s_user_schema sus on sus.internal_code=suse.ref_s_user_schema_code
								left join s_ref_type s on s.internal_code=suse.data_type
								inner join s_ref_type s1 on s1.internal_code=suse.s_ref_type_meta
								where suse.internal_code=" . $aobj_context->mobj_data["internal_code"] . " and s1.value='Meta'";
            //echo $is_group;
            $lobj_rs_group = $aobj_context->mobj_db->GetRow($is_group);
            //echo $is_group;
            //echo 'count'.count($lobj_rs_group['table_name']);
            // if count of Group is greater than one the alter the table 
            if (!empty($lobj_rs_group['table_name'])) {
                $table_count = "show tables like '" . $lobj_rs_group['table_name'] . "'";
                $lobj_rs_table_count = $aobj_context->mobj_db->GetRow($table_count);
                if (!empty($lobj_rs_table_count)) {
                    foreach ($lobj_rs_table_count as $key => $value)
                        $alter_qr = "alter table " . $value . " drop " . $lobj_rs_group['field_name'];
                    $lobj_rs_drop = $aobj_context->mobj_db->Execute($alter_qr);

                    $alt_drop_indx = "ALTER   TABLE " . $value . " drop INDEX  indx_" . $lobj_rs_group["field_name"];
                    $lobj_alt_drop_indx = $aobj_context->mobj_db->Execute($alt_drop_indx);
                    if ($is_unique == '1') {
                        $alt_add_indx = "ALTER   TABLE " . $value . " add   UNIQUE INDEX  indx_" . $lobj_rs_group["field_name"] . "(" . $lobj_rs_group["field_name"] . ")";
                        $lobj_alt_add_indx = $aobj_context->mobj_db->Execute($alt_add_indx);
                    }
                }
            }
        }
        //if users updates Type from Users to Meta need to insert new column in field
        if ($lobj_select_id['value'] == 'Meta' && $lobj_get_group_multiple['count'] == 0) {
            $is_group = "select CONCAT('module_',sus.INTERNAL_CODE) 
								as table_name,CONCAT('m_',sus.INTERNAL_CODE,'_ele_'," . $aobj_context->mobj_data["internal_code"] . ") as field_name,
								case when s.value='Text' then 'varchar(500)'
									when s.value='List Of Values' then 'varchar(50)'
									when s.value='Reference Data' then 'int(11)'
									when s.value ='Number' and {$decml}>12 then 'decimal(" . $decml . "," . $aobj_context->mobj_data["decimal"] . ")'
								when s.value ='Number' and {$decml}=12 then 'int(11)'
									when s.value ='Date' then 'date'
									when s.value='Month' then 'int(6)'
									when s.value='Image' then 'varchar(250)'
									else 'text' end as data_type
								from 
								s_user_schema_elements suse 
								inner join s_user_schema sus on sus.internal_code=suse.ref_s_user_schema_code
								left join s_ref_type s on s.internal_code=suse.data_type
								inner join s_ref_type s1 on s1.internal_code=suse.s_ref_type_meta
								where suse.internal_code=" . $aobj_context->mobj_data["internal_code"] . " and s1.value='User'";
            //echo $is_group;
            $lobj_rs_group = $aobj_context->mobj_db->GetRow($is_group);


            if (!empty($lobj_rs_group['table_name'])) {
                $table_count = "show tables like '" . $lobj_rs_group['table_name'] . "'";
                $lobj_rs_table_count = $aobj_context->mobj_db->GetRow($table_count);
                if (!empty($lobj_rs_table_count)) {
                    $getafterId = "select max(s.internal_code) as max_id
									from s_user_schema_elements s inner join s_ref_type t on t.internal_code=s_ref_type_meta where ref_s_user_schema_code={$aobj_context->mobj_data["elemenmt_user_schema"]}
									and s.internal_code<" . $aobj_context->mobj_data["internal_code"] . "  and is_multiple_group=0 and t.value='meta'";
                    $lobj_getafterId = $aobj_context->mobj_db->GetRow($getafterId);
                    $after = (!is_null($lobj_getafterId['max_id'])) ? "m_" . $aobj_context->mobj_data["elemenmt_user_schema"] . "_ele_" . $lobj_getafterId["max_id"] : " ref_s_user_schema_code";

                    foreach ($lobj_rs_table_count as $key => $value)
                        $alter_qr = "alter table " . $value . " add " . $lobj_rs_group['field_name'] . " " . $lobj_rs_group['data_type'] . " after " . $after;
                    $lobj_rs_drop = $aobj_context->mobj_db->Execute($alter_qr);
                }
            }
        }
        $decml = $aobj_context->mobj_data["decimal"] + 12;
        //if element type is META and user changes data type from int->date  etc....need to alter the table by droping the exsting field and insert new field
        $check_data_type = "select is_unique,data_type,s.value,concat('module_',ref_s_user_schema_code) as table_name,
												concat('m_',ref_s_user_schema_code,'_ele_',suse.internal_code) as field_name,data_type
												from s_user_schema_elements suse
												inner join s_ref_type s on s.internal_code=s_ref_type_meta
												where suse.internal_code=" . $aobj_context->mobj_data["internal_code"];

        $lobj_check_data_type = $aobj_context->mobj_db->GetRow($check_data_type);
        if (($lobj_check_data_type['value'] == 'Meta' && $lobj_check_data_type['data_type'] != $aobj_context->mobj_data["user_schema_element_type"]) || ($lobj_check_data_type['is_unique'] != $aobj_context->mobj_data["is_unique"])) {
            $table_count = "show tables like '" . $lobj_check_data_type['table_name'] . "'";
            $lobj_rs_table_count = $aobj_context->mobj_db->GetRow($table_count);
            if (count($lobj_rs_table_count) > 0) {
                $desc_qry = "desc " . $lobj_check_data_type['table_name'];
                //echo $desc_qry;
                $obj_desc = $aobj_context->mobj_db->GetAll($desc_qry);
                foreach ($obj_desc as $key => $value) {
                    //to check whether table has secific fied name
                    if ($value['Field'] == $lobj_check_data_type["field_name"]) {
                        $get_data_type = "select case when s.value='Text' then 'varchar(500)'
											when s.value='List Of Values' then 'varchar(50)'
											when s.value='Reference Data' then 'int(11)'
											when s.value ='Number' and {$decml}>12 then 'decimal(" . $decml . "," . $aobj_context->mobj_data["decimal"] . ")'
											when s.value ='Number' and {$decml}=12 then 'int(11)'
											when s.value ='Date' then 'date'
											when s.value='Month' then 'int(6)'
											when s.value='Image' then 'varchar(250)'
											else 'text' end as data_type from s_ref_type s  where internal_code=" . $aobj_context->mobj_data["user_schema_element_type"];
                        //echo $get_data_type;
                        $get_data_type_for_update = $aobj_context->mobj_db->GetRow($get_data_type);
                        $update_field_row = "alter table " . $lobj_check_data_type["table_name"] . " change " . $lobj_check_data_type["field_name"] . "  " . $lobj_check_data_type["field_name"] . " " . $get_data_type_for_update["data_type"];

                        $lobj_rs = $aobj_context->mobj_db->Execute($update_field_row);
                        $alt_drop_indx = "ALTER   TABLE " . $lobj_check_data_type["table_name"] . " drop INDEX  indx_" . $lobj_check_data_type["field_name"];
                        $lobj_alt_drop_indx = $aobj_context->mobj_db->Execute($alt_drop_indx);

                        if ($is_unique == '1') {
                            $alt_add_indx = "ALTER   TABLE " . $lobj_check_data_type["table_name"] . " add   UNIQUE INDEX  indx_" . $lobj_check_data_type["field_name"] . "(" . $lobj_check_data_type["field_name"] . ")";
                            $lobj_alt_add_indx = $aobj_context->mobj_db->Execute($alt_add_indx);
                        }
                    }
                }
            }
        }
        //to insert the linked fields table id
        $insert_linked_table = "select ref_module_code from s_user_schema_elements where internal_code=" . $aobj_context->mobj_data["linked_field"];
        $lobj_inked_table = $aobj_context->mobj_db->GetRow($insert_linked_table);
        if (!empty($lobj_inked_table))
            $lobj_inked_table_id = $insert_linked_table['ref_module_code'];
        else
            $lobj_inked_table_id = 0;
        $lstr_qry = "update s_user_schema_elements
				set ref_s_user_schema_code='" .   trim($aobj_context->mobj_data["elemenmt_user_schema"]) . "'," .
            "name='" . stripcslashes($aobj_context->mobj_data["user_schema_element_name"]) . "'," .
            "system_name='" . stripcslashes($aobj_context->mobj_data["user_schema_system_name"]) . "'," .
            "data_type='" . addslashes($aobj_context->mobj_data["user_schema_element_type"]) . "'," .
            "sequence='" . addslashes($aobj_context->mobj_data["user_schema_element_sequence"]) . "'," .
            "field_position='" . addslashes($aobj_context->mobj_data["user_schema_element_field_position"]) . "'," .
            "ref_s_user_schema_group_code='" . addslashes($aobj_context->mobj_data["elemenmt_user_schema_group"]) . "'," .
            "description='" . addslashes($aobj_context->mobj_data["user_schema_element_description"]) . "'," .
            "default_value='" . addslashes($aobj_context->mobj_data["user_schema_element_default_value"]) . "'," .
            "optional='" . $aobj_context->mobj_data["is_user_schema_element_optional"] . "',
				list_of_values='" . addslashes($aobj_context->mobj_data["lov"]) . "',
				linked_field='" . addslashes($aobj_context->mobj_data["linked_field"]) . "',
				source_field='" . addslashes($aobj_context->mobj_data["source_fields"]) . "',
				suffix='" . addslashes($aobj_context->mobj_data["suffix"]) . "',
				width='" . addslashes($aobj_context->mobj_data["width"]) . "',
				padding='" . addslashes($aobj_context->mobj_data["padding"]) . "',
				is_read_only='" . addslashes($aobj_context->mobj_data["is_read_only"]) . "',
				is_unique={$is_unique},include_in_popup={$include_in_pop_up},
				linked_field_table='" . $lobj_inked_table_id . "',
				is_visible='" . $aobj_context->mobj_data["is_visible"] . "',
				is_ref_module_code_lov='" . $aobj_context->mobj_data["is_lov"] . "',
				populate_lov='" . $aobj_context->mobj_data["is_populate_lov"] . "',
				conditional_display='" . $aobj_context->mobj_data["user_schema_element_conditional_display"] . "',
				on_enter='{$on_enter}',on_exit='{$on_exit}',is_key_field={$is_key_field},include_in_grid={$include_in_grid},
				ref_module_code_ref_field='{$ref_module_code_ref_field}',
				ref_module_code_ref_field_on='{$ref_module_code_ref_field_on}',
				ref_module_code_ref_field_const='{$ref_module_code_ref_field_const}',
				decimals =" . addslashes($aobj_context->mobj_data["decimal"]) . ",ref_module_code=" . addslashes($aobj_context->mobj_data["ref_module_code"]) . ", 
				s_ref_type_meta=" . addslashes($aobj_context->mobj_data["meta_type"]) . ",
				is_multiple_group={$is_multiple_group}" . ",
				grid_width={$grid_width},text_area_rows={$text_area_rows}  where internal_code =" . $aobj_context->mobj_data["internal_code"];
        $lobj_rs = $aobj_context->mobj_db->Execute($lstr_qry);



        // to update the db_value dec-1-2008
        $update_db_value = " update s_user_schema_elements suse,
								(select  suse.internal_code as db_field_ic_code, 
								case when t.value in('User','Meta')  and ref_s_user_schema_group_code>0 and 
								(is_multiple_group=1 or ug.parent_group_code<>0)
								then 
								if(suse.is_multiple_group=1,
								concat('u_',suse.ref_s_user_schema_code,'_g_',ref_s_user_schema_group_code,'_ele_',suse.internal_code),
								concat('u_',suse.ref_s_user_schema_code,'_g_',ug.parent_group_code,'_ele_',suse.internal_code)
								)
								when t.value='Meta' and ref_s_user_schema_group_code=0
								then
								concat('m_',suse.ref_s_user_schema_code,'_ele_',suse.internal_code)
								when t.value='Meta' and ref_s_user_schema_group_code>0 and is_multiple_group=0
								then
								concat('m_',suse.ref_s_user_schema_code,'_ele_',suse.internal_code)
								else ''
								end as db_fields_s_user_schema_elements
								from s_user_schema_elements suse 
								left join s_user_schema_group ug on ug.internal_code=ref_s_user_schema_group_code
								inner join s_ref_type t on t.internal_code=s_ref_type_meta
								 where suse.ref_s_user_schema_code={$schema_id}
								) a
								set db_field=db_fields_s_user_schema_elements
								where db_field_ic_code=suse.internal_code";
        $lobj_rs_update_db_value = $aobj_context->mobj_db->Execute($update_db_value);
    }
    $aobj_context->mobj_data["schema_id"] = $schema_id;
    include_once(dirname(__FILE__) . "/create_all_grid_files.php");
    populateAllGridFiles($aobj_context);
    include(dirname(__FILE__) . "/create_module_meta_columns.php");
    createModuleMetaColumns($aobj_context);
    include(dirname(__FILE__) . "/create_all_grid_group_files.php");
    populateAllGridGroupFiles($aobj_context);
    include(dirname(__FILE__) . "/create_html_files.php");
    CreateModuleHtmlFiles($aobj_context);
    include(dirname(__FILE__) . "/create_all_popup_files.php");
    populateAllPopupGridFiles($aobj_context);
    include(dirname(__FILE__) . "/update_defined_flag.php");
    UpdateDefinedFlag($aobj_context);
    if ($lobj_rs) {
        $rdata[0] = "Updated  Successfully.";
        $rdata[1] = $max_seq;
        print_r($aobj_context->mobj_output->ToJSONEnvelope($rdata, 0, "success"));
        return;
    } else {
        $rdata[0] = "Udatation Failed.";
        $rdata[1] = $max_seq;
        print_r($aobj_context->mobj_output->ToJSONEnvelope($rdata, -1, "Failure"));
    }
}

function UpdateDbField($aobj_context, $schema_id)
{
    $update_db_value = " update s_user_schema_elements suse,
				(select  suse.internal_code as db_field_ic_code, 
				case when t.value in('User','Meta')  and ref_s_user_schema_group_code>0 and 
				(is_multiple_group=1 or ug.parent_group_code<>0)
				then 
				if(suse.is_multiple_group=1,
				concat('u_',suse.ref_s_user_schema_code,'_g_',ref_s_user_schema_group_code,'_ele_',suse.internal_code),
				concat('u_',suse.ref_s_user_schema_code,'_g_',ug.parent_group_code,'_ele_',suse.internal_code)
				)
				when t.value='Meta' and ref_s_user_schema_group_code=0
				then
				concat('m_',suse.ref_s_user_schema_code,'_ele_',suse.internal_code)
				when t.value='Meta' and ref_s_user_schema_group_code>0 and is_multiple_group=0
				then
				concat('m_',suse.ref_s_user_schema_code,'_ele_',suse.internal_code)
				else ''
				end as db_fields_s_user_schema_elements
				from s_user_schema_elements suse 
				left join s_user_schema_group ug on ug.internal_code=ref_s_user_schema_group_code
				inner join s_ref_type t on t.internal_code=s_ref_type_meta
				 where suse.ref_s_user_schema_code={$schema_id}
				) a
				set db_field=db_fields_s_user_schema_elements
				where db_field_ic_code=suse.internal_code";
    $lobj_rs_update_db_value = $aobj_context->mobj_db->Execute($update_db_value);
}

function updateUserSchemaElementDbField($aobj_context)
{
    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);

    $update_db_value = " update s_user_schema_elements suse,
		(select  suse.internal_code as db_field_ic_code, 
		case when t.value in('User','Meta')  and ref_s_user_schema_group_code>0 and 
		(is_multiple_group=1 or ug.parent_group_code<>0)
		then 
		if(suse.is_multiple_group=1,
		concat('u_',suse.ref_s_user_schema_code,'_g_',ref_s_user_schema_group_code,'_ele_',suse.internal_code),
		concat('u_',suse.ref_s_user_schema_code,'_g_',ug.parent_group_code,'_ele_',suse.internal_code)
		)
		when t.value='Meta' and ref_s_user_schema_group_code=0
		then
		concat('m_',suse.ref_s_user_schema_code,'_ele_',suse.internal_code)
		when t.value='Meta' and ref_s_user_schema_group_code>0 and is_multiple_group=0
		then
		concat('m_',suse.ref_s_user_schema_code,'_ele_',suse.internal_code)
		else ''
		end as db_fields_s_user_schema_elements
		from s_user_schema_elements suse 
		left join s_user_schema_group ug on ug.internal_code=ref_s_user_schema_group_code
		inner join s_ref_type t on t.internal_code=s_ref_type_meta
		#where suse.ref_s_user_schema_code={$schema_id}
		) a
		set db_field=db_fields_s_user_schema_elements
		where db_field_ic_code=suse.internal_code";

    $lobj_rs_update_db_value = $aobj_context->mobj_db->Execute($update_db_value);
    $rdata = "Udatation Done.";
    print_r($aobj_context->mobj_output->ToJSONEnvelope($rdata, 0, "Success"));
}

function GetUserSchemaElementsRefModuleFilterDetails($aobj_context)
{
    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
    $schema_id = $aobj_context->mobj_data["schema_id"];
    $ref_module_code = $aobj_context->mobj_data["ref_module_code"];

    $select_ref_module_field_names = "select internal_code,name
								from s_user_schema_elements 
								where ref_s_user_schema_code={$ref_module_code}";

    $lobj_select_ref_module_field_names = $aobj_context->mobj_db->GetAll($select_ref_module_field_names);

    $select_schema_field_names = "select internal_code,name 
							from s_user_schema_elements
							where ref_s_user_schema_code={$schema_id}";

    $lobj_select_schema_field_names = $aobj_context->mobj_db->GetAll($select_schema_field_names);

    $rdata['ref_module_fields'] = $lobj_select_ref_module_field_names;
    $rdata['schema_fields'] = $lobj_select_schema_field_names;
    print_r($aobj_context->mobj_output->ToJSONEnvelope($rdata, 0, "success"));
}