FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
reuser-plugin.php
Go to the documentation of this file.
1 <?php
2 /***********************************************************
3  * Copyright (C) 2014-2018, Siemens AG
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  ***********************************************************/
21 namespace Fossology\Reuser;
22 
29 
30 include_once(__DIR__ . "/../agent/version.php");
31 
37 {
38  const NAME = "plugin_reuser";
39 
40  const REUSE_FOLDER_SELECTOR_NAME = 'reuseFolderSelectorName';
41  const UPLOAD_TO_REUSE_SELECTOR_NAME = 'uploadToReuse';
42  const FOLDER_PARAMETER_NAME = 'folder';
43 
47  public $AgentName = 'agent_reuser';
51  private $folderDao;
52 
53  public function __construct()
54  {
55  parent::__construct(self::NAME, array(
56  self::TITLE => _("Automatic Clearing Decision Reuser"),
57  self::PERMISSION => Auth::PERM_WRITE
58  ));
59 
60  $this->folderDao = $this->getObject('dao.folder');
61  }
62 
70  function getAllUploads()
71  {
72  $allFolder = $this->folderDao->getAllFolderIds();
73  $result = array();
74  for ($i=0; $i < sizeof($allFolder); $i++) {
75  $listObject = $this->prepareFolderUploads($allFolder[$i]);
76  foreach ($listObject as $key => $value) {
77  $result[explode(",",$key)[0]] = $value;
78  }
79  }
80  return $result;
81  }
82 
87  protected function handle(Request $request)
88  {
89  $this->folderDao->ensureTopLevelFolder();
90  list($folderId, $trustGroupId) = $this->getFolderIdAndTrustGroup($request->get(self::FOLDER_PARAMETER_NAME));
91  $ajaxMethodName = $request->get('do');
92 
93  if ($ajaxMethodName == "getUploads") {
94  $uploadsById = "";
95  if (empty($folderId) || empty($trustGroupId)) {
96  $uploadsById = $this->getAllUploads();
97  } else {
98  $uploadsById = $this->prepareFolderUploads($folderId, $trustGroupId);
99  }
100  return new JsonResponse($uploadsById, JsonResponse::HTTP_OK);
101  }
102 
103  return new Response('called without valid method', Response::HTTP_METHOD_NOT_ALLOWED);
104  }
105 
111  public function getFolderIdAndTrustGroup($folderGroup)
112  {
113  $folderGroupPair = explode(',', $folderGroup,2);
114  if (count($folderGroupPair) == 2) {
115  list($folder, $trustGroup) = $folderGroupPair;
116  $folderId = intval($folder);
117  $trustGroupId = intval($trustGroup);
118  } else {
119  $trustGroupId = Auth::getGroupId();
120  $folderId = 0;
121  }
122  return array($folderId, $trustGroupId);
123  }
124 
130  public function renderContent(&$vars)
131  {
132  if (!array_key_exists('folderStructure', $vars)) {
133  $rootFolderId = $this->folderDao->getRootFolder(Auth::getUserId())->getId();
134  $vars['folderStructure'] = $this->folderDao->getFolderStructure($rootFolderId);
135  }
136  if ($this->folderDao->isWithoutReusableFolders($vars['folderStructure'])) {
137  return '';
138  }
139  $pair = array_key_exists(self::FOLDER_PARAMETER_NAME, $vars) ? $vars[self::FOLDER_PARAMETER_NAME] : '';
140 
141  list($folderId, $trustGroupId) = $this->getFolderIdAndTrustGroup($pair);
142  if (empty($folderId) && !empty($vars['folderStructure'])) {
143  $folderId = $vars['folderStructure'][0][FolderDao::FOLDER_KEY]->getId();
144  }
145 
146  $vars['reuseFolderSelectorName'] = self::REUSE_FOLDER_SELECTOR_NAME;
147  $vars['folderParameterName'] = self::FOLDER_PARAMETER_NAME;
148  $vars['uploadToReuseSelectorName'] = self::UPLOAD_TO_REUSE_SELECTOR_NAME;
149  $vars['folderUploads'] = $this->prepareFolderUploads($folderId, $trustGroupId);
150 
151  $renderer = $this->getObject('twig.environment');
152  return $renderer->loadTemplate('agent_reuser.html.twig')->render($vars);
153  }
154 
160  public function renderFoot(&$vars)
161  {
162  $vars['reuseFolderSelectorName'] = self::REUSE_FOLDER_SELECTOR_NAME;
163  $vars['folderParameterName'] = self::FOLDER_PARAMETER_NAME;
164  $vars['uploadToReuseSelectorName'] = self::UPLOAD_TO_REUSE_SELECTOR_NAME;
165  $renderer = $this->getObject('twig.environment');
166  return $renderer->loadTemplate('agent_reuser.js.twig')->render($vars);
167  }
168 
178  protected function prepareFolderUploads($folderId, $trustGroupId=null)
179  {
180  if (empty($trustGroupId)) {
181  $trustGroupId = Auth::getGroupId();
182  }
183  $folderUploads = $this->folderDao->getFolderUploads($folderId, $trustGroupId);
184 
185  $uploadsById = array();
186  foreach ($folderUploads as $uploadProgress) {
187  $key = $uploadProgress->getId().','.$uploadProgress->getGroupId();
188  $display = $uploadProgress->getFilename() . _(" from ")
189  . Convert2BrowserTime(date("Y-m-d H:i:s",$uploadProgress->getTimestamp()))
190  . ' ('. $uploadProgress->getStatusString() . ')';
191  $uploadsById[$key] = $display;
192  }
193  return $uploadsById;
194  }
195 }
196 
197 register_plugin(new ReuserPlugin());
getAllUploads()
Get all uploads accessible to curent user.
static getUserId()
Get the current user&#39;s id.
Definition: Auth.php:69
renderContent(&$vars)
Load the data in array and render twig template.
renderFoot(&$vars)
Render footer template.
Convert2BrowserTime($server_time)
Convert the server time to browser time.
Definition: common-ui.php:298
const FOLDER_PARAMETER_NAME
Folder parameter HTML element name.
const UPLOAD_TO_REUSE_SELECTOR_NAME
Upload to reuse HTML element name.
prepareFolderUploads($folderId, $trustGroupId=null)
For a given folder id, collect all uploads.
const REUSE_FOLDER_SELECTOR_NAME
Reuse upload folder element name.
UI plugin for reuser.
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Definition: nomos.h:321
static getGroupId()
Get the current user&#39;s group id.
Definition: Auth.php:78
getFolderIdAndTrustGroup($folderGroup)
For a given folder group, extract forder id and trust group id.