Files
homepage/lib/main.php

65 lines
2.0 KiB
PHP
Executable File

<?php
ini_set('memory_limit', '512M');
function getEvents() {
$content = file_get_contents("dat/events.json");
$entries = json_decode($content, $associative = true);
for ( $i = 0; $i < count($entries); $i++ ) {
$entry = $entries[$i];
$dt = new DateTime( $entry["start"] );
$ctl = $dt -> format("YmdHi");
$entries[$i]["ctl"] = $ctl;
$start = new DateTime($entries[$i]["start"]);
$end = new DateTime($entries[$i]["end"]);
$delta = $start->diff($end);
$hours = $delta -> days * 24 + $delta -> h + $delta -> i / 60 + $delta -> s / 3600;
$entries[$i]["hours"] = $hours;
$datestamp = $dt -> format("Ymd");
$files = array( "agenda" => sprintf("dat/agenda/%d.md", $datestamp)
, "minutes" => sprintf("dat/minutes/%d.md", $datestamp)
);
foreach ( $files as $key => $file ) {
if ( file_exists( $file ) ) {
$entries[$i][$key] = file_get_contents($file);
}
}
}
usort($entries, function ($a, $b) {
$dateA = new DateTime($a['start']);
$dateB = new DateTime($b['start']);
return $dateA < $dateB ? 1 : -1;
});
return($entries);
}
function putEvents($events) {
for ( $i = 0; $i < count($events); $i++ ) {
$entry = $events[$i];
$dt = new DateTime( $entry["start"] );
$start = new DateTime($entry["start"]);
$datestamp = $dt -> format("Ymd");
$files = array( "agenda" => sprintf("dat/agenda/%d.md", $datestamp)
, "minutes" => sprintf("dat/minutes/%d.md", $datestamp)
);
foreach ( $files as $key => $path ) {
if ( in_array($key, array_keys($entry)) ) {
$fout = fopen($path, "wt");
fwrite($fout, $entry[$key]);
fclose($fout);
} else {
if ( file_exists($path) ) {
unlink($path);
}
}
}
foreach ( array( "ctl", "hours", "agenda", "minutes" ) as $field ) {
unset($events[$i][$field]);
}
}
$json = json_encode($events, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT);
file_put_contents("dat/events.json", $json);
}
?>