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

if (isset($_POST['id_delete_level'])) {
	if(settype($_POST['id_delete_level'], 'integer')) $id_delete_level = $_POST['id_delete_level'];
} else $id_delete_level = 0;

if ($id_delete){
	$sql = "SELECT * 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 (isset($c_levels) && $c_levels){
			$sql = "DELETE FROM ".$module."_levels_links WHERE id_module = :id ";
			$result = $conn->prepare($sql);
			$result->bindValue(':id', $id_delete, PDO::PARAM_INT);
			$result->execute();
		}

		$file_dir_delete = "../".$module."/";
		if ($module == 'images') {
			$result_sub = $conn->query("SHOW TABLES");
			while ($arr_sub = $result_sub->fetch(PDO::FETCH_COLUMN)){
				if (str_ends_with($arr_sub, '_images')){
					$thumb_module = str_replace('_images', '', $arr_sub);
					$thumb_module_options = $thumb_module."_options";
					
					$do_delete = true;
					if (!check_table($thumb_module_options)) {
						if (str_contains($thumb_module, '_')){
							$thumb_module_expr = explode('_', $thumb_module);
							if (check_table($thumb_module_expr[0].'_options')) $thumb_module_options = $thumb_module_expr[0]."_options";
							else $do_delete = false;
						} else $do_delete = false;
					}
					
					if ($do_delete === true){
						$sql_tmp = "SELECT title FROM ".$thumb_module_options." WHERE title LIKE 'image_width_%' OR title LIKE 'image_height_%' ORDER BY id ASC";
						$result_tmp = $conn->prepare($sql_tmp);
						$result_tmp->execute();
						if ($result_tmp->rowCount()){
							$module_dir = $file_dir_delete.$thumb_module."/";
							while ($arr_tmp = $result_tmp->fetch()){
								$key = str_replace('image_width_', '', str_replace('image_height_', '', $arr_tmp['title']));
								if (is_file($module_dir.'thumb'.$key.'/'.$arr['file'])){
									unlink($module_dir.'thumb'.$key.'/'.$arr['file']);
								}
								if (is_dir_empty($module_dir.'thumb'.$key.'/')){
									rmdir($module_dir.'thumb'.$key.'/');
								}
							}
							if (is_dir_empty($module_dir)){
								rmdir($module_dir);
							}
						}
						$sql_tmp = "SELECT title FROM ".$thumb_module_options." WHERE title LIKE 'lightbox_%' ORDER BY id ASC";
						$result_tmp = $conn->prepare($sql_tmp);
						$result_tmp->execute();
						if ($result_tmp->rowCount()){
							$module_dir = $file_dir_delete.$thumb_module."/";
							while ($arr_tmp = $result_tmp->fetch()){
								if (is_file($module_dir.'lightbox/'.$arr['file'])){
									unlink($module_dir.'lightbox/'.$arr['file']);
								}
								if (is_dir_empty($module_dir.'lightbox/')){
									rmdir($module_dir.'lightbox/');
								}
							}
							if (is_dir_empty($module_dir)){
								rmdir($module_dir);
							}
						}
					}
					
					$sql = "DELETE FROM ".$thumb_module."_".$module." WHERE id_media = :id ";
					$result = $conn->prepare($sql);
					$result->bindValue(':id', $id_delete, PDO::PARAM_INT);
					$result->execute();
				}
			}
			if (is_file($file_dir_delete."original/".$arr['file'])) unlink($file_dir_delete."original/".$arr['file']);
			if (is_dir_empty($file_dir_delete."original/")){
				rmdir($file_dir_delete."original/");
			}
			if (is_dir_empty($file_dir_delete)){
				rmdir($file_dir_delete);
			}
			
		} else if ($module == 'files'){
			$result_sub = $conn->query("SHOW TABLES");
			while ($arr_sub = $result_sub->fetch(PDO::FETCH_COLUMN)){
				if (str_ends_with($arr_sub, '_files')){
					$file_module = str_replace('_files', '', $arr_sub);
					$sql = "DELETE FROM ".$file_module."_".$module." WHERE id_media = :id ";
					$result = $conn->prepare($sql);
					$result->bindValue(':id', $id_delete, PDO::PARAM_INT);
					$result->execute();
				}
			}
			if (is_file($file_dir_delete.$arr['file'])) unlink($file_dir_delete.$arr['file']);
			if (is_dir_empty($file_dir_delete)){
				rmdir($file_dir_delete);
			}
		}
		
		
		$sql = "DELETE FROM ".$module." WHERE id = :id ";
		$result = $conn->prepare($sql);
		$result->bindValue(':id', $id_delete, PDO::PARAM_INT);
		$result->execute();
		
	}
	$id_delete = 0;
};

