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
|
|
|
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 id, uid, name, grouping, shared, mtime, deleted, note FROM *PREFIX*ownnote n WHERE n.id= ? $uidSql and n.deleted = 0"; |
53
|
|
|
$results = []; |
54
|
|
View Code Duplication |
foreach($this->execute($sql, $params)->fetchAll() as $item){ |
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var $note OwnNote |
57
|
|
|
*/ |
58
|
|
|
$note = $this->makeEntityFromDBResult($item); |
59
|
|
|
$results[] = $note; |
60
|
|
|
} |
61
|
|
|
return array_shift($results); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param $userId |
67
|
|
|
* @param int $deleted |
68
|
|
|
* @return OwnNote[] if not found |
69
|
|
|
*/ |
70
|
|
|
public function findNotesFromUser($userId, $deleted = 0) { |
71
|
|
|
$params = [$userId, $deleted]; |
72
|
|
|
$sql = "SELECT id, uid, name, grouping, shared, mtime, deleted, note FROM *PREFIX*ownnote n WHERE `uid` = ? and n.deleted = ?"; |
73
|
|
|
$results = []; |
74
|
|
View Code Duplication |
foreach($this->execute($sql, $params)->fetchAll() as $item){ |
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var $note OwnNote |
77
|
|
|
*/ |
78
|
|
|
$note = $this->makeEntityFromDBResult($item); |
79
|
|
|
$results[] = $note; |
80
|
|
|
} |
81
|
|
|
return $results; |
82
|
|
|
} |
83
|
|
|
/** |
84
|
|
|
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found |
85
|
|
|
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result |
86
|
|
|
* @return OwnNote[] |
87
|
|
|
*/ |
88
|
|
|
public function findNotesByGroup($group, $userId) { |
89
|
|
|
$params = [$group]; |
90
|
|
|
$uidSql = ''; |
91
|
|
|
if($userId){ |
92
|
|
|
$params[] = $userId; |
93
|
|
|
$uidSql = 'and n.uid = ?'; |
94
|
|
|
} |
95
|
|
|
$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"; |
96
|
|
|
$results = []; |
97
|
|
|
foreach($this->execute($sql, $params)->fetchAll() as $item){ |
98
|
|
|
$note = new OwnNote(); |
99
|
|
|
$note->setId($item['id']); |
100
|
|
|
$note->setName($item['name']); |
101
|
|
|
$note->setGrouping($item['grouping']); |
102
|
|
|
$note->setMtime($item['mtime']); |
103
|
|
|
$note->setDeleted($item['deleted']); |
104
|
|
|
$note->setNote($item['note']); |
105
|
|
|
$note->setUid($item['uid']); |
106
|
|
|
$results[] = $note; |
107
|
|
|
} |
108
|
|
|
return $results; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Creates a note |
113
|
|
|
* |
114
|
|
|
* @param OwnNote $note |
115
|
|
|
* @return OwnNote|Entity |
116
|
|
|
* @internal param $userId |
117
|
|
|
*/ |
118
|
|
View Code Duplication |
public function create($note) { |
119
|
|
|
$parts = $this->utils->splitContent($note->getNote()); |
120
|
|
|
$note->setNote(''); |
121
|
|
|
/** |
122
|
|
|
* @var $note OwnNote |
123
|
|
|
*/ |
124
|
|
|
$note = parent::insert($note); |
|
|
|
|
125
|
|
|
|
126
|
|
|
foreach ($parts as $part) { |
127
|
|
|
$this->createNotePart($note, $part); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$note->setNote(implode('', $parts)); |
131
|
|
|
|
132
|
|
|
return $note; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Update note |
137
|
|
|
* |
138
|
|
|
* @param OwnNote $note |
139
|
|
|
* @return OwnNote|Entity |
140
|
|
|
*/ |
141
|
|
View Code Duplication |
public function updateNote($note) { |
142
|
|
|
$parts = $this->utils->splitContent($note->getNote()); |
143
|
|
|
$this->deleteNoteParts($note); |
144
|
|
|
|
145
|
|
|
foreach ($parts as $part) { |
146
|
|
|
$this->createNotePart($note, $part); |
147
|
|
|
} |
148
|
|
|
$note->setNote(''); |
149
|
|
|
/** |
150
|
|
|
* @var $note OwnNote |
151
|
|
|
*/ |
152
|
|
|
$note = parent::update($note); |
|
|
|
|
153
|
|
|
$note->setNote(implode('', $parts)); |
154
|
|
|
return $note; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param OwnNote $note |
159
|
|
|
* @param $content |
160
|
|
|
*/ |
161
|
|
|
public function createNotePart(OwnNote $note, $content) { |
162
|
|
|
$sql = "INSERT INTO *PREFIX*ownnote_parts VALUES (NULL, ?, ?);"; |
163
|
|
|
$this->execute($sql, array($note->getId(), $content)); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Delete the note parts |
168
|
|
|
* |
169
|
|
|
* @param OwnNote $note |
170
|
|
|
*/ |
171
|
|
|
public function deleteNoteParts(OwnNote $note) { |
172
|
|
|
$sql = 'DELETE FROM *PREFIX*ownnote_parts where id = ?'; |
173
|
|
|
$this->execute($sql, array($note->getId())); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get the note parts |
178
|
|
|
* |
179
|
|
|
* @param OwnNote $note |
180
|
|
|
* @return array |
181
|
|
|
*/ |
182
|
|
|
public function getNoteParts(OwnNote $note) { |
183
|
|
|
$sql = 'SELECT * from *PREFIX*ownnote_parts where id = ?'; |
184
|
|
|
return $this->execute($sql, array($note->getId()))->fetchAll(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param OwnNote $note |
189
|
|
|
* @return bool |
190
|
|
|
*/ |
191
|
|
|
public function deleteNote(OwnNote $note) { |
192
|
|
|
$this->deleteNoteParts($note); |
193
|
|
|
$this->delete($note); |
194
|
|
|
return true; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param $arr |
199
|
|
|
* @return OwnNote |
200
|
|
|
*/ |
201
|
|
|
public function makeEntityFromDBResult($arr){ |
202
|
|
|
$note = new OwnNote(); |
203
|
|
|
$note->setId($arr['id']); |
204
|
|
|
$note->setName($arr['name']); |
205
|
|
|
$note->setGrouping($arr['grouping']); |
206
|
|
|
$note->setMtime($arr['mtime']); |
207
|
|
|
$note->setDeleted($arr['deleted']); |
208
|
|
|
$note->setUid($arr['uid']); |
209
|
|
|
$noteParts = $this->getNoteParts($note); |
210
|
|
|
$partsTxt = implode('', array_map(function ($part) { |
211
|
|
|
return $part['note']; |
212
|
|
|
}, $noteParts)); |
213
|
|
|
$note->setNote($partsTxt); |
214
|
|
|
return $note; |
215
|
|
|
} |
216
|
|
|
} |
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.