13 namespace Fossology\UI\Api\Controllers;
27 use Psr\Http\Message\ServerRequestInterface;
48 $allUserFolders =
null;
50 $folderDao = $this->restHelper->getFolderDao();
51 if (isset($args[
'id'])) {
52 $id = intval($args[
'id']);
53 if (! $folderDao->isFolderAccessible($id, $this->restHelper->getUserId())) {
56 if ($folderDao->getFolder($id) ===
null) {
63 $rootFolder = $folderDao->getRootFolder($this->restHelper->getUserId())->getId();
64 $allUserFolders = array();
66 $allUserFolders = array_keys($allUserFolders);
68 $foldersList = array();
69 foreach ($allUserFolders as $folderId) {
70 $folder = $folderDao->getFolder($folderId);
71 $parentId = $folderDao->getFolderParentId($folderId);
72 $folderModel =
new Folder($folder->getId(), $folder->getName(),
73 $folder->getDescription(), $parentId);
74 $foldersList[] = $folderModel->getArray();
77 $foldersList = $foldersList[0];
79 return $response->withJson($foldersList, 200);
91 public function createFolder($request, $response, $args)
94 $queryParams = $request->getQueryParams();
95 $parentFolder = $queryParams[
'parentFolder'] ??
'';
96 $folderName =
trim($queryParams[
'folderName'] ??
'');
97 $folderDescription =
trim($queryParams[
'folderDescription'] ??
'');
99 $parentFolder = $request->getHeaderLine(
'parentFolder');
100 $folderName =
trim($request->getHeaderLine(
'folderName'));
101 $folderDescription =
trim($request->getHeaderLine(
'folderDescription'));
104 if (! is_numeric($parentFolder) || $parentFolder < 0) {
105 throw new HttpBadRequestException(
106 "Parent folder id must be a positive integer!");
108 if (empty($folderName)) {
109 throw new HttpBadRequestException(
"Folder name can not be empty!");
111 if (! $this->restHelper->getFolderDao()->isFolderAccessible($parentFolder,
112 $this->restHelper->getUserId())) {
113 throw new HttpForbiddenException(
"Parent folder is not accessible!");
116 $folderCreate = $this->restHelper->getPlugin(
'folder_create');
117 $rc = $folderCreate->create($parentFolder, $folderName, $folderDescription);
119 $info =
new Info(200,
"Folder $folderName already exists!", InfoType::INFO);
120 } elseif ($rc == 0) {
121 throw new HttpNotFoundException(
"Parent folder not found!");
123 $folderId = $this->restHelper->getFolderDao()->getFolderId($folderName, $parentFolder);
124 $info =
new Info(201, intval($folderId), InfoType::INFO);
126 return $response->withJson($info->getArray(), $info->getCode());
138 public function deleteFolder($request, $response, $args)
141 $folderDao = $this->restHelper->getFolderDao();
142 $folderId = $args[
'id'];
144 if (! is_numeric($folderId) || $folderId < 0) {
145 throw new HttpBadRequestException(
146 "Folder id must be a positive integer!");
148 if ($folderDao->getFolder($folderId) ===
null) {
149 throw new HttpNotFoundException(
"Folder id not found!");
152 $folderDelete = $this->restHelper->getPlugin(
'admin_folder_delete');
155 $folderParent = intval($folderArray[count($folderArray) - 2][
'folder_pk']);
156 $folderId =
"$folderParent $folderId";
158 $rc = $folderDelete->Delete($folderId, $this->restHelper->getUserId());
159 if ($rc ==
"No access to delete this folder") {
160 throw new HttpForbiddenException($rc);
161 } elseif ($rc !==
null) {
162 throw new HttpInternalServerErrorException($rc);
164 $info =
new Info(202,
"Folder, \"$folderName\" deleted.", InfoType::INFO);
165 return $response->withJson($info->getArray(), $info->getCode());
177 public function editFolder($request, $response, $args)
179 $folderDao = $this->restHelper->getFolderDao();
180 $folderId = $args[
'id'];
182 $queryParams = $request->getQueryParams();
183 $newName = $queryParams[
'name'] ??
'';
184 $newDesc = $queryParams[
'description'] ??
'';
186 $newName = $request->getHeaderLine(
'name');
187 $newDesc = $request->getHeaderLine(
'description');
190 if ($folderDao->getFolder($folderId) ===
null) {
191 throw new HttpNotFoundException(
"Folder id not found!");
193 if (! $folderDao->isFolderAccessible($folderId, $this->restHelper->getUserId())) {
194 throw new HttpForbiddenException(
"Folder is not accessible!");
197 $folderEdit = $this->restHelper->getPlugin(
'folder_properties');
199 $rc = $folderEdit->Edit($folderId, $newName, $newDesc);
201 $newName =
trim($newName);
202 $info =
new Info(200,
"Folder $newName already exists!", InfoType::INFO);
204 $info =
new Info(200,
"Folder \"$folderName\" updated.", InfoType::INFO);
206 return $response->withJson($info->getArray(), $info->getCode());
218 public function copyFolder($request, $response, $args)
220 $folderDao = $this->restHelper->getFolderDao();
221 $folderId = $args[
'id'];
223 $queryParams = $request->getQueryParams();
224 $newParent = $queryParams[
'parent'] ??
'';
225 $action = strtolower($queryParams[
'action'] ??
'');
227 $newParent = $request->getHeaderLine(
'parent');
228 $action = strtolower($request->getHeaderLine(
'action'));
231 if (! is_numeric($newParent) || $newParent < 0) {
232 throw new HttpBadRequestException(
233 "Parent id must be a positive integer!");
235 if ($folderDao->getFolder($folderId) ===
null) {
236 throw new HttpNotFoundException(
"Folder id not found!");
238 if ($folderDao->getFolder($newParent) ===
null) {
239 throw new HttpNotFoundException(
"Parent folder id not found!");
241 if (! $folderDao->isFolderAccessible($folderId,
242 $this->restHelper->getUserId())) {
243 throw new HttpForbiddenException(
"Folder is not accessible!");
245 if (! $folderDao->isFolderAccessible($newParent,
246 $this->restHelper->getUserId())) {
247 throw new HttpForbiddenException(
"Parent folder is not accessible!");
249 if (strcmp($action,
"copy") != 0 && strcmp($action,
"move") != 0) {
250 throw new HttpBadRequestException(
251 "Action can be one of [copy,move]!");
254 $folderMove = $this->restHelper->getPlugin(
'content_move');
257 $isCopy = (strcmp($action,
"copy") == 0);
258 $message = $folderMove->copyContent(
260 $folderDao->getFolderContentsId($folderId, $folderDao::MODE_FOLDER)
261 ], $newParent, $isCopy);
262 if (!empty($message)) {
263 throw new HttpInternalServerErrorException($message);
265 $info =
new Info(202,
266 "Folder \"$folderName\" $action(ed) under \"$parentFolderName\".",
268 return $response->withJson($info->getArray(), $info->getCode());
280 public function getUnlinkableFolderContents($request, $response, $args)
282 $folderId = $args[
'id'];
283 $folderDao = $this->restHelper->getFolderDao();
285 if ($folderDao->getFolder($folderId) ===
null) {
286 throw new HttpNotFoundException(
"Folder id not found!");
288 if (! $folderDao->isFolderAccessible($folderId, $this->restHelper->getUserId())) {
289 throw new HttpForbiddenException(
"Folder is not accessible!");
293 $folderContents = $this->restHelper->getPlugin(
'foldercontents');
294 $symfonyRequest = new \Symfony\Component\HttpFoundation\Request();
295 $symfonyRequest->request->set(
'folder', $folderId);
296 $symfonyRequest->request->set(
'removable', 1);
297 $symfonyRequest->request->set(
'fromRest',
true);
298 $res = $folderContents->handle($symfonyRequest);
299 return $response->withJson($res, 200);
311 public function unlinkFolder($request, $response, $args)
313 $folderContentId = $args[
'contentId'];
314 if (!$this->dbHelper->doesIdExist(
"foldercontents",
"foldercontents_pk", $folderContentId)) {
315 throw new HttpNotFoundException(
"Folder content id not found!");
318 $folderDao = $this->restHelper->getFolderDao();
319 $content = $folderDao->getContent($folderContentId);
320 if (! $folderDao->isFolderAccessible($content[
'parent_fk'], $this->restHelper->getUserId())) {
321 throw new HttpForbiddenException(
"Folder is not accessible!");
323 if (!$folderDao->removeContent($folderContentId)) {
324 throw new HttpBadRequestException(
"Content cannot be unlinked.");
326 $info =
new Info(200,
"Folder unlinked successfully.", InfoType::INFO);
327 return $response->withJson($info->getArray(), $info->getCode());
339 public function getAllFolderContents($request, $response, $args)
341 $folderId = $args[
'id'];
342 $folderDao = $this->restHelper->getFolderDao();
344 if ($folderDao->getFolder($folderId) ===
null) {
345 throw new HttpNotFoundException(
"Folder id not found!");
347 if (! $folderDao->isFolderAccessible($folderId, $this->restHelper->getUserId())) {
348 throw new HttpForbiddenException(
"Folder is not accessible!");
352 $folderContents = $this->restHelper->getPlugin(
'foldercontents');
353 $symfonyRequest = new \Symfony\Component\HttpFoundation\Request();
354 $symfonyRequest->request->set(
'folder', $folderId);
355 $symfonyRequest->request->set(
'fromRest',
true);
356 $contentList = $folderContents->handle($symfonyRequest);
357 $removableContents = $folderDao->getRemovableContents($folderId);
359 foreach ($contentList as &$value) {
360 if (in_array($value[
'id'], $removableContents)) {
361 $value[
'removable'] =
true;
364 return $response->withJson($contentList, 200);
Controller for Folder model.
getFolders($request, $response, $args)
Base controller for REST calls.
Override Slim response for withJson function.
static getVersion(ServerRequestInterface $request)
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.