-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIcsGenerator.module
94 lines (72 loc) · 2.29 KB
/
IcsGenerator.module
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
<?php
namespace ProcessWire;
class IcsGenerator extends WireData implements Module {
public static function getModuleInfo() {
return array(
'title' => 'ICS Generator',
'summary' => 'This module can generate ICS files.',
'author' => 'Timo Hausmann',
'version' => '200',
'autoload' => false,
'requires' => array(
'PHP>=5.0.0',
'ProcessWire>=3.0.0',
),
'icon' => 'calendar',
);
}
public function init() {
require_once(__DIR__ . '/ICS.php');
$this->set('events', new WireArray());
}
public function getDefaultEvent() {
return [
'timezone' => new \DateTimeZone(date_default_timezone_get()),
'dtstart' => 'now',
'dtend' => 'now + 60 minutes',
'summary' => 'ICS Calendar File',
'description' => '',
'location' => '',
'url' => '',
'rrule' => '',
];
}
public function getICS() {
return new \ICSGen\ICS($this->events);
}
public function getString() {
$ics = $this->getICS();
return $ics->to_string();
}
public function getFile() {
$str = $this->getString();
$filepath = $this->getTempFilepath();
file_put_contents($filepath, $str);
return $filepath;
}
public function getFileByID($id = '') {
if ($id == '') throw new WireException('No ID given');
$filepath = $this->getTempFilepath("ics-{$id}.ics");
// if the file has already been generated, return it
if (file_exists($filepath)) {
return $filepath;
}
$str = $this->getString();
file_put_contents($filepath, $str);
return $filepath;
}
private function getTempFilepath($filename = null) {
if ($filename === null) {
$filename = 'ics-' . time() . '-' . mt_rand() . '.ics';
}
$tempDir = wire()->files->tempDir('icsgenerator');
return $tempDir->get() . $filename;
}
/**
* @param DateTime|string $dt
* @param string|null $timezone
*/
public function format_timestamp($dt, $timezone = null) {
return \ICSGen\format_timestamp($dt, $timezone);
}
}