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 | $tags = $tagger->getTagsForObjects(array_keys($filesById)); |
||
55 | 1 | ||
56 | $notes = []; |
||
57 | foreach($filesById as $id=>$file) { |
||
58 | $notes[] = Note::fromFile($file, array_key_exists($id, $tags) ? $tags[$id] : []); |
||
59 | } |
||
60 | |||
61 | return $notes; |
||
62 | } |
||
63 | |||
64 | |||
65 | /** |
||
66 | 3 | * Used to get a single note by id |
|
67 | 3 | * @param int $id the id of the note to get |
|
68 | 3 | * @param string $userId |
|
69 | * @throws NoteDoesNotExistException if note does not exist |
||
70 | * @return Note |
||
71 | */ |
||
72 | public function get ($id, $userId) { |
||
73 | $folder = $this->getFolderForUser($userId); |
||
74 | return Note::fromFile($this->getFileById($folder, $id), $this->getTags($id)); |
||
75 | } |
||
76 | |||
77 | private function getTags ($id) { |
||
78 | 2 | $tagger = \OC::$server->getTagManager()->load('files'); |
|
79 | 2 | $tags = $tagger->getTagsForObjects([$id]); |
|
80 | 2 | return $tags[$id]; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * Creates a note and returns the empty note |
||
85 | 2 | * @param string $userId |
|
86 | 2 | * @see update for setting note content |
|
87 | * @return Note the newly created note |
||
88 | 2 | */ |
|
89 | public function create ($userId) { |
||
101 | 2 | ||
102 | 2 | ||
103 | 2 | /** |
|
104 | * Updates a note. Be sure to check the returned note since the title is |
||
105 | * dynamically generated and filename conflicts are resolved |
||
106 | 2 | * @param int $id the id of the note used to update |
|
107 | 2 | * @param string $content the content which will be written into the note |
|
108 | * the title is generated from the first line of the content |
||
109 | 2 | * @throws NoteDoesNotExistException if note does not exist |
|
110 | 1 | * @return \OCA\Notes\Db\Note the updated note |
|
111 | 1 | */ |
|
112 | public function update ($id, $content, $userId){ |
||
147 | 3 | ||
148 | 1 | ||
149 | 1 | /** |
|
150 | * Set or unset a note as favorite. |
||
151 | * @param int $id the id of the note used to update |
||
152 | * @param boolean $favorite whether the note should be a favorite or not |
||
153 | * @throws NoteDoesNotExistException if note does not exist |
||
154 | * @return boolean the new favorite state of the note |
||
155 | */ |
||
156 | public function favorite ($id, $favorite, $userId){ |
||
171 | |||
172 | |||
173 | 11 | /** |
|
174 | 11 | * Deletes a note |
|
175 | 11 | * @param int $id the id of the note which should be deleted |
|
176 | 11 | * @param string $userId |
|
177 | 11 | * @throws NoteDoesNotExistException if note does not |
|
178 | * exist |
||
179 | */ |
||
180 | 11 | public function delete ($id, $userId) { |
|
185 | |||
186 | |||
187 | /** |
||
188 | * @param Folder $folder |
||
189 | * @param int $id |
||
190 | * @throws NoteDoesNotExistException |
||
191 | * @return \OCP\Files\File |
||
192 | */ |
||
193 | private function getFileById ($folder, $id) { |
||
201 | |||
202 | 4 | ||
203 | 4 | /** |
|
204 | * @param string $userId the user id |
||
205 | * @return Folder |
||
206 | 3 | */ |
|
207 | 3 | private function getFolderForUser ($userId) { |
|
216 | |||
217 | |||
218 | /** |
||
219 | * get path of file and the title.txt and check if they are the same |
||
220 | * file. If not the title needs to be renamed |
||
221 | * |
||
222 | * @param Folder $folder a folder to the notes directory |
||
223 | * @param string $title the filename which should be used |
||
224 | 7 | * @param string $extension the extension which should be used |
|
225 | 7 | * @param int $id the id of the note for which the title should be generated |
|
226 | * used to see if the file itself has the title and not a different file for |
||
227 | 7 | * checking for filename collisions |
|
228 | 7 | * @return string the resolved filename to prevent overwriting different |
|
229 | 7 | * files with the same title |
|
230 | */ |
||
231 | 7 | private function generateFileName (Folder $folder, $title, $extension, $id) { |
|
251 | |||
252 | /** |
||
253 | * test if file is a note |
||
254 | * |
||
255 | * @param \OCP\Files\File $file |
||
256 | * @return bool |
||
257 | */ |
||
258 | private function isNote($file) { |
||
269 | |||
270 | } |
||
271 |