Completed
Pull Request — master (#331)
by John
04:37
created

ViewController::publicIndex()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 34
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 27
nc 2
nop 0
dl 0
loc 34
ccs 25
cts 25
cp 1
crap 3
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Calendar App
4
 *
5
 * @author Georg Ehrke
6
 * @copyright 2016 Georg Ehrke <[email protected]>
7
 * @author Raghu Nayyar
8
 * @copyright 2016 Raghu Nayyar <[email protected]>
9
 *
10
 * This library is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
12
 * License as published by the Free Software Foundation; either
13
 * version 3 of the License, or any later version.
14
 *
15
 * This library is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public
21
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
namespace OCA\Calendar\Controller;
25
26
use OCP\AppFramework\Controller;
27
use OCP\AppFramework\Http\ContentSecurityPolicy;
28
use OCP\AppFramework\Http\TemplateResponse;
29
use OCP\IConfig;
30
use OCP\IRequest;
31
use OCP\IUserSession;
32
use OCP\IURLGenerator;
33
34
class ViewController extends Controller {
35
36
	/**
37
	 * @var IConfig
38
	 */
39
	private $config;
40
41
	/**
42
	 * @var IURLGenerator
43
	 */
44
	private $urlGenerator;
45
46
	/**
47
	 * @var IUserSession
48
	 */
49
	private $userSession;
50
51
	/**
52
	 * @param string $appName
53
	 * @param IRequest $request an instance of the request
54
	 * @param IUserSession $userSession
55
	 * @param IConfig $config
56
	 * @param IURLGenerator $urlGenerator
57
	 */
58 13
	public function __construct($appName, IRequest $request, IUserSession $userSession,
59
								IConfig $config, IURLGenerator $urlGenerator) {
60 13
		parent::__construct($appName, $request);
61 13
		$this->config = $config;
62 13
		$this->userSession = $userSession;
63 13
		$this->urlGenerator = $urlGenerator;
64 13
	}
65
66
	/**
67
	 * @NoAdminRequired
68
	 * @NoCSRFRequired
69
	 *
70
	 * @return TemplateResponse
71
	 */
72 9
	public function index() {
73 9
		$runningOn = $this->config->getSystemValue('version');
74 9
		$runningOnNextcloud10OrLater = version_compare($runningOn, '9.1', '>=');
75 9
		$runningOnNextcloud11OrLater = version_compare($runningOn, '11', '>=');
76
77 9
		$supportsClass = $runningOnNextcloud10OrLater;
78 9
		$assetPipelineBroken = !$runningOnNextcloud10OrLater;
79 9
		$needsAutosize = !$runningOnNextcloud11OrLater;
80
81 9
		$isAssetPipelineEnabled = $this->config->getSystemValue('asset-pipeline.enabled', false);
82 9
		if ($isAssetPipelineEnabled && $assetPipelineBroken) {
83 2
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
84
		}
85
86 7
		$user = $this->userSession->getUser();
87 7
		$userId = $user->getUID();
88 7
		$emailAddress = $user->getEMailAddress();
89
90 7
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
91 7
		$initialView = $this->config->getUserValue($userId, $this->appName, 'currentView', null);
92 7
		$skipPopover = $this->config->getUserValue($userId, $this->appName, 'skipPopover', 'no');
93 7
		$weekNumbers = $this->config->getUserValue($userId, $this->appName, 'showWeekNr', 'no');
94 7
		$firstRun = $this->config->getUserValue($userId, $this->appName, 'firstRun', null);
95 7
		$defaultColor = $this->config->getAppValue('theming', 'color', '#0082C9');
96
97
		// the default view will be saved as soon as a user
98
		// opens the calendar app, therefore this is a good
99
		// indication if the calendar was used before
100 7
		if ($firstRun === null) {
101 2
			if ($initialView === null) {
102 1
				$firstRun = 'yes';
103
			} else {
104 1
				$this->config->setUserValue($userId, $this->appName, 'firstRun', 'no');
105 1
				$firstRun = 'no';
106
			}
107
		}
108
109 7
		if ($initialView === null) {
110 2
			$initialView = 'month';
111
		}
112
		
113 7
		$webCalWorkaround = $runningOnNextcloud10OrLater ? 'no' : 'yes';
114
115 7
		return new TemplateResponse('calendar', 'main', [
116 7
			'appVersion' => $appVersion,
117 7
			'initialView' => $initialView,
118 7
			'emailAddress' => $emailAddress,
119 7
			'skipPopover' => $skipPopover,
120 7
			'weekNumbers' => $weekNumbers,
121 7
			'firstRun' => $firstRun,
122 7
			'supportsClass' => $supportsClass,
123 7
			'defaultColor' => $defaultColor,
124 7
			'webCalWorkaround' => $webCalWorkaround,
125
			'isPublic' => false,
126 7
			'needsAutosize' => $needsAutosize,
127
		]);
128
	}
129
130
	/**
131
	 * @PublicPage
132
	 * @NoCSRFRequired
133
	 *
134
	 * @return TemplateResponse
135
	 */
136 4
	public function publicIndex() {
137 4
		$runningOn = $this->config->getSystemValue('version');
138 4
		$runningOnServer91OrLater = version_compare($runningOn, '9.1', '>=');
139 4
		$runningOnNextcloud11OrLater = version_compare($runningOn, '11', '>=');
140
141 4
		$supportsClass = $runningOnServer91OrLater;
142 4
		$assetPipelineBroken = !$runningOnServer91OrLater;
143 4
		$needsAutosize = !$runningOnNextcloud11OrLater;
144
145 4
		$isAssetPipelineEnabled = $this->config->getSystemValue('asset-pipeline.enabled', false);
146 4
		if ($isAssetPipelineEnabled && $assetPipelineBroken) {
147
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
148
		}
149
150 4
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
151
152 4
		$response = new TemplateResponse('calendar', 'main', [
153 4
			'appVersion' => $appVersion,
154 4
			'initialView' => 'month',
155 4
			'emailAddress' => '',
156 4
			'supportsClass' => $supportsClass,
157 4
			'firstRun' => 'no',
158
			'isPublic' => true,
159 4
			'shareURL' => $this->request->getServerProtocol() . '://' . $this->request->getServerHost() . $this->request->getRequestUri(),
160 4
			'previewImage' => $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'favicon-touch.png')),
161 4
			'needsAutosize' => $needsAutosize,
162 4
		], 'public');
163 4
		$response->addHeader('X-Frame-Options', 'ALLOW');
164 4
		$csp = new ContentSecurityPolicy();
165 4
		$csp->addAllowedScriptDomain('*');
166 4
		$response->setContentSecurityPolicy($csp);
167
168 4
		return $response;
169
	}
170
}
171