1 | <?php |
||
25 | class NotesService { |
||
26 | |||
27 | private $l10n; |
||
28 | private $root; |
||
29 | |||
30 | /** |
||
31 | * @param IRootFolder $root |
||
32 | * @param IL10N $l10n |
||
33 | */ |
||
34 | 11 | public function __construct (IRootFolder $root, IL10N $l10n) { |
|
38 | |||
39 | |||
40 | /** |
||
41 | * @param string $userId |
||
42 | * @return array with all notes in the current directory |
||
43 | */ |
||
44 | 1 | public function getAll ($userId){ |
|
45 | 1 | $folder = $this->getFolderForUser($userId); |
|
46 | 1 | $files = $folder->getDirectoryListing(); |
|
47 | 1 | $filesById = []; |
|
48 | foreach($files as $file) { |
||
49 | 1 | if($this->isNote($file)) { |
|
50 | 1 | $filesById[$file->getId()] = $file; |
|
51 | 1 | } |
|
52 | 1 | } |
|
53 | 1 | $tagger = \OC::$server->getTagManager()->load('files'); |
|
54 | if($tagger==null) { |
||
55 | 1 | $tags = []; |
|
56 | } else { |
||
57 | $tags = $tagger->getTagsForObjects(array_keys($filesById)); |
||
58 | } |
||
59 | |||
60 | $notes = []; |
||
61 | foreach($filesById as $id=>$file) { |
||
62 | $notes[] = Note::fromFile($file, array_key_exists($id, $tags) ? $tags[$id] : []); |
||
63 | } |
||
64 | |||
65 | return $notes; |
||
66 | 3 | } |
|
67 | 3 | ||
68 | 3 | ||
69 | /** |
||
70 | * Used to get a single note by id |
||
71 | * @param int $id the id of the note to get |
||
72 | * @param string $userId |
||
73 | * @throws NoteDoesNotExistException if note does not exist |
||
74 | * @return Note |
||
75 | */ |
||
76 | public function get ($id, $userId) { |
||
77 | $folder = $this->getFolderForUser($userId); |
||
78 | 2 | return Note::fromFile($this->getFileById($folder, $id), $this->getTags($id)); |
|
79 | 2 | } |
|
80 | 2 | ||
81 | private function getTags ($id) { |
||
82 | $tagger = \OC::$server->getTagManager()->load('files'); |
||
83 | if($tagger==null) { |
||
84 | $tags = []; |
||
85 | 2 | } else { |
|
86 | 2 | $tags = $tagger->getTagsForObjects([$id]); |
|
87 | } |
||
88 | 2 | return array_key_exists($id, $tags) ? $tags[$id] : []; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * Creates a note and returns the empty note |
||
93 | * @param string $userId |
||
94 | * @see update for setting note content |
||
95 | * @return Note the newly created note |
||
96 | */ |
||
97 | public function create ($userId) { |
||
109 | 2 | ||
110 | 1 | ||
111 | 1 | /** |
|
112 | * Updates a note. Be sure to check the returned note since the title is |
||
113 | * dynamically generated and filename conflicts are resolved |
||
114 | 2 | * @param int $id the id of the note used to update |
|
115 | * @param string $content the content which will be written into the note |
||
116 | * the title is generated from the first line of the content |
||
117 | 2 | * @throws NoteDoesNotExistException if note does not exist |
|
118 | * @return \OCA\Notes\Db\Note the updated note |
||
119 | 2 | */ |
|
120 | public function update ($id, $content, $userId){ |
||
155 | |||
156 | |||
157 | /** |
||
158 | 8 | * Set or unset a note as favorite. |
|
159 | 8 | * @param int $id the id of the note used to update |
|
160 | * @param boolean $favorite whether the note should be a favorite or not |
||
161 | 8 | * @throws NoteDoesNotExistException if note does not exist |
|
162 | 4 | * @return boolean the new favorite state of the note |
|
163 | */ |
||
164 | public function favorite ($id, $favorite, $userId){ |
||
179 | |||
180 | 11 | ||
181 | /** |
||
182 | * Deletes a note |
||
183 | * @param int $id the id of the note which should be deleted |
||
184 | * @param string $userId |
||
185 | * @throws NoteDoesNotExistException if note does not |
||
186 | * exist |
||
187 | */ |
||
188 | public function delete ($id, $userId) { |
||
193 | |||
194 | |||
195 | /** |
||
196 | * @param Folder $folder |
||
197 | 4 | * @param int $id |
|
198 | 4 | * @throws NoteDoesNotExistException |
|
199 | * @return \OCP\Files\File |
||
200 | */ |
||
201 | private function getFileById ($folder, $id) { |
||
202 | 4 | $file = $folder->getById($id); |
|
203 | 4 | ||
204 | if(count($file) <= 0 || !$this->isNote($file[0])) { |
||
205 | throw new NoteDoesNotExistException(); |
||
206 | 3 | } |
|
207 | 3 | return $file[0]; |
|
208 | 3 | } |
|
209 | 3 | ||
210 | 3 | ||
211 | 3 | /** |
|
212 | 3 | * @param string $userId the user id |
|
213 | * @return Folder |
||
214 | 3 | */ |
|
215 | private function getFolderForUser ($userId) { |
||
224 | 7 | ||
225 | 7 | ||
226 | /** |
||
227 | 7 | * get path of file and the title.txt and check if they are the same |
|
228 | 7 | * file. If not the title needs to be renamed |
|
229 | 7 | * |
|
230 | * @param Folder $folder a folder to the notes directory |
||
231 | 7 | * @param string $title the filename which should be used |
|
232 | * @param string $extension the extension which should be used |
||
233 | 5 | * @param int $id the id of the note for which the title should be generated |
|
234 | * used to see if the file itself has the title and not a different file for |
||
235 | * checking for filename collisions |
||
236 | * @return string the resolved filename to prevent overwriting different |
||
237 | * files with the same title |
||
238 | */ |
||
239 | private function generateFileName (Folder $folder, $title, $extension, $id) { |
||
259 | |||
260 | /** |
||
261 | * test if file is a note |
||
262 | * |
||
263 | * @param \OCP\Files\File $file |
||
264 | * @return bool |
||
265 | */ |
||
266 | private function isNote($file) { |
||
277 | |||
278 | } |
||
279 |