1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Import. |
5
|
|
|
*/ |
6
|
|
|
declare(strict_types=1); |
7
|
|
|
|
8
|
|
|
namespace HDNET\Calendarize\Command; |
9
|
|
|
|
10
|
|
|
use HDNET\Calendarize\Service\IndexerService; |
11
|
|
|
use TYPO3\CMS\Core\Messaging\FlashMessage; |
12
|
|
|
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility; |
13
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
14
|
|
|
use TYPO3\CMS\Core\Utility\MathUtility; |
15
|
|
|
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Import. |
19
|
|
|
*/ |
20
|
|
|
class ImportCommandController extends AbstractCommandController |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Import command. |
24
|
|
|
* |
25
|
|
|
* @param string $icsCalendarUri |
26
|
|
|
* @param int $pid |
27
|
|
|
*/ |
28
|
|
|
public function importCommand($icsCalendarUri = null, $pid = null) |
29
|
|
|
{ |
30
|
|
|
if (null === $icsCalendarUri || !\filter_var($icsCalendarUri, FILTER_VALIDATE_URL)) { |
31
|
|
|
$this->enqueueMessage('You have to enter a valid URL to the iCalendar ICS', 'Error', FlashMessage::ERROR); |
32
|
|
|
|
33
|
|
|
return; |
34
|
|
|
} |
35
|
|
|
if (!MathUtility::canBeInterpretedAsInteger($pid)) { |
36
|
|
|
$this->enqueueMessage('You have to enter a valid PID for the new created elements', 'Error', FlashMessage::ERROR); |
37
|
|
|
|
38
|
|
|
return; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// fetch external URI and write to file |
42
|
|
|
$this->enqueueMessage('Start to checkout the calendar: ' . $icsCalendarUri, 'Calendar', FlashMessage::INFO); |
43
|
|
|
$relativeIcalFile = 'typo3temp/ical.' . GeneralUtility::shortMD5($icsCalendarUri) . '.ical'; |
44
|
|
|
$absoluteIcalFile = GeneralUtility::getFileAbsFileName($relativeIcalFile); |
45
|
|
|
$content = GeneralUtility::getUrl($icsCalendarUri); |
46
|
|
|
GeneralUtility::writeFile($absoluteIcalFile, $content); |
47
|
|
|
|
48
|
|
|
// get Events from file |
49
|
|
|
$icalEvents = $this->getIcalEvents($absoluteIcalFile); |
50
|
|
|
$this->enqueueMessage('Found ' . \count($icalEvents) . ' events in the given calendar', 'Items', FlashMessage::INFO); |
51
|
|
|
$events = $this->prepareEvents($icalEvents); |
52
|
|
|
|
53
|
|
|
$this->enqueueMessage('Found ' . \count($events) . ' events in ' . $icsCalendarUri, 'Items', FlashMessage::INFO); |
54
|
|
|
|
55
|
|
|
$signalSlotDispatcher = GeneralUtility::makeInstance(Dispatcher::class); |
56
|
|
|
|
57
|
|
|
$this->enqueueMessage('Send the ' . __CLASS__ . '::importCommand signal for each event.', 'Signal', FlashMessage::INFO); |
58
|
|
|
foreach ($events as $event) { |
59
|
|
|
$arguments = [ |
60
|
|
|
'event' => $event, |
61
|
|
|
'commandController' => $this, |
62
|
|
|
'pid' => $pid, |
63
|
|
|
'handled' => false, |
64
|
|
|
]; |
65
|
|
|
$signalSlotDispatcher->dispatch(__CLASS__, 'importCommand', $arguments); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->enqueueMessage('Run Reindex proces after import', 'Reindex', FlashMessage::INFO); |
69
|
|
|
$indexer = $this->objectManager->get(IndexerService::class); |
70
|
|
|
$indexer->reindexAll(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Prepare the events. |
75
|
|
|
* |
76
|
|
|
* @param array $icalEvents |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
protected function prepareEvents(array $icalEvents) |
81
|
|
|
{ |
82
|
|
|
$events = []; |
83
|
|
|
foreach ($icalEvents as $icalEvent) { |
84
|
|
|
$startDateTime = null; |
85
|
|
|
$endDateTime = null; |
86
|
|
|
try { |
87
|
|
|
$startDateTime = new \DateTime($icalEvent['DTSTART']); |
88
|
|
|
if ($icalEvent['DTEND']) { |
89
|
|
|
$endDateTime = new \DateTime($icalEvent['DTEND']); |
90
|
|
|
} else { |
91
|
|
|
$endDateTime = clone $startDateTime; |
92
|
|
|
$endDateTime->add(new \DateInterval($icalEvent['DURATION'])); |
93
|
|
|
} |
94
|
|
|
} catch (\Exception $ex) { |
95
|
|
|
$this->enqueueMessage( |
96
|
|
|
'Could not convert the date in the right format of "' . $icalEvent['SUMMARY'] . '"', |
97
|
|
|
'Warning', |
98
|
|
|
FlashMessage::WARNING |
99
|
|
|
); |
100
|
|
|
continue; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$events[] = [ |
104
|
|
|
'uid' => $icalEvent['UID'], |
105
|
|
|
'start' => $startDateTime, |
106
|
|
|
'end' => $endDateTime, |
107
|
|
|
'title' => $icalEvent['SUMMARY'] ? $icalEvent['SUMMARY'] : '', |
108
|
|
|
'description' => $icalEvent['DESCRIPTION'] ? $icalEvent['DESCRIPTION'] : '', |
109
|
|
|
'location' => $icalEvent['LOCATION'] ? $icalEvent['LOCATION'] : '', |
110
|
|
|
]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $events; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get the events from the given ical file. |
118
|
|
|
* |
119
|
|
|
* @param string $absoluteIcalFile |
120
|
|
|
* |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
protected function getIcalEvents($absoluteIcalFile) |
124
|
|
|
{ |
125
|
|
|
if (!\class_exists('ICal')) { |
126
|
|
|
require_once ExtensionManagementUtility::extPath( |
127
|
|
|
'calendarize', |
128
|
|
|
'Resources/Private/Php/ics-parser/class.iCalReader.php' |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return (array)(new \ICal($absoluteIcalFile))->events(); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|