‪TYPO3CMS  ‪main
DashboardRepository.php
Go to the documentation of this file.
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 Psr\Container\ContainerInterface;
22 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
25 
30 {
31  private const ‪TABLE = 'be_dashboards';
32 
36  protected array ‪$allowedFields = ['title'];
37 
41  protected array ‪$widgets = [];
42 
43  public function ‪__construct(
44  protected readonly ‪ConnectionPool $connectionPool,
45  protected readonly ‪WidgetRegistry $widgetRegistry,
46  protected readonly ContainerInterface $container
47  ) {}
48 
49  public function ‪getDashboardsForUser(int $userId): array
50  {
51  $queryBuilder = $this->‪getQueryBuilder();
52  $rows = $queryBuilder
53  ->select('*')
54  ->from(self::TABLE)
55  ->where(
56  $queryBuilder->expr()->eq('cruser_id', $queryBuilder->createNamedParameter($userId))
57  )
58  ->executeQuery()
59  ->fetchAllAssociative();
60  $results = [];
61  foreach ($rows as $row) {
62  $results[] = $this->‪createFromRow($row);
63  }
64  return $results;
65  }
66 
67  public function ‪create(‪DashboardPreset $dashboardPreset, int $userId, string $title = ''): ?‪Dashboard
68  {
69  ‪$widgets = [];
70  $title = $title ?: $dashboardPreset->‪getTitle();
71 
72  foreach ($dashboardPreset->‪getDefaultWidgets() as $widget) {
73  $hash = sha1($widget . '-' . time());
74  ‪$widgets[$hash] = ['identifier' => $widget];
75  }
76  ‪$identifier = sha1($dashboardPreset->‪getIdentifier() . '-' . time());
77  $this->‪getQueryBuilder()
78  ->insert(self::TABLE)
79  ->values([
80  'identifier' => ‪$identifier,
81  'title' => $title,
82  'tstamp' => time(),
83  'crdate' => time(),
84  'cruser_id' => $userId,
85  'widgets' => json_encode(‪$widgets),
86  ])
87  ->executeStatement();
89  }
90 
94  public function ‪updateDashboardSettings(string ‪$identifier, array $values)
95  {
96  $checkedValues = $this->‪checkAllowedFields($values);
97 
98  if (empty($checkedValues)) {
99  return null;
100  }
101 
102  $queryBuilder = $this->‪getQueryBuilder();
103  $queryBuilder->update(self::TABLE)
104  ->where(
105  $queryBuilder->expr()->eq(
106  'identifier',
107  $queryBuilder->createNamedParameter(‪$identifier)
108  )
109  );
110 
111  foreach ($checkedValues as $field => $value) {
112  $queryBuilder->set($field, $value);
113  }
114 
115  return $queryBuilder->executeStatement();
116  }
117 
118  protected function ‪checkAllowedFields(array $values): array
119  {
120  ‪$allowedFields = [];
121  foreach ($values as $field => $value) {
122  if (!empty($value) && in_array((string)$field, $this->allowedFields, true)) {
123  ‪$allowedFields[$field] = $value;
124  }
125  }
126 
127  return ‪$allowedFields;
128  }
129 
131  {
132  $queryBuilder = $this->‪getQueryBuilder();
133  $row = $queryBuilder
134  ->select('*')
135  ->from(self::TABLE)
136  ->where($queryBuilder->expr()->eq('identifier', $queryBuilder->createNamedParameter(‪$identifier)))
137  ->executeQuery()
138  ->fetchAllAssociative();
139  if (count($row)) {
140  return $this->‪createFromRow($row[0]);
141  }
142  return null;
143  }
144 
148  public function ‪updateWidgetConfig(‪Dashboard $dashboard, array ‪$widgets): void
149  {
150  $queryBuilder = $this->‪getQueryBuilder();
151  $queryBuilder
152  ->update(self::TABLE)
153  ->set('widgets', json_encode(‪$widgets))
154  ->where($queryBuilder->expr()->eq('identifier', $queryBuilder->createNamedParameter($dashboard->‪getIdentifier())))
155  ->executeStatement();
156  }
157 
158  public function delete(‪Dashboard $dashboard): void
159  {
160  $queryBuilder = $this->‪getQueryBuilder();
161  $queryBuilder
162  ->update(self::TABLE)
163  ->set('deleted', 1)
164  ->where($queryBuilder->expr()->eq('identifier', $queryBuilder->createNamedParameter($dashboard->getIdentifier())))
165  ->executeStatement();
166  }
167 
168  protected function ‪createFromRow(array $row): ‪Dashboard
169  {
170  return GeneralUtility::makeInstance(
171  Dashboard::class,
172  $row['identifier'] ?? '',
173  $row['title'] ?? '',
174  json_decode((string)($row['widgets'] ?? ''), true) ?? [],
175  $this->widgetRegistry,
176  $this->container
177  );
178  }
179 
180  protected function ‪getQueryBuilder(): QueryBuilder
181  {
182  return $this->connectionPool->getQueryBuilderForTable(self::TABLE);
183  }
184 }
‪TYPO3\CMS\Dashboard\DashboardRepository\$allowedFields
‪array $allowedFields
Definition: DashboardRepository.php:36
‪TYPO3\CMS\Dashboard\DashboardRepository\__construct
‪__construct(protected readonly ConnectionPool $connectionPool, protected readonly WidgetRegistry $widgetRegistry, protected readonly ContainerInterface $container)
Definition: DashboardRepository.php:43
‪TYPO3\CMS\Dashboard\DashboardPreset
Definition: DashboardPreset.php:26
‪TYPO3\CMS\Dashboard\Dashboard\getIdentifier
‪getIdentifier()
Definition: Dashboard.php:49
‪TYPO3\CMS\Dashboard\DashboardRepository\updateWidgetConfig
‪updateWidgetConfig(Dashboard $dashboard, array $widgets)
Definition: DashboardRepository.php:148
‪TYPO3\CMS\Dashboard\DashboardPreset\getIdentifier
‪getIdentifier()
Definition: DashboardPreset.php:39
‪TYPO3\CMS\Dashboard\DashboardRepository
Definition: DashboardRepository.php:30
‪TYPO3\CMS\Dashboard\Widgets\WidgetInterface
Definition: WidgetInterface.php:26
‪TYPO3\CMS\Dashboard\DashboardPreset\getTitle
‪getTitle()
Definition: DashboardPreset.php:49
‪TYPO3\CMS\Dashboard\Dashboard
Definition: Dashboard.php:29
‪TYPO3\CMS\Dashboard\WidgetRegistry
Definition: WidgetRegistry.php:33
‪TYPO3\CMS\Dashboard\DashboardPreset\getDefaultWidgets
‪string[] getDefaultWidgets()
Definition: DashboardPreset.php:61
‪TYPO3\CMS\Dashboard\DashboardRepository\getDashboardsForUser
‪getDashboardsForUser(int $userId)
Definition: DashboardRepository.php:49
‪TYPO3\CMS\Dashboard\DashboardRepository\$widgets
‪array $widgets
Definition: DashboardRepository.php:41
‪TYPO3\CMS\Dashboard\DashboardRepository\createFromRow
‪createFromRow(array $row)
Definition: DashboardRepository.php:168
‪TYPO3\CMS\Dashboard\DashboardRepository\updateDashboardSettings
‪int null updateDashboardSettings(string $identifier, array $values)
Definition: DashboardRepository.php:94
‪TYPO3\CMS\Dashboard\DashboardRepository\getDashboardByIdentifier
‪getDashboardByIdentifier(string $identifier)
Definition: DashboardRepository.php:130
‪TYPO3\CMS\Dashboard\DashboardRepository\checkAllowedFields
‪checkAllowedFields(array $values)
Definition: DashboardRepository.php:118
‪TYPO3\CMS\Dashboard\DashboardRepository\getQueryBuilder
‪getQueryBuilder()
Definition: DashboardRepository.php:180
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Dashboard\DashboardRepository\TABLE
‪const TABLE
Definition: DashboardRepository.php:31
‪TYPO3\CMS\Dashboard\DashboardRepository\create
‪create(DashboardPreset $dashboardPreset, int $userId, string $title='')
Definition: DashboardRepository.php:67
‪TYPO3\CMS\Dashboard
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37