FOSSology  4.4.0
Open Source License Compliance by Open Source Software
reuser-plugin.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2018 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
10 namespace Fossology\Reuser;
11 
15 use Symfony\Component\HttpFoundation\JsonResponse;
16 use Symfony\Component\HttpFoundation\Request;
17 use Symfony\Component\HttpFoundation\Response;
18 
19 include_once(__DIR__ . "/../agent/version.php");
20 
26 {
27  const NAME = "plugin_reuser";
28 
29  const REUSE_FOLDER_SELECTOR_NAME = 'reuseFolderSelectorName';
30  const UPLOAD_TO_REUSE_SELECTOR_NAME = 'uploadToReuse';
31  const FOLDER_PARAMETER_NAME = 'folder';
32 
36  public $AgentName = 'agent_reuser';
40  private $folderDao;
41 
42  public function __construct()
43  {
44  parent::__construct(self::NAME, array(
45  self::TITLE => _("Automatic Clearing Decision Reuser"),
46  self::PERMISSION => Auth::PERM_WRITE
47  ));
48 
49  $this->folderDao = $this->getObject('dao.folder');
50  }
51 
59  function getAllUploads()
60  {
61  $allFolder = $this->folderDao->getAllFolderIds();
62  $result = array();
63  for ($i=0; $i < sizeof($allFolder); $i++) {
64  $listObject = $this->prepareFolderUploads($allFolder[$i]);
65  foreach ($listObject as $key => $value) {
66  $result[explode(",",$key)[0]] = $value;
67  }
68  }
69  return $result;
70  }
71 
76  protected function handle(Request $request)
77  {
78  $this->folderDao->ensureTopLevelFolder();
79  list($folderId, $trustGroupId) = $this->getFolderIdAndTrustGroup($request->get(self::FOLDER_PARAMETER_NAME));
80  $ajaxMethodName = $request->get('do');
81 
82  if ($ajaxMethodName == "getUploads") {
83  $uploadsById = "";
84  if (empty($folderId) || empty($trustGroupId)) {
85  $uploadsById = $this->getAllUploads();
86  } else {
87  $uploadsById = $this->prepareFolderUploads($folderId, $trustGroupId);
88  }
89  return new JsonResponse($uploadsById, JsonResponse::HTTP_OK);
90  }
91 
92  return new Response('called without valid method', Response::HTTP_METHOD_NOT_ALLOWED);
93  }
94 
100  public function getFolderIdAndTrustGroup($folderGroup)
101  {
102  $folderGroupPair = explode(',', $folderGroup,2);
103  if (count($folderGroupPair) == 2) {
104  list($folder, $trustGroup) = $folderGroupPair;
105  $folderId = intval($folder);
106  $trustGroupId = intval($trustGroup);
107  } else {
108  $trustGroupId = Auth::getGroupId();
109  $folderId = 0;
110  }
111  return array($folderId, $trustGroupId);
112  }
113 
119  public function renderContent(&$vars)
120  {
121  if (!array_key_exists('folderStructure', $vars)) {
122  $rootFolderId = $this->folderDao->getRootFolder(Auth::getUserId())->getId();
123  $vars['folderStructure'] = $this->folderDao->getFolderStructure($rootFolderId);
124  }
125  if ($this->folderDao->isWithoutReusableFolders($vars['folderStructure'])) {
126  return '';
127  }
128  $pair = array_key_exists(self::FOLDER_PARAMETER_NAME, $vars) ? $vars[self::FOLDER_PARAMETER_NAME] : '';
129 
130  list($folderId, $trustGroupId) = $this->getFolderIdAndTrustGroup($pair);
131  if (empty($folderId) && !empty($vars['folderStructure'])) {
132  $folderId = $vars['folderStructure'][0][FolderDao::FOLDER_KEY]->getId();
133  }
134 
135  $vars['reuseFolderSelectorName'] = self::REUSE_FOLDER_SELECTOR_NAME;
136  $vars['folderParameterName'] = self::FOLDER_PARAMETER_NAME;
137  $vars['uploadToReuseSelectorName'] = self::UPLOAD_TO_REUSE_SELECTOR_NAME;
138  $vars['folderUploads'] = $this->prepareFolderUploads($folderId, $trustGroupId);
139 
140  $renderer = $this->getObject('twig.environment');
141  return $renderer->load('agent_reuser.html.twig')->render($vars);
142  }
143 
149  public function renderFoot(&$vars)
150  {
151  $vars['reuseFolderSelectorName'] = self::REUSE_FOLDER_SELECTOR_NAME;
152  $vars['folderParameterName'] = self::FOLDER_PARAMETER_NAME;
153  $vars['uploadToReuseSelectorName'] = self::UPLOAD_TO_REUSE_SELECTOR_NAME;
154  $renderer = $this->getObject('twig.environment');
155  return $renderer->load('agent_reuser.js.twig')->render($vars);
156  }
157 
167  protected function prepareFolderUploads($folderId, $trustGroupId=null)
168  {
169  if (empty($trustGroupId)) {
170  $trustGroupId = Auth::getGroupId();
171  }
172  $folderUploads = $this->folderDao->getFolderUploads($folderId, $trustGroupId);
173 
174  $uploadsById = array();
175  foreach ($folderUploads as $uploadProgress) {
176  $key = $uploadProgress->getId().','.$uploadProgress->getGroupId();
177  $display = $uploadProgress->getFilename() . _(" from ")
178  . Convert2BrowserTime(date("Y-m-d H:i:s",$uploadProgress->getTimestamp()))
179  . ' ('. $uploadProgress->getStatusString() . ')';
180  $uploadsById[$key] = $display;
181  }
182  return $uploadsById;
183  }
184 }
185 
186 register_plugin(new ReuserPlugin());
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
static getUserId()
Get the current user's id.
Definition: Auth.php:68
static getGroupId()
Get the current user's group id.
Definition: Auth.php:80
UI plugin for reuser.
getFolderIdAndTrustGroup($folderGroup)
For a given folder group, extract forder id and trust group id.
renderFoot(&$vars)
Render footer template.
const REUSE_FOLDER_SELECTOR_NAME
Reuse upload folder element name.
renderContent(&$vars)
Load the data in array and render twig template.
const FOLDER_PARAMETER_NAME
Folder parameter HTML element name.
prepareFolderUploads($folderId, $trustGroupId=null)
For a given folder id, collect all uploads.
getAllUploads()
Get all uploads accessible to curent user.
const UPLOAD_TO_REUSE_SELECTOR_NAME
Upload to reuse HTML element name.
Convert2BrowserTime($server_time)
Convert the server time to browser time.
Definition: common-ui.php:312
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Definition: nomos.h:308