Issues (2553)

dashboard/lib/Controller/DashboardController.php (2 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2020 Julius Härtl <[email protected]>
7
 *
8
 * @author Julien Veyssier <[email protected]>
9
 * @author Julius Härtl <[email protected]>
10
 * @author Morris Jobke <[email protected]>
11
 * @author Roeland Jago Douma <[email protected]>
12
 *
13
 * @license GNU AGPL version 3 or any later version
14
 *
15
 * This program is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License
26
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27
 *
28
 */
29
namespace OCA\Dashboard\Controller;
30
31
use OCA\Files\Event\LoadSidebar;
32
use OCA\Viewer\Event\LoadViewer;
0 ignored issues
show
The type OCA\Viewer\Event\LoadViewer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
33
use OCP\AppFramework\Controller;
34
use OCP\AppFramework\Http;
35
use OCP\AppFramework\Http\JSONResponse;
36
use OCP\AppFramework\Http\TemplateResponse;
37
use OCP\AppFramework\Services\IInitialState;
38
use OCP\Dashboard\IManager;
39
use OCP\Dashboard\IWidget;
40
use OCP\Dashboard\RegisterWidgetEvent;
41
use OCP\EventDispatcher\IEventDispatcher;
42
use OCP\IConfig;
43
use OCP\IRequest;
44
45
class DashboardController extends Controller {
46
47
	/** @var IInitialState */
48
	private $initialState;
49
	/** @var IEventDispatcher */
50
	private $eventDispatcher;
51
	/** @var IManager */
52
	private $dashboardManager;
53
	/** @var IConfig */
54
	private $config;
55
	/** @var string */
56
	private $userId;
57
58
	public function __construct(
59
		string $appName,
60
		IRequest $request,
61
		IInitialState $initialState,
62
		IEventDispatcher $eventDispatcher,
63
		IManager $dashboardManager,
64
		IConfig $config,
65
		$userId
66
	) {
67
		parent::__construct($appName, $request);
68
69
		$this->initialState = $initialState;
70
		$this->eventDispatcher = $eventDispatcher;
71
		$this->dashboardManager = $dashboardManager;
72
		$this->config = $config;
73
		$this->userId = $userId;
74
	}
75
76
	/**
77
	 * @NoCSRFRequired
78
	 * @NoAdminRequired
79
	 * @return TemplateResponse
80
	 */
81
	public function index(): TemplateResponse {
82
		\OCP\Util::addStyle('dashboard', 'dashboard');
83
		\OCP\Util::addScript('dashboard', 'main', 'theming');
84
85
		$this->eventDispatcher->dispatchTyped(new LoadSidebar());
86
		if (class_exists(LoadViewer::class)) {
87
			$this->eventDispatcher->dispatchTyped(new LoadViewer());
88
		}
89
90
		$this->eventDispatcher->dispatchTyped(new RegisterWidgetEvent($this->dashboardManager));
0 ignored issues
show
Deprecated Code introduced by
The class OCP\Dashboard\RegisterWidgetEvent has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

90
		$this->eventDispatcher->dispatchTyped(/** @scrutinizer ignore-deprecated */ new RegisterWidgetEvent($this->dashboardManager));
Loading history...
91
92
		$systemDefault = $this->config->getAppValue('dashboard', 'layout', 'recommendations,spreed,mail,calendar');
93
		$userLayout = explode(',', $this->config->getUserValue($this->userId, 'dashboard', 'layout', $systemDefault));
94
		$widgets = array_map(function (IWidget $widget) {
95
			return [
96
				'id' => $widget->getId(),
97
				'title' => $widget->getTitle(),
98
				'iconClass' => $widget->getIconClass(),
99
				'url' => $widget->getUrl()
100
			];
101
		}, $this->dashboardManager->getWidgets());
102
		$configStatuses = $this->config->getUserValue($this->userId, 'dashboard', 'statuses', '');
103
		$statuses = json_decode($configStatuses, true);
104
		// We avoid getting an empty array as it will not produce an object in UI's JS
105
		// It does not matter if some statuses are missing from the array, missing ones are considered enabled
106
		$statuses = ($statuses && count($statuses) > 0) ? $statuses : ['weather' => true];
107
108
		$this->initialState->provideInitialState('panels', $widgets);
109
		$this->initialState->provideInitialState('statuses', $statuses);
110
		$this->initialState->provideInitialState('layout', $userLayout);
111
		$this->initialState->provideInitialState('firstRun', $this->config->getUserValue($this->userId, 'dashboard', 'firstRun', '1') === '1');
112
		$this->config->setUserValue($this->userId, 'dashboard', 'firstRun', '0');
113
114
		$response = new TemplateResponse('dashboard', 'index', [
115
			'id-app-content' => '#app-dashboard',
116
			'id-app-navigation' => null,
117
		]);
118
119
		// For the weather widget we should allow the geolocation
120
		$featurePolicy = new Http\FeaturePolicy();
121
		$featurePolicy->addAllowedGeoLocationDomain('\'self\'');
122
		$response->setFeaturePolicy($featurePolicy);
123
124
		return $response;
125
	}
126
127
	/**
128
	 * @NoAdminRequired
129
	 * @param string $layout
130
	 * @return JSONResponse
131
	 */
132
	public function updateLayout(string $layout): JSONResponse {
133
		$this->config->setUserValue($this->userId, 'dashboard', 'layout', $layout);
134
		return new JSONResponse(['layout' => $layout]);
135
	}
136
137
	/**
138
	 * @NoAdminRequired
139
	 * @param string $statuses
140
	 * @return JSONResponse
141
	 */
142
	public function updateStatuses(string $statuses): JSONResponse {
143
		$this->config->setUserValue($this->userId, 'dashboard', 'statuses', $statuses);
144
		return new JSONResponse(['statuses' => $statuses]);
145
	}
146
}
147