1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Nextcloud - ownnote |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2015, Ben Curtis <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2017, Sander Brand ([email protected]) |
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\OwnNote\Db; |
25
|
|
|
|
26
|
|
|
use \OCA\OwnNote\Utility\Utils; |
27
|
|
|
use OCP\AppFramework\Db\Entity; |
28
|
|
|
use OCP\IDBConnection; |
29
|
|
|
use OCP\AppFramework\Db\Mapper; |
30
|
|
|
|
31
|
|
|
class OwnNoteMapper extends Mapper { |
32
|
|
|
private $utils; |
33
|
|
|
|
34
|
|
|
public function __construct(IDBConnection $db, Utils $utils) { |
35
|
|
|
parent::__construct($db, 'ownnote'); |
36
|
|
|
$this->utils = $utils; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param $note_id |
42
|
|
|
* @param null $user_id |
43
|
|
|
* @return OwnNote if not found |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function find($note_id, $user_id = null) { |
|
|
|
|
46
|
|
|
$params = [$note_id]; |
47
|
|
|
$uidSql = ''; |
48
|
|
|
if($user_id){ |
49
|
|
|
$params[] = $user_id; |
50
|
|
|
$uidSql = 'and n.uid = ?'; |
51
|
|
|
} |
52
|
|
|
$sql = "SELECT n.id, n.uid, n.name, n.grouping, n.shared, n.mtime, n.deleted, p.pid, GROUP_CONCAT(p.note SEPARATOR '') as note FROM *PREFIX*ownnote n INNER JOIN *PREFIX*ownnote_parts p ON n.id = p.id WHERE n.id= ? $uidSql and n.deleted = 0 GROUP BY p.id"; |
53
|
|
|
$results = []; |
54
|
|
|
foreach($this->execute($sql, $params)->fetchAll() as $item){ |
55
|
|
|
$note = new OwnNote(); |
56
|
|
|
$note->setId($item['id']); |
57
|
|
|
$note->setName($item['name']); |
58
|
|
|
$note->setGrouping($item['grouping']); |
59
|
|
|
$note->setMtime($item['mtime']); |
60
|
|
|
$note->setDeleted($item['deleted']); |
61
|
|
|
$note->setNote($item['note']); |
62
|
|
|
$note->setUid($item['uid']); |
63
|
|
|
$results[] = $note; |
64
|
|
|
} |
65
|
|
|
return array_shift($results); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found |
71
|
|
|
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result |
72
|
|
|
* @return OwnNote[] |
73
|
|
|
*/ |
74
|
|
|
public function findNotesFromUser($userId) { |
75
|
|
|
$params = [$userId]; |
76
|
|
|
$sql = "SELECT n.uid, n.id, n.name, n.grouping, n.shared, n.mtime, n.deleted, p.pid, GROUP_CONCAT(p.note SEPARATOR '') as note FROM *PREFIX*ownnote n INNER JOIN *PREFIX*ownnote_parts p ON n.id = p.id WHERE `uid` = ? and n.deleted = 0 GROUP BY p.id"; |
77
|
|
|
$results = []; |
78
|
|
|
foreach($this->execute($sql, $params)->fetchAll() as $item){ |
79
|
|
|
$note = new OwnNote(); |
80
|
|
|
$note->setId($item['id']); |
81
|
|
|
$note->setName($item['name']); |
82
|
|
|
$note->setGrouping($item['grouping']); |
83
|
|
|
$note->setMtime($item['mtime']); |
84
|
|
|
$note->setDeleted($item['deleted']); |
85
|
|
|
$note->setNote($item['note']); |
86
|
|
|
$note->setUid($item['uid']); |
87
|
|
|
$results[] = $note; |
88
|
|
|
} |
89
|
|
|
return $results; |
90
|
|
|
} |
91
|
|
|
/** |
92
|
|
|
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found |
93
|
|
|
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result |
94
|
|
|
* @return OwnNote[] |
95
|
|
|
*/ |
96
|
|
View Code Duplication |
public function findNotesByGroup($group, $userId) { |
|
|
|
|
97
|
|
|
$params = [$group]; |
98
|
|
|
$uidSql = ''; |
99
|
|
|
if($userId){ |
100
|
|
|
$params[] = $userId; |
101
|
|
|
$uidSql = 'and n.uid = ?'; |
102
|
|
|
} |
103
|
|
|
$sql = "SELECT n.uid, n.id, n.name, n.grouping, n.shared, n.mtime, n.deleted, p.pid, GROUP_CONCAT(p.note SEPARATOR '') as note FROM *PREFIX*ownnote n INNER JOIN *PREFIX*ownnote_parts p ON n.id = p.id WHERE n.deleted = 0 $uidSql and n.grouping = ? GROUP BY p.id"; |
104
|
|
|
$results = []; |
105
|
|
|
foreach($this->execute($sql, $params)->fetchAll() as $item){ |
106
|
|
|
$note = new OwnNote(); |
107
|
|
|
$note->setId($item['id']); |
108
|
|
|
$note->setName($item['name']); |
109
|
|
|
$note->setGrouping($item['grouping']); |
110
|
|
|
$note->setMtime($item['mtime']); |
111
|
|
|
$note->setDeleted($item['deleted']); |
112
|
|
|
$note->setNote($item['note']); |
113
|
|
|
$note->setUid($item['uid']); |
114
|
|
|
$results[] = $note; |
115
|
|
|
} |
116
|
|
|
return $results; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Creates a note |
121
|
|
|
* |
122
|
|
|
* @param OwnNote $note |
123
|
|
|
* @return OwnNote|Entity |
124
|
|
|
* @internal param $userId |
125
|
|
|
*/ |
126
|
|
View Code Duplication |
public function create($note) { |
|
|
|
|
127
|
|
|
$parts = $this->utils->splitContent($note->getNote()); |
128
|
|
|
$note->setNote(''); |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @var $note OwnNote |
132
|
|
|
*/ |
133
|
|
|
$note = parent::insert($note); |
|
|
|
|
134
|
|
|
|
135
|
|
|
foreach ($parts as $part) { |
136
|
|
|
$this->createNotePart($note, $part); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$note->setNote(implode('', $parts)); |
140
|
|
|
|
141
|
|
|
return $note; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Update note |
146
|
|
|
* |
147
|
|
|
* @param OwnNote $note |
148
|
|
|
* @return OwnNote|Entity |
149
|
|
|
*/ |
150
|
|
View Code Duplication |
public function updateNote($note) { |
|
|
|
|
151
|
|
|
$parts = $this->utils->splitContent($note->getNote()); |
152
|
|
|
$this->deleteNoteParts($note); |
153
|
|
|
|
154
|
|
|
foreach ($parts as $part) { |
155
|
|
|
$this->createNotePart($note, $part); |
156
|
|
|
} |
157
|
|
|
$note->setNote(''); |
158
|
|
|
/** |
159
|
|
|
* @var $note OwnNote |
160
|
|
|
*/ |
161
|
|
|
$note = parent::update($note); |
|
|
|
|
162
|
|
|
$note->setNote(implode('', $parts)); |
163
|
|
|
return $note; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param OwnNote $note |
168
|
|
|
* @param $content |
169
|
|
|
*/ |
170
|
|
|
public function createNotePart(OwnNote $note, $content) { |
171
|
|
|
$sql = "INSERT INTO *PREFIX*ownnote_parts VALUES (NULL, ?, ?);"; |
172
|
|
|
$this->execute($sql, array($note->getId(), $content)); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Delete the note parts |
177
|
|
|
* |
178
|
|
|
* @param OwnNote $note |
179
|
|
|
*/ |
180
|
|
|
public function deleteNoteParts(OwnNote $note) { |
181
|
|
|
$sql = 'DELETE FROM *PREFIX*ownnote_parts where id = ?'; |
182
|
|
|
$this->execute($sql, array($note->getId())); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param OwnNote $note |
187
|
|
|
* @return bool |
188
|
|
|
*/ |
189
|
|
|
public function deleteNote(OwnNote $note) { |
190
|
|
|
$this->deleteNoteParts($note); |
191
|
|
|
$this->delete($note); |
192
|
|
|
return true; |
193
|
|
|
} |
194
|
|
|
} |
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.