Completed
Pull Request — master (#372)
by Georg
20:37
created

ViewController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 204
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 204
ccs 76
cts 76
cp 1
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B index() 0 42 5
A publicIndexWithBranding() 0 11 2
A publicIndex() 0 18 2
A getTemplateParams() 0 22 2
B getPublicTemplateParameters() 0 31 1
A needsAssetPipelineWarning() 0 7 2
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 OC\AppFramework\Http\Request;
27
use OCP\AppFramework\Controller;
28
use OCP\AppFramework\Http\ContentSecurityPolicy;
29
use OCP\AppFramework\Http\TemplateResponse;
30
use OCP\IConfig;
31
use OCP\IRequest;
32
use OCP\IUserSession;
33
use OCP\IURLGenerator;
34
35
class ViewController extends Controller {
36
37
	/**
38
	 * @var IConfig
39
	 */
40
	private $config;
41
42
	/**
43
	 * @var IURLGenerator
44
	 */
45
	private $urlGenerator;
46
47
	/**
48
	 * @var IUserSession
49
	 */
50
	private $userSession;
51
52
	/**
53
	 * @param string $appName
54
	 * @param IRequest $request an instance of the request
55
	 * @param IUserSession $userSession
56
	 * @param IConfig $config
57
	 * @param IURLGenerator $urlGenerator
58
	 */
59 15
	public function __construct($appName, IRequest $request, IUserSession $userSession,
60
								IConfig $config, IURLGenerator $urlGenerator) {
61 15
		parent::__construct($appName, $request);
62 15
		$this->config = $config;
63 15
		$this->userSession = $userSession;
64 15
		$this->urlGenerator = $urlGenerator;
65 15
	}
66
67
	/**
68
	 * @NoAdminRequired
69
	 * @NoCSRFRequired
70
	 *
71
	 * @return TemplateResponse
72
	 */
73 10
	public function index() {
74 10
		if ($this->needsAssetPipelineWarning()) {
75 10
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
76 10
		}
77
78 10
		$templateParameters = $this->getTemplateParams();
79 10
80 10
		$user = $this->userSession->getUser();
81
		$userId = $user->getUID();
82 10
		$emailAddress = $user->getEMailAddress();
83 10
84 2
		$initialView = $this->config->getUserValue($userId, $this->appName, 'currentView', null);
85
		$skipPopover = $this->config->getUserValue($userId, $this->appName, 'skipPopover', 'no');
86
		$weekNumbers = $this->config->getUserValue($userId, $this->appName, 'showWeekNr', 'no');
87 8
		$firstRun = $this->config->getUserValue($userId, $this->appName, 'firstRun', null);
88 8
89 8
		// the default view will be saved as soon as a user
90
		// opens the calendar app, therefore this is a good
91 8
		// indication if the calendar was used before
92 8
		if ($firstRun === null) {
93 8
			if ($initialView === null) {
94 8
				$firstRun = 'yes';
95 8
			} else {
96 8
				$this->config->setUserValue($userId, $this->appName, 'firstRun', 'no');
97
				$firstRun = 'no';
98
			}
99
		}
100
101 8
		if ($initialView === null) {
102 2
			$initialView = 'month';
103 1
		}
104
105 1
		return new TemplateResponse('calendar', 'main', array_merge($templateParameters, [
106 1
			'initialView' => $initialView,
107
			'emailAddress' => $emailAddress,
108
			'skipPopover' => $skipPopover,
109
			'weekNumbers' => $weekNumbers,
110 8
			'firstRun' => $firstRun,
111 2
			'isPublic' => false,
112
			'token' => '',
113
		]));
114 8
	}
115 8
116
	/**
117 8
	 * @PublicPage
118 8
	 * @NoCSRFRequired
119 8
	 *
120 8
	 * @param string $token
121 8
	 *
122 8
	 * @return TemplateResponse
123 8
	 */
124 8
	public function publicIndexWithBranding($token) {
125 8
		if ($this->needsAssetPipelineWarning()) {
126 8
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
127
		}
128 8
129 8
		$templateParameters = $this->getTemplateParams();
130
		$publicTemplateParameters = $this->getPublicTemplateParameters($token);
131
		$params = array_merge($templateParameters, $publicTemplateParameters);
132
133
		return new TemplateResponse('calendar', 'public', $params, 'base');
134
	}
135
136
	/**
137
	 * @PublicPage
138
	 * @NoCSRFRequired
139 5
	 *
140 5
	 * @param string $token
141 5
	 *
142 5
	 * @return TemplateResponse
143
	 */
144 5
	public function publicIndex($token) {
145 5
		if ($this->needsAssetPipelineWarning()) {
146 5
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
147
		}
148 5
149 5
		$templateParameters = $this->getTemplateParams();
150
		$publicTemplateParameters = $this->getPublicTemplateParameters($token);
151
		$params = array_merge($templateParameters, $publicTemplateParameters);
152
153 5
		$response = new TemplateResponse('calendar', 'main', $params, 'public');
154 5
155
		$response->addHeader('X-Frame-Options', 'ALLOW');
156 5
		$csp = new ContentSecurityPolicy();
157 5
		$csp->addAllowedScriptDomain('*');
158 5
		$response->setContentSecurityPolicy($csp);
159 5
160 5
		return $response;
161 5
	}
162 5
163 5
	/**
164 5
	 * get common parameters used for all three routes
165 5
	 * @return array
166
	 */
167 5
	private function getTemplateParams() {
168 5
		$runningOn = $this->config->getSystemValue('version');
169 5
		$runningOnNextcloud10OrLater = version_compare($runningOn, '9.1', '>=');
170 5
		$runningOnNextcloud11OrLater = version_compare($runningOn, '11', '>=');
171 5
172 5
		$supportsClass = $runningOnNextcloud10OrLater;
173 5
		$needsAutosize = !$runningOnNextcloud11OrLater;
174 5
175
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
176 5
		$webCalWorkaround = $runningOnNextcloud10OrLater ? 'no' : 'yes';
177
		$isIE = $this->request->isUserAgent([Request::USER_AGENT_IE]);
178
		$defaultColor = $this->config->getAppValue('theming', 'color', '#0082C9');
179
180
		return [
181
			'appVersion' => $appVersion,
182
			'supportsClass' => $supportsClass,
183
			'isIE' => $isIE,
184
			'webCalWorkaround' => $webCalWorkaround,
185
			'needsAutosize' => $needsAutosize,
186
			'defaultColor' => $defaultColor,
187
		];
188
	}
189
190
	/**
191
	 * get common parameters for public sites
192
	 * @param string $token
193
	 * @return array
194
	 */
195
	private function getPublicTemplateParameters($token) {
196
		$shareURL = $this->request->getServerProtocol() . '://';
197
		$shareURL .= $this->request->getServerHost();
198
		$shareURL .= $this->request->getRequestUri();
199
200
		$relativeImagePath = $this->urlGenerator->imagePath('core', 'favicon-touch.png');
201
		$previewImage = $this->urlGenerator->getAbsoluteURL($relativeImagePath);
202
203
		$remoteBase = $this->urlGenerator->linkTo('', 'remote.php');
204
		$remoteBase .= '/dav/public-calendars/' . $token . '?export';
205
206
		$downloadUrl = $this->urlGenerator->getAbsoluteURL($remoteBase);
207
208
		$protocolLength = strlen($this->request->getServerProtocol()) + 3;
209
		$webcalUrl = 'webcal://' . substr($downloadUrl, $protocolLength);
210
211
		return [
212
			'initialView' => 'month',
213
			'emailAddress' => '',
214
			'skipPopover' => 'no',
215
			'weekNumbers' => 'no',
216
			'firstRun' => 'no',
217
			'webCalWorkaround' => 'no',
218
			'isPublic' => true,
219
			'shareURL' => $shareURL,
220
			'previewImage' => $previewImage,
221
			'webcalURL' => $webcalUrl,
222
			'downloadURL' => $downloadUrl,
223
			'token' => $token,
224
		];
225
	}
226
227
	/**
228
	 * check if we need to show the asset pipeline warning
229
	 * @return bool
230
	 */
231
	private function needsAssetPipelineWarning() {
232
		$runningOn = $this->config->getSystemValue('version');
233
		$assetPipelineBroken = version_compare($runningOn, '9.1', '<');
234
		$isAssetPipelineEnabled = $this->config->getSystemValue('asset-pipeline.enabled', false);
235
236
		return ($isAssetPipelineEnabled && $assetPipelineBroken);
237
	}
238
}
239