1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* External service. |
5
|
|
|
*/ |
6
|
|
|
declare(strict_types=1); |
7
|
|
|
|
8
|
|
|
namespace HDNET\Calendarize\Service\TimeTable; |
9
|
|
|
|
10
|
|
|
use HDNET\Calendarize\Domain\Model\Configuration; |
11
|
|
|
use HDNET\Calendarize\Service\Ical\ICalServiceInterface; |
12
|
|
|
use HDNET\Calendarize\Utility\DateTimeUtility; |
13
|
|
|
use HDNET\Calendarize\Utility\HelperUtility; |
14
|
|
|
use TYPO3\CMS\Core\Messaging\FlashMessage; |
15
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* External service. |
19
|
|
|
*/ |
20
|
|
|
class ExternalTimeTable extends AbstractTimeTable |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Ical service. |
25
|
|
|
* |
26
|
|
|
* @var ICalServiceInterface |
27
|
|
|
*/ |
28
|
|
|
protected $iCalService; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Inject ical service. |
32
|
|
|
* |
33
|
|
|
* @param ICalServiceInterface $iCalService |
34
|
|
|
*/ |
35
|
|
|
public function injectICalServiceInterface(ICalServiceInterface $iCalService) |
36
|
|
|
{ |
37
|
|
|
$this->iCalService = $iCalService; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Modify the given times via the configuration. |
42
|
|
|
* |
43
|
|
|
* @param array $times |
44
|
|
|
* @param Configuration $configuration |
45
|
|
|
* |
46
|
|
|
* @throws \TYPO3\CMS\Core\Exception |
47
|
|
|
*/ |
48
|
|
|
public function handleConfiguration(array &$times, Configuration $configuration) |
49
|
|
|
{ |
50
|
|
|
$url = $configuration->getExternalIcsUrl(); |
51
|
|
|
if (!GeneralUtility::isValidUrl($url)) { |
52
|
|
|
HelperUtility::createFlashMessage( |
53
|
|
|
'Configuration with invalid ICS URL: ' . $url, |
54
|
|
|
'Index ICS URL', |
55
|
|
|
FlashMessage::ERROR |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$fileName = $this->getCachedUrlFile($url); |
62
|
|
|
$externalTimes = $this->iCalService->getEvents($fileName); |
63
|
|
|
|
64
|
|
|
foreach ($externalTimes as $event) { |
65
|
|
|
$time = [ |
66
|
|
|
'start_date' => $event->getStartDate(), |
67
|
|
|
'end_date' => $event->getEndDate(), |
68
|
|
|
'start_time' => $event->getStartTime(), |
69
|
|
|
'end_time' => $event->getEndTime(), |
70
|
|
|
'all_day' => $event->isAllDay(), |
71
|
|
|
'open_end_time' => $event->isOpenEndTime(), |
72
|
|
|
'state' => $event->getState(), |
73
|
|
|
]; |
74
|
|
|
$time['pid'] = $configuration->getPid(); |
75
|
|
|
$time['state'] = $configuration->getState(); |
76
|
|
|
$times[$this->calculateEntryKey($time)] = $time; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get cached URL file. |
82
|
|
|
* |
83
|
|
|
* @param string $url |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
protected function getCachedUrlFile(string $url): string |
88
|
|
|
{ |
89
|
|
|
$tempFileName = $this->getCheckedCacheFolder() . \md5($url); |
90
|
|
|
if (!\is_file($tempFileName) || \filemtime($tempFileName) < (\time() - DateTimeUtility::SECONDS_HOUR)) { |
91
|
|
|
$icsFile = GeneralUtility::getUrl($url); |
92
|
|
|
GeneralUtility::writeFile($tempFileName, $icsFile); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $tempFileName; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Return the cache folder and check if the folder exists. |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
protected function getCheckedCacheFolder(): string |
104
|
|
|
{ |
105
|
|
|
$cacheFolder = GeneralUtility::getFileAbsFileName('typo3temp/var/transient/calendarize/'); |
106
|
|
|
if (!\is_dir($cacheFolder)) { |
107
|
|
|
GeneralUtility::mkdir_deep($cacheFolder); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $cacheFolder; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|