79 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
			
		
		
	
	
			79 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
<?php
 | 
						|
require $_SERVER['DOCUMENT_ROOT'] . '/spreadSheetReader/read_write_xlsx.php';
 | 
						|
$fileName = $_SERVER['DOCUMENT_ROOT'] . '/data/data.xlsx';
 | 
						|
function objectToArray($object, $fullJSON) {
 | 
						|
    return [$object -> id,
 | 
						|
        $object -> lastSave,
 | 
						|
        $fullJSON,
 | 
						|
        $object -> timeWatchM,
 | 
						|
        $object -> timeWatchW,
 | 
						|
        $object -> timeWatchD,
 | 
						|
        $object -> countWatchM,
 | 
						|
        $object -> countWatchW,
 | 
						|
        $object -> countWatchD];
 | 
						|
}
 | 
						|
 | 
						|
$databaseAddress = "127.0.0.1";
 | 
						|
$databasePort = "3306";
 | 
						|
$databaseName = "u517357132_genderwatchpro";
 | 
						|
$databaseUser = "u517357132_genderwatchpro";
 | 
						|
$databasePassword = "zU!7gRHA6x";
 | 
						|
$conn = new mysqli($databaseAddress, $databaseUser, $databasePassword, $databaseName);
 | 
						|
// Check connection
 | 
						|
if ($conn->connect_error) {
 | 
						|
    die("Connection failed: " . $conn->connect_error);
 | 
						|
} else {
 | 
						|
    echo "connection sucessful<br>";
 | 
						|
}
 | 
						|
 | 
						|
function saveToDatabase($contentArray, $conn) {
 | 
						|
    echo "sending Query to Database<br>";
 | 
						|
    $sql1 = "SELECT * FROM `topics` WHERE id='".$contentArray[0]."'";
 | 
						|
    $result1 = $conn->query($sql1);
 | 
						|
    echo "Select Query sent<br>";
 | 
						|
    if($result1->num_rows === 0) {
 | 
						|
        $sql = "INSERT INTO `topics` (`id`, `lastSave`, `fullJSON`, `timeWatchM`,`timeWatchW`, `timeWatchD`, `countWatchM`, `countWatchW`, `countWatchD`) values ('".$contentArray[0]."', ".$contentArray[1]." , '".$contentArray[2]."', ".$contentArray[3].", ".$contentArray[4].", ".$contentArray[5].", ".$contentArray[6].", ".$contentArray[7].", ".$contentArray[8].") ON DUPLICATE KEY UPDATE `id` = '".$contentArray[0]."'";
 | 
						|
        $result = $conn->query($sql);
 | 
						|
        echo "Insert Query sent<br> Result: " . $result;
 | 
						|
    } else {
 | 
						|
        $sql = "UPDATE `topics` SET `id`='".$contentArray[0]."',`lastSave`='".$contentArray[1]."',`fullJSON`='".$contentArray[2]."',`timeWatchM`='".$contentArray[3]."',`timeWatchW`='".$contentArray[4]."',`timeWatchD`='".$contentArray[5]."',`countWatchM`='".$contentArray[6]."',`countWatchW`='".$contentArray[7]."',`countWatchD`='".$contentArray[8]."' WHERE `id` = '".$contentArray[0]."'";
 | 
						|
        $result = $conn->query($sql);
 | 
						|
        echo "Update Query sent<br> Result: " . $result;
 | 
						|
    }
 | 
						|
    return;
 | 
						|
}
 | 
						|
 | 
						|
function getFromDatabase($conn) {
 | 
						|
    $sql = "SELECT `id`, `firstSave`, `lastSave`, `fullJSON`, `timeWatchM`,`timeWatchW`, `timeWatchD`, `countWatchM`, `countWatchW`, `countWatchD` FROM `topics`";
 | 
						|
    $result = $conn->query($sql);
 | 
						|
    $dataArray = Array();
 | 
						|
    while($row = $result->fetch_assoc()) {
 | 
						|
        array_push($dataArray, [$row["id"],$row["firstSave"],$row["lastSave"],$row["fullJSON"],$row["timeWatchM"],$row["timeWatchW"],$row["timeWatchD"],$row["countWatchM"],$row["countWatchW"],$row["countWatchD"]]);
 | 
						|
    }
 | 
						|
    return $dataArray;
 | 
						|
}
 | 
						|
 | 
						|
$entityBody = '{"lastSave": 0, "timeWatchM": 0, "timeWatchW": 0, "timeWatchD": 0, "countWatchM": 0, "countWatchW": 0, "countWatchD": 0, "id":"test","watches":[{"actions":[]},{"actions":[]},{"actions":[]}]}';
 | 
						|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
 | 
						|
    $entityBody = file_get_contents('php://input');
 | 
						|
    $entityBodyObject = json_decode($entityBody);
 | 
						|
 | 
						|
    echo "POST Request received.<br>";
 | 
						|
 | 
						|
    if(property_exists($entityBodyObject, "id")) {
 | 
						|
        if(! ($entityBodyObject->countWatchM + $entityBodyObject->countWatchW + $entityBodyObject->countWatchD === 0)){
 | 
						|
            saveToDatabase(objectToArray($entityBodyObject, $entityBody), $conn);
 | 
						|
            echo "<br>Data saved.<br>";
 | 
						|
        }
 | 
						|
    }
 | 
						|
} elseif($_SERVER['REQUEST_METHOD'] === 'GET') {
 | 
						|
    download_xlsxFile(getFromDatabase($conn));
 | 
						|
} else {
 | 
						|
    echo "invalid Request.<br> Type: ".$_SERVER['REQUEST_METHOD'];
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
 | 
						|
$conn->close();
 | 
						|
 |