This repository has been archived by the owner on Jul 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
103 lines (82 loc) · 3.28 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
/*
* Hanze Tentamenrooster
* This application loads an excel sheet from the website of the Hanze and loads each relevant event into Google Calender
* Written by Sander van Kasteel <info -at- sandervankasteel.nl>
* License: MIT
*/
date_default_timezone_set('Europe/Amsterdam');
error_reporting(null);
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/hanze.php';
require __DIR__ . '/utils.php';
$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();
$hanze = new Hanze($_ENV['HANZE_USERNAME'], $_ENV['PASSWORD']);
$client = $hanze->getToken();
// Get the Excel file
$response = $client->request('GET', $_ENV['URL_EXCEL_FILE']);
// Create temp file and write to that file
$tmpFile = tmpfile();
fwrite($tmpFile, $response->getBody()->getContents());
// Create an Excel Reader object
$objReader = new PHPExcel_Reader_Excel2007();
$objReader->setReadDataOnly(true); //optional
$objPHPExcel = $objReader->load(stream_get_meta_data($tmpFile)['uri']);
try {
// All the information is based in the first sheet
$sheet = $objPHPExcel->getSheet(0);
} catch(PHPExcel_Exception $e) {
// If loading of the excel sheet doesn't work, then die
die("Died because Excel sheet is corrupt \n");
}
// First let's clear all the old events
$googleClient = getGoogleClient();
clearGoogleCalender($_ENV['GOOGLE_CALENDER_ID'], $googleClient);
// Iterate over all the rows *\o/*
foreach($sheet->getRowIterator() as $row) {
// Skip over the first row because we don't need it!
if($row->getRowIndex() == 1) {
continue;
}
$tmpEvent = [];
foreach($row->getCellIterator() as $cell) {
switch ($cell->getColumn()) {
case "G":
$cell->getStyle()->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_DMYSLASH);
break;
case "H":
case "I":
//h:mm
$cell->getStyle()->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME3);
break;
default:
break;
};
array_push($tmpEvent, $cell->getFormattedValue());
}
// If we match a certain code, then we need to add it.
if(strpos($tmpEvent[9], $_ENV['EXAM_CODE_PATTERN']) !== false) {
$beginDateRaw = explode('/', $tmpEvent[6]);
$beginTimeRaw = explode(':', $tmpEvent[7]);
$endTimeRaw = explode(':', $tmpEvent[8]);
$beginTime = \Carbon\Carbon::create("20" .$beginDateRaw[2], $beginDateRaw[1], $beginDateRaw[0], $beginTimeRaw[0], $beginTimeRaw[1]);
$endTime = \Carbon\Carbon::create("20" .$beginDateRaw[2], $beginDateRaw[1], $beginDateRaw[0], $endTimeRaw[0], $endTimeRaw[1]);
$event = new Google_Service_Calendar_Event([
'summary' => $tmpEvent[10],
'location' => $tmpEvent[15],
'start' => [
'dateTime' => $beginTime->toIso8601String(),
'timeZone' => 'Europe/Amsterdam'
],
'end' => [
'dateTime' => $endTime->toIso8601String(),
'timeZone' => 'Europe/Amsterdam'
],
]);
$calender = new Google_Service_Calendar($googleClient);
$calender->events->insert($_ENV['GOOGLE_CALENDER_ID'], $event);
}
}
// Clean up as a good boy I am
fclose($tmpFile);