Skip to content

Instantly share code, notes, and snippets.

@agallou

agallou/calendar.php Secret

Created Apr 16, 2020
Embed
What would you like to do?
<?php
function get_next_day_of_Y_m_d($input) {
return \DateTime::createFromFormat('Y-m-d', $input)->add(new \DateInterval('P1D'))->format('Y-m-d');
}
$start = <<<EOF
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Decitre//decitre.Calendar.Export 1.0//EN
EOF;
$format = <<<EOF
BEGIN:VEVENT
DTSTART;VALUE=DATE:{date}
DTEND;VALUE=DATE:{date_end}
TRANSP:TRANSPARENT
SUMMARY: {label}
DESCRIPTION:
UID:astreinte-{date}
END:VEVENT
EOF;
$end = <<<EOF
END:VCALENDAR
EOF;
header('Content-Type: text/Calendar; charset=UTF-8');
header('Content-Disposition: inline; filename=iCal.vcs');
header('Cache-Control: no-cache');
header('Pragma: no-cache');
$gdocUrl = 'https://docs.google.com/spreadsheets/d/IDDOCUMENT/export?format=csv';
$events = [];
$fp = fopen($gdocUrl, 'rb');
if (!$fp) {
http_response_code(503);
die("Unable to load Google Spreadsheet document");
}
$cpt = 0;
$displayedItems = 60;
$currentDate = date('Y-m-d');
$events = [];
$filter = isset($_GET['filter']) ? $_GET['filter'] : null;
$oneEventPerDay = ($_GET['oneEventPerDay'] ?? 1) == 1;
$previousRow = null;
$currentEvent = [];
while (false !== ($row = fgetcsv($fp))) {
if (count($row) < 2 || !preg_match('/^\d{4}-\d{2}-\d{2}$/', $row[0])) {
continue; // invalid line
}
list ($date, $onDuty) = $row;
$eventDate = str_replace('-', '', $date);
$eventLabel = $onDuty;
if (0 === strlen(trim($eventLabel))) {
continue;
}
if ($date < $currentDate) {
continue;
}
if (null !== $filter && $eventLabel != $filter) {
continue;
}
if (null !== $filter) {
$eventLabel = 'Astreinte';
}
if ($oneEventPerDay) {
$events[] = [
'start' => $eventDate,
'label' => $eventLabel,
'end' => str_replace('-', '', get_next_day_of_Y_m_d($date)) // +1 jour à la date de fin pour que ca apparaisse correctement
];
} else {
if ($previousRow === null) {
$currentEvent['start'] = $eventDate;
$currentEvent['label'] = $eventLabel;
} elseif ($onDuty !== $previousRow[1] || get_next_day_of_Y_m_d($previousRow[0]) !== $date) {
// Si le nom de la personne est différent de la ligne précédente
// OU que le lendemain de la date précédente n'est pas ce jour ci
// => on finalise l'event qu'on construisait
$currentEvent['end'] = str_replace('-', '', get_next_day_of_Y_m_d($previousRow[0])); // +1 jour à la date de fin pour que ca apparaisse correctement
$events[] = $currentEvent;
$currentEvent['start'] = $eventDate;
$currentEvent['label'] = $eventLabel;
}
$previousRow = $row;
}
$cpt++;
if ($cpt === $displayedItems) {
break;
}
}
// On fini de construire le dernier event
if (isset($currentEvent['start']) && $previousRow !== null) {
$currentEvent['end'] = str_replace('-', '', get_next_day_of_Y_m_d($previousRow[0])); // +1 jour à la date de fin pour que ca apparaisse correctement
$events[] = $currentEvent;
}
$content = $start;
foreach ($events as $event) {
$content .= strtr($format, ['{date}' => $event['start'], '{date_end}' => $event['end'], '{label}' => $event['label']]);
}
$content .= $end;
echo str_replace("\n", "\r\n", $content);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment