genderwatchprotocol/dataCollector/dataCollector.php

56 lines
1.5 KiB
PHP
Raw Normal View History

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-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];
}
function findRowIndex($id, $data) {
$rowCounter = -1;
foreach ($data as $row) {
$rowCounter ++;
if($row[0] == $id) {
return $rowCounter;
}
}
return $rowCounter + 1;
}
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":[]}]}';
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")) {
$data = read_xls_file($fileName);
2023-02-14 19:56:51 +01:00
2023-02-14 23:47:03 +01:00
foreach ($data as $row) {
echo "new Row: <br>";
foreach ($row as $cell) {
echo $cell." ";
}
}
$rowIndex = findRowIndex($entityBodyObject->id, $data);
$data[$rowIndex] = objectToArray($entityBodyObject, $entityBody);
write_xlsxFile($fileName, $data);
echo "<br>Data saved.<br>";
}
2023-02-14 19:56:51 +01:00
}
2023-02-14 23:47:03 +01:00