1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ownCloud - News |
4
|
|
|
* |
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
6
|
|
|
* later. See the COPYING file. |
7
|
|
|
* |
8
|
|
|
* @author Alessandro Cosentino <[email protected]> |
9
|
|
|
* @author Bernhard Posselt <[email protected]> |
10
|
|
|
* @copyright Alessandro Cosentino 2012 |
11
|
|
|
* @copyright Bernhard Posselt 2012, 2014 |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace OCA\News\Service; |
15
|
|
|
|
16
|
|
|
use \OCP\IL10N; |
17
|
|
|
use \OCP\AppFramework\Utility\ITimeFactory; |
18
|
|
|
use \OCA\News\Db\Folder; |
19
|
|
|
use \OCA\News\Db\FolderMapper; |
20
|
|
|
use \OCA\News\Config\Config; |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
class FolderService extends Service { |
24
|
|
|
|
25
|
|
|
private $l10n; |
26
|
|
|
private $timeFactory; |
27
|
|
|
private $autoPurgeMinimumInterval; |
28
|
|
|
private $folderMapper; |
29
|
|
|
|
30
|
|
|
public function __construct(FolderMapper $folderMapper, |
31
|
|
|
IL10N $l10n, |
32
|
|
|
ITimeFactory $timeFactory, |
33
|
|
|
Config $config){ |
34
|
|
|
parent::__construct($folderMapper); |
35
|
|
|
$this->l10n = $l10n; |
36
|
|
|
$this->timeFactory = $timeFactory; |
37
|
|
|
$this->autoPurgeMinimumInterval = |
38
|
|
|
$config->getAutoPurgeMinimumInterval(); |
39
|
|
|
$this->folderMapper = $folderMapper; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Returns all folders of a user |
44
|
|
|
* @param string $userId the name of the user |
45
|
|
|
* @return array of folders |
46
|
|
|
*/ |
47
|
|
|
public function findAll($userId) { |
48
|
|
|
return $this->folderMapper->findAllFromUser($userId); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
private function validateFolder($folderName, $userId){ |
53
|
|
|
$existingFolders = |
54
|
|
|
$this->folderMapper->findByName($folderName, $userId); |
55
|
|
|
if(count($existingFolders) > 0){ |
56
|
|
|
|
57
|
|
|
throw new ServiceConflictException( |
58
|
|
|
$this->l10n->t('Can not add folder: Exists already')); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if(mb_strlen($folderName) === 0) { |
62
|
|
|
throw new ServiceValidationException( |
63
|
|
|
'Folder name can not be empty' |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Creates a new folder |
71
|
|
|
* @param string $folderName the name of the folder |
72
|
|
|
* @param string $userId the name of the user for whom it should be created |
73
|
|
|
* @param int $parentId the parent folder id, deprecated we don't nest |
74
|
|
|
* folders |
75
|
|
|
* @throws ServiceConflictException if name exists already |
76
|
|
|
* @throws ServiceValidationException if the folder has invalid parameters |
77
|
|
|
* @return Folder the newly created folder |
78
|
|
|
*/ |
79
|
|
|
public function create($folderName, $userId, $parentId=0) { |
80
|
|
|
$this->validateFolder($folderName, $userId); |
81
|
|
|
|
82
|
|
|
$folder = new Folder(); |
83
|
|
|
$folder->setName($folderName); |
84
|
|
|
$folder->setUserId($userId); |
85
|
|
|
$folder->setParentId($parentId); |
86
|
|
|
$folder->setOpened(true); |
87
|
|
|
return $this->folderMapper->insert($folder); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @throws ServiceException if the folder does not exist |
93
|
|
|
*/ |
94
|
|
|
public function open($folderId, $opened, $userId){ |
95
|
|
|
$folder = $this->find($folderId, $userId); |
96
|
|
|
$folder->setOpened($opened); |
97
|
|
|
$this->folderMapper->update($folder); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Renames a folder |
103
|
|
|
* @param int $folderId the id of the folder that should be deleted |
104
|
|
|
* @param string $folderName the new name of the folder |
105
|
|
|
* @param string $userId the name of the user for security reasons |
106
|
|
|
* @throws ServiceConflictException if name exists already |
107
|
|
|
* @throws ServiceValidationException if the folder has invalid parameters |
108
|
|
|
* @throws ServiceNotFoundException if the folder does not exist |
109
|
|
|
* @return Folder the updated folder |
110
|
|
|
*/ |
111
|
|
|
public function rename($folderId, $folderName, $userId){ |
112
|
|
|
$this->validateFolder($folderName, $userId); |
113
|
|
|
|
114
|
|
|
$folder = $this->find($folderId, $userId); |
115
|
|
|
$folder->setName($folderName); |
116
|
|
|
return $this->folderMapper->update($folder); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Use this to mark a folder as deleted. That way it can be un-deleted |
122
|
|
|
* @param int $folderId the id of the folder that should be deleted |
123
|
|
|
* @param string $userId the name of the user for security reasons |
124
|
|
|
* @throws ServiceNotFoundException when folder does not exist |
125
|
|
|
*/ |
126
|
|
|
public function markDeleted($folderId, $userId) { |
127
|
|
|
$folder = $this->find($folderId, $userId); |
128
|
|
|
$folder->setDeletedAt($this->timeFactory->getTime()); |
129
|
|
|
$this->folderMapper->update($folder); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Use this to restore a folder |
135
|
|
|
* @param int $folderId the id of the folder that should be restored |
136
|
|
|
* @param string $userId the name of the user for security reasons |
137
|
|
|
* @throws ServiceNotFoundException when folder does not exist |
138
|
|
|
*/ |
139
|
|
|
public function unmarkDeleted($folderId, $userId) { |
140
|
|
|
$folder = $this->find($folderId, $userId); |
141
|
|
|
$folder->setDeletedAt(0); |
142
|
|
|
$this->folderMapper->update($folder); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Deletes all deleted folders |
148
|
|
|
* @param string $userId if given it purges only folders of that user |
149
|
|
|
* @param boolean $useInterval defaults to true, if true it only purges |
150
|
|
|
* entries in a given interval to give the user a chance to undo the |
151
|
|
|
* deletion |
152
|
|
|
*/ |
153
|
|
|
public function purgeDeleted($userId=null, $useInterval=true) { |
154
|
|
|
$deleteOlderThan = null; |
155
|
|
|
|
156
|
|
|
if ($useInterval) { |
157
|
|
|
$now = $this->timeFactory->getTime(); |
158
|
|
|
$deleteOlderThan = $now - $this->autoPurgeMinimumInterval; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$toDelete = $this->folderMapper->getToDelete($deleteOlderThan, $userId); |
162
|
|
|
|
163
|
|
|
foreach ($toDelete as $folder) { |
164
|
|
|
$this->folderMapper->delete($folder); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Deletes all folders of a user |
171
|
|
|
* @param string $userId the name of the user |
172
|
|
|
*/ |
173
|
|
|
public function deleteUser($userId) { |
174
|
|
|
$this->folderMapper->deleteUser($userId); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
|
178
|
|
|
} |
179
|
|
|
|