<?php
include_once "../common/connect.php";
include_once "../common/db.php";
include_once "../common/defines.php";
include_once"../includes/encryption_min.php";
include_once"../common/scripts.php";

//error_reporting( E_ALL | E_STRICT );

////////////////////////////// json live check added 10/3/20

if(!empty($_REQUEST['section_flag'])){
if($_REQUEST['section_flag']=="cat_fetch"){
$mysqli_user=uplink();
$enc = i2d_secured_input($_REQUEST['encode']);	
$ref= i2d_secured_input($_REQUEST['ref']);	
$tbl=i2d_secured_input($_REQUEST['tbl']);
$title=i2d_secured_input($_REQUEST['tbl_title']);
$id_col=i2d_secured_input($_REQUEST['tbl_id_col']);
$status=status;
$ref_col=ref;

$d="SELECT $id_col , $title FROM $tbl WHERE $status='Active' AND $ref_col='$ref'";

	    //$dropd_query=$mysqli_user->query("SELECT $id_col , $title FROM $tbl WHERE $status='Active' AND $ref_col='$ref'");
	    $dropd_query=$mysqli_user->query($d);
		
		//echo $d;
	 // JSON start edge([)
	
    if($dropd_query){
	while($fetch_col =$dropd_query->fetch_array()){  //JSON objects will be generated in this loop
	if($enc!=0){
		    $ret_data[]=array(
		'id' => $fetch_col[$id_col], 
		'name' => base64_decode($fetch_col[$title]));
		 }
	
	else{
		    $ret_data[]=array(
		'id' => $fetch_col[$id_col], 
		'name' => $fetch_col[$title]);
		 }
	}
	
	}
	else{
	//echo "TABLE : -".$tbl."- DB Error:: ".mysqli_error($mysqli_user).":: i2d Technologies - Website Engine (c)2015";
	//$ret_data="TABLE : -".$tbl."- DB Error:: ".mysqli_error($mysqli_user).":: i2d Technologies - Website Engine (c)2015";
	}

		$count = 1;
		$sep = ",";								// seperator b/w objects
	
	
	header('Content-type: application/json');
	if(isset($ret_data)){
	echo json_encode($ret_data);
	}
}

}
	/////////////////////////////////////////////////
////////////////////////////// JSONs CASES/////////////////////////////////////
//////////////////////////////////////////////////////////////////
if(!empty($_REQUEST['section_flag'])){
if($_REQUEST['section_flag']=="cat_add"){
$mysqli_user=uplink();	
$enc=i2d_secured_input($_REQUEST['encode']);
$ref=i2d_secured_input($_REQUEST['ref']);
$dt1=date('Y-m-d');
$cat_named1 = i2d_secured_input($_REQUEST['item_name']);
$cat_named2 = remove_chars($cat_named1);	
if($enc!=0){
$cat_named = encode($cat_named2);
}
else{
$cat_named = $cat_named2;	
}	
$col_named = i2d_secured_input($_REQUEST['tbl_title']);
$table = i2d_secured_input($_REQUEST['tbl']);


    $query_check=$mysqli_user->query( "SELECT $col_named FROM $table WHERE BINARY $col_named='".$cat_named."' AND alias ='".$ref."';");
	if($query_check->num_rows > 0){ 
      
	  $rt = "This Item Already exists!";
	exit();
	}

	
	$cat_insert_query=$mysqli_user->query("INSERT INTO $table ($col_named,status,alias) values ('".$cat_named."','Active','".$ref."')");
	if($cat_insert_query){
		
		$rt = "Inserted";
	}	
	else{
		
		echo "error!".mysqli_error($mysqli_user);
	}	

echo json_encode($rt);
	
}

}

