Completed
Pull Request — master (#309)
by Georg
04:46
created

ViewController::sendEmail()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 14
nc 2
nop 4
dl 0
loc 19
ccs 3
cts 3
cp 1
crap 2
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 OCP\AppFramework\Controller;
27
use OCP\AppFramework\Http;
28
use OCP\AppFramework\Http\ContentSecurityPolicy;
29
use OCP\AppFramework\Http\JSONResponse;
30
use OCP\AppFramework\Http\TemplateResponse;
31
use OCP\Defaults;
32
use OCP\IConfig;
33
use OCP\IL10N;
34
use OCP\IRequest;
35
use OCP\IUserSession;
36
use OCP\Mail\IMailer;
37
use OCP\IURLGenerator;
38
39
class ViewController extends Controller {
40
41
	/**
42
	 * @var IConfig
43
	 */
44
	private $config;
45
46
	/**
47
	 * @var IURLGenerator
48
	 */
49
	private $urlGenerator;
50
51
	/**
52
	 * @var IUserSession
53
	 */
54
	private $userSession;
55
56
	/**
57
	 * @param string $appName
58
	 * @param IRequest $request an instance of the request
59
	 * @param IUserSession $userSession
60
	 * @param IConfig $config
61
	 * @param IURLGenerator $urlGenerator
62
	 */
63 13
	public function __construct($appName, IRequest $request, IUserSession $userSession,
64
								IConfig $config, IURLGenerator $urlGenerator) {
65 13
		parent::__construct($appName, $request);
66 13
		$this->config = $config;
67 13
		$this->userSession = $userSession;
68 13
		$this->urlGenerator = $urlGenerator;
69 13
	}
70
71
	/**
72
	 * @NoAdminRequired
73
	 * @NoCSRFRequired
74
	 *
75
	 * @return TemplateResponse
76
	 */
77 9
	public function index() {
78 9
		$runningOn = $this->config->getSystemValue('version');
79 9
		$runningOnNextcloud10OrLater = version_compare($runningOn, '9.1', '>=');
80 9
		$runningOnNextcloud11OrLater = version_compare($runningOn, '11', '>=');
81
82 9
		$supportsClass = $runningOnNextcloud10OrLater;
83 9
		$assetPipelineBroken = !$runningOnNextcloud10OrLater;
84 9
		$needsAutosize = !$runningOnNextcloud11OrLater;
85
86 9
		$isAssetPipelineEnabled = $this->config->getSystemValue('asset-pipeline.enabled', false);
87 9
		if ($isAssetPipelineEnabled && $assetPipelineBroken) {
88 2
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
89
		}
90
91 7
		$user = $this->userSession->getUser();
92 7
		$userId = $user->getUID();
93 7
		$emailAddress = $user->getEMailAddress();
94
95 7
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
96 7
		$initialView = $this->config->getUserValue($userId, $this->appName, 'currentView', null);
97 7
		$skipPopover = $this->config->getUserValue($userId, $this->appName, 'skipPopover', 'no');
98 7
		$weekNumbers = $this->config->getUserValue($userId, $this->appName, 'showWeekNr', 'no');
99 7
		$firstRun = $this->config->getUserValue($userId, $this->appName, 'firstRun', null);
100 7
		$defaultColor = $this->config->getAppValue('theming', 'color', '#0082C9');
101
102
		// the default view will be saved as soon as a user
103
		// opens the calendar app, therefore this is a good
104
		// indication if the calendar was used before
105 7
		if ($firstRun === null) {
106 2
			if ($initialView === null) {
107 1
				$firstRun = 'yes';
108
			} else {
109 1
				$this->config->setUserValue($userId, $this->appName, 'firstRun', 'no');
110 1
				$firstRun = 'no';
111
			}
112
		}
113
114 7
		if ($initialView === null) {
115 2
			$initialView = 'month';
116
		}
117
		
118 7
		$webCalWorkaround = $runningOnNextcloud10OrLater ? 'no' : 'yes';
119
120 7
		return new TemplateResponse('calendar', 'main', [
121 7
			'appVersion' => $appVersion,
122 7
			'initialView' => $initialView,
123 7
			'emailAddress' => $emailAddress,
124 7
			'skipPopover' => $skipPopover,
125 7
			'weekNumbers' => $weekNumbers,
126 7
			'firstRun' => $firstRun,
127 7
			'supportsClass' => $supportsClass,
128 7
			'defaultColor' => $defaultColor,
129 7
			'webCalWorkaround' => $webCalWorkaround,
130
			'isPublic' => false,
131 7
			'needsAutosize' => $needsAutosize,
132
		]);
133
	}
134
135
	/**
136
	 * @PublicPage
137
	 * @NoCSRFRequired
138
	 *
139
	 * @return TemplateResponse
140
	 */
141 4
	public function publicIndex() {
142 4
		$runningOn = $this->config->getSystemValue('version');
143 4
		$runningOnServer91OrLater = version_compare($runningOn, '9.1', '>=');
144 4
		$runningOnNextcloud11OrLater = version_compare($runningOn, '11', '>=');
145
146 4
		$supportsClass = $runningOnServer91OrLater;
147 4
		$assetPipelineBroken = !$runningOnServer91OrLater;
148 4
		$needsAutosize = !$runningOnNextcloud11OrLater;
149
150 4
		$isAssetPipelineEnabled = $this->config->getSystemValue('asset-pipeline.enabled', false);
151 4
		if ($isAssetPipelineEnabled && $assetPipelineBroken) {
152
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
153
		}
154
155 4
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
156
157 4
		$response = new TemplateResponse('calendar', 'main', [
158 4
			'appVersion' => $appVersion,
159 4
			'initialView' => 'month',
160 4
			'emailAddress' => '',
161 4
			'supportsClass' => $supportsClass,
162 4
			'firstRun' => 'no',
163
			'isPublic' => true,
164 4
			'shareURL' => $this->request->getServerProtocol() . '://' . $this->request->getServerHost() . $this->request->getRequestUri(),
165 4
			'previewImage' => $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'favicon-touch.png')),
166 4
			'needsAutosize' => $needsAutosize,
167 4
		], 'public');
168 4
		$response->addHeader('X-Frame-Options', 'ALLOW');
169 4
		$csp = new ContentSecurityPolicy();
170 4
		$csp->addAllowedScriptDomain('*');
171 4
		$response->setContentSecurityPolicy($csp);
172
173 4
		return $response;
174
	}
175
}
176