1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Extension "sf_event_mgt" for TYPO3 CMS. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the |
9
|
|
|
* LICENSE.txt file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace DERHANSEN\SfEventMgt\Service; |
13
|
|
|
|
14
|
|
|
use DERHANSEN\SfEventMgt\Domain\Model\Event; |
15
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
16
|
|
|
|
17
|
|
|
class ICalendarService |
18
|
|
|
{ |
19
|
|
|
protected FluidStandaloneService $fluidStandaloneService; |
20
|
|
|
|
21
|
|
|
public function injectFluidStandaloneService(FluidStandaloneService $fluidStandaloneService): void |
22
|
|
|
{ |
23
|
|
|
$this->fluidStandaloneService = $fluidStandaloneService; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Initiates the ICS download for the given event |
28
|
|
|
*/ |
29
|
|
|
public function downloadiCalendarFile(Event $event): void |
30
|
|
|
{ |
31
|
|
|
$content = $this->getICalendarContent($event); |
32
|
|
|
header('Content-Disposition: attachment; filename="event' . $event->getUid() . '.ics"'); |
33
|
|
|
header('Content-Type: text/calendar'); |
34
|
|
|
header('Content-Length: ' . strlen($content)); |
35
|
|
|
header('Expires: 0'); |
36
|
|
|
header('Cache-Control: must-revalidate'); |
37
|
|
|
header('Pragma: no-cache'); |
38
|
|
|
echo $content; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Returns the rendered iCalendar entry for the given event |
43
|
|
|
* according to RFC 2445 |
44
|
|
|
*/ |
45
|
|
|
public function getiCalendarContent(Event $event): string |
46
|
|
|
{ |
47
|
|
|
$variables = [ |
48
|
|
|
'event' => $event, |
49
|
|
|
'typo3Host' => GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY'), |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
$icalContent = $this->fluidStandaloneService->renderTemplate( |
53
|
|
|
'Event/ICalendar.txt', |
54
|
|
|
$variables, |
55
|
|
|
'SfEventMgt', |
56
|
|
|
'Pieventdetail', |
57
|
|
|
'txt' |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
// Remove empty lines |
61
|
|
|
$icalContent = preg_replace('/^\h*\v+/m', '', $icalContent); |
62
|
|
|
// Finally replace new lines with CRLF |
63
|
|
|
return str_replace(chr(10), chr(13) . chr(10), $icalContent); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|