2023-02-14 19:56:51 +01:00
< ? php
2023-02-14 23:47:03 +01:00
require $_SERVER [ 'DOCUMENT_ROOT' ] . '/spreadSheetReader/read_write_xlsx.php' ;
2023-02-16 20:46:12 +01:00
require $_SERVER [ 'DOCUMENT_ROOT' ] . '/databaseConnection/databaseConnection.php' ;
2023-02-14 19:56:51 +01:00
$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 ];
}
2023-02-16 20:46:12 +01:00
$conn = getDatabaseConnection ();
2023-02-15 13:53:57 +01:00
// Check connection
if ( $conn -> connect_error ) {
die ( " Connection failed: " . $conn -> connect_error );
2023-02-15 13:55:01 +01:00
} else {
echo " connection sucessful<br> " ;
2023-02-15 13:46:05 +01:00
}
2023-02-15 13:31:48 +01:00
2023-02-15 13:53:57 +01:00
function saveToDatabase ( $contentArray , $conn ) {
2023-02-15 13:50:12 +01:00
echo " sending Query to Database<br> " ;
2023-02-15 14:40:55 +01:00
$sql1 = " SELECT * FROM `topics` WHERE id=' " . $contentArray [ 0 ] . " ' " ;
2023-02-15 14:35:51 +01:00
$result1 = $conn -> query ( $sql1 );
2023-02-15 14:37:50 +01:00
echo " Select Query sent<br> " ;
2023-02-15 14:44:27 +01:00
if ( $result1 -> num_rows === 0 ) {
2023-02-15 16:50:32 +01:00
$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 ] . " ' " ;
2023-02-15 14:27:19 +01:00
$result = $conn -> query ( $sql );
echo " Insert Query sent<br> Result: " . $result ;
} else {
2023-02-15 14:18:54 +01:00
$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 );
2023-02-15 14:27:19 +01:00
echo " Update Query sent<br> Result: " . $result ;
2023-02-15 14:18:54 +01:00
}
return ;
2023-02-15 13:31:48 +01:00
}
2023-02-15 17:26:23 +01:00
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 ;
2023-02-14 19:56:51 +01:00
}
2023-02-14 23:47:03 +01:00
$entityBody = '{"lastSave": 0, "timeWatchM": 0, "timeWatchW": 0, "timeWatchD": 0, "countWatchM": 0, "countWatchW": 0, "countWatchD": 0, "id":"test","watches":[{"actions":[]},{"actions":[]},{"actions":[]}]}' ;
2023-02-15 00:28:12 +01:00
if ( $_SERVER [ 'REQUEST_METHOD' ] === 'POST' ) {
2023-02-14 19:56:51 +01:00
$entityBody = file_get_contents ( 'php://input' );
2023-02-14 23:47:03 +01:00
$entityBodyObject = json_decode ( $entityBody );
2023-02-14 19:56:51 +01:00
2023-02-14 23:47:03 +01:00
echo " POST Request received.<br> " ;
2023-02-14 19:56:51 +01:00
2023-02-14 23:47:03 +01:00
if ( property_exists ( $entityBodyObject , " id " )) {
2023-02-15 17:00:11 +01:00
if ( ! ( $entityBodyObject -> countWatchM + $entityBodyObject -> countWatchW + $entityBodyObject -> countWatchD === 0 )){
saveToDatabase ( objectToArray ( $entityBodyObject , $entityBody ), $conn );
echo " <br>Data saved.<br> " ;
}
2023-02-14 23:47:03 +01:00
}
2023-02-15 13:31:48 +01:00
} elseif ( $_SERVER [ 'REQUEST_METHOD' ] === 'GET' ) {
2023-02-15 17:26:23 +01:00
download_xlsxFile ( getFromDatabase ( $conn ));
2023-02-15 00:28:12 +01:00
} else {
echo " invalid Request.<br> Type: " . $_SERVER [ 'REQUEST_METHOD' ];
2023-02-14 19:56:51 +01:00
}
2023-02-15 13:53:57 +01:00
$conn -> close ();
2023-02-14 23:47:03 +01:00