|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the TYPO3 CMS project. |
|
7
|
|
|
* |
|
8
|
|
|
* It is free software; you can redistribute it and/or modify it under |
|
9
|
|
|
* the terms of the GNU General Public License, either version 2 |
|
10
|
|
|
* of the License, or any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* For the full copyright and license information, please read the |
|
13
|
|
|
* LICENSE.txt file that was distributed with this source code. |
|
14
|
|
|
* |
|
15
|
|
|
* The TYPO3 project - inspiring people to share! |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace TYPO3\CMS\Dashboard; |
|
19
|
|
|
|
|
20
|
|
|
use Doctrine\DBAL\Driver\Statement; |
|
21
|
|
|
use Psr\Container\ContainerInterface; |
|
22
|
|
|
use TYPO3\CMS\Core\Database\ConnectionPool; |
|
23
|
|
|
use TYPO3\CMS\Core\Database\Query\QueryBuilder; |
|
24
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
25
|
|
|
use TYPO3\CMS\Dashboard\Widgets\WidgetInterface; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @internal |
|
29
|
|
|
*/ |
|
30
|
|
|
class DashboardRepository |
|
31
|
|
|
{ |
|
32
|
|
|
private const TABLE = 'be_dashboards'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string[] |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $allowedFields = ['title']; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var ConnectionPool |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $connectionPool; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var WidgetRegistry |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $widgetRegistry; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var ContainerInterface |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $container; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var WidgetInterface[] |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $widgets = []; |
|
58
|
|
|
|
|
59
|
|
|
public function __construct( |
|
60
|
|
|
ConnectionPool $connectionPool, |
|
61
|
|
|
WidgetRegistry $widgetRegistry, |
|
62
|
|
|
ContainerInterface $container |
|
63
|
|
|
) { |
|
64
|
|
|
$this->connectionPool = $connectionPool; |
|
65
|
|
|
$this->widgetRegistry = $widgetRegistry; |
|
66
|
|
|
$this->container = $container; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function getDashboardsForUser(int $userId): array |
|
70
|
|
|
{ |
|
71
|
|
|
$queryBuilder = $this->getQueryBuilder(); |
|
72
|
|
|
$rows = $queryBuilder |
|
73
|
|
|
->select('*') |
|
74
|
|
|
->from(self::TABLE) |
|
75
|
|
|
->where( |
|
76
|
|
|
$queryBuilder->expr()->eq('cruser_id', $queryBuilder->createNamedParameter($userId)) |
|
77
|
|
|
) |
|
78
|
|
|
->execute() |
|
79
|
|
|
->fetchAll(); |
|
80
|
|
|
$results = []; |
|
81
|
|
|
foreach ($rows as $row) { |
|
82
|
|
|
$results[] = $this->createFromRow($row); |
|
83
|
|
|
} |
|
84
|
|
|
return $results; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function create(DashboardPreset $dashboardPreset, int $userId, string $title = ''): ?Dashboard |
|
88
|
|
|
{ |
|
89
|
|
|
$widgets = []; |
|
90
|
|
|
$title = $title ?: $dashboardPreset->getTitle(); |
|
91
|
|
|
|
|
92
|
|
|
foreach ($dashboardPreset->getDefaultWidgets() as $widget) { |
|
93
|
|
|
$hash = sha1($widget . '-' . time()); |
|
94
|
|
|
$widgets[$hash] = ['identifier' => $widget]; |
|
95
|
|
|
} |
|
96
|
|
|
$identifier = sha1($dashboardPreset->getIdentifier() . '-' . time()); |
|
97
|
|
|
$this->getQueryBuilder() |
|
98
|
|
|
->insert(self::TABLE) |
|
99
|
|
|
->values([ |
|
100
|
|
|
'identifier' => $identifier, |
|
101
|
|
|
'title' => $title, |
|
102
|
|
|
'tstamp' => time(), |
|
103
|
|
|
'crdate' => time(), |
|
104
|
|
|
'cruser_id' => $userId, |
|
105
|
|
|
'widgets' => json_encode($widgets) |
|
106
|
|
|
]) |
|
107
|
|
|
->execute(); |
|
108
|
|
|
return $this->getDashboardByIdentifier($identifier); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param string $identifier |
|
113
|
|
|
* @param array $values |
|
114
|
|
|
* @return Statement|int |
|
115
|
|
|
*/ |
|
116
|
|
|
public function updateDashboardSettings(string $identifier, array $values) |
|
117
|
|
|
{ |
|
118
|
|
|
$checkedValues = $this->checkAllowedFields($values); |
|
119
|
|
|
|
|
120
|
|
|
if (empty($checkedValues)) { |
|
121
|
|
|
return null; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$queryBuilder = $this->getQueryBuilder(); |
|
125
|
|
|
$queryBuilder->update(self::TABLE) |
|
126
|
|
|
->where( |
|
127
|
|
|
$queryBuilder->expr()->eq( |
|
128
|
|
|
'identifier', |
|
129
|
|
|
$queryBuilder->createNamedParameter($identifier) |
|
130
|
|
|
) |
|
131
|
|
|
); |
|
132
|
|
|
|
|
133
|
|
|
foreach ($checkedValues as $field => $value) { |
|
134
|
|
|
$queryBuilder->set($field, $value); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $queryBuilder->execute(); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param array $values |
|
142
|
|
|
* @return array |
|
143
|
|
|
*/ |
|
144
|
|
|
protected function checkAllowedFields($values): array |
|
145
|
|
|
{ |
|
146
|
|
|
$allowedFields = []; |
|
147
|
|
|
foreach ($values as $field => $value) { |
|
148
|
|
|
if (!empty($value) && in_array((string)$field, $this->allowedFields, true)) { |
|
149
|
|
|
$allowedFields[$field] = $value; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return $allowedFields; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param string $identifier |
|
158
|
|
|
* @return Dashboard |
|
159
|
|
|
*/ |
|
160
|
|
|
public function getDashboardByIdentifier(string $identifier): ?Dashboard |
|
161
|
|
|
{ |
|
162
|
|
|
$queryBuilder = $this->getQueryBuilder(); |
|
163
|
|
|
$row = $queryBuilder |
|
164
|
|
|
->select('*') |
|
165
|
|
|
->from(self::TABLE) |
|
166
|
|
|
->where($queryBuilder->expr()->eq('identifier', $queryBuilder->createNamedParameter($identifier))) |
|
167
|
|
|
->execute() |
|
168
|
|
|
->fetchAll(); |
|
169
|
|
|
if (count($row)) { |
|
170
|
|
|
return $this->createFromRow($row[0]); |
|
171
|
|
|
} |
|
172
|
|
|
return null; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @param Dashboard $dashboard |
|
177
|
|
|
* @param string[] $widgets |
|
178
|
|
|
*/ |
|
179
|
|
|
public function updateWidgetConfig(Dashboard $dashboard, array $widgets): void |
|
180
|
|
|
{ |
|
181
|
|
|
$queryBuilder = $this->getQueryBuilder(); |
|
182
|
|
|
$queryBuilder |
|
183
|
|
|
->update(self::TABLE) |
|
184
|
|
|
->set('widgets', json_encode($widgets)) |
|
185
|
|
|
->where($queryBuilder->expr()->eq('identifier', $queryBuilder->createNamedParameter($dashboard->getIdentifier()))) |
|
186
|
|
|
->execute(); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @param Dashboard $dashboard |
|
191
|
|
|
*/ |
|
192
|
|
|
public function delete(Dashboard $dashboard): void |
|
193
|
|
|
{ |
|
194
|
|
|
$queryBuilder = $this->getQueryBuilder(); |
|
195
|
|
|
$queryBuilder |
|
196
|
|
|
->update(self::TABLE) |
|
197
|
|
|
->set('deleted', 1) |
|
198
|
|
|
->where($queryBuilder->expr()->eq('identifier', $queryBuilder->createNamedParameter($dashboard->getIdentifier()))) |
|
199
|
|
|
->execute(); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @param array $row |
|
204
|
|
|
* @return Dashboard |
|
205
|
|
|
*/ |
|
206
|
|
|
protected function createFromRow(array $row): Dashboard |
|
207
|
|
|
{ |
|
208
|
|
|
return GeneralUtility::makeInstance( |
|
209
|
|
|
Dashboard::class, |
|
210
|
|
|
$row['identifier'], |
|
211
|
|
|
$row['title'], |
|
212
|
|
|
json_decode((string)$row['widgets'], true) ?? [], |
|
213
|
|
|
$this->widgetRegistry, |
|
214
|
|
|
$this->container |
|
215
|
|
|
); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @return QueryBuilder |
|
220
|
|
|
*/ |
|
221
|
|
|
protected function getQueryBuilder(): QueryBuilder |
|
222
|
|
|
{ |
|
223
|
|
|
return $this->connectionPool->getQueryBuilderForTable(self::TABLE); |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|