This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCal.php
128 lines (105 loc) · 3.58 KB
/
Cal.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
class Cal
{
const CAL_URL = "https://data.bordeaux-metropole.fr/files.php?gid=489&format=6";
const FILE_URL = "";
const FINAL_URL = "";
/**
* @var json
*/
private $file;
/**
* Cal constructor.
*/
public function __construct()
{
$this->context = stream_context_create([
"http" => [
"method" => "GET",
"header" => "Accept-language: en\r\n" . "Cookie: foo=bar\r\n"
]
]);
}
/**
* Get the file
*/
public function getFile()
{
if (is_null($this->file)) {
$handle = fopen(Cal::CAL_URL, 'r');
$content = [];
$i = 0;
while (($data = fgetcsv($handle) ) !== false) {
$i++;
if ($i == 1) {
continue;
}
$content[] = $data[0];
}
$this->file = $content;
}
return $this->file;
}
/**
* Build ics
*/
public function buildCalendar()
{
$data = $this->getFile();
if (count($data) > 0) {
unset($data[0]);
$ics = "BEGIN:VCALENDAR".chr(10);
$ics = $ics."VERSION:2.0".chr(10);
$ics = $ics."PRODID:-//LFP/iCal 3.0//FR".chr(10);
foreach ($data as $value) {
$value = explode(';', $value);
$ics = $ics."BEGIN:VEVENT".chr(10);
$monthAndDay = substr($value[1], 3, 2).substr($value[1], 0, 2);
$currentYear = substr($value[1], 6, 4);
$closedHour = str_replace(':', 'h', $value[2]);
$openedHour = str_replace(':', 'h', $value[3]);
$openedDate = new DateTime($currentYear.$monthAndDay.$value[2], new DateTimeZone('Europe/Paris'));
$openedDate->sub(new DateInterval('PT2H'));
$openedDate = $openedDate->format('Ymd\THis\Z');
$closedDate = new DateTime($currentYear.$monthAndDay.$value[3], new DateTimeZone('Europe/Paris'));
$closedDate->sub(new DateInterval('PT2H'));
$closedDate = $closedDate->format('Ymd\THis\Z');
$ics = $ics."DTSTART:".$openedDate.chr(10);
$ics = $ics."DTEND:".$closedDate.chr(10);
$ics = $ics."SUMMARY:Pont Chaban Delmas fermé - ".$value[0]." à ".$closedHour.".".chr(10);
$ics = $ics."LOCATION:Pont Chaban Delmas".chr(10);
$ics = $ics."DESCRIPTION:Pont Chaban Delmas fermé - ".$value[0]." à ".$closedHour.". Ré-ouverture à ".$openedHour.".".chr(10);
$ics = $ics."END:VEVENT".chr(10);
}
$ics = $ics."END:VCALENDAR".chr(10);
return $this->writeFile($ics);
}
}
/**
* Write file
*
* @param string $content
* @param bool $download
*/
public function writeFile($content, $download = true)
{
$fileName = "pont.ics";
$fileUrl = sprintf('%s%s', Cal::FINAL_URL, $fileName);
$myFile = fopen(sprintf("%s%s", Cal::FILE_URL, $fileName), "w");
fwrite($myFile, $content);
fclose($myFile);
if ($download) {
return $this->download($fileName, $fileUrl);
}
}
/**
* Download action
*/
public function download($fileName, $fileUrl)
{
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header(sprintf("Content-disposition: attachment; filename=%s", $fileName));
return readfile($fileUrl);
}
}