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\IDBConnection; |
28
|
|
|
use OCP\AppFramework\Db\QBMapper; |
29
|
|
|
use Doctrine\DBAL\Exception\TableNotFoundException; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @template-extends QBMapper<Preferences> |
33
|
|
|
*/ |
34
|
|
|
class PreferencesMapper extends QBMapper { |
35
|
|
|
public function __construct(IDBConnection $db) { |
36
|
|
|
parent::__construct($db, 'polls_preferences', '\OCA\Polls\Db\Preferences'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found |
41
|
|
|
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result |
42
|
|
|
* @return Preferences |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
public function find($userId): Preferences { |
46
|
|
|
$qb = $this->db->getQueryBuilder(); |
47
|
|
|
|
48
|
|
|
$qb->select('*') |
49
|
|
|
->from($this->getTableName()) |
50
|
|
|
->where( |
51
|
|
|
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId)) |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
return $this->findEntity($qb); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
public function removeDuplicates() { |
61
|
|
|
try { |
62
|
|
|
$query = $this->db->getQueryBuilder(); |
63
|
|
|
$query->delete($this->getTableName()) |
64
|
|
|
->where('user_id = :userId') |
65
|
|
|
->setParameter('userId', ''); |
66
|
|
|
$query->execute(); |
67
|
|
|
|
68
|
|
|
// remove duplicate preferences from oc_polls_preferences |
69
|
|
|
// preserve the last user setting in the db |
70
|
|
|
$query = $this->db->getQueryBuilder(); |
71
|
|
|
$query->select('id', 'user_id') |
72
|
|
|
->from($this->getTableName()); |
73
|
|
|
$users = $query->execute(); |
74
|
|
|
|
75
|
|
|
$delete = $this->db->getQueryBuilder(); |
76
|
|
|
$delete->delete($this->getTableName()) |
77
|
|
|
->where('id = :id'); |
78
|
|
|
|
79
|
|
|
$userskeep = []; |
80
|
|
|
|
81
|
|
|
while ($row = $users->fetch()) { |
82
|
|
|
if (in_array($row['user_id'], $userskeep)) { |
83
|
|
|
$delete->setParameter('id', $row['id']); |
84
|
|
|
$delete->execute(); |
85
|
|
|
} else { |
86
|
|
|
$userskeep[] = $row['user_id']; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} catch (TableNotFoundException $e) { |
90
|
|
|
// ignore |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
|
|
public function deleteByUserId($userId){ |
98
|
|
|
$query = $this->db->getQueryBuilder(); |
99
|
|
|
$query->delete($this->getTableName()) |
100
|
|
|
->where('user_id = :userId') |
101
|
|
|
->setParameter('userId', $userId); |
102
|
|
|
$query->execute(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|