<?
	require_once('user.php');
	require_once('mysql.php');
	require_once('functions.php');
	
	// SETUP MYSQL HANDLERS
	$mysqldb1 = new mysql($host, $user, $pswd, $db); $mysqldb1->connect(); $mysqldb1->select();
	$mysqldb2 = new mysql($host, $user, $pswd, $db); $mysqldb2->connect(); $mysqldb2->select();
	
	$log = '';
	
	$response = 0;
	switch ($_GET['table']) {
	// DELETE FILE
	case 'file':
		$query = "SELECT * FROM file WHERE id=".$_GET['id'];
		$mysqldb1->query($query);
		$file = $mysqldb1->fetchArray();
		
		$query = "DELETE FROM file WHERE id=".$_GET['id'];
		$file_record_deleted = $mysqldb1->query($query);
		
		if ($file_record_deleted) {
		
			$file_deleted 			= deleteFile('../../'.$fileDIR.$file['name']);
			$large_image_deleted 	= deleteFile('../../'.$largeDIR.$file['name']);
			$medium_image_deleted 	= deleteFile('../../'.$mediumDIR.$file['name']);
			$small_image_deleted 	= deleteFile('../../'.$smallDIR.$file['name']);
			$tiny_image_deleted 	= deleteFile('../../'.$tinyDIR.$file['name']);
					
			if ($file_deleted and $medium_image_deleted and $small_image_deleted and $tiny_image_deleted and $large_image_deleted) {
				
				$log .= '<div class="success">[FILE: '.$file['name'].' DELETED]</div>';
			} else {
				$log .= '<div class="error">ERROR! [FILE: '.$file['name'].' FAILED] (file could not be deleted)</div>';
			}
			$response = 1;
		} else {
			$log .= '<div class="error">ERROR! [FILE: '.$file['name'].' FAILED] (record could not be deleted)</div>';
		}
		print $response;
		break;
	default:
		// DELETE TABLE RECORD	
		$delete_record = "DELETE FROM ".$_GET['table']." WHERE id='".$_GET['id']."'";
		$record_deleted = $mysqldb1->query($delete_record);
			
		if ($record_deleted) {
		
			$select_file_records = "SELECT * FROM file WHERE record_id=".$_GET['id']." AND source='".$_GET['table']."'";
			$file_records_exist = $mysqldb1->query($select_file_records);
			
			if ($file_records_exist) {
				while ($row1 = $mysqldb1->fetchObject()) {
					
					$delete_file_record = "DELETE FROM file WHERE name='".$row1->name."'";
					$file_record_deleted = $mysqldb2->query($delete_file_record);
					
					if ($file_record_deleted) {
					
						$file_deleted 			= deleteFile('../../'.$fileDIR.$row1->name);
						$large_image_deleted 	= deleteFile('../../'.$largeDIR.$row1->name);
						$medium_image_deleted 	= deleteFile('../../'.$mediumDIR.$row1->name);
						$small_image_deleted 	= deleteFile('../../'.$smallDIR.$row1->name);
						$tiny_image_deleted 	= deleteFile('../../'.$tinyDIR.$row1->name);
					
						if ($file_deleted and $medium_image_deleted and $small_image_deleted and $tiny_image_deleted and $large_image_deleted) {
							$log .= '<div class="success">[FILE: '.$row1->name.' DELETED]</div>';
						} else {
							$log .= '<div class="error">ERROR! [FILE: '.$row1->name.' FAILED] (file could not be deleted)</div>';
						}
					} else {
						$log .= '<div class="error">ERROR! [FILE: '.$row1->name.' FAILED] (file record could not be deleted)</div>';
					}
				}
			}	
			$response = 1;
			$log .= '<div class="success">[RECORD: ID-'.$_GET['id'].' DELETED]</div>';
		} else {
			$log .= '<div class="error">ERROR! [RECORD: ID-'.$_GET['id'].' FAILED] (record could not be deleted)</div>';
		}
		print $response;
	}
	
	// STUB: UPDATE EVENT LOG
?>
<?
	$mysqldb1->close();
	$mysqldb2->close();
?>