‪TYPO3CMS  10.4
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 Doctrine\DBAL\Driver\Statement;
21 use Psr\Container\ContainerInterface;
26 
31 {
32  private const ‪TABLE = 'be_dashboards';
33 
37  protected ‪$allowedFields = ['title'];
38 
42  protected ‪$connectionPool;
43 
47  protected ‪$widgetRegistry;
48 
52  protected ‪$container;
53 
57  protected ‪$widgets = [];
58 
59  public function ‪__construct(
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 
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 
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 
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 
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 
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 
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 
221  protected function ‪getQueryBuilder(): ‪QueryBuilder
222  {
223  return $this->connectionPool->getQueryBuilderForTable(self::TABLE);
224  }
225 }
‪TYPO3\CMS\Dashboard\DashboardPreset
Definition: DashboardPreset.php:26
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\values
‪QueryBuilder values(array $values, bool $createNamedParameters=true)
Definition: QueryBuilder.php:775
‪TYPO3\CMS\Dashboard\DashboardRepository\updateWidgetConfig
‪updateWidgetConfig(Dashboard $dashboard, array $widgets)
Definition: DashboardRepository.php:174
‪TYPO3\CMS\Dashboard\DashboardRepository\createFromRow
‪Dashboard createFromRow(array $row)
Definition: DashboardRepository.php:201
‪TYPO3\CMS\Dashboard\DashboardRepository
Definition: DashboardRepository.php:31
‪TYPO3\CMS\Dashboard\DashboardRepository\$allowedFields
‪string[] $allowedFields
Definition: DashboardRepository.php:36
‪TYPO3\CMS\Dashboard\Widgets\WidgetInterface
Definition: WidgetInterface.php:26
‪TYPO3\CMS\Dashboard\DashboardRepository\checkAllowedFields
‪array checkAllowedFields($values)
Definition: DashboardRepository.php:139
‪TYPO3\CMS\Dashboard\Dashboard
Definition: Dashboard.php:29
‪TYPO3\CMS\Dashboard\DashboardRepository\$connectionPool
‪ConnectionPool $connectionPool
Definition: DashboardRepository.php:40
‪TYPO3\CMS\Dashboard\DashboardPreset\getIdentifier
‪string getIdentifier()
Definition: DashboardPreset.php:70
‪TYPO3\CMS\Dashboard\DashboardRepository\$widgetRegistry
‪WidgetRegistry $widgetRegistry
Definition: DashboardRepository.php:44
‪TYPO3\CMS\Core\Database\Query\QueryBuilder
Definition: QueryBuilder.php:52
‪TYPO3\CMS\Dashboard\DashboardRepository\$widgets
‪WidgetInterface[] $widgets
Definition: DashboardRepository.php:52
‪TYPO3\CMS\Dashboard\WidgetRegistry
Definition: WidgetRegistry.php:31
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\execute
‪Statement Doctrine DBAL ForwardCompatibility Result Doctrine DBAL Driver ResultStatement int execute()
Definition: QueryBuilder.php:204
‪TYPO3\CMS\Dashboard\Dashboard\getIdentifier
‪string getIdentifier()
Definition: Dashboard.php:71
‪TYPO3\CMS\Dashboard\DashboardPreset\getDefaultWidgets
‪string[] getDefaultWidgets()
Definition: DashboardPreset.php:101
‪TYPO3\CMS\Dashboard\DashboardPreset\getTitle
‪string getTitle()
Definition: DashboardPreset.php:86
‪TYPO3\CMS\Dashboard\DashboardRepository\getDashboardsForUser
‪getDashboardsForUser(int $userId)
Definition: DashboardRepository.php:64
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\insert
‪QueryBuilder insert(string $insert)
Definition: QueryBuilder.php:515
‪TYPO3\CMS\Dashboard\DashboardRepository\getQueryBuilder
‪QueryBuilder getQueryBuilder()
Definition: DashboardRepository.php:216
‪TYPO3\CMS\Dashboard\DashboardRepository\$container
‪ContainerInterface $container
Definition: DashboardRepository.php:48
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Dashboard\DashboardRepository\getDashboardByIdentifier
‪Dashboard getDashboardByIdentifier(string $identifier)
Definition: DashboardRepository.php:155
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Dashboard\DashboardRepository\__construct
‪__construct(ConnectionPool $connectionPool, WidgetRegistry $widgetRegistry, ContainerInterface $container)
Definition: DashboardRepository.php:54
‪TYPO3\CMS\Dashboard\DashboardRepository\TABLE
‪const TABLE
Definition: DashboardRepository.php:32
‪TYPO3\CMS\Dashboard\DashboardRepository\create
‪create(DashboardPreset $dashboardPreset, int $userId, string $title='')
Definition: DashboardRepository.php:82
‪TYPO3\CMS\Dashboard\DashboardRepository\updateDashboardSettings
‪Statement int updateDashboardSettings(string $identifier, array $values)
Definition: DashboardRepository.php:111
‪TYPO3\CMS\Dashboard