Completed
Pull Request — master (#372)
by Georg
23:32 queued 20:13
created

ViewController::getPublicTemplateParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 15
ccs 13
cts 13
cp 1
crap 1
rs 9.4285
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 2
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
76
		}
77
78 8
		$templateParameters = $this->getTemplateParams();
79
80 8
		$user = $this->userSession->getUser();
81 8
		$userId = $user->getUID();
82 8
		$emailAddress = $user->getEMailAddress();
83
84 8
		$initialView = $this->config->getUserValue($userId, $this->appName, 'currentView', null);
85 8
		$skipPopover = $this->config->getUserValue($userId, $this->appName, 'skipPopover', 'no');
86 8
		$weekNumbers = $this->config->getUserValue($userId, $this->appName, 'showWeekNr', 'no');
87 8
		$firstRun = $this->config->getUserValue($userId, $this->appName, 'firstRun', null);
88
89
		// the default view will be saved as soon as a user
90
		// opens the calendar app, therefore this is a good
91
		// indication if the calendar was used before
92 8
		if ($firstRun === null) {
93 2
			if ($initialView === null) {
94 1
				$firstRun = 'yes';
95 1
			} else {
96 1
				$this->config->setUserValue($userId, $this->appName, 'firstRun', 'no');
97 1
				$firstRun = 'no';
98
			}
99 2
		}
100
101 8
		if ($initialView === null) {
102 2
			$initialView = 'month';
103 2
		}
104
105 8
		return new TemplateResponse('calendar', 'main', array_merge($templateParameters, [
106 8
			'initialView' => $initialView,
107 8
			'emailAddress' => $emailAddress,
108 8
			'skipPopover' => $skipPopover,
109 8
			'weekNumbers' => $weekNumbers,
110 8
			'firstRun' => $firstRun,
111 8
			'isPublic' => false,
112 8
		]));
113
	}
114
115
	/**
116
	 * @PublicPage
117
	 * @NoCSRFRequired
118
	 *
119
	 * @return TemplateResponse
120
	 */
121
	public function publicIndexWithBranding() {
122
		if ($this->needsAssetPipelineWarning()) {
123
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
124
		}
125
126
		$templateParameters = $this->getTemplateParams();
127
		$publicTemplateParameters = $this->getPublicTemplateParameters();
128
		$params = array_merge($templateParameters, $publicTemplateParameters);
129
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
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
142
		}
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
150 5
		$response->addHeader('X-Frame-Options', 'ALLOW');
151 5
		$csp = new ContentSecurityPolicy();
152 5
		$csp->addAllowedScriptDomain('*');
153 5
		$response->setContentSecurityPolicy($csp);
154
155 5
		return $response;
156
	}
157
158
	/**
159
	 * get common parameters used for all three routes
160
	 * @return array
161
	 */
162 13
	private function getTemplateParams() {
163 13
		$runningOn = $this->config->getSystemValue('version');
164 13
		$runningOnNextcloud10OrLater = version_compare($runningOn, '9.1', '>=');
165 13
		$runningOnNextcloud11OrLater = version_compare($runningOn, '11', '>=');
166
167 13
		$supportsClass = $runningOnNextcloud10OrLater;
168 13
		$needsAutosize = !$runningOnNextcloud11OrLater;
169
170 13
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
171 13
		$webCalWorkaround = $runningOnNextcloud10OrLater ? 'no' : 'yes';
172 13
		$isIE = $this->request->isUserAgent([Request::USER_AGENT_IE]);
173 13
		$defaultColor = $this->config->getAppValue('theming', 'color', '#0082C9');
174
175
		return [
176 13
			'appVersion' => $appVersion,
177 13
			'supportsClass' => $supportsClass,
178 13
			'isIE' => $isIE,
179 13
			'webCalWorkaround' => $webCalWorkaround,
180 13
			'needsAutosize' => $needsAutosize,
181 13
			'defaultColor' => $defaultColor,
182 13
		];
183
	}
184
185
	/**
186
	 * get common parameters for public sites
187
	 * @return array
188
	 */
189 5
	private function getPublicTemplateParameters() {
190
		return [
191 5
			'initialView' => 'month',
192 5
			'emailAddress' => '',
193 5
			'skipPopover' => 'no',
194 5
			'weekNumbers' => 'no',
195 5
			'firstRun' => 'no',
196 5
			'webCalWorkaround' => 'no',
197 5
			'isPublic' => true,
198 5
			'shareURL' => $this->request->getServerProtocol() . '://' . $this->request->getServerHost() . $this->request->getRequestUri(),
199 5
			'previewImage' => $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'favicon-touch.png')),
200 5
			'webcalURL' => '',
201 5
			'downloadURL' => '',
202 5
		];
203
	}
204
205
	/**
206
	 * check if we need to show the asset pipeline warning
207
	 * @return bool
208
	 */
209 15
	private function needsAssetPipelineWarning() {
210 15
		$runningOn = $this->config->getSystemValue('version');
211 15
		$assetPipelineBroken = version_compare($runningOn, '9.1', '<');
212 15
		$isAssetPipelineEnabled = $this->config->getSystemValue('asset-pipeline.enabled', false);
213
214 15
		return ($isAssetPipelineEnabled && $assetPipelineBroken);
215
	}
216
}
217