1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Nextcloud - NextNote |
4
|
|
|
* |
5
|
|
|
* |
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\NextNote\Db; |
25
|
|
|
|
26
|
|
|
use OCA\NextNote\Service\NotebookService; |
27
|
|
|
use \OCA\NextNote\Utility\Utils; |
28
|
|
|
use OCP\AppFramework\Db\Entity; |
29
|
|
|
use OCP\IDBConnection; |
30
|
|
|
use OCP\AppFramework\Db\Mapper; |
31
|
|
|
|
32
|
|
|
class NoteMapper extends Mapper { |
33
|
|
|
private $utils; |
34
|
|
|
private $notebookService; |
35
|
|
|
|
36
|
|
|
public function __construct(IDBConnection $db, Utils $utils, NotebookService $notebookService) { |
37
|
|
|
parent::__construct($db, 'nextnote_notes'); |
38
|
|
|
$this->utils = $utils; |
39
|
|
|
$this->notebookService = $notebookService; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param $note_id |
45
|
|
|
* @param null $user_id |
46
|
|
|
* @param int|bool $deleted |
47
|
|
|
* @return Note if not found |
48
|
|
|
*/ |
49
|
|
|
public function find($note_id, $user_id = null, $deleted = false) { |
50
|
|
|
$qb = $this->db->getQueryBuilder(); |
51
|
|
|
$qb->select('*') |
52
|
|
|
->from('nextnote_notes') |
53
|
|
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($note_id))); |
54
|
|
|
|
55
|
|
|
if ($user_id) { |
56
|
|
|
$qb->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user_id))); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if ($deleted !== false) { |
60
|
|
|
$qb->andWhere($qb->expr()->eq('deleted', $qb->createNamedParameter($deleted))); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$results = []; |
64
|
|
|
$result = $qb->execute(); |
65
|
|
|
while ($item = $result->fetch()) { |
66
|
|
|
/** |
67
|
|
|
* @var $note Note |
68
|
|
|
*/ |
69
|
|
|
$note = $this->makeEntityFromDBResult($item); |
70
|
|
|
/* fetch note parts */ |
71
|
|
|
$noteParts = $this->getNoteParts($note); |
72
|
|
|
$partsTxt = implode('', array_map(function ($part) { |
73
|
|
|
return $part['note']; |
74
|
|
|
}, $noteParts)); |
75
|
|
|
$note->setNote($item['note'] . $partsTxt); |
76
|
|
|
|
77
|
|
|
$results[] = $note; |
78
|
|
|
} |
79
|
|
|
$result->closeCursor(); |
80
|
|
|
if (count($results) === 1) { |
81
|
|
|
return reset($results); |
82
|
|
|
} |
83
|
|
|
return array_shift($results); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param $userId |
89
|
|
|
* @param int|bool $deleted |
90
|
|
|
* @param string|bool $group |
91
|
|
|
* @return Note[] if not found |
92
|
|
|
*/ |
93
|
|
|
public function findNotesFromUser($userId, $deleted = 0, $group = false) { |
94
|
|
|
$qb = $this->db->getQueryBuilder(); |
95
|
|
|
$qb->select('*') |
96
|
|
|
->from('nextnote_notes') |
97
|
|
|
->where($qb->expr()->eq('uid', $qb->createNamedParameter($userId))); |
98
|
|
|
|
99
|
|
|
if ($group) { |
100
|
|
|
$qb->andWhere($qb->expr()->eq('notebook', $qb->createNamedParameter($group))); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if ($deleted !== false) { |
104
|
|
|
$qb->andWhere($qb->expr()->eq('notebook', $qb->createNamedParameter((int)$deleted))); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$results = []; |
108
|
|
|
$result = $qb->execute(); |
109
|
|
|
while ($item = $result->fetch()) { |
110
|
|
|
/** |
111
|
|
|
* @var $note Note |
112
|
|
|
*/ |
113
|
|
|
$note = $this->makeEntityFromDBResult($item); |
114
|
|
|
$note->setNote($item['note']); |
115
|
|
|
|
116
|
|
|
$results[] = $note; |
117
|
|
|
} |
118
|
|
|
$result->closeCursor(); |
119
|
|
|
if (count($results) === 1) { |
120
|
|
|
return reset($results); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $results; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Creates a note |
129
|
|
|
* |
130
|
|
|
* @param Note|Entity $note |
131
|
|
|
* @return Note|Entity |
132
|
|
|
* @internal param $userId |
133
|
|
|
*/ |
134
|
|
|
public function insert(Entity $note) { |
135
|
|
|
$len = mb_strlen($note->getNote()); |
136
|
|
|
$parts = false; |
137
|
|
|
if ($len > Utils::$maxPartSize) { |
138
|
|
|
$parts = $this->utils->splitContent($note->getNote()); |
139
|
|
|
$note->setNote(''); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$note = parent::insert($note); |
143
|
|
|
/** |
144
|
|
|
* @var $note Note |
145
|
|
|
*/ |
146
|
|
|
if ($parts) { |
147
|
|
|
foreach ($parts as $part) { |
148
|
|
|
$this->createNotePart($note, $part); |
149
|
|
|
} |
150
|
|
|
$note->setNote(implode('', $parts)); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
return $note; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Update note |
159
|
|
|
* |
160
|
|
|
* @param Note|Entity $note |
161
|
|
|
* @return Note|Entity |
162
|
|
|
*/ |
163
|
|
|
public function updateNote(Entity $note) { |
164
|
|
|
$len = mb_strlen($note->getNote()); |
165
|
|
|
$parts = false; |
166
|
|
|
$this->deleteNoteParts($note); |
167
|
|
|
|
168
|
|
|
if ($len > Utils::$maxPartSize) { |
169
|
|
|
$parts = $this->utils->splitContent($note->getNote()); |
170
|
|
|
$note->setNote(''); |
171
|
|
|
} |
172
|
|
|
/** |
173
|
|
|
* @var $note Note |
174
|
|
|
*/ |
175
|
|
|
|
176
|
|
|
$note = parent::update($note); |
|
|
|
|
177
|
|
|
if ($parts) { |
178
|
|
|
foreach ($parts as $part) { |
179
|
|
|
$this->createNotePart($note, $part); |
180
|
|
|
} |
181
|
|
|
$note->setNote(implode('', $parts)); |
182
|
|
|
} |
183
|
|
|
return $this->find($note->getId()); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param Note $note |
188
|
|
|
* @param $content |
189
|
|
|
*/ |
190
|
|
|
public function createNotePart(Note $note, $content) { |
191
|
|
|
$qb = $this->db->getQueryBuilder(); |
192
|
|
|
$qb->insert('nextnote_parts') |
193
|
|
|
->values([ |
194
|
|
|
'id' => $qb->createNamedParameter($note->getId()), |
195
|
|
|
'note' => $qb->createNamedParameter($content), |
196
|
|
|
]); |
197
|
|
|
$qb->execute(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Delete the note parts |
202
|
|
|
* |
203
|
|
|
* @param Note $note |
204
|
|
|
*/ |
205
|
|
|
public function deleteNoteParts(Note $note) { |
206
|
|
|
$qb = $this->db->getQueryBuilder(); |
207
|
|
|
$qb->delete('nextnote_parts') |
208
|
|
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($note->getId()))); |
209
|
|
|
$qb->execute(); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Get the note parts |
214
|
|
|
* |
215
|
|
|
* @param Note $note |
216
|
|
|
* @return array |
217
|
|
|
*/ |
218
|
|
|
public function getNoteParts(Note $note) { |
219
|
|
|
$qb = $this->db->getQueryBuilder(); |
220
|
|
|
$qb->select('*') |
221
|
|
|
->from('nextnote_parts') |
222
|
|
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($note->getId()))); |
223
|
|
|
$result = $qb->execute(); |
224
|
|
|
$results = $result->fetchAll(); |
225
|
|
|
$result->closeCursor(); |
226
|
|
|
|
227
|
|
|
return $results; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param Note $note |
232
|
|
|
* @return bool |
233
|
|
|
*/ |
234
|
|
|
public function deleteNote(Note $note) { |
235
|
|
|
$this->deleteNoteParts($note); |
236
|
|
|
parent::delete($note); |
|
|
|
|
237
|
|
|
return true; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param $arr |
242
|
|
|
* @return Note |
243
|
|
|
*/ |
244
|
|
|
public function makeEntityFromDBResult($arr) { |
245
|
|
|
|
246
|
|
|
$note = new Note(); |
247
|
|
|
$note->setId($arr['id']); |
248
|
|
|
$note->setName($arr['name']); |
249
|
|
|
$note->setGuid($arr['guid']); |
250
|
|
|
$note->setGrouping($arr['grouping']); |
251
|
|
|
if ($arr['notebook']) { |
252
|
|
|
$notebook = $this->notebookService->find($arr['notebook']); |
253
|
|
|
$note->setNotebook($notebook); |
254
|
|
|
} |
255
|
|
|
$note->setMtime($arr['mtime']); |
256
|
|
|
$note->setDeleted($arr['deleted']); |
257
|
|
|
$note->setUid($arr['uid']); |
258
|
|
|
return $note; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.