Passed
Push — master ( c597bf...58c4da )
by Torben
08:11 queued 05:32
created

OnlineCalendarViewHelper::renderStatic()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 21
c 1
b 0
f 0
nc 10
nop 3
dl 0
loc 33
rs 8.9617
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace DERHANSEN\SfEventMgt\ViewHelpers\Uri;
17
18
use DERHANSEN\SfEventMgt\Domain\Model\Event;
19
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
20
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
21
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
22
23
/**
24
 * ViewHelper to render a link to add an event to a given online calender
25
 * Currently supports the following online calendar types:
26
 *
27
 * - Google Calendar
28
 * - Outlook Calendar
29
 * - Office 365 Calendar
30
 * - Yahoo Calendar
31
 */
32
class OnlineCalendarViewHelper extends AbstractViewHelper
33
{
34
    use CompileWithRenderStatic;
35
36
    /**
37
     * Initialize arguments
38
     */
39
    public function initializeArguments()
40
    {
41
        parent::initializeArguments();
42
        $this->registerArgument('type', 'string', 'The type of online calender', true, 'google');
43
        $this->registerArgument('event', 'DERHANSEN\SfEventMgt\Domain\Model\Event', 'The event');
44
    }
45
46
    /**
47
     * @param array $arguments
48
     * @param \Closure $renderChildrenClosure
49
     * @param RenderingContextInterface $renderingContext
50
     * @return string
51
     */
52
    public static function renderStatic(
53
        array $arguments,
54
        \Closure $renderChildrenClosure,
55
        RenderingContextInterface $renderingContext
56
    ) {
57
        /** @var Event $event */
58
        $event = $arguments['event'];
59
        $type = strtolower($arguments['type']);
60
61
        // If event has no enddate, set a default enddate (startdate + 1 hour)
62
        if (!$event->getEnddate()) {
63
            $enddate = (clone $event->getStartdate())->modify('+1 hour');
64
            $event->setEnddate($enddate);
65
        }
66
67
        switch ($type) {
68
            case 'google':
69
                $link = self::getGoogleCalendarLink($event);
70
                break;
71
            case 'outlook':
72
                $link = self::getMicrosoftCalendarLink($event, 'live');
73
                break;
74
            case 'office365':
75
                $link = self::getMicrosoftCalendarLink($event, 'office');
76
                break;
77
            case 'yahoo':
78
                $link = self::getYahooCalendarLink($event);
79
                break;
80
            default:
81
                $link = '';
82
        }
83
84
        return $link;
85
    }
86
87
    private static function getGoogleCalendarLink(Event $event): string
88
    {
89
        $baseLink = 'https://www.google.com/calendar/render?';
90
91
        $dateFormat = 'Ymd\\THi00\\ZO';
92
        $arguments = [
93
            'action' => 'TEMPLATE',
94
            'text' => $event->getTitle(),
95
            'dates' => $event->getStartdate()->format($dateFormat) . '/' . $event->getEnddate()->format($dateFormat),
96
            'details' => strip_tags($event->getDescription()),
97
        ];
98
99
        if ($event->getLocation()) {
100
            $arguments['location'] = $event->getLocation()->getFullAddress(', ');
101
        }
102
103
        return $baseLink . http_build_query($arguments, '', '&', PHP_QUERY_RFC3986);
104
    }
105
106
    private static function getMicrosoftCalendarLink(Event $event, string $product): string
107
    {
108
        $baseLink = 'https://outlook.' . $product . '.com/calendar/0/deeplink/compose?';
109
110
        $dateFormat = 'Y-m-d\\TH:i:00O';
111
        $arguments = [
112
            'subject' => $event->getTitle(),
113
            'startdt' => $event->getStartdate()->format($dateFormat),
114
            'enddt' => $event->getEnddate()->format($dateFormat),
115
            'body' => strip_tags($event->getDescription()),
116
            'path' => '/calendar/action/compose&rru=addevent',
117
        ];
118
119
        if ($event->getLocation()) {
120
            $arguments['location'] = $event->getLocation()->getFullAddress(', ');
121
        }
122
123
        return $baseLink . http_build_query($arguments, '', '&', PHP_QUERY_RFC3986);
124
    }
125
126
    private static function getYahooCalendarLink(Event $event): string
127
    {
128
        $baseLink = 'https://calendar.yahoo.com/?';
129
130
        $dateFormat = 'Ymd\\THi00\\ZO';
131
        $arguments = [
132
            'title' => $event->getTitle(),
133
            'st' => $event->getStartdate()->format($dateFormat),
134
            'et' => $event->getEnddate()->format($dateFormat),
135
            'desc' => strip_tags($event->getDescription()),
136
            'v' => 60,
137
        ];
138
139
        if ($event->getLocation()) {
140
            $arguments['in_loc'] = $event->getLocation()->getFullAddress(', ');
141
        }
142
143
        return $baseLink . http_build_query($arguments, '', '&', PHP_QUERY_RFC3986);
144
    }
145
}
146