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');
153 $rootFolder = $folderDao->getRootFolder($this->restHelper->getUserId());
154 if ($rootFolder !==
null && $rootFolder->getId() == $folderId) {
155 throw new HttpForbiddenException(
"Can not delete the root folder!");
159 if (count($folderArray) < 2) {
160 throw new HttpForbiddenException(
"Can not delete a folder without a parent!");
162 $folderParent = intval($folderArray[count($folderArray) - 2][
'folder_pk']);
163 $folderId =
"$folderParent $folderId";
165 $rc = $folderDelete->Delete($folderId, $this->restHelper->getUserId());
166 if ($rc ==
"No access to delete this folder" ||
167 $rc ==
"Can Not Delete Root Folder") {
168 throw new HttpForbiddenException($rc);
169 } elseif ($rc !==
null) {
170 throw new HttpInternalServerErrorException($rc);
172 $info =
new Info(202,
"Folder, \"$folderName\" deleted.", InfoType::INFO);
173 return $response->withJson($info->getArray(), $info->getCode());
185 public function editFolder($request, $response, $args)
187 $folderDao = $this->restHelper->getFolderDao();
188 $folderId = $args[
'id'];
190 $queryParams = $request->getQueryParams();
191 $newName = $queryParams[
'name'] ??
'';
192 $newDesc = $queryParams[
'description'] ??
'';
194 $newName = $request->getHeaderLine(
'name');
195 $newDesc = $request->getHeaderLine(
'description');
198 if ($folderDao->getFolder($folderId) ===
null) {
199 throw new HttpNotFoundException(
"Folder id not found!");
201 if (! $folderDao->isFolderAccessible($folderId, $this->restHelper->getUserId())) {
202 throw new HttpForbiddenException(
"Folder is not accessible!");
205 $folderEdit = $this->restHelper->getPlugin(
'folder_properties');
207 $rc = $folderEdit->Edit($folderId, $newName, $newDesc);
209 $newName =
trim($newName);
210 $info =
new Info(200,
"Folder $newName already exists!", InfoType::INFO);
212 $info =
new Info(200,
"Folder \"$folderName\" updated.", InfoType::INFO);
214 return $response->withJson($info->getArray(), $info->getCode());
226 public function copyFolder($request, $response, $args)
228 $folderDao = $this->restHelper->getFolderDao();
229 $folderId = $args[
'id'];
231 $queryParams = $request->getQueryParams();
232 $newParent = $queryParams[
'parent'] ??
'';
233 $action = strtolower($queryParams[
'action'] ??
'');
235 $newParent = $request->getHeaderLine(
'parent');
236 $action = strtolower($request->getHeaderLine(
'action'));
239 if (! is_numeric($newParent) || $newParent < 0) {
240 throw new HttpBadRequestException(
241 "Parent id must be a positive integer!");
243 if ($folderDao->getFolder($folderId) ===
null) {
244 throw new HttpNotFoundException(
"Folder id not found!");
246 if ($folderDao->getFolder($newParent) ===
null) {
247 throw new HttpNotFoundException(
"Parent folder id not found!");
249 if (! $folderDao->isFolderAccessible($folderId,
250 $this->restHelper->getUserId())) {
251 throw new HttpForbiddenException(
"Folder is not accessible!");
253 if (! $folderDao->isFolderAccessible($newParent,
254 $this->restHelper->getUserId())) {
255 throw new HttpForbiddenException(
"Parent folder is not accessible!");
257 if (strcmp($action,
"copy") != 0 && strcmp($action,
"move") != 0) {
258 throw new HttpBadRequestException(
259 "Action can be one of [copy,move]!");
262 $folderMove = $this->restHelper->getPlugin(
'content_move');
265 $isCopy = (strcmp($action,
"copy") == 0);
266 $message = $folderMove->copyContent(
268 $folderDao->getFolderContentsId($folderId, $folderDao::MODE_FOLDER)
269 ], $newParent, $isCopy);
270 if (!empty($message)) {
271 throw new HttpInternalServerErrorException($message);
273 $info =
new Info(202,
274 "Folder \"$folderName\" $action(ed) under \"$parentFolderName\".",
276 return $response->withJson($info->getArray(), $info->getCode());
288 public function getUnlinkableFolderContents($request, $response, $args)
290 $folderId = $args[
'id'];
291 $folderDao = $this->restHelper->getFolderDao();
293 if ($folderDao->getFolder($folderId) ===
null) {
294 throw new HttpNotFoundException(
"Folder id not found!");
296 if (! $folderDao->isFolderAccessible($folderId, $this->restHelper->getUserId())) {
297 throw new HttpForbiddenException(
"Folder is not accessible!");
301 $folderContents = $this->restHelper->getPlugin(
'foldercontents');
302 $symfonyRequest = new \Symfony\Component\HttpFoundation\Request();
303 $symfonyRequest->request->set(
'folder', $folderId);
304 $symfonyRequest->request->set(
'removable', 1);
305 $symfonyRequest->request->set(
'fromRest',
true);
306 $res = $folderContents->handle($symfonyRequest);
307 return $response->withJson($res, 200);
319 public function unlinkFolder($request, $response, $args)
321 $folderContentId = $args[
'contentId'];
322 if (!$this->dbHelper->doesIdExist(
"foldercontents",
"foldercontents_pk", $folderContentId)) {
323 throw new HttpNotFoundException(
"Folder content id not found!");
326 $folderDao = $this->restHelper->getFolderDao();
327 $content = $folderDao->getContent($folderContentId);
328 if (! $folderDao->isFolderAccessible($content[
'parent_fk'], $this->restHelper->getUserId())) {
329 throw new HttpForbiddenException(
"Folder is not accessible!");
331 if (!$folderDao->removeContent($folderContentId)) {
332 throw new HttpBadRequestException(
"Content cannot be unlinked.");
334 $info =
new Info(200,
"Folder unlinked successfully.", InfoType::INFO);
335 return $response->withJson($info->getArray(), $info->getCode());
347 public function getAllFolderContents($request, $response, $args)
349 $folderId = $args[
'id'];
350 $folderDao = $this->restHelper->getFolderDao();
352 if ($folderDao->getFolder($folderId) ===
null) {
353 throw new HttpNotFoundException(
"Folder id not found!");
355 if (! $folderDao->isFolderAccessible($folderId, $this->restHelper->getUserId())) {
356 throw new HttpForbiddenException(
"Folder is not accessible!");
360 $folderContents = $this->restHelper->getPlugin(
'foldercontents');
361 $symfonyRequest = new \Symfony\Component\HttpFoundation\Request();
362 $symfonyRequest->request->set(
'folder', $folderId);
363 $symfonyRequest->request->set(
'fromRest',
true);
364 $contentList = $folderContents->handle($symfonyRequest);
365 $removableContents = $folderDao->getRemovableContents($folderId);
367 foreach ($contentList as &$value) {
368 if (in_array($value[
'id'], $removableContents)) {
369 $value[
'removable'] =
true;
372 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.