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)) {
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 $folderEdit->Edit($folderId, $newName, $newDesc);
200 $info =
new Info(200,
"Folder \"$folderName\" updated.", InfoType::INFO);
201 return $response->withJson($info->getArray(), $info->getCode());
213 public function copyFolder($request, $response, $args)
215 $folderDao = $this->restHelper->getFolderDao();
216 $folderId = $args[
'id'];
218 $queryParams = $request->getQueryParams();
219 $newParent = $queryParams[
'parent'];
220 $action = strtolower($queryParams[
'action']);
222 $newParent = $request->getHeaderLine(
'parent');
223 $action = strtolower($request->getHeaderLine(
'action'));
226 if (! is_numeric($newParent) || $newParent < 0) {
227 throw new HttpBadRequestException(
228 "Parent id must be a positive integer!");
230 if ($folderDao->getFolder($folderId) ===
null) {
231 throw new HttpNotFoundException(
"Folder id not found!");
233 if ($folderDao->getFolder($newParent) ===
null) {
234 throw new HttpNotFoundException(
"Parent folder id not found!");
236 if (! $folderDao->isFolderAccessible($folderId,
237 $this->restHelper->getUserId())) {
238 throw new HttpForbiddenException(
"Folder is not accessible!");
240 if (! $folderDao->isFolderAccessible($newParent,
241 $this->restHelper->getUserId())) {
242 throw new HttpForbiddenException(
"Parent folder is not accessible!");
244 if (strcmp($action,
"copy") != 0 && strcmp($action,
"move") != 0) {
245 throw new HttpBadRequestException(
246 "Action can be one of [copy,move]!");
249 $folderMove = $this->restHelper->getPlugin(
'content_move');
252 $isCopy = (strcmp($action,
"copy") == 0);
253 $message = $folderMove->copyContent(
255 $folderDao->getFolderContentsId($folderId, $folderDao::MODE_FOLDER)
256 ], $newParent, $isCopy);
257 if (!empty($message)) {
258 throw new HttpInternalServerErrorException($message);
260 $info =
new Info(202,
261 "Folder \"$folderName\" $action(ed) under \"$parentFolderName\".",
263 return $response->withJson($info->getArray(), $info->getCode());
275 public function getUnlinkableFolderContents($request, $response, $args)
277 $folderId = $args[
'id'];
278 $folderDao = $this->restHelper->getFolderDao();
280 if ($folderDao->getFolder($folderId) ===
null) {
281 throw new HttpNotFoundException(
"Folder id not found!");
283 if (! $folderDao->isFolderAccessible($folderId, $this->restHelper->getUserId())) {
284 throw new HttpForbiddenException(
"Folder is not accessible!");
288 $folderContents = $this->restHelper->getPlugin(
'foldercontents');
289 $symfonyRequest = new \Symfony\Component\HttpFoundation\Request();
290 $symfonyRequest->request->set(
'folder', $folderId);
291 $symfonyRequest->request->set(
'removable', 1);
292 $symfonyRequest->request->set(
'fromRest',
true);
293 $res = $folderContents->handle($symfonyRequest);
294 return $response->withJson($res, 200);
306 public function unlinkFolder($request, $response, $args)
308 $folderContentId = $args[
'contentId'];
309 if (!$this->dbHelper->doesIdExist(
"foldercontents",
"foldercontents_pk", $folderContentId)) {
310 throw new HttpNotFoundException(
"Folder content id not found!");
313 $folderDao = $this->container->get(
'dao.folder');
314 if (!$folderDao->removeContent($folderContentId)) {
315 throw new HttpBadRequestException(
"Content cannot be unlinked.");
317 $info =
new Info(200,
"Folder unlinked successfully.", InfoType::INFO);
318 return $response->withJson($info->getArray(), $info->getCode());
330 public function getAllFolderContents($request, $response, $args)
332 $folderId = $args[
'id'];
333 $folderDao = $this->restHelper->getFolderDao();
335 if ($folderDao->getFolder($folderId) ===
null) {
336 throw new HttpNotFoundException(
"Folder id not found!");
338 if (! $folderDao->isFolderAccessible($folderId, $this->restHelper->getUserId())) {
339 throw new HttpForbiddenException(
"Folder is not accessible!");
343 $folderContents = $this->restHelper->getPlugin(
'foldercontents');
344 $symfonyRequest = new \Symfony\Component\HttpFoundation\Request();
345 $symfonyRequest->request->set(
'folder', $folderId);
346 $symfonyRequest->request->set(
'fromRest',
true);
347 $contentList = $folderContents->handle($symfonyRequest);
348 $removableContents = $folderDao->getRemovableContents($folderId);
350 foreach ($contentList as &$value) {
351 if (in_array($value[
'id'], $removableContents)) {
352 $value[
'removable'] =
true;
355 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.