aboutsummaryrefslogtreecommitdiffstats
path: root/apps/theming/lib/Settings/Personal.php
blob: 9f78f2cbc35764f8b21bbafd8503f0a0c618d311 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/**
 * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
namespace OCA\Theming\Settings;

use OCA\Theming\ITheme;
use OCA\Theming\Service\BackgroundService;
use OCA\Theming\Service\ThemesService;
use OCA\Theming\ThemingDefaults;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\IConfig;
use OCP\INavigationManager;
use OCP\Settings\ISettings;
use OCP\Util;

class Personal implements ISettings {

	public function __construct(
		protected string $appName,
		private string $userId,
		private IConfig $config,
		private ThemesService $themesService,
		private IInitialState $initialStateService,
		private ThemingDefaults $themingDefaults,
		private INavigationManager $navigationManager,
	) {
	}

	public function getForm(): TemplateResponse {
		$enforcedTheme = $this->config->getSystemValueString('enforce_theme', '');

		$themes = array_map(function ($theme) {
			return [
				'id' => $theme->getId(),
				'type' => $theme->getType(),
				'title' => $theme->getTitle(),
				'enableLabel' => $theme->getEnableLabel(),
				'description' => $theme->getDescription(),
				'enabled' => $this->themesService->isEnabled($theme),
			];
		}, $this->themesService->getThemes());

		if ($enforcedTheme !== '') {
			$themes = array_filter($themes, function ($theme) use ($enforcedTheme) {
				return $theme['type'] !== ITheme::TYPE_THEME || $theme['id'] === $enforcedTheme;
			});
		}

		// Get the default entry enforced by admin
		$forcedDefaultEntry = $this->navigationManager->getDefaultEntryIdForUser(null, false);

		/** List of all shipped backgrounds */
		$this->initialStateService->provideInitialState('shippedBackgrounds', BackgroundService::SHIPPED_BACKGROUNDS);

		/**
		 * Admin theming
		 */
		$this->initialStateService->provideInitialState('themingDefaults', [
			/** URL of admin configured background image */
			'backgroundImage' => $this->themingDefaults->getBackground(),
			/** `backgroundColor` if disabled, mime type if defined and empty by default */
			'backgroundMime' => $this->config->getAppValue('theming', 'backgroundMime', ''),
			/** Admin configured background color */
			'backgroundColor' => $this->themingDefaults->getDefaultColorBackground(),
			/** Admin configured primary color */
			'primaryColor' => $this->themingDefaults->getDefaultColorPrimary(),
			/** Nextcloud default background image */
			'defaultShippedBackground' => BackgroundService::DEFAULT_BACKGROUND_IMAGE,
		]);

		$this->initialStateService->provideInitialState('userBackgroundImage', $this->config->getUserValue($this->userId, 'theming', 'background_image', BackgroundService::BACKGROUND_DEFAULT));
		$this->initialStateService->provideInitialState('themes', array_values($themes));
		$this->initialStateService->provideInitialState('enforceTheme', $enforcedTheme);
		$this->initialStateService->provideInitialState('isUserThemingDisabled', $this->themingDefaults->isUserThemingDisabled());
		$this->initialStateService->provideInitialState('enableBlurFilter', $this->config->getUserValue($this->userId, 'theming', 'force_enable_blur_filter', ''));
		$this->initialStateService->provideInitialState('navigationBar', [
			'userAppOrder' => json_decode($this->config->getUserValue($this->userId, 'core', 'apporder', '[]'), true, flags:JSON_THROW_ON_ERROR),
			'enforcedDefaultApp' => $forcedDefaultEntry
		]);

		Util::addScript($this->appName, 'personal-theming');

		return new TemplateResponse($this->appName, 'settings-personal');
	}

	/**
	 * @return string the section ID, e.g. 'sharing'
	 * @since 9.1
	 */
	public function getSection(): string {
		return $this->appName;
	}

	/**
	 * @return int whether the form should be rather on the top or bottom of
	 *             the admin section. The forms are arranged in ascending order of the
	 *             priority values. It is required to return a value between 0 and 100.
	 *
	 * E.g.: 70
	 * @since 9.1
	 */
	public function getPriority(): int {
		return 40;
	}
}