13 namespace Fossology\UI\Api\Controllers;
26 use Psr\Http\Message\ServerRequestInterface;
47 $allUserFolders =
null;
49 $folderDao = $this->restHelper->getFolderDao();
50 if (isset($args[
'id'])) {
51 $id = intval($args[
'id']);
52 if (! $folderDao->isFolderAccessible($id)) {
55 if ($folderDao->getFolder($id) ===
null) {
62 $rootFolder = $folderDao->getRootFolder($this->restHelper->getUserId())->getId();
63 $allUserFolders = array();
65 $allUserFolders = array_keys($allUserFolders);
67 $foldersList = array();
68 foreach ($allUserFolders as $folderId) {
69 $folder = $folderDao->getFolder($folderId);
70 $parentId = $folderDao->getFolderParentId($folderId);
71 $folderModel =
new Folder($folder->getId(), $folder->getName(),
72 $folder->getDescription(), $parentId);
73 $foldersList[] = $folderModel->getArray();
76 $foldersList = $foldersList[0];
78 return $response->withJson($foldersList, 200);
90 public function createFolder($request, $response, $args)
92 $parentFolder = $request->getHeaderLine(
'parentFolder');
93 $folderName =
trim($request->getHeaderLine(
'folderName'));
94 $folderDescription =
trim($request->getHeaderLine(
'folderDescription'));
96 if (! is_numeric($parentFolder) || $parentFolder < 0) {
98 "Parent folder id must be a positive integer!");
100 if (empty($folderName)) {
101 throw new HttpBadRequestException(
"Folder name can not be empty!");
103 if (! $this->restHelper->getFolderDao()->isFolderAccessible($parentFolder,
104 $this->restHelper->getUserId())) {
105 throw new HttpForbiddenException(
"Parent folder is not accessible!");
108 $folderCreate = $this->restHelper->getPlugin(
'folder_create');
109 $rc = $folderCreate->create($parentFolder, $folderName, $folderDescription);
111 $info =
new Info(200,
"Folder $folderName already exists!", InfoType::INFO);
112 } elseif ($rc == 0) {
113 throw new HttpNotFoundException(
"Parent folder not found!");
115 $folderId = $this->restHelper->getFolderDao()->getFolderId($folderName, $parentFolder);
116 $info =
new Info(201, intval($folderId), InfoType::INFO);
118 return $response->withJson($info->getArray(), $info->getCode());
130 public function deleteFolder($request, $response, $args)
133 $folderDao = $this->restHelper->getFolderDao();
134 $folderId = $args[
'id'];
136 if (! is_numeric($folderId) || $folderId < 0) {
137 throw new HttpBadRequestException(
138 "Folder id must be a positive integer!");
140 if ($folderDao->getFolder($folderId) ===
null) {
141 throw new HttpNotFoundException(
"Folder id not found!");
144 $folderDelete = $this->restHelper->getPlugin(
'admin_folder_delete');
147 $folderParent = intval($folderArray[count($folderArray) - 2][
'folder_pk']);
148 $folderId =
"$folderParent $folderId";
150 $rc = $folderDelete->Delete($folderId, $this->restHelper->getUserId());
151 if ($rc ==
"No access to delete this folder") {
152 throw new HttpForbiddenException($rc);
153 } elseif ($rc !==
null) {
154 throw new HttpInternalServerErrorException($rc);
156 $info =
new Info(202,
"Folder, \"$folderName\" deleted.", InfoType::INFO);
157 return $response->withJson($info->getArray(), $info->getCode());
169 public function editFolder($request, $response, $args)
171 $folderDao = $this->restHelper->getFolderDao();
172 $folderId = $args[
'id'];
173 $newName = $request->getHeaderLine(
'name');
174 $newDesc = $request->getHeaderLine(
'description');
176 if ($folderDao->getFolder($folderId) ===
null) {
177 throw new HttpNotFoundException(
"Folder id not found!");
179 if (! $folderDao->isFolderAccessible($folderId, $this->restHelper->getUserId())) {
180 throw new HttpForbiddenException(
"Folder is not accessible!");
183 $folderEdit = $this->restHelper->getPlugin(
'folder_properties');
185 $folderEdit->Edit($folderId, $newName, $newDesc);
186 $info =
new Info(200,
"Folder \"$folderName\" updated.", InfoType::INFO);
187 return $response->withJson($info->getArray(), $info->getCode());
199 public function copyFolder($request, $response, $args)
201 $folderDao = $this->restHelper->getFolderDao();
202 $folderId = $args[
'id'];
203 $newParent = $request->getHeaderLine(
'parent');
204 $action = strtolower($request->getHeaderLine(
'action'));
206 if (! is_numeric($newParent) || $newParent < 0) {
207 throw new HttpBadRequestException(
208 "Parent id must be a positive integer!");
210 if ($folderDao->getFolder($folderId) ===
null) {
211 throw new HttpNotFoundException(
"Folder id not found!");
213 if ($folderDao->getFolder($newParent) ===
null) {
214 throw new HttpNotFoundException(
"Parent folder id not found!");
216 if (! $folderDao->isFolderAccessible($folderId,
217 $this->restHelper->getUserId())) {
218 throw new HttpForbiddenException(
"Folder is not accessible!");
220 if (! $folderDao->isFolderAccessible($newParent,
221 $this->restHelper->getUserId())) {
222 throw new HttpForbiddenException(
"Parent folder is not accessible!");
224 if (strcmp($action,
"copy") != 0 && strcmp($action,
"move") != 0) {
225 throw new HttpBadRequestException(
226 "Action can be one of [copy,move]!");
229 $folderMove = $this->restHelper->getPlugin(
'content_move');
232 $isCopy = (strcmp($action,
"copy") == 0);
233 $message = $folderMove->copyContent(
235 $folderDao->getFolderContentsId($folderId, $folderDao::MODE_FOLDER)
236 ], $newParent, $isCopy);
237 if (!empty($message)) {
238 throw new HttpInternalServerErrorException($message);
240 $info =
new Info(202,
241 "Folder \"$folderName\" $action(ed) under \"$parentFolderName\".",
243 return $response->withJson($info->getArray(), $info->getCode());
255 public function getUnlinkableFolderContents($request, $response, $args)
257 $folderId = $args[
'id'];
258 $folderDao = $this->restHelper->getFolderDao();
260 if ($folderDao->getFolder($folderId) ===
null) {
261 throw new HttpNotFoundException(
"Folder id not found!");
263 if (! $folderDao->isFolderAccessible($folderId, $this->restHelper->getUserId())) {
264 throw new HttpForbiddenException(
"Folder is not accessible!");
268 $folderContents = $this->restHelper->getPlugin(
'foldercontents');
269 $symfonyRequest = new \Symfony\Component\HttpFoundation\Request();
270 $symfonyRequest->request->set(
'folder', $folderId);
271 $symfonyRequest->request->set(
'removable', 1);
272 $symfonyRequest->request->set(
'fromRest',
true);
273 $res = $folderContents->handle($symfonyRequest);
274 return $response->withJson($res, 200);
286 public function unlinkFolder($request, $response, $args)
288 $folderContentId = $args[
'contentId'];
289 if (!$this->dbHelper->doesIdExist(
"foldercontents",
"foldercontents_pk", $folderContentId)) {
290 throw new HttpNotFoundException(
"Folder content id not found!");
293 $folderDao = $this->container->get(
'dao.folder');
294 if (!$folderDao->removeContent($folderContentId)) {
295 throw new HttpBadRequestException(
"Content cannot be unlinked.");
297 $info =
new Info(200,
"Folder unlinked successfully.", InfoType::INFO);
298 return $response->withJson($info->getArray(), $info->getCode());
310 public function getAllFolderContents($request, $response, $args)
312 $folderId = $args[
'id'];
313 $folderDao = $this->restHelper->getFolderDao();
315 if ($folderDao->getFolder($folderId) ===
null) {
316 throw new HttpNotFoundException(
"Folder id not found!");
318 if (! $folderDao->isFolderAccessible($folderId, $this->restHelper->getUserId())) {
319 throw new HttpForbiddenException(
"Folder is not accessible!");
323 $folderContents = $this->restHelper->getPlugin(
'foldercontents');
324 $symfonyRequest = new \Symfony\Component\HttpFoundation\Request();
325 $symfonyRequest->request->set(
'folder', $folderId);
326 $symfonyRequest->request->set(
'fromRest',
true);
327 $contentList = $folderContents->handle($symfonyRequest);
328 $removableContents = $folderDao->getRemovableContents($folderId);
330 foreach ($contentList as &$value) {
331 if (in_array($value[
'id'], $removableContents)) {
332 $value[
'removable'] =
true;
335 return $response->withJson($contentList, 200);
Controller for Folder model.
getFolders($request, $response, $args)
Base controller for REST calls.
Override Slim response for withJson function.
Different type of infos provided by REST.
Info model to contain general error and return values.
FolderGetName($FolderPk, $Top=-1)
Given a folder_pk, return the full path to this folder.
GetFolderArray($RootFolder, &$FolderArray)
Get an array of all the folders from a $RootFolder on down.
Folder2Path($folder_pk)
Return an array of folder_pk, folder_name from the users.root_folder_fk to $folder_pk.
int deleteFolder(long cFolder, long pFolder, int userId, int userPerm)
recursively delete a folder
char * trim(char *ptext)
Trimming whitespace.