1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
5
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace eZ\Publish\Core\Persistence\Legacy\UserPreference\Gateway; |
10
|
|
|
|
11
|
|
|
use Doctrine\DBAL\Connection; |
12
|
|
|
use Doctrine\DBAL\FetchMode; |
13
|
|
|
use Doctrine\DBAL\ParameterType; |
14
|
|
|
use eZ\Publish\Core\Persistence\Legacy\UserPreference\Gateway; |
15
|
|
|
use eZ\Publish\SPI\Persistence\UserPreference\UserPreferenceSetStruct; |
16
|
|
|
|
17
|
|
|
class DoctrineDatabase extends Gateway |
18
|
|
|
{ |
19
|
|
|
const TABLE_USER_PREFERENCES = 'ezpreferences'; |
20
|
|
|
|
21
|
|
|
const COLUMN_ID = 'id'; |
22
|
|
|
const COLUMN_NAME = 'name'; |
23
|
|
|
const COLUMN_USER_ID = 'user_id'; |
24
|
|
|
const COLUMN_VALUE = 'value'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \Doctrine\DBAL\Connection |
28
|
|
|
*/ |
29
|
|
|
protected $connection; |
30
|
|
|
|
31
|
|
|
public function __construct(Connection $connection) |
32
|
|
|
{ |
33
|
|
|
$this->connection = $connection; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
public function setUserPreference(UserPreferenceSetStruct $userPreference): int |
40
|
|
|
{ |
41
|
|
|
$query = $this->connection->createQueryBuilder(); |
42
|
|
|
|
43
|
|
|
$userPreferences = $this->getUserPreferenceByUserIdAndName($userPreference->userId, $userPreference->name); |
44
|
|
|
|
45
|
|
|
if (0 < count($userPreferences)) { |
46
|
|
|
$currentUserPreference = reset($userPreferences); |
47
|
|
|
$currentUserPreferenceId = (int)$currentUserPreference['id']; |
48
|
|
|
|
49
|
|
|
$query |
50
|
|
|
->update(self::TABLE_USER_PREFERENCES) |
51
|
|
|
->set(self::COLUMN_VALUE, ':value') |
52
|
|
|
->where($query->expr()->eq(self::COLUMN_ID, ':id')) |
53
|
|
|
->setParameter(':id', $currentUserPreferenceId, ParameterType::INTEGER) |
54
|
|
|
->setParameter(':value', $userPreference->value, ParameterType::STRING); |
55
|
|
|
|
56
|
|
|
$query->execute(); |
57
|
|
|
|
58
|
|
|
return $currentUserPreferenceId; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$query |
62
|
|
|
->insert(self::TABLE_USER_PREFERENCES) |
63
|
|
|
->values([ |
64
|
|
|
self::COLUMN_NAME => ':name', |
65
|
|
|
self::COLUMN_USER_ID => ':user_id', |
66
|
|
|
self::COLUMN_VALUE => ':value', |
67
|
|
|
]) |
68
|
|
|
->setParameter(':name', $userPreference->name, ParameterType::STRING) |
69
|
|
|
->setParameter(':user_id', $userPreference->userId, ParameterType::INTEGER) |
70
|
|
|
->setParameter(':value', $userPreference->value, ParameterType::STRING); |
71
|
|
|
|
72
|
|
|
$query->execute(); |
73
|
|
|
|
74
|
|
|
return (int) $this->connection->lastInsertId(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
View Code Duplication |
public function getUserPreferenceByUserIdAndName(int $userId, string $name): array |
78
|
|
|
{ |
79
|
|
|
$query = $this->connection->createQueryBuilder(); |
80
|
|
|
$query |
81
|
|
|
->select(...$this->getColumns()) |
82
|
|
|
->from(self::TABLE_USER_PREFERENCES) |
83
|
|
|
->where($query->expr()->eq(self::COLUMN_USER_ID, ':userId')) |
84
|
|
|
->andWhere($query->expr()->eq(self::COLUMN_NAME, ':name')); |
85
|
|
|
|
86
|
|
|
$query->setParameter(':userId', $userId, ParameterType::INTEGER); |
87
|
|
|
$query->setParameter(':name', $name, ParameterType::STRING); |
88
|
|
|
|
89
|
|
|
return $query->execute()->fetchAll(FetchMode::ASSOCIATIVE); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
public function loadUserPreferences(int $userId, int $offset = 0, int $limit = -1): array |
96
|
|
|
{ |
97
|
|
|
$query = $this->connection->createQueryBuilder(); |
98
|
|
|
$query |
99
|
|
|
->select(...$this->getColumns()) |
100
|
|
|
->from(self::TABLE_USER_PREFERENCES) |
101
|
|
|
->where($query->expr()->eq(self::COLUMN_USER_ID, ':user_id')) |
102
|
|
|
->setFirstResult($offset); |
103
|
|
|
|
104
|
|
|
if ($limit > 0) { |
105
|
|
|
$query->setMaxResults($limit); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$query->orderBy(self::COLUMN_ID, 'ASC'); |
109
|
|
|
$query->setParameter(':user_id', $userId, ParameterType::INTEGER); |
110
|
|
|
|
111
|
|
|
return $query->execute()->fetchAll(FetchMode::ASSOCIATIVE); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
|
View Code Duplication |
public function countUserPreferences(int $userId): int |
118
|
|
|
{ |
119
|
|
|
$query = $this->connection->createQueryBuilder(); |
120
|
|
|
$query |
121
|
|
|
->select( |
122
|
|
|
$this->connection->getDatabasePlatform()->getCountExpression(self::COLUMN_ID) |
123
|
|
|
) |
124
|
|
|
->from(self::TABLE_USER_PREFERENCES) |
125
|
|
|
->where($query->expr()->eq(self::COLUMN_USER_ID, ':user_id')) |
126
|
|
|
->setParameter(':user_id', $userId, ParameterType::INTEGER); |
127
|
|
|
|
128
|
|
|
return (int) $query->execute()->fetchColumn(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
private function getColumns(): array |
132
|
|
|
{ |
133
|
|
|
return [ |
134
|
|
|
self::COLUMN_ID, |
135
|
|
|
self::COLUMN_NAME, |
136
|
|
|
self::COLUMN_USER_ID, |
137
|
|
|
self::COLUMN_VALUE, |
138
|
|
|
]; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|