PanoramaService::read()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Analytics
4
 *
5
 * SPDX-FileCopyrightText: 2019-2022 Marcel Scherello
6
 * SPDX-License-Identifier: AGPL-3.0-or-later
7
 */
8
9
namespace OCA\Analytics\Service;
10
11
use OCA\Analytics\Activity\ActivityManager;
12
use OCA\Analytics\Db\PanoramaMapper;
13
use OCP\AppFramework\Http\DataDownloadResponse;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Http\DataDownloadResponse 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...
14
use OCP\DB\Exception;
0 ignored issues
show
Bug introduced by
The type OCP\DB\Exception 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...
15
use OCP\ITagManager;
0 ignored issues
show
Bug introduced by
The type OCP\ITagManager 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...
16
use OCP\IConfig;
0 ignored issues
show
Bug introduced by
The type OCP\IConfig 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...
17
use OCP\PreConditionNotMetException;
0 ignored issues
show
Bug introduced by
The type OCP\PreConditionNotMetException 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...
18
use Psr\Log\LoggerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Log\LoggerInterface 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...
19
use OCP\IL10N;
0 ignored issues
show
Bug introduced by
The type OCP\IL10N 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...
20
21
class PanoramaService {
22
	/** @var IConfig */
23
	protected $config;
24
	private $userId;
25
	private $logger;
26
	private $tagManager;
27
	private $ShareService;
28
	private $PanoramaMapper;
29
	private $VariableService;
30
	private $l10n;
31
	private $ActivityManager;
32
33
	const REPORT_TYPE_GROUP = 0;
34
35
	public function __construct(
36
		$userId,
37
		IL10N $l10n,
38
		LoggerInterface $logger,
39
		ITagManager $tagManager,
40
		ShareService $ShareService,
41
		PanoramaMapper $PanoramaMapper,
42
		IConfig $config,
43
		VariableService $VariableService,
44
		ActivityManager $ActivityManager,
45
	) {
46
		$this->userId = $userId;
47
		$this->logger = $logger;
48
		$this->tagManager = $tagManager;
49
		$this->ShareService = $ShareService;
50
		$this->PanoramaMapper = $PanoramaMapper;
51
		$this->VariableService = $VariableService;
52
		$this->config = $config;
53
		$this->l10n = $l10n;
54
		$this->ActivityManager = $ActivityManager;
55
	}
56
57
	/**
58
	 * get all reports
59
	 *
60
	 * @return array
61
	 * @throws PreConditionNotMetException
62
	 * @throws Exception
63
	 */
64
	public function index(): array {
65
		$ownPanorama = $this->PanoramaMapper->index();
66
		$sharedPanoramas = $this->ShareService->getSharedItems(ShareService::SHARE_ITEM_TYPE_PANORAMA);
67
		$keysToKeep = array('id', 'name', 'dataset', 'favorite', 'parent', 'type', 'pages', 'isShare', 'shareId');
68
69
		// get shared reports and remove duplicates
70
		foreach ($sharedPanoramas as $sharedPanorama) {
71
			if (!array_search($sharedPanorama['id'], array_column($ownPanorama, 'id'))) {
72
				// just keep the necessary fields
73
				$ownPanorama[] = array_intersect_key($sharedPanorama, array_flip($keysToKeep));;
74
			}
75
		}
76
77
		$favorites = $this->tagManager->load('analyticsPanorama')->getFavorites();
78
		foreach ($ownPanorama as &$ownReport) {
79
			$hasTag = 0;
80
			if (is_array($favorites) and in_array($ownReport['id'], $favorites)) {
81
				$hasTag = 1;
82
			}
83
			$ownReport['favorite'] = $hasTag;
84
			$ownReport['item_type'] = ShareService::SHARE_ITEM_TYPE_PANORAMA;
85
			$ownReport = $this->VariableService->replaceTextVariables($ownReport);
86
		}
87
88
		return $ownPanorama;
89
	}
90
91
	/**
92
	 * get own report details
93
	 *
94
	 * @param int $panoramaId
95
	 * @return array
96
	 * @throws Exception
97
	 */
98
	public function read(int $panoramaId) {
99
		$ownReport = $this->PanoramaMapper->readOwn($panoramaId);
100
		return $ownReport;
101
	}
102
103
	/**
104
	 * check if own report
105
	 *
106
	 * @param int $panoramaId
107
	 * @return bool
108
	 */
109
	public function isOwn(int $panoramaId) {
110
		$ownReport = $this->PanoramaMapper->readOwn($panoramaId);
111
		if (!empty($ownReport)) {
112
			return true;
113
		} else {
114
			return false;
115
		}
116
	}
117
118
	/**
119
	 * create new blank report
120
	 *
121
	 * @param $name
122
	 * @param int $type
123
	 * @param int $parent
124
	 * @param $pages
125
	 * @return int
126
	 * @throws Exception
127
	 */
128
	public function create(int $type, int $parent): int {
129
		$reportId = $this->PanoramaMapper->create($this->l10n->t('New'), $type, $parent, '[]');
130
		$this->ActivityManager->triggerEvent($reportId, ActivityManager::OBJECT_PANORAMA, ActivityManager::SUBJECT_PANORAMA_ADD);
131
		return $reportId;
132
	}
133
134
	/**
135
	 * update report details
136
	 *
137
	 * @param int $id
138
	 * @param $name
139
	 * @param $subheader
140
	 * @param int $type
141
	 * @param int $parent
142
	 * @param $pages
143
	 * @return bool
144
	 * @throws Exception
145
	 */
146
	public function update(int $id, $name, int $type, int $parent, $pages) {
147
		return $this->PanoramaMapper->update($id, $name, $type, $parent, $pages);
148
	}
149
150
	/**
151
	 * Delete Dataset and all depending objects
152
	 *
153
	 * @param int $reportId
154
	 * @return string
155
	 * @throws Exception
156
	 */
157
	public function delete(int $reportId) {
158
		$this->ActivityManager->triggerEvent($reportId, ActivityManager::OBJECT_PANORAMA, ActivityManager::SUBJECT_PANORAMA_DELETE);
159
		$this->PanoramaMapper->delete($reportId);
160
		return 'true';
161
	}
162
163
	/**
164
	 * get dataset by user
165
	 *
166
	 * @param string $userId
167
	 * @return array|bool
168
	 * @throws Exception
169
	 */
170
	public function deleteByUser(string $userId) {
171
		$panoramas = $this->PanoramaMapper->indexByUser($userId);
172
		foreach ($panoramas as $panorama) {
173
			$this->ShareService->deleteSharesByItem(ShareService::SHARE_ITEM_TYPE_PANORAMA, $panorama['id']);
174
			$this->setFavorite($panorama['id'], 'false');
175
		}
176
		return true;
177
	}
178
179
	/**
180
	 * get own reports which are marked as favorites
181
	 *
182
	 * @return array|bool
183
	 * @throws Exception
184
	 */
185
	public function getOwnFavoriteReports() {
186
		$ownReports = $this->PanoramaMapper->index();
187
		//$sharedReports = $this->ShareService->getSharedItems(ShareService::SHARE_ITEM_TYPE_REPORT);
188
		$sharedReports = [];
189
		$favorites = $this->tagManager->load('analyticsPanorama')->getFavorites();
190
191
		// remove the favorite if the report is not existing anymore
192
		foreach ($favorites as $favorite) {
193
			if (!in_array($favorite, array_column($ownReports, 'id')) && !in_array($favorite, array_column($sharedReports, 'id'))) {
194
				unset($favorites[$favorite]);
195
				$this->tagManager->load('analyticsPanorama')->removeFromFavorites($favorite);
196
			}
197
		}
198
		return $favorites;
199
	}
200
201
	/**
202
	 * set/remove the favorite flag for a report
203
	 *
204
	 * @param int $panoramaId
205
	 * @param string $favorite
206
	 * @return bool
207
	 */
208
	public function setFavorite(int $panoramaId, string $favorite) {
209
		if ($favorite === 'true') {
210
			$return = $this->tagManager->load('analyticsPanorama')->addToFavorites($panoramaId);
211
		} else {
212
			$return = $this->tagManager->load('analyticsPanorama')->removeFromFavorites($panoramaId);
213
		}
214
		return $return;
215
	}
216
217
	/**
218
	 * search for reports
219
	 *
220
	 * @param string $searchString
221
	 * @return array
222
	 */
223
	public function search(string $searchString) {
224
		return $this->PanoramaMapper->search($searchString);
225
	}
226
}
227