|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Nextcloud - namespace OCA\Nextnote |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2016, Sander Brand ([email protected]) |
|
6
|
|
|
* |
|
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\Service; |
|
25
|
|
|
|
|
26
|
|
|
use OCA\NextNote\Db\Notebook; |
|
27
|
|
|
use OCA\NextNote\Db\NotebookMapper; |
|
28
|
|
|
use OCA\NextNote\ShareBackend\NextNoteShareBackend; |
|
29
|
|
|
use OCA\NextNote\Utility\Utils; |
|
30
|
|
|
use OCP\AppFramework\Db\Entity; |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
class NotebookService { |
|
34
|
|
|
|
|
35
|
|
|
private $notebookMapper; |
|
36
|
|
|
private $utils; |
|
37
|
|
|
private $sharing; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct(NotebookMapper $notebookMapper, Utils $utils, NextNoteShareBackend $shareBackend) { |
|
40
|
|
|
$this->notebookMapper = $notebookMapper; |
|
41
|
|
|
$this->utils = $utils; |
|
42
|
|
|
$this->sharing = $shareBackend; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Get notebooks from a user. |
|
49
|
|
|
* |
|
50
|
|
|
* @param $userId |
|
51
|
|
|
* @param int|bool $deleted |
|
52
|
|
|
* @return Notebook[] |
|
53
|
|
|
*/ |
|
54
|
|
|
public function findNotebooksFromUser($userId, $deleted = false) { |
|
55
|
|
|
// Get shares |
|
56
|
|
|
$dbNotebooks = $this->notebookMapper->find(null, $userId, $deleted); |
|
57
|
|
|
$n = $dbNotebooks; |
|
58
|
|
|
if($dbNotebooks instanceof Notebook){ |
|
59
|
|
|
$dbNotebooks = []; |
|
60
|
|
|
/** |
|
61
|
|
|
* @var $result Notebook |
|
62
|
|
|
*/ |
|
63
|
|
|
$dbNotebooks[$n->getId()] = $n; |
|
64
|
|
|
} |
|
65
|
|
|
$sharedNotebooks = []; |
|
66
|
|
|
$notebooks = array_merge($dbNotebooks, $sharedNotebooks); |
|
67
|
|
|
return $notebooks; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Find a notebook by id |
|
73
|
|
|
* |
|
74
|
|
|
* @param null|int $notebook_id |
|
75
|
|
|
* @param null $user_id |
|
76
|
|
|
* @param int|bool $deleted |
|
77
|
|
|
* @return Notebook[]|Notebook |
|
78
|
|
|
*/ |
|
79
|
|
|
public function find($notebook_id=null, $user_id = null, $deleted = false) { |
|
80
|
|
|
return $this->notebookMapper->find($notebook_id, $user_id, $deleted); |
|
81
|
|
|
} |
|
82
|
|
|
/** |
|
83
|
|
|
* Find a notebook by name |
|
84
|
|
|
* |
|
85
|
|
|
* @param $notebook_name string |
|
86
|
|
|
* @param null $user_id |
|
87
|
|
|
* @param bool $deleted |
|
88
|
|
|
* @return Notebook[]|Notebook |
|
89
|
|
|
*/ |
|
90
|
|
|
public function findByName($notebook_name=null, $user_id = null, $deleted = false) { |
|
91
|
|
|
return $this->notebookMapper->findByName($notebook_name, $user_id, $deleted); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Creates a notebook |
|
96
|
|
|
* |
|
97
|
|
|
* @param array|Notebook $notebook |
|
98
|
|
|
* @param $userId |
|
99
|
|
|
* @return Notebook|Entity |
|
100
|
|
|
* @throws \Exception |
|
101
|
|
|
*/ |
|
102
|
|
|
public function create($notebook, $userId) { |
|
103
|
|
|
if (is_array($notebook)) { |
|
104
|
|
|
$entity = new Notebook(); |
|
105
|
|
|
$entity->setName($notebook['name']); |
|
106
|
|
|
$entity->setParentId($notebook['parent_id']); |
|
107
|
|
|
$entity->setUid($userId); |
|
108
|
|
|
$entity->setGuid($notebook['guid']); |
|
109
|
|
|
$entity->setColor($notebook['color']); |
|
110
|
|
|
$notebook = $entity; |
|
111
|
|
|
} |
|
112
|
|
|
if (!$notebook instanceof Notebook) { |
|
113
|
|
|
throw new \Exception("Expected Notebook object!"); |
|
114
|
|
|
} |
|
115
|
|
|
return $this->notebookMapper->insert($notebook); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Update a notebook |
|
120
|
|
|
* |
|
121
|
|
|
* @param $notebook array|Notebook |
|
122
|
|
|
* @return Notebook|Entity|bool |
|
123
|
|
|
* @throws \Exception |
|
124
|
|
|
* @internal param $userId |
|
125
|
|
|
* @internal param $vault |
|
126
|
|
|
*/ |
|
127
|
|
|
public function update($notebook) { |
|
128
|
|
|
|
|
129
|
|
|
if (is_array($notebook)) { |
|
130
|
|
|
$entity = $this->find($notebook['id']); |
|
131
|
|
|
$entity->setName($notebook['title']); |
|
132
|
|
|
$entity->setParentId($notebook['parent_id']); |
|
133
|
|
|
$entity->setColor($notebook['color']); |
|
134
|
|
|
$notebook = $entity; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
if (!$notebook instanceof Notebook) { |
|
138
|
|
|
throw new \Exception("Expected Notebook object!"); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return $this->notebookMapper->update($notebook); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Delete a notebook |
|
146
|
|
|
* |
|
147
|
|
|
* @param $notebook_id |
|
148
|
|
|
* @param string $user_id |
|
149
|
|
|
* @return bool |
|
150
|
|
|
*/ |
|
151
|
|
|
public function delete($notebook_id, $user_id = null) { |
|
152
|
|
|
if (!$this->checkPermissions()) { |
|
153
|
|
|
return false; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
$notebook = $this->notebookMapper->find($notebook_id, $user_id); |
|
157
|
|
|
if ($notebook instanceof Notebook) { |
|
158
|
|
|
$this->notebookMapper->delete($notebook); |
|
159
|
|
|
return true; |
|
160
|
|
|
} else { |
|
161
|
|
|
return false; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
private function checkPermissions() { |
|
166
|
|
|
return true; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|