|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016 Julius Härtl <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @author Julius Härtl <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @license GNU AGPL version 3 or any later version |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
12
|
|
|
* License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace OCA\Deck\Db; |
|
25
|
|
|
|
|
26
|
|
|
use OCP\AppFramework\Db\Entity; |
|
27
|
|
|
use OCP\IDBConnection; |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
class CardMapper extends DeckMapper implements IPermissionMapper { |
|
31
|
|
|
|
|
32
|
|
|
private $labelMapper; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct(IDBConnection $db, LabelMapper $labelMapper) { |
|
35
|
|
|
parent::__construct($db, 'deck_cards', '\OCA\Deck\Db\Card'); |
|
36
|
|
|
$this->labelMapper = $labelMapper; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function insert(Entity $entity) { |
|
40
|
|
|
$entity->setCreatedAt(time()); |
|
41
|
|
|
$entity->setLastModified(time()); |
|
42
|
|
|
return parent::insert($entity); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function update(Entity $entity) { |
|
46
|
|
|
$entity->setLastModified(time()); |
|
47
|
|
|
return parent::update($entity); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param $id |
|
52
|
|
|
* @return RelationalEntity if not found |
|
53
|
|
|
*/ |
|
54
|
|
|
public function find($id) { |
|
55
|
|
|
$sql = 'SELECT * FROM `*PREFIX*deck_cards` ' . |
|
56
|
|
|
'WHERE `id` = ?'; |
|
57
|
|
|
$card = $this->findEntity($sql, [$id]); |
|
58
|
|
|
$labels = $this->labelMapper->findAssignedLabelsForCard($card->id); |
|
59
|
|
|
$card->setLabels($labels); |
|
60
|
|
|
return $card; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function findAll($stackId, $limit = null, $offset = null) { |
|
64
|
|
|
$sql = 'SELECT * FROM `*PREFIX*deck_cards` |
|
65
|
|
|
WHERE `stack_id` = ? AND NOT archived ORDER BY `order`'; |
|
66
|
|
|
$entities = $this->findEntities($sql, [$stackId], $limit, $offset); |
|
67
|
|
|
return $entities; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function findAllArchived($stackId, $limit = null, $offset = null) { |
|
71
|
|
|
$sql = 'SELECT * FROM `*PREFIX*deck_cards` WHERE `stack_id`=? AND archived ORDER BY `last_modified`'; |
|
72
|
|
|
$entities = $this->findEntities($sql, [$stackId], $limit, $offset); |
|
73
|
|
|
return $entities; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function findAllByStack($stackId, $limit = null, $offset = null) { |
|
77
|
|
|
$sql = 'SELECT id FROM `*PREFIX*deck_cards` |
|
78
|
|
|
WHERE `stack_id` = ?'; |
|
79
|
|
|
$entities = $this->findEntities($sql, [$stackId], $limit, $offset); |
|
80
|
|
|
return $entities; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function delete(Entity $entity) { |
|
84
|
|
|
// delete assigned labels |
|
85
|
|
|
$this->labelMapper->deleteLabelAssignmentsForCard($entity->getId()); |
|
86
|
|
|
// delete card |
|
87
|
|
|
return parent::delete($entity); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function deleteByStack($stackId) { |
|
91
|
|
|
$cards = $this->findAllByStack($stackId); |
|
92
|
|
|
foreach ($cards as $card) { |
|
93
|
|
|
$this->delete($card); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
View Code Duplication |
public function assignLabel($card, $label) { |
|
|
|
|
|
|
99
|
|
|
$sql = 'INSERT INTO `*PREFIX*deck_assigned_labels` (`label_id`,`card_id`) VALUES (?,?)'; |
|
100
|
|
|
$stmt = $this->db->prepare($sql); |
|
101
|
|
|
$stmt->bindParam(1, $label, \PDO::PARAM_INT); |
|
102
|
|
|
$stmt->bindParam(2, $card, \PDO::PARAM_INT); |
|
103
|
|
|
$stmt->execute(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
View Code Duplication |
public function removeLabel($card, $label) { |
|
|
|
|
|
|
107
|
|
|
$sql = 'DELETE FROM `*PREFIX*deck_assigned_labels` WHERE card_id = ? AND label_id = ?'; |
|
108
|
|
|
$stmt = $this->db->prepare($sql); |
|
109
|
|
|
$stmt->bindParam(1, $card, \PDO::PARAM_INT); |
|
110
|
|
|
$stmt->bindParam(2, $label, \PDO::PARAM_INT); |
|
111
|
|
|
$stmt->execute(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
View Code Duplication |
public function isOwner($userId, $cardId) { |
|
|
|
|
|
|
115
|
|
|
$sql = 'SELECT owner FROM `*PREFIX*deck_boards` WHERE `id` IN (SELECT board_id FROM `*PREFIX*deck_stacks` WHERE id IN (SELECT stack_id FROM `*PREFIX*deck_cards` WHERE id = ?))'; |
|
116
|
|
|
$stmt = $this->execute($sql, [$cardId]); |
|
117
|
|
|
$row = $stmt->fetch(); |
|
118
|
|
|
return ($row['owner'] === $userId); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function findBoardId($cardId) { |
|
122
|
|
|
$sql = 'SELECT id FROM `*PREFIX*deck_boards` WHERE `id` IN (SELECT board_id FROM `*PREFIX*deck_stacks` WHERE id IN (SELECT stack_id FROM `*PREFIX*deck_cards` WHERE id = ?))'; |
|
123
|
|
|
$stmt = $this->execute($sql, [$cardId]); |
|
124
|
|
|
$row = $stmt->fetch(); |
|
125
|
|
|
return $row['id']; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.