Passed
Push — master ( 138f6a...e189b2 )
by Torben
66:36 queued 63:19
created

OnlineCalendarViewHelper::getYahooCalendarLink()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 18
rs 9.9
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
        switch ($type) {
62
            case 'google':
63
                $link = self::getGoogleCalendarLink($event);
64
                break;
65
            case 'outlook':
66
                $link = self::getMicrosoftCalendarLink($event, 'live');
67
                break;
68
            case 'office365':
69
                $link = self::getMicrosoftCalendarLink($event, 'office');
70
                break;
71
            case 'yahoo':
72
                $link = self::getYahooCalendarLink($event);
73
                break;
74
            default:
75
                $link = '';
76
        }
77
78
        return $link;
79
    }
80
81
    private static function getGoogleCalendarLink(Event $event): string
82
    {
83
        $baseLink = 'https://www.google.com/calendar/render?';
84
85
        $dateFormat = 'Ymd\\THi00\\ZO';
86
        $arguments = [
87
            'action' => 'TEMPLATE',
88
            'text' => $event->getTitle(),
89
            'dates' => $event->getStartdate()->format($dateFormat) . '/' . $event->getEnddate()->format($dateFormat),
90
            'details' => strip_tags($event->getDescription()),
91
        ];
92
93
        if ($event->getLocation()) {
94
            $arguments['location'] = $event->getLocation()->getFullAddress(', ');
95
        }
96
97
        return $baseLink . http_build_query($arguments, '', '&', PHP_QUERY_RFC3986);
98
    }
99
100
    private static function getMicrosoftCalendarLink(Event $event, string $product): string
101
    {
102
        $baseLink = 'https://outlook.' . $product . '.com/calendar/0/deeplink/compose?';
103
104
        $dateFormat = 'Y-m-d\\TH:i:00O';
105
        $arguments = [
106
            'subject' => $event->getTitle(),
107
            'startdt' => $event->getStartdate()->format($dateFormat),
108
            'enddt' => $event->getEnddate()->format($dateFormat),
109
            'body' => strip_tags($event->getDescription()),
110
            'path' => '/calendar/action/compose&rru=addevent',
111
        ];
112
113
        if ($event->getLocation()) {
114
            $arguments['location'] = $event->getLocation()->getFullAddress(', ');
115
        }
116
117
        return $baseLink . http_build_query($arguments, '', '&', PHP_QUERY_RFC3986);
118
    }
119
120
    private static function getYahooCalendarLink(Event $event): string
121
    {
122
        $baseLink = 'https://calendar.yahoo.com/?';
123
124
        $dateFormat = 'Ymd\\THi00\\ZO';
125
        $arguments = [
126
            'title' => $event->getTitle(),
127
            'st' => $event->getStartdate()->format($dateFormat),
128
            'et' => $event->getEnddate()->format($dateFormat),
129
            'desc' => strip_tags($event->getDescription()),
130
            'v' => 60,
131
        ];
132
133
        if ($event->getLocation()) {
134
            $arguments['in_loc'] = $event->getLocation()->getFullAddress(', ');
135
        }
136
137
        return $baseLink . http_build_query($arguments, '', '&', PHP_QUERY_RFC3986);
138
    }
139
}
140