-
Notifications
You must be signed in to change notification settings - Fork 346
Expand file tree
/
Copy pathLabelMapper.php
More file actions
202 lines (179 loc) · 6.34 KB
/
Copy pathLabelMapper.php
File metadata and controls
202 lines (179 loc) · 6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Deck\Db;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\Entity;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
/** @template-extends DeckMapper<Label> */
class LabelMapper extends DeckMapper implements IPermissionMapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'deck_labels', Label::class);
}
/**
* @return Label[]
* @throws \OCP\DB\Exception
*/
public function findAll(int $boardId, ?int $limit = null, int $offset = 0): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where($qb->expr()->eq('board_id', $qb->createNamedParameter($boardId, IQueryBuilder::PARAM_INT)))
->setMaxResults($limit)
->setFirstResult($offset);
$labels = $this->findEntities($qb);
// manual order first (see LabelService::reorder), NULLs last, then id for stability
usort($labels, static function (Label $a, Label $b): int {
$aOrder = $a->getOrder();
$bOrder = $b->getOrder();
if ($aOrder !== null && $bOrder !== null && $aOrder !== $bOrder) {
return $aOrder <=> $bOrder;
}
if (($aOrder === null) !== ($bOrder === null)) {
return $aOrder === null ? 1 : -1;
}
return $a->getId() <=> $b->getId();
});
return $labels;
}
/**
* Check if a label with the given title already exists on the board.
* Pass $excludeId to skip a specific label (used during updates).
*
* @throws \OCP\DB\Exception
*/
public function existsByBoardIdAndTitle(int $boardId, string $title, ?int $excludeId = null): bool {
$qb = $this->db->getQueryBuilder();
$qb->select('id')
->from($this->getTableName())
->where($qb->expr()->eq('board_id', $qb->createNamedParameter($boardId, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('title', $qb->createNamedParameter($title, IQueryBuilder::PARAM_STR)))
->setMaxResults(1);
if ($excludeId !== null) {
$qb->andWhere($qb->expr()->neq('id', $qb->createNamedParameter($excludeId, IQueryBuilder::PARAM_INT)));
}
$cursor = $qb->executeQuery();
$exists = $cursor->fetch() !== false;
$cursor->closeCursor();
return $exists;
}
public function delete(Entity $entity): Entity {
// delete assigned labels
$this->deleteLabelAssignments($entity->getId());
// delete label
return parent::delete($entity);
}
/**
* @return Label[]
* @throws \OCP\DB\Exception
*/
public function findAssignedLabelsForCard(int $cardId, ?int $limit = null, int $offset = 0): array {
$qb = $this->db->getQueryBuilder();
$qb->select('l.*', 'card_id')
->from($this->getTableName(), 'l')
->innerJoin('l', 'deck_assigned_labels', 'al', 'l.id = al.label_id')
->where($qb->expr()->eq('card_id', $qb->createNamedParameter($cardId, IQueryBuilder::PARAM_INT)))
->orderBy('l.id')
->setMaxResults($limit)
->setFirstResult($offset);
return $this->findEntities($qb);
}
public function findAssignedLabelsForCards(array $cardIds, ?int $limit = null, int $offset = 0): array {
$qb = $this->db->getQueryBuilder();
$qb->select('l.*', 'card_id')
->from($this->getTableName(), 'l')
->innerJoin('l', 'deck_assigned_labels', 'al', 'l.id = al.label_id')
->where($qb->expr()->in('card_id', $qb->createNamedParameter($cardIds, IQueryBuilder::PARAM_INT_ARRAY)))
->orderBy('l.id')
->setMaxResults($limit)
->setFirstResult($offset);
return $this->findEntities($qb);
}
/**
* @return Label[]
* @throws \OCP\DB\Exception
*/
public function findAssignedLabelsForBoard(int $boardId, ?int $limit = null, int $offset = 0): array {
$qb = $this->db->getQueryBuilder();
$qb->select('l.id as id', 'l.title as title', 'l.color as color')
->selectAlias('l.order', 'order')
->selectAlias('c.id', 'card_id')
->from($this->getTableName(), 'l')
->innerJoin('l', 'deck_assigned_labels', 'al', 'al.label_id = l.id')
->innerJoin('l', 'deck_cards', 'c', 'al.card_id = c.id')
->where($qb->expr()->eq('board_id', $qb->createNamedParameter($boardId, IQueryBuilder::PARAM_INT)))
->orderBy('l.id')
->setMaxResults($limit)
->setFirstResult($offset);
return $this->findEntities($qb);
}
public function insert(Entity $entity): Entity {
if (!isset($entity->getUpdatedFields()['lastModified'])) {
$entity->setLastModified(time());
}
return parent::insert($entity);
}
public function update(Entity $entity, bool $updateModified = true): Entity {
if ($updateModified) {
$entity->setLastModified(time());
}
return parent::update($entity);
}
/**
* @return array<int, list<Label>>
* @throws \OCP\DB\Exception
*/
public function getAssignedLabelsForBoard(int $boardId): array {
$labels = $this->findAssignedLabelsForBoard($boardId);
$result = [];
foreach ($labels as $label) {
if (!array_key_exists($label->getCardId(), $result)) {
$result[$label->getCardId()] = [];
}
$result[$label->getCardId()][] = $label;
}
return $result;
}
/**
* @throws \OCP\DB\Exception
*/
public function deleteLabelAssignments(int $labelId): void {
$qb = $this->db->getQueryBuilder();
$qb->delete('deck_assigned_labels')
->where($qb->expr()->eq('label_id', $qb->createNamedParameter($labelId, IQueryBuilder::PARAM_INT)));
$qb->executeStatement();
}
/**
* @throws \OCP\DB\Exception
*/
public function deleteLabelAssignmentsForCard(int $cardId): void {
$qb = $this->db->getQueryBuilder();
$qb->delete('deck_assigned_labels')
->where($qb->expr()->eq('card_id', $qb->createNamedParameter($cardId, IQueryBuilder::PARAM_INT)));
$qb->executeStatement();
}
/**
* @throws \OCP\DB\Exception
*/
public function isOwner(string $userId, int $id): bool {
$qb = $this->db->getQueryBuilder();
$qb->select('l.id')
->from($this->getTableName(), 'l')
->innerJoin('l', 'deck_boards', 'b', 'l.board_id = b.id')
->where($qb->expr()->eq('l.id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('b.owner', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)));
return count($qb->executeQuery()->fetchAll()) > 0;
}
public function findBoardId(int $id): ?int {
try {
$entity = $this->find($id);
return $entity->getBoardId();
} catch (DoesNotExistException|MultipleObjectsReturnedException) {
return null;
}
}
}