///////////////////////////////////////////
////////////////////////////// JSONs CASES/////////////////////////////////////
//////////////////////////////////////////////////////////////////
if(!empty($_REQUEST['section_flag'])){
if($_REQUEST['section_flag']=="sub_cat_add_multi"){
$mysqli_user=uplink();	
$enc=i2d_secured_input($_REQUEST['encode']);
$ref=i2d_secured_input($_REQUEST['ref']);
$parent=i2d_secured_input($_REQUEST['parent']);
$parent_id=i2d_secured_input($_REQUEST['parent_id']);
$tbl=i2d_secured_input($_REQUEST['tbl']);
$tbl_title=i2d_secured_input($_REQUEST['tbl_title']);
$dt1=date('Y-m-d');
$cat_named1 = i2d_secured_input($_REQUEST['item_name']);
$cat_named2 = remove_chars($cat_named1);	
if($enc!=0){
$cat_named = encode($cat_named2);
}
else{
$cat_named = $cat_named2;	
}	
$col_named=$parent_id;
$col_named2=$tbl_title;
$table=$tbl;

    $query_check=$mysqli_user->query( "SELECT $col_named2,$col_named FROM $table WHERE BINARY $col_named2='".$cat_named."' AND alias ='".$ref."' 
	AND $col_named='".$parent."';");
	if($query_check->num_rows > 0){ 
      
	  $rt = "This Item Already exists!";
	exit();
	}

	
	$cat_insert_query=$mysqli_user->query("INSERT INTO $table ($col_named2,$col_named,status,alias) values ('".$cat_named."','".$parent."','Active','".$ref."')");
	if($cat_insert_query){
		
		$rt = "inserted";
	}	
	else{
		
		echo "error!".mysqli_error($mysqli_user);
	}	

echo json_encode($rt);
	
}

}

///////////////////////////////////////////
if(!empty($_REQUEST['section_flag'])){
if($_REQUEST['section_flag']=="cat_fetch_multi"){
$mysqli_user=uplink();
$enc = i2d_secured_input($_REQUEST['encode']);	
$ref2= i2d_secured_input($_REQUEST['ref']);	
$parent_id= i2d_secured_input($_REQUEST['parent']);	
$parent= i2d_secured_input($_REQUEST['tbl_parent_col']);	
$title= i2d_secured_input($_REQUEST['tbl_title']);	
$tbl= i2d_secured_input($_REQUEST['tbl']);	
$id_col= i2d_secured_input($_REQUEST['tbl_id_col']);	
$status=status;
$ref_col=ref;
//$id_col=mmh_sub_table_id;



	    $dropd_query2=$mysqli_user->query("SELECT $id_col , $title FROM $tbl WHERE $parent='$parent_id' AND alias='$ref2'");
	 // JSON start edge([)
	if($dropd_query2->num_rows > 0){							
	
    while($fetch_col =$dropd_query2->fetch_array()){  //JSON objects will be generated in this loop
	if($enc!=0){
		    $ret_data[]=array(
		'id' => $fetch_col[$id_col], 
		'name' => base64_decode($fetch_col[$title]));
		 }
	
	else{
		    $ret_data[]=array(
		'id' => $fetch_col[$id_col], 
		'name' => $fetch_col[$title]);
		 }
	}
	
		
	//mysqli_error($mysqli_user)

		$count = 1;
		$sep = ",";								// seperator b/w objects
	
	
  }
	else{
	$ret_data[]=array(
		'id' => "0", 
		'name' => "No Data Found !");
	}
	
	header('Content-type: application/json');
	
	echo json_encode($ret_data);
	
}

}
	/////////////////////////////////////////////////



if(!empty($_REQUEST['section_flag'])){
if($_REQUEST['section_flag']=="del_rec"){
$mysqli_user=uplink();
$id_deler = i2d_secured_input($_REQUEST['ref_id']);	
$tabledel = i2d_secured_input($_REQUEST['tbl']);	
$coldel = i2d_secured_input($_REQUEST['id_col']);	

		
	
	$query_del=$mysqli_user->query( "DELETE FROM $tabledel WHERE $coldel='$id_deler'");


	if($query_del){ 
      	  echo "ho".$id_deler;
		  
return;

	}

	
	else{
		
		echo "error!".mysqli_error($mysqli_user);
	}
	
	
	

}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////	/////////////////////////////////////////////////



if(!empty($_REQUEST['section_flag'])){
if($_REQUEST['section_flag']=="del_rec_status"){
$mysqli_user=uplink();
$id_deler = i2d_secured_input($_REQUEST['ref_id']);	
$tabledel = i2d_secured_input($_REQUEST['tbl']);	
$coldel = i2d_secured_input($_REQUEST['id_col']);	

		
	
	$query_del=$mysqli_user->query( "UPDATE $tabledel SET status='INACTIVE' WHERE $coldel='$id_deler'");


	if($query_del){ 
      	  echo "ho".$id_deler;
		  
return;

	}

	
	else{
		
		echo "error!".mysqli_error($mysqli_user);
	}
	
	
	

}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

if(!empty($_REQUEST['section_flag'])){
if($_REQUEST['section_flag']=="del_rec_with_image"){
$mysqli_user=uplink();
$id_deler = i2d_secured_input($_REQUEST['ref_id']);	
$tabledel = i2d_secured_input($_REQUEST['tbl']);	
$coldel = i2d_secured_input($_REQUEST['id_col']);	

/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
$img1=db_ref(master,id_col,$id_deler,photo1);
$img2=db_ref(master,id_col,$id_deler,photo2);
$img3=db_ref(master,id_col,$id_deler,photo3);
$img4=db_ref(master,id_col,$id_deler,photo4);

$images_array=array($img1,$img2,$img3,$img4);

$image_path1 = photo_folder;	
$image_path2 = photo_folder_og;
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////

foreach($images_array as $item){
unlink($image_path1.$item);	
unlink($image_path2.$item);	
}


	$query_del=$mysqli_user->query( "DELETE FROM $tabledel WHERE $coldel='$id_deler'");

//echo $image_path."/".$image_name;

/*	if(file_exists($image_path."/".$image_name)){	 
	*/ //unlink($image_path.'/'.$image_name);
	/* } */


	if($query_del){ 
      	  echo "ho".$id_deler;
		  
return;

	}

	
	else{
		
		echo "error!".mysqli_error($mysqli_user);
	}
	
	
	

}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

if(!empty($_REQUEST['section_flag'])){
if($_REQUEST['section_flag']=="del_rec_by_cat"){
$mysqli_user=uplink();
$id_deler = i2d_secured_input($_REQUEST['ref_id']);	
$tabledel = i2d_secured_input($_REQUEST['tbl']);	
$coldel = i2d_secured_input($_REQUEST['id_col']);	

///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
$image_path1 = photo_folder;	
$image_path2 = photo_folder_og;


$array_cols=array(id_col,product_sub_category,photo1,photo2,photo3,photo4,status);

$tdsettings=array(
	'table'=>master,
	'id_col'=>id_col,
	'ref_value'=>'product_entry',
	'where_col'=>product_sub_category,
	'fields_array'=>$array_cols,
	'where_value'=>$id_deler,
	'echo'=>'0',
	'status_vaue'=>'Active',
	'type'=>'cart_fetch');

$count=db_fetch($tdsettings);

foreach($count as $count_item){
unlink($image_path1.$count_item[photo1]);	
unlink($image_path1.$count_item[photo2]);	
unlink($image_path1.$count_item[photo3]);	
unlink($image_path1.$count_item[photo4]);	

unlink($image_path2.$count_item[photo1]);	
unlink($image_path2.$count_item[photo2]);	
unlink($image_path2.$count_item[photo3]);	
unlink($image_path2.$count_item[photo4]);	
$id_prod=$count_item[id_col];

	$query_del1=$mysqli_user->query( "DELETE FROM master WHERE id='$id_prod'");

}

/////////////////////////////////////////////////////////////////

	$query_del=$mysqli_user->query( "DELETE FROM $tabledel WHERE $coldel='$id_deler'");

//echo $image_path."/".$image_name;

/*	if(file_exists($image_path."/".$image_name)){	 
	*/ //unlink($image_path.'/'.$image_name);
	/* } */


	if($query_del){ 
      	  echo "ho".$id_deler;
		  
return;

	}

	
	else{
		
		echo "error!".mysqli_error($mysqli_user);
	}
	
	
	

}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

if(!empty($_REQUEST['section_flag'])){
if($_REQUEST['section_flag']=="del_rec_by_brand"){
$mysqli_user=uplink();
$id_deler = i2d_secured_input($_REQUEST['ref_id']);	
$tabledel = i2d_secured_input($_REQUEST['tbl']);	
$coldel = i2d_secured_input($_REQUEST['id_col']);	

///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
$image_path1 = photo_folder;	
$image_path2 = photo_folder_og;


$array_cols=array(id_col,company_id,photo1,photo2,photo3,photo4,status);

$tdsettings=array(
	'table'=>master,
	'id_col'=>id_col,
	'ref_value'=>'product_entry',
	'where_col'=>company_id,
	'fields_array'=>$array_cols,
	'where_value'=>$id_deler,
	'echo'=>'0',
	'status_vaue'=>'Active',
	'type'=>'cart_fetch');

$count=db_fetch($tdsettings);

foreach($count as $count_item){
unlink($image_path1.$count_item[photo1]);	
unlink($image_path1.$count_item[photo2]);	
unlink($image_path1.$count_item[photo3]);	
unlink($image_path1.$count_item[photo4]);	

unlink($image_path2.$count_item[photo1]);	
unlink($image_path2.$count_item[photo2]);	
unlink($image_path2.$count_item[photo3]);	
unlink($image_path2.$count_item[photo4]);	
$id_prod=$count_item[id_col];

	$query_del1=$mysqli_user->query( "DELETE FROM master WHERE id='$id_prod'");

}

/////////////////////////////////////////////////////////////////

	$query_del=$mysqli_user->query( "DELETE FROM $tabledel WHERE $coldel='$id_deler'");

//echo $image_path."/".$image_name;

/*	if(file_exists($image_path."/".$image_name)){	 
	*/ //unlink($image_path.'/'.$image_name);
	/* } */


	if($query_del){ 
      	  echo "ho".$id_deler;
		  
return;

	}

	
	else{
		
		echo "error!".mysqli_error($mysqli_user);
	}
	
	
	

}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	/////////////////////////////////////////////////
if(!empty($_REQUEST['section_flag'])){
if($_REQUEST['section_flag']=="live_check"){
$mysqli_user=uplink();
$user1 = i2d_secured_input($_REQUEST['user']);
$id_col = i2d_secured_input($_REQUEST['id_col']);
$matching_col = i2d_secured_input($_REQUEST['matching_col']);
$table= i2d_secured_input($_REQUEST['table']);
if($_REQUEST['encoding']!=0){
$user=base64_encode($user1);
}
else{
$user=$user1;
}

$table_query333=$mysqli_user->query("SELECT $id_col FROM $table where status='Active' AND $matching_col='$user'");

if($table_query333){

$deal_count333=mysqli_num_rows($table_query333);

if($deal_count333 > 0){
   	
	$rt=$user1." - record already exists!";
}


else{
$rt="free";
}

		echo $rt;
	
	return;


}


	else{
	    
	    echo "error!".mysqli_error($mysqli_user);
	}
	


}
}

////////////////////////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

if(!empty($_REQUEST['section_flag'])){
if($_REQUEST['section_flag']=="test_json"){
$mysqli_user=uplink();
$table = i2d_secured_input($_REQUEST['table']);	
$where_col = i2d_secured_input($_REQUEST['where_col']);	
$id = i2d_secured_input($_REQUEST['id']);	
$id_col = i2d_secured_input($_REQUEST['id_col']);	
$to_find = i2d_secured_input($_REQUEST['to_find']);	
$enc=0;
		
	
    $dropd_query2=$mysqli_user->query("SELECT $where_col FROM $table WHERE $id_col='$id'");
	 // JSON start edge([)
	
    while($fetch_col =$dropd_query2->fetch_array()){  //JSON objects will be generated in this loop
	if($enc!=0){
		    $ret_data[]=array(
		'id' => $fetch_col[$id_col], 
		'name' => base64_decode($fetch_col[$title]));
		 }
	
	else{
		    $ret_data[]=array(
		'title' => $fetch_col[$to_find]);
		 }
	}
	
		
	//mysqli_error($mysqli_user)

	
	
	header('Content-type: application/json');
	
	echo json_encode($ret_data);
	
	

}
}


?>