FOSSology  4.4.0
Open Source License Compliance by Open Source Software
AjaxFolderContents.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2015, 2022 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\UI\Ajax;
9 
15 use Symfony\Component\HttpFoundation\JsonResponse;
16 use Symfony\Component\HttpFoundation\Request;
17 
19 {
20  const NAME = "foldercontents";
21 
26  private $folderDao;
27 
28  function __construct()
29  {
30  parent::__construct(self::NAME, array(
31  self::PERMISSION => Auth::PERM_WRITE
32  ));
33  $this->folderDao = $this->getObject('dao.folder');
34  }
35 
36  public function handle(Request $request)
37  {
38  $folderId = intval($request->get('folder'));
39  $uploadName = $request->get('upload');
40  if (!empty($uploadName)) {
41  return $this->uploadExists(Auth::getGroupId(), $folderId, $uploadName);
42  }
43  $results = array();
44  $childFolders = $this->folderDao->getFolderChildFolders($folderId);
45  foreach ($childFolders as $folder) {
46  $results[$folder['foldercontents_pk']] = '/'.$folder['folder_name'];
47  }
48  $childUploads = $this->folderDao->getFolderChildUploads($folderId, Auth::getGroupId());
49  foreach ($childUploads as $upload) {
50  $uploadStatus = new UploadStatus();
51  $uploadDate = explode(".", $upload['upload_ts'])[0];
52  $uploadStatus = " (" . $uploadStatus->getTypeName($upload['status_fk']) . ")";
53  $results[$upload['foldercontents_pk']] = $upload['upload_filename'] . _(" from ") . Convert2BrowserTime($uploadDate) . $uploadStatus;
54  }
55 
56  if (!$request->get('removable')) {
57  if ($request->get('fromRest')) {
58  return array_map(function($key, $value) {
59  return array(
60  'id' => $key,
61  'content' => $value,
62  'removable' => false
63  );
64  }, array_keys($results), $results);
65  }
66  return new JsonResponse($results);
67  }
68 
69  $filterResults = array();
70  foreach ($this->folderDao->getRemovableContents($folderId) as $content) {
71  $filterResults[$content] = $results[$content];
72  }
73 
74  if ($request->get('fromRest')) {
75  return array_map(function ($key, $value) {
76  return array(
77  'id' => $key,
78  'content' => $value,
79  'removable' => true
80  );
81  }, array_keys($filterResults), $filterResults);
82  }
83 
84  if (empty($filterResults)) {
85  $filterResults["-1"] = "No removable content found";
86  }
87 
88  return new JsonResponse($filterResults);
89  }
90 
102  private function uploadExists($groupId, $folderId, $uploadName)
103  {
104  $childUploads = $this->folderDao->getFolderChildUploads($folderId, $groupId);
105  $found = false;
106  $date = null;
107  foreach ($childUploads as $upload) {
108  if (strcasecmp($upload['upload_filename'], $uploadName) === 0) {
109  $found = $upload['upload_pk'];
110  $date = Convert2BrowserTime($upload['upload_ts']);
111  break;
112  }
113  }
114  if ($found) {
119  $uploadDao = $this->getObject('dao.upload');
120  $parent = $uploadDao->getUploadParent($found);
121  $found = tracebackTotalUri() . "?mod=browse&upload=$found&item=$parent";
122  }
123  return new JsonResponse(["upload" => $found, "date" => $date]);
124  }
125 }
126 
127 register_plugin(new AjaxFolderContents());
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
static getGroupId()
Get the current user's group id.
Definition: Auth.php:80
tracebackTotalUri()
Get the total url without query.
Convert2BrowserTime($server_time)
Convert the server time to browser time.
Definition: common-ui.php:312