brantje /
nextnote
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Nextcloud - NextNote |
||
| 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\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 | $params = [$note_id]; |
||
| 51 | $uidSql = ''; |
||
| 52 | if ($user_id) { |
||
| 53 | $params[] = $user_id; |
||
| 54 | $uidSql = 'and n.uid = ?'; |
||
| 55 | } |
||
| 56 | |||
| 57 | $deletedSql = ''; |
||
| 58 | if ($deleted !== false) { |
||
| 59 | $params[] = $deleted; |
||
| 60 | $deletedSql = 'and n.deleted = ?'; |
||
| 61 | } |
||
| 62 | $sql = "SELECT n.* FROM *PREFIX*nextnote_notes n WHERE n.id= ? $uidSql $deletedSql"; |
||
| 63 | $results = []; |
||
| 64 | foreach ($this->execute($sql, $params)->fetchAll() as $item) { |
||
| 65 | /** |
||
| 66 | * @var $note Note |
||
| 67 | */ |
||
| 68 | $note = $this->makeEntityFromDBResult($item); |
||
| 69 | /* fetch note parts */ |
||
| 70 | $noteParts = $this->getNoteParts($note); |
||
| 71 | $partsTxt = implode('', array_map(function ($part) { |
||
| 72 | return $part['note']; |
||
| 73 | }, $noteParts)); |
||
| 74 | $note->setNote($item['note'] . $partsTxt); |
||
| 75 | |||
| 76 | $results[] = $note; |
||
| 77 | } |
||
| 78 | return array_shift($results); |
||
| 79 | } |
||
| 80 | |||
| 81 | |||
| 82 | /** |
||
| 83 | * @param $userId |
||
| 84 | * @param int|bool $deleted |
||
| 85 | * @param string|bool $group |
||
| 86 | * @return Note[] if not found |
||
| 87 | */ |
||
| 88 | public function findNotesFromUser($userId, $deleted = 0, $group = false) { |
||
| 89 | $params = [$userId]; |
||
| 90 | $groupSql = ''; |
||
| 91 | if ($group) { |
||
| 92 | $groupSql = 'and n.notebook = ?'; |
||
| 93 | $params[] = $group; |
||
| 94 | } |
||
| 95 | $deletedSql = ''; |
||
| 96 | if ($deleted !== false) { |
||
| 97 | $deleted = (int) $deleted; |
||
| 98 | $deletedSql = 'and n.deleted = ?'; |
||
| 99 | $params[] = $deleted; |
||
| 100 | } |
||
| 101 | $sql = "SELECT n.* FROM *PREFIX*nextnote_notes n WHERE n.uid = ? $groupSql $deletedSql"; |
||
| 102 | $results = []; |
||
| 103 | foreach ($this->execute($sql, $params)->fetchAll() as $item) { |
||
| 104 | /** |
||
| 105 | * @var $note Note |
||
| 106 | */ |
||
| 107 | $note = $this->makeEntityFromDBResult($item); |
||
| 108 | $note->setNote($item['note']); |
||
| 109 | |||
| 110 | $results[] = $note; |
||
| 111 | } |
||
| 112 | return $results; |
||
| 113 | } |
||
| 114 | |||
| 115 | |||
| 116 | |||
| 117 | /** |
||
| 118 | * Creates a note |
||
| 119 | * |
||
| 120 | * @param Note $note |
||
| 121 | * @return Note|Entity |
||
| 122 | * @internal param $userId |
||
| 123 | */ |
||
| 124 | public function create($note) { |
||
| 125 | $len = mb_strlen($note->getNote()); |
||
| 126 | $parts = false; |
||
| 127 | if ($len > Utils::$maxPartSize) { |
||
| 128 | $parts = $this->utils->splitContent($note->getNote()); |
||
| 129 | $note->setNote(''); |
||
| 130 | } |
||
| 131 | $note->setShared(false); |
||
| 132 | |||
| 133 | $note = parent::insert($note); |
||
|
0 ignored issues
–
show
|
|||
| 134 | |||
| 135 | if ($parts) { |
||
| 136 | foreach ($parts as $part) { |
||
| 137 | $this->createNotePart($note, $part); |
||
| 138 | } |
||
| 139 | $note->setNote(implode('', $parts)); |
||
| 140 | } |
||
| 141 | |||
| 142 | |||
| 143 | return $note; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Update note |
||
| 148 | * |
||
| 149 | * @param Note $note |
||
| 150 | * @return Note|Entity |
||
| 151 | */ |
||
| 152 | public function updateNote($note) { |
||
| 153 | $len = mb_strlen($note->getNote()); |
||
| 154 | $parts = false; |
||
| 155 | $this->deleteNoteParts($note); |
||
| 156 | |||
| 157 | if ($len > Utils::$maxPartSize) { |
||
| 158 | $parts = $this->utils->splitContent($note->getNote()); |
||
| 159 | $note->setNote(''); |
||
| 160 | } |
||
| 161 | /** |
||
| 162 | * @var $note Note |
||
| 163 | */ |
||
| 164 | |||
| 165 | $note = parent::update($note); |
||
| 166 | if ($parts) { |
||
| 167 | foreach ($parts as $part) { |
||
| 168 | $this->createNotePart($note, $part); |
||
| 169 | } |
||
| 170 | $note->setNote(implode('', $parts)); |
||
| 171 | } |
||
| 172 | return $this->find($note->getId()); |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param Note $note |
||
| 177 | * @param $content |
||
| 178 | */ |
||
| 179 | public function createNotePart(Note $note, $content) { |
||
| 180 | $sql = "INSERT INTO *PREFIX*nextnote_parts VALUES (NULL, ?, ?);"; |
||
| 181 | $this->execute($sql, array($note->getId(), $content)); |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Delete the note parts |
||
| 186 | * |
||
| 187 | * @param Note $note |
||
| 188 | */ |
||
| 189 | public function deleteNoteParts(Note $note) { |
||
| 190 | $sql = 'DELETE FROM *PREFIX*nextnote_parts where id = ?'; |
||
| 191 | $this->execute($sql, array($note->getId())); |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Get the note parts |
||
| 196 | * |
||
| 197 | * @param Note $note |
||
| 198 | * @return array |
||
| 199 | */ |
||
| 200 | public function getNoteParts(Note $note) { |
||
| 201 | $sql = 'SELECT * from *PREFIX*nextnote_parts where id = ?'; |
||
| 202 | return $this->execute($sql, array($note->getId()))->fetchAll(); |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param Note $note |
||
| 207 | * @return bool |
||
| 208 | */ |
||
| 209 | public function deleteNote(Note $note) { |
||
| 210 | $this->deleteNoteParts($note); |
||
| 211 | parent::delete($note); |
||
| 212 | return true; |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @param $arr |
||
| 217 | * @return Note |
||
| 218 | */ |
||
| 219 | public function makeEntityFromDBResult($arr) { |
||
| 220 | |||
| 221 | $note = new Note(); |
||
| 222 | $note->setId($arr['id']); |
||
| 223 | $note->setName($arr['name']); |
||
| 224 | $note->setGuid($arr['guid']); |
||
| 225 | $note->setGrouping($arr['grouping']); |
||
| 226 | if($arr['notebook']){ |
||
| 227 | $notebook = $this->notebookService->find($arr['notebook']); |
||
| 228 | $note->setNotebook($notebook); |
||
| 229 | } |
||
| 230 | $note->setMtime($arr['mtime']); |
||
| 231 | $note->setDeleted($arr['deleted']); |
||
| 232 | $note->setUid($arr['uid']); |
||
| 233 | return $note; |
||
| 234 | } |
||
| 235 | } |
||
| 236 |
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 theSoncalls the wrong method in the parent class.