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

			$resolutions_data = array();
			foreach (get_defined_vars() AS $var => $value){
				if (str_starts_with($var, 'image_width_') OR str_starts_with($var, 'image_height_')){
					$key = str_replace('image_width_', '', str_replace('image_height_', '', $var));
					if (str_starts_with($var, 'image_width_') && $value) $resolutions_data[$key]['width'] = intval($value);
					else if (str_starts_with($var, 'image_width_')) $resolutions_data[$key]['width'] = NULL;
					else if (str_starts_with($var, 'image_height_') && $value) $resolutions_data[$key]['height'] = intval($value);
					else if (str_starts_with($var, 'image_height_')) $resolutions_data[$key]['height'] = NULL;
				}
			}

			$images_remove = array();
			$sql_sub = "SELECT images.file, module.id_media FROM ".$module."_images AS module 
									LEFT JOIN images AS images ON module.id_media = images.id 
									WHERE id_module = :id GROUP BY images.file ";
			$result_sub = $conn->prepare($sql_sub);
			$result_sub->bindValue(':id', $id_delete, PDO::PARAM_INT);
			$result_sub->execute();
			if ($result_sub->rowCount()){
				while ($arr_sub = $result_sub->fetch()){
					$sql_check = "SELECT id_media FROM ".$module."_images WHERE id_media = :id_media AND id_module != :id LIMIT 1";
					$result_check = $conn->prepare($sql_check);
					$result_check->bindValue(':id_media', $arr_sub['id_media'], PDO::PARAM_INT);
					$result_check->bindValue(':id', $id_delete, PDO::PARAM_INT);
					$result_check->execute();
					if (!$result_check->rowCount()) $images_remove[$arr_sub['id_media']] = $arr_sub['file'];
				}
			}

			$sql_sub = "DELETE FROM ".$module."_images WHERE id_module = :id ";
			$result_sub = $conn->prepare($sql_sub);
			$result_sub->bindValue(':id', $id_delete, PDO::PARAM_INT);
			$result_sub->execute();
			
			if (isset($images_remove) && is_array($images_remove)){
				foreach ($resolutions_data AS $thumb => $resolution){
					foreach ($images_remove AS $file){
						if (is_file($module_dir.'thumb'.$thumb.'/'.$file)) {
							unlink($module_dir.'thumb'.$thumb.'/'.$file);
						}
					}
					if (is_dir_empty($module_dir.'thumb'.$thumb.'/')){
						rmdir($module_dir.'thumb'.$thumb.'/');
					}
					if (is_dir_empty($module_dir)){
						rmdir($module_dir);
					}
				}
			}
			
		}
		
			
		if (isset($c_files) && $c_files && check_table($module.'_files')){
			$sql = "DELETE FROM ".$module."_files WHERE id_module = :id ";
			$result = $conn->prepare($sql);
			$result->bindValue(':id', $id_delete, PDO::PARAM_INT);
			$result->execute();
		}
		
		if (isset($c_links) && $c_links && check_table($module.'_links')){
			$sql = "DELETE FROM ".$module."_links WHERE id_module = :id ";
			$result = $conn->prepare($sql);
			$result->bindValue(':id', $id_delete, PDO::PARAM_INT);
			$result->execute();
		}
		
		$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 (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 && check_table($module.'_levels_links')){
					$sql = "DELETE FROM ".$module."_levels_links WHERE id_module = :id ";
					$result = $conn->prepare($sql);
					$result->bindValue(':id', $check_id, PDO::PARAM_INT);
					$result->execute();
				}
				
				if (isset($c_images) && $c_images && check_table($module.'_images')){
					$file_dir_delete = '../images/';
					$module_dir = $file_dir_delete.$module."/";
		
					$resolutions_data = array();
					foreach (get_defined_vars() AS $var => $value){
						if (str_starts_with($var, 'image_width_') OR str_starts_with($var, 'image_height_')){
							$key = str_replace('image_width_', '', str_replace('image_height_', '', $var));
							if (str_starts_with($var, 'image_width_') && $value) $resolutions_data[$key]['width'] = intval($value);
							else if (str_starts_with($var, 'image_width_')) $resolutions_data[$key]['width'] = NULL;
							else if (str_starts_with($var, 'image_height_') && $value) $resolutions_data[$key]['height'] = intval($value);
							else if (str_starts_with($var, 'image_height_')) $resolutions_data[$key]['height'] = NULL;
						}
					}
		
					$images_remove = array();
					$sql_sub = "SELECT images.file, module.id_media FROM ".$module."_images AS module 
											LEFT JOIN images AS images ON module.id_media = images.id 
											WHERE id_module = :id GROUP BY images.file ";
					$result_sub = $conn->prepare($sql_sub);
					$result_sub->bindValue(':id', $check_id, PDO::PARAM_INT);
					$result_sub->execute();
					if ($result_sub->rowCount()){
						while ($arr_sub = $result_sub->fetch()){
							$sql_check = "SELECT id_media FROM ".$module."_images WHERE id_media = :id_media AND id_module != :id LIMIT 1";
							$result_check = $conn->prepare($sql_check);
							$result_check->bindValue(':id_media', $arr_sub['id_media'], PDO::PARAM_INT);
							$result_check->bindValue(':id', $check_id, PDO::PARAM_INT);
							$result_check->execute();
							if (!$result_check->rowCount()) $images_remove[$arr_sub['id_media']] = $arr_sub['file'];
						}
					}
		
					$sql_sub = "DELETE FROM ".$module."_images WHERE id_module = :id ";
					$result_sub = $conn->prepare($sql_sub);
					$result_sub->bindValue(':id', $check_id, PDO::PARAM_INT);
					$result_sub->execute();
					
					if (isset($images_remove) && is_array($images_remove)){
						foreach ($resolutions_data AS $thumb => $resolution){
							foreach ($images_remove AS $file){
								if (is_file($module_dir.'thumb'.$thumb.'/'.$file)) {
									unlink($module_dir.'thumb'.$thumb.'/'.$file);
								}
							}
							if (is_dir_empty($module_dir.'thumb'.$thumb.'/')){
								rmdir($module_dir.'thumb'.$thumb.'/');
							}
							if (is_dir_empty($module_dir)){
								rmdir($module_dir);
							}
						}
					}
				}
				
					
				if (isset($c_files) && $c_files && check_table($module.'_files')){
					$sql = "DELETE FROM ".$module."_files WHERE id_module = :id ";
					$result = $conn->prepare($sql);
					$result->bindValue(':id', $check_id, PDO::PARAM_INT);
					$result->execute();
				}
				
				if (isset($c_links) && $c_links && check_table($module.'_links')){
					$sql = "DELETE FROM ".$module."_links WHERE id_module = :id ";
					$result = $conn->prepare($sql);
					$result->bindValue(':id', $check_id, PDO::PARAM_INT);
					$result->execute();
				}
				
				$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='multi_delete' value='0' />";