1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @author Vinzenz Rosenkranz <[email protected]> |
6
|
|
|
* @author René Gieling <[email protected]> |
7
|
|
|
* |
8
|
|
|
* @license GNU AGPL version 3 or any later version |
9
|
|
|
* |
10
|
|
|
* This program is free software: you can redistribute it and/or modify |
11
|
|
|
* it under the terms of the GNU Affero General Public License as |
12
|
|
|
* published by the Free Software Foundation, either version 3 of the |
13
|
|
|
* License, or (at your option) any later version. |
14
|
|
|
* |
15
|
|
|
* This program is distributed in the hope that it will be useful, |
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
* GNU Affero General Public License for more details. |
19
|
|
|
* |
20
|
|
|
* You should have received a copy of the GNU Affero General Public License |
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace OCA\Polls\Db; |
26
|
|
|
|
27
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder; |
28
|
|
|
use OCP\IDBConnection; |
29
|
|
|
use OCP\AppFramework\Db\QBMapper; |
30
|
|
|
use Doctrine\DBAL\Exception\TableNotFoundException; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @template-extends QBMapper<Subscription> |
34
|
|
|
*/ |
35
|
|
|
class SubscriptionMapper extends QBMapper { |
36
|
|
|
public function __construct(IDBConnection $db) { |
37
|
1 |
|
parent::__construct($db, 'polls_notif', '\OCA\Polls\Db\Subscription'); |
38
|
1 |
|
} |
39
|
1 |
|
|
40
|
|
|
/** |
41
|
|
|
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found |
42
|
|
|
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result |
43
|
|
|
* @return Subscription[] |
44
|
|
|
*/ |
45
|
|
|
public function findAll() { |
46
|
|
|
$qb = $this->db->getQueryBuilder(); |
47
|
|
|
|
48
|
|
|
$qb->select('*') |
49
|
|
|
->from($this->getTableName()); |
50
|
|
|
|
51
|
|
|
return $this->findEntities($qb); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found |
56
|
|
|
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result |
57
|
|
|
* @return Subscription[] |
58
|
|
|
*/ |
59
|
|
|
public function findAllByPoll(int $pollId) { |
60
|
|
|
$qb = $this->db->getQueryBuilder(); |
61
|
|
|
|
62
|
|
|
$qb->select('*') |
63
|
|
|
->from($this->getTableName()) |
64
|
|
|
->where( |
65
|
|
|
$qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT)) |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
return $this->findEntities($qb); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found |
73
|
|
|
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result |
74
|
|
|
* @return Subscription |
75
|
|
|
*/ |
76
|
|
|
public function findByPollAndUser(int $pollId, $userId) { |
77
|
|
|
$qb = $this->db->getQueryBuilder(); |
78
|
|
|
|
79
|
|
|
$qb->select('*') |
80
|
|
|
->from($this->getTableName()) |
81
|
|
|
->where( |
82
|
|
|
$qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT)) |
83
|
|
|
) |
84
|
|
|
->andWhere( |
85
|
|
|
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)) |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
return $this->findEntity($qb); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function unsubscribe($pollId, $userId): void { |
92
|
|
|
$qb = $this->db->getQueryBuilder(); |
93
|
|
|
|
94
|
|
|
$qb->delete($this->getTableName()) |
95
|
|
|
->where( |
96
|
|
|
$qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT)) |
97
|
|
|
) |
98
|
|
|
->andWhere( |
99
|
|
|
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)) |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
$qb->execute(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
|
|
public function removeDuplicates() { |
109
|
|
|
try { |
110
|
|
|
// remove duplicates from oc_polls_share |
111
|
|
|
// preserve the first entry |
112
|
|
|
$query = $this->db->getQueryBuilder(); |
113
|
|
|
$query->select('id', 'poll_id', 'user_id') |
114
|
|
|
->from($this->getTableName()); |
115
|
|
|
$foundEntries = $query->execute(); |
116
|
|
|
|
117
|
|
|
$delete = $this->db->getQueryBuilder(); |
118
|
|
|
$delete->delete($this->getTableName())->where('id = :id'); |
119
|
|
|
|
120
|
|
|
$entries2Keep = []; |
121
|
|
|
|
122
|
|
|
while ($row = $foundEntries->fetch()) { |
123
|
|
|
$currentRecord = [ |
124
|
|
|
$row['poll_id'], |
125
|
|
|
$row['user_id'] |
126
|
|
|
]; |
127
|
|
|
|
128
|
|
|
if (in_array($currentRecord, $entries2Keep)) { |
129
|
|
|
$delete->setParameter('id', $row['id']); |
130
|
|
|
$delete->execute(); |
131
|
|
|
} else { |
132
|
|
|
$entries2Keep[] = $currentRecord; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} catch (TableNotFoundException $e) { |
136
|
|
|
// ignore |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return void |
142
|
|
|
*/ |
143
|
|
|
public function deleteByUserId(string $userId): void { |
144
|
|
|
$query = $this->db->getQueryBuilder(); |
145
|
|
|
$query->delete($this->getTableName()) |
146
|
|
|
->where('user_id = :userId') |
147
|
|
|
->setParameter('userId', $userId); |
148
|
|
|
$query->execute(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
} |
153
|
|
|
|