<?php
if (isset($_POST['id_delete'])) {
	if(settype($_POST['id_delete'], 'integer')) $id_delete = $_POST['id_delete'];
} else $id_delete = 0;

if ($id_delete){
	
	$sql = "SELECT guidv4, parent_guidv4 FROM ".$module." WHERE id = :id ";
	$result = $conn->prepare($sql);
	$result->bindValue(':id', $id_delete, PDO::PARAM_INT);
	$result->execute();
	
	if ($result->rowCount()){
		$arr = $result->fetch();
		
			if ($arr['parent_guidv4']){
				$sql = "UPDATE ".$module." SET parent_guidv4 = :parent_guidv4 WHERE parent_guidv4 = :guidv4 ";
				$result = $conn->prepare($sql);
				$result->bindValue(':parent_guidv4', $arr['parent_guidv4'], PDO::PARAM_STR);
				$result->bindValue(':guidv4', $arr['guidv4'], PDO::PARAM_STR);
				$result->execute();
				
				$sql = "UPDATE ".$module."_links SET guidv4 = :parent_guidv4 WHERE guidv4 = :guidv4 ";
				$result = $conn->prepare($sql);
				$result->bindValue(':parent_guidv4', $arr['parent_guidv4'], PDO::PARAM_STR);
				$result->bindValue(':guidv4', $arr['guidv4'], PDO::PARAM_STR);
				$result->execute();
			} else {
				$sql = "UPDATE ".$module." SET parent_guidv4 = NULL WHERE parent_guidv4 = :guidv4 ";
				$result = $conn->prepare($sql);
				$result->bindValue(':guidv4', $arr['guidv4'], PDO::PARAM_STR);
				$result->execute();
				
				$sql = "DELETE FROM ".$module."_links WHERE guidv4 = :guidv4 ";
				$result = $conn->prepare($sql);
				$result->bindValue(':guidv4', $arr['guidv4'], PDO::PARAM_STR);
				$result->execute();
			}
			
			
			$sql = "DELETE FROM ".$module." WHERE guidv4 = :guidv4 ";
			$result = $conn->prepare($sql);
			$result->bindValue(':guidv4', $arr['guidv4'], PDO::PARAM_STR);
			$result->execute();
		
	}
	$id_delete = 0;
}

if (isset($_POST['multi_delete']) && $_POST['multi_delete']){
	if (isset($_POST['c_check'])){
		foreach ($_POST['c_check'] AS $check_id => $value){
			
			$sql = "SELECT guidv4, parent_guidv4 FROM ".$module." WHERE id = :id ";
			$result = $conn->prepare($sql);
			$result->bindValue(':id', $check_id, PDO::PARAM_INT);
			$result->execute();
			
			if ($result->rowCount()){
				$arr = $result->fetch();
		
				if ($arr['parent_guidv4']){
					$sql = "UPDATE ".$module." SET parent_guidv4 = :parent_guidv4 WHERE parent_guidv4 = :guidv4 ";
					$result = $conn->prepare($sql);
					$result->bindValue(':parent_guidv4', $arr['parent_guidv4'], PDO::PARAM_STR);
					$result->bindValue(':guidv4', $arr['guidv4'], PDO::PARAM_STR);
					$result->execute();
					
					$sql = "UPDATE ".$module."_links SET guidv4 = :parent_guidv4 WHERE guidv4 = :guidv4 ";
					$result = $conn->prepare($sql);
					$result->bindValue(':parent_guidv4', $arr['parent_guidv4'], PDO::PARAM_STR);
					$result->bindValue(':guidv4', $arr['guidv4'], PDO::PARAM_STR);
					$result->execute();
				} else {
					$sql = "UPDATE ".$module." SET parent_guidv4 = NULL WHERE parent_guidv4 = :guidv4 ";
					$result = $conn->prepare($sql);
					$result->bindValue(':guidv4', $arr['guidv4'], PDO::PARAM_STR);
					$result->execute();
					
					$sql = "DELETE FROM ".$module."_links WHERE guidv4 = :guidv4 ";
					$result = $conn->prepare($sql);
					$result->bindValue(':guidv4', $arr['guidv4'], PDO::PARAM_STR);
					$result->execute();
				}
				
				
				$sql = "DELETE FROM ".$module." WHERE guidv4 = :guidv4 ";
				$result = $conn->prepare($sql);
				$result->bindValue(':guidv4', $arr['guidv4'], PDO::PARAM_STR);
				$result->execute();
				
			}
		}
	}
}


print "
<input type='hidden' name='id_delete' value='".$id_delete."' />
<input type='hidden' name='multi_delete' value='0' />";