if ($id_delete_level){
	
	$sql = "SELECT guidv4, parent_guidv4 FROM ".$module."_levels WHERE id = :id ";
	$result = $conn->prepare($sql);
	$result->bindValue(':id', $id_delete_level, PDO::PARAM_INT);
	$result->execute();
	
	if ($result->rowCount()){
		$arr = $result->fetch();
		
		if ($arr['parent_guidv4']){
			$sql = "UPDATE ".$module."_levels 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."_levels_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."_levels 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."_levels_links WHERE guidv4 = :guidv4 ";
			$result = $conn->prepare($sql);
			$result->bindValue(':guidv4', $arr['guidv4'], PDO::PARAM_STR);
			$result->execute();
		}
		
		
		$sql = "DELETE FROM ".$module."_levels WHERE guidv4 = :guidv4 ";
		$result = $conn->prepare($sql);
		$result->bindValue(':guidv4', $arr['guidv4'], PDO::PARAM_STR);
		$result->execute();
		
	}
	$id_delete_level = 0;
}

if (isset($_POST['multi_delete']) && $_POST['multi_delete']){
	if (isset($_POST['c_check'])){
		foreach ($_POST['c_check'] AS $check_id => $value){
			$sql = "SELECT * 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 (isset($c_levels) && $c_levels){
					$sql = "DELETE FROM ".$module."_levels_links WHERE id_module = :id ";
					$result = $conn->prepare($sql);
					$result->bindValue(':id', $check_id, PDO::PARAM_INT);
					$result->execute();
				}

				$file_dir_delete = "../".$module."/";
				if ($module == 'images') {
					$result_sub = $conn->query("SHOW TABLES");
					while ($arr_sub = $result_sub->fetch(PDO::FETCH_COLUMN)){
						if (str_ends_with($arr_sub, '_images')){
							$thumb_module = str_replace('_images', '', $arr_sub);
							$thumb_module_options = $thumb_module."_options";
							
							$do_delete = true;
							if (!check_table($thumb_module_options)) {
								if (str_contains($thumb_module, '_')){
									$thumb_module_expr = explode('_', $thumb_module);
									if (check_table($thumb_module_expr[0].'_options')) $thumb_module_options = $thumb_module_expr[0]."_options";
									else $do_delete = false;
								} else $do_delete = false;
							}
							
							if ($do_delete === true){
								$sql_tmp = "SELECT title FROM ".$thumb_module_options." WHERE title LIKE 'image_width_%' OR title LIKE 'image_height_%' ORDER BY id ASC";
								$result_tmp = $conn->prepare($sql_tmp);
								$result_tmp->execute();
								if ($result_tmp->rowCount()){
									$module_dir = $file_dir_delete.$thumb_module."/";
									while ($arr_tmp = $result_tmp->fetch()){
										$key = str_replace('image_width_', '', str_replace('image_height_', '', $arr_tmp['title']));
										if (is_file($module_dir.'thumb'.$key.'/'.$arr['file'])){
											unlink($module_dir.'thumb'.$key.'/'.$arr['file']);
										}
										if (is_dir_empty($module_dir.'thumb'.$key.'/')){
											rmdir($module_dir.'thumb'.$key.'/');
										}
									}
									if (is_dir_empty($module_dir)){
										rmdir($module_dir);
									}
								}
								$sql_tmp = "SELECT title FROM ".$thumb_module_options." WHERE title LIKE 'lightbox_%' ORDER BY id ASC";
								$result_tmp = $conn->prepare($sql_tmp);
								$result_tmp->execute();
								if ($result_tmp->rowCount()){
									$module_dir = $file_dir_delete.$thumb_module."/";
									while ($arr_tmp = $result_tmp->fetch()){
										if (is_file($module_dir.'lightbox/'.$arr['file'])){
											unlink($module_dir.'lightbox/'.$arr['file']);
										}
										if (is_dir_empty($module_dir.'lightbox/')){
											rmdir($module_dir.'lightbox/');
										}
									}
									if (is_dir_empty($module_dir)){
										rmdir($module_dir);
									}
								}
							}
							$sql = "DELETE FROM ".$thumb_module."_".$module." WHERE id_media = :id ";
							$result = $conn->prepare($sql);
							$result->bindValue(':id', $check_id, PDO::PARAM_INT);
							$result->execute();
						}
					}
					if (is_file($file_dir_delete."original/".$arr['file'])) unlink($file_dir_delete."original/".$arr['file']);
					if (is_dir_empty($file_dir_delete."original/")){
						rmdir($file_dir_delete."original/");
					}
					if (is_dir_empty($file_dir_delete)){
						rmdir($file_dir_delete);
					}
					
				} else if ($module == 'files'){
					$result_sub = $conn->query("SHOW TABLES");
					while ($arr_sub = $result_sub->fetch(PDO::FETCH_COLUMN)){
						if (str_ends_with($arr_sub, '_files')){
							$file_module = str_replace('_files', '', $arr_sub);
							$sql = "DELETE FROM ".$file_module."_".$module." WHERE id_media = :id ";
							$result = $conn->prepare($sql);
							$result->bindValue(':id', $check_id, PDO::PARAM_INT);
							$result->execute();
						}
					}
					if (is_file($file_dir_delete.$arr['file'])) unlink($file_dir_delete.$arr['file']);
					if (is_dir_empty($file_dir_delete)){
						rmdir($file_dir_delete);
					}
				}
				
				$sql = "DELETE FROM ".$module." WHERE id = :id ";
				$result = $conn->prepare($sql);
				$result->bindValue(':id', $check_id, PDO::PARAM_INT);
				$result->execute();
				
			}
		}
	}
}


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