Completed
Push — master ( 1801e4...143145 )
by Morris
19:04 queued 02:13
created

Admin::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 6
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2016, ownCloud, Inc.
5
 *
6
 * @author Arthur Schiwon <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 * @author Lukas Reschke <[email protected]>
9
 * @author Morris Jobke <[email protected]>
10
 *
11
 * @license AGPL-3.0
12
 *
13
 * This code is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License, version 3,
15
 * as published by the Free Software Foundation.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License, version 3,
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
24
 *
25
 */
26
27
namespace OCA\UpdateNotification\Settings;
28
29
use OCA\UpdateNotification\UpdateChecker;
30
use OCP\AppFramework\Http\TemplateResponse;
31
use OCP\IConfig;
32
use OCP\IDateTimeFormatter;
33
use OCP\IGroupManager;
34
use OCP\IUserSession;
35
use OCP\L10N\IFactory;
36
use OCP\Settings\ISettings;
37
use OCP\Util;
38
39
class Admin implements ISettings {
40
	/** @var IConfig */
41
	private $config;
42
	/** @var UpdateChecker */
43
	private $updateChecker;
44
	/** @var IGroupManager */
45
	private $groupManager;
46
	/** @var IDateTimeFormatter */
47
	private $dateTimeFormatter;
48
	/** @var IUserSession */
49
	private $session;
50
	/** @var IFactory */
51
	private $l10nFactory;
52
53
	public function __construct(
54
		IConfig $config,
55
		UpdateChecker $updateChecker,
56
		IGroupManager $groupManager,
57
		IDateTimeFormatter $dateTimeFormatter,
58
		IUserSession $session,
59
		IFactory $l10nFactory
60
	) {
61
		$this->config = $config;
62
		$this->updateChecker = $updateChecker;
63
		$this->groupManager = $groupManager;
64
		$this->dateTimeFormatter = $dateTimeFormatter;
65
		$this->session = $session;
66
		$this->l10nFactory = $l10nFactory;
67
	}
68
69
	/**
70
	 * @return TemplateResponse
71
	 */
72
	public function getForm(): TemplateResponse {
73
		$lastUpdateCheckTimestamp = $this->config->getAppValue('core', 'lastupdatedat');
74
		$lastUpdateCheck = $this->dateTimeFormatter->formatDateTime($lastUpdateCheckTimestamp);
75
76
		$channels = [
77
			'daily',
78
			'beta',
79
			'stable',
80
			'production',
81
		];
82
		$currentChannel = Util::getChannel();
83
		if ($currentChannel === 'git') {
84
			$channels[] = 'git';
85
		}
86
87
		$updateState = $this->updateChecker->getUpdateState();
88
89
		$notifyGroups = json_decode($this->config->getAppValue('updatenotification', 'notify_groups', '["admin"]'), true);
90
91
		$defaultUpdateServerURL = 'https://updates.nextcloud.com/updater_server/';
92
		$updateServerURL = $this->config->getSystemValue('updater.server.url', $defaultUpdateServerURL);
93
94
		$params = [
95
			'isNewVersionAvailable' => !empty($updateState['updateAvailable']),
96
			'isUpdateChecked' => $lastUpdateCheckTimestamp > 0,
97
			'lastChecked' => $lastUpdateCheck,
98
			'currentChannel' => $currentChannel,
99
			'channels' => $channels,
100
			'newVersionString' => empty($updateState['updateVersion']) ? '' : $updateState['updateVersion'],
101
			'downloadLink' => empty($updateState['downloadLink']) ? '' : $updateState['downloadLink'],
102
			'changes' => $this->filterChanges($updateState['changes'] ?? []),
103
			'updaterEnabled' => empty($updateState['updaterEnabled']) ? false : $updateState['updaterEnabled'],
104
			'versionIsEol' => empty($updateState['versionIsEol']) ? false : $updateState['versionIsEol'],
105
			'isDefaultUpdateServerURL' => $updateServerURL === $defaultUpdateServerURL,
106
			'updateServerURL' => $updateServerURL,
107
			'notifyGroups' => $this->getSelectedGroups($notifyGroups),
108
		];
109
110
		$params = [
111
			'json' => json_encode($params),
112
		];
113
114
		return new TemplateResponse('updatenotification', 'admin', $params, '');
115
	}
116
117
	protected function filterChanges(array $changes) {
118
		$filtered = [];
119
		if(isset($changes['changelogURL'])) {
120
			$filtered['changelogURL'] = $changes['changelogURL'];
121
		}
122
		if(!isset($changes['whatsNew'])) {
123
			return $filtered;
124
		}
125
126
		$iterator = $this->l10nFactory->getLanguageIterator();
127 View Code Duplication
		do {
128
			$lang = $iterator->current();
129
			if(isset($changes['whatsNew'][$lang])) {
130
				return $filtered['whatsNew'][$lang];
131
			}
132
			$iterator->next();
133
		} while($lang !== 'en' && $iterator->valid());
134
135
		return $filtered;
136
	}
137
138
	/**
139
	 * @param array $groupIds
140
	 * @return array
141
	 */
142
	protected function getSelectedGroups(array $groupIds): array {
143
		$result = [];
144
		foreach ($groupIds as $groupId) {
145
			$group = $this->groupManager->get($groupId);
146
147
			if ($group === null) {
148
				continue;
149
			}
150
151
			$result[] = ['value' => $group->getGID(), 'label' => $group->getDisplayName()];
152
		}
153
154
		return $result;
155
	}
156
157
	/**
158
	 * @return string the section ID, e.g. 'sharing'
159
	 */
160
	public function getSection(): string {
161
		return 'overview';
162
	}
163
164
	/**
165
	 * @return int whether the form should be rather on the top or bottom of
166
	 * the admin section. The forms are arranged in ascending order of the
167
	 * priority values. It is required to return a value between 0 and 100.
168
	 *
169
	 * E.g.: 70
170
	 */
171
	public function getPriority(): int {
172
		return 11;
173
	}
174
}
175