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

ViewController::index()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 41
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 26
nc 7
nop 0
dl 0
loc 41
ccs 26
cts 26
cp 1
crap 5
rs 8.439
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 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
		]));
113
	}
114 8
115 8
	/**
116
	 * @PublicPage
117 8
	 * @NoCSRFRequired
118 8
	 *
119 8
	 * @return TemplateResponse
120 8
	 */
121 8
	public function publicIndexWithBranding() {
122 8
		if ($this->needsAssetPipelineWarning()) {
123 8
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
124 8
		}
125 8
126 8
		$templateParameters = $this->getTemplateParams();
127
		$publicTemplateParameters = $this->getPublicTemplateParameters();
128 8
		$params = array_merge($templateParameters, $publicTemplateParameters);
129 8
130
		return new TemplateResponse('calendar', 'public', $params, 'base');
131
	}
132
133
	/**
134
	 * @PublicPage
135
	 * @NoCSRFRequired
136
	 *
137
	 * @return TemplateResponse
138
	 */
139 5
	public function publicIndex() {
140 5
		if ($this->needsAssetPipelineWarning()) {
141 5
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
142 5
		}
143
144 5
		$templateParameters = $this->getTemplateParams();
145 5
		$publicTemplateParameters = $this->getPublicTemplateParameters();
146 5
		$params = array_merge($templateParameters, $publicTemplateParameters);
147
148 5
		$response = new TemplateResponse('calendar', 'main', $params, 'public');
149 5
150
		$response->addHeader('X-Frame-Options', 'ALLOW');
151
		$csp = new ContentSecurityPolicy();
152
		$csp->addAllowedScriptDomain('*');
153 5
		$response->setContentSecurityPolicy($csp);
154 5
155
		return $response;
156 5
	}
157 5
158 5
	/**
159 5
	 * get common parameters used for all three routes
160 5
	 * @return array
161 5
	 */
162 5
	private function getTemplateParams() {
163 5
		$runningOn = $this->config->getSystemValue('version');
164 5
		$runningOnNextcloud10OrLater = version_compare($runningOn, '9.1', '>=');
165 5
		$runningOnNextcloud11OrLater = version_compare($runningOn, '11', '>=');
166
167 5
		$supportsClass = $runningOnNextcloud10OrLater;
168 5
		$needsAutosize = !$runningOnNextcloud11OrLater;
169 5
170 5
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
171 5
		$webCalWorkaround = $runningOnNextcloud10OrLater ? 'no' : 'yes';
172 5
		$isIE = $this->request->isUserAgent([Request::USER_AGENT_IE]);
173 5
		$defaultColor = $this->config->getAppValue('theming', 'color', '#0082C9');
174 5
175
		return [
176 5
			'appVersion' => $appVersion,
177
			'supportsClass' => $supportsClass,
178
			'isIE' => $isIE,
179
			'webCalWorkaround' => $webCalWorkaround,
180
			'needsAutosize' => $needsAutosize,
181
			'defaultColor' => $defaultColor,
182
		];
183
	}
184
185
	/**
186
	 * get common parameters for public sites
187
	 * @return array
188
	 */
189
	private function getPublicTemplateParameters() {
190
		return [
191
			'initialView' => 'month',
192
			'emailAddress' => '',
193
			'skipPopover' => 'no',
194
			'weekNumbers' => 'no',
195
			'firstRun' => 'no',
196
			'webCalWorkaround' => 'no',
197
			'isPublic' => true,
198
			'shareURL' => $this->request->getServerProtocol() . '://' . $this->request->getServerHost() . $this->request->getRequestUri(),
199
			'previewImage' => $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'favicon-touch.png')),
200
			'webcalURL' => '',
201
			'downloadURL' => '',
202
		];
203
	}
204
205
	/**
206
	 * check if we need to show the asset pipeline warning
207
	 * @return bool
208
	 */
209
	private function needsAssetPipelineWarning() {
210
		$runningOn = $this->config->getSystemValue('version');
211
		$assetPipelineBroken = version_compare($runningOn, '9.1', '<');
212
		$isAssetPipelineEnabled = $this->config->getSystemValue('asset-pipeline.enabled', false);
213
214
		return ($isAssetPipelineEnabled && $assetPipelineBroken);
215
	}
216
}
217