FOSSology  4.4.0
Open Source License Compliance by Open Source Software
ui-clearing-view.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2017 Siemens AG
4  Author: Daniele Fognini, Johannes Najjar
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
30 use Monolog\Logger;
31 use Symfony\Component\HttpFoundation\RedirectResponse;
32 use Symfony\Component\HttpFoundation\Response;
33 
34 define("TITLE_CLEARINGVIEW", _("Change concluded License "));
35 
36 class ClearingView extends FO_Plugin
37 {
39  private $uploadDao;
41  private $licenseDao;
43  private $clearingDao;
45  private $agentsDao;
47  private $logger;
49  private $highlightDao;
51  private $highlightProcessor;
53  private $highlightRenderer;
55  private $clearingDecisionEventProcessor;
57  private $clearingDecisionFilter;
59  private $invalidParm = false;
61  private $decisionTypes;
62 
63  function __construct()
64  {
65  $this->Name = "view-license";
66  $this->Title = TITLE_CLEARINGVIEW;
67  $this->DBaccess = PLUGIN_DB_WRITE;
68  $this->Dependency = array("view");
69  $this->LoginFlag = 0;
70  $this->NoMenu = 0;
71  parent::__construct();
72 
73  global $container;
74  $this->licenseDao = $container->get('dao.license');
75  $this->uploadDao = $container->get('dao.upload');
76  $this->clearingDao = $container->get('dao.clearing');
77  $this->agentsDao = $container->get('dao.agent');
78  $this->logger = $container->get("logger");
79  $this->highlightDao = $container->get("dao.highlight");
80  $this->highlightRenderer = $container->get("view.highlight_renderer");
81  $this->highlightProcessor = $container->get("view.highlight_processor");
82 
83  $this->decisionTypes = $container->get('decision.types');
84 
85  $this->clearingDecisionEventProcessor = $container->get(
86  'businessrules.clearing_decision_processor');
87  $this->clearingDecisionFilter = $container->get(
88  'businessrules.clearing_decision_filter');
89  }
90 
91 
101  public function getSelectedHighlighting(ItemTreeBounds $itemTreeBounds, $licenseId, $selectedAgentId, $highlightId, $clearingId, $uploadId)
102  {
103  $unmaskAgents = $selectedAgentId;
104  if (empty($selectedAgentId)) {
105  $scanJobProxy = new ScanJobProxy($this->agentsDao,$uploadId);
106  $scanJobProxy->createAgentStatus(array_keys(AgentRef::AGENT_LIST));
107  $unmaskAgents = $scanJobProxy->getLatestSuccessfulAgentIds();
108  }
109  $highlightEntries = $this->highlightDao->getHighlightEntries($itemTreeBounds,
110  $licenseId, $unmaskAgents, $highlightId, $clearingId);
111  $groupId = Auth::getGroupId();
112  if (($selectedAgentId > 0) || ($clearingId > 0)) {
113  $this->highlightProcessor->addReferenceTexts($highlightEntries, $groupId);
114  } else {
115  $this->highlightProcessor->flattenHighlights($highlightEntries, array("K", "K "));
116  }
117  return $highlightEntries;
118  }
119 
120  public function execute()
121  {
122  $openOutput = $this->OutputOpen();
123  if ($openOutput instanceof RedirectResponse) {
124  $response = $openOutput;
125  } else {
126  $response = $this->getResponse();
127  }
128  $response->prepare($this->getRequest());
129  $response->send();
130  }
131 
132  function OutputOpen()
133  {
134  if ($this->State != PLUGIN_STATE_READY) {
135  return (0);
136  }
137  $uploadId = GetParm("upload", PARM_INTEGER);
138  if (empty($uploadId)) {
139  return;
140  }
141 
142  $uploadTreeId = GetParm("item", PARM_INTEGER);
143  if (empty($uploadTreeId)) {
144  $parent = $this->uploadDao->getUploadParent($uploadId);
145  if (!isset($parent)) {
146  $this->invalidParm = true;
147  return;
148  }
149 
150  $item = $this->uploadDao->getNextItem($uploadId, $parent);
151  if ($item === UploadDao::NOT_FOUND) {
152  $this->invalidParm = true;
153  return;
154  }
155  $uploadTreeId = $item->getId();
156  return new RedirectResponse(Traceback_uri() . '?mod=' . $this->Name
157  . Traceback_parm_keep(array("upload", "show")) . "&item=$uploadTreeId");
158  }
159 
160  $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($uploadId);
161  $uploadEntry = $this->uploadDao->getUploadEntry($uploadTreeId, $uploadTreeTableName);
162  if (Isdir($uploadEntry['ufile_mode']) || Iscontainer($uploadEntry['ufile_mode'])) {
163  $parent = $this->uploadDao->getUploadParent($uploadId);
164  if (!isset($parent)) {
165  $this->invalidParm = true;
166  return;
167  }
168 
169  $item = $this->uploadDao->getNextItem($uploadId, $parent);
170  if ($item === UploadDao::NOT_FOUND) {
171  $this->invalidParm = true;
172  return;
173  }
174  $uploadTreeId = $item->getId();
175  return new RedirectResponse(Traceback_uri() . '?mod=' . $this->Name
176  . Traceback_parm_keep(array("upload", "show")) . "&item=$uploadTreeId");
177  }
178 
179  return parent::OutputOpen();
180  }
181 
182 
186  function Output()
187  {
188  if ($this->invalidParm) {
189  $this->vars['content'] = 'This upload contains no files!<br><a href="'
190  . Traceback_uri() . '?mod=browse">Go back to browse view</a>';
191  return $this->render("include/base.html.twig");
192  }
193 
194  $uploadId = GetParm("upload", PARM_INTEGER);
195  if (empty($uploadId)) {
196  return new Response("", Response::HTTP_BAD_REQUEST);
197  }
198  $uploadTreeId = GetParm("item", PARM_INTEGER);
199  if (empty($uploadTreeId)) {
200  return new Response("", Response::HTTP_BAD_REQUEST);
201  }
202 
203  $userId = Auth::getUserId();
204  $groupId = Auth::getGroupId();
205 
206  $lastItem = GetParm("lastItem", PARM_INTEGER);
207 
208  if (!empty($lastItem)) {
209  $currentUploadtreeId = $lastItem;
210  if ($lastItem == $uploadTreeId) {
211  $currentUploadtreeId = $uploadTreeId;
212  }
213  $this->updateLastItem($userId, $groupId, $lastItem, $currentUploadtreeId);
214  }
215 
216  $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($uploadId);
217  $itemTreeBounds = $this->uploadDao->getItemTreeBounds($uploadTreeId, $uploadTreeTableName);
218 
219  $this->vars['micromenu'] = Dir2Browse('license', $uploadTreeId, NULL,
220  $showBox = 0, "View", -1, '', '', $uploadTreeTableName);
221 
222  global $Plugins;
224  $view = &$Plugins[plugin_find_id("view")];
225 
226  $licenseId = GetParm("licenseId", PARM_INTEGER);
227  $selectedAgentId = GetParm("agentId", PARM_INTEGER);
228  $highlightId = GetParm("highlightId", PARM_INTEGER);
229  $clearingId = GetParm("clearingId", PARM_INTEGER);
230 
231  if ($clearingId !== null) {
232  $highlightId = -1;
233  } else if ($highlightId !== null) {
234  $clearingId = -1;
235  }
236 
237  $baseUri = Traceback_uri();
238  $this->vars['baseuri'] = $baseUri;
239  $this->vars['uri'] = $baseUri . "?mod=" . $this->Name . Traceback_parm_keep(array('upload', 'folder'));
240  $this->vars['bulkHistoryHighlightUri'] = $this->vars['uri'];
241  $this->vars['optionName'] = "skipFile";
242  $this->vars['formName'] = "uiClearingForm";
243  $this->vars['ajaxAction'] = "setNextPrev";
244  $highlights = $this->getSelectedHighlighting($itemTreeBounds, $licenseId,
245  $selectedAgentId, $highlightId, $clearingId, $uploadId);
246 
247  $isSingleFile = !$itemTreeBounds->containsFiles();
248  $hasWritePermission = $this->uploadDao->isEditable($uploadId, $groupId);
249 
250  $clearingDecisions = null;
251  if ($isSingleFile || $hasWritePermission) {
252  $clearingDecisions = $this->clearingDao->getFileClearings($itemTreeBounds, $groupId, false);
253  }
254 
255  if ($isSingleFile && $hasWritePermission) {
256  $this->vars['bulkUri'] = Traceback_uri() . "?mod=popup-license";
257  $licenseArray = $this->licenseDao->getLicenseArray($groupId);
258  list($addedResults, $removedResults) = $this->clearingDecisionEventProcessor->getCurrentClearings($itemTreeBounds, $groupId, LicenseMap::CONCLUSION);
259  if (count($addedResults)+count($removedResults)>0) {
260  array_unshift($licenseArray, array('id'=>0,'fullname'=>'','shortname'=>'------'));
261  }
263  foreach ($removedResults as $result) {
264  array_unshift($licenseArray, array( 'id'=>$result->getLicenseId() ,'fullname'=>$result->getLicenseFullName() ,'shortname'=>$result->getLicenseShortName()));
265  }
267  foreach ($addedResults as $result) {
268  array_unshift($licenseArray, array( 'id'=>$result->getLicenseId() ,'fullname'=>$result->getLicenseFullName() ,'shortname'=>$result->getLicenseShortName()));
269  }
270  $this->vars['licenseArray'] = $licenseArray;
271  } elseif ($isSingleFile) {
272  $this->vars['auditDenied'] = true;
273  }
274 
275  $selectedClearingType = false;
276  $selectedClearingScope = false;
277  if (!empty($clearingDecisions)) {
278  $selectedClearingType = $clearingDecisions[0]->getType();
279  $selectedClearingScope = $clearingDecisions[0]->getScope();
280  }
281  $bulkHistory = $this->clearingDao->getBulkHistory($itemTreeBounds, $groupId);
282 
283  $ModBack = GetParm("modback", PARM_STRING) ?: "license";
284  list($pageMenu, $textView) = $view->getView(NULL, $ModBack, 0, "", $highlights, false, true);
285 
286  $this->vars['uploadId'] = $uploadId;
287  $this->vars['itemId'] = $uploadTreeId;
288  $this->vars['pageMenu'] = $pageMenu;
289  $this->vars['textView'] = $textView;
290  $this->vars['legendData'] = $this->highlightRenderer->getLegendData($selectedAgentId || $clearingId);
291  $this->vars['clearingTypes'] = $this->decisionTypes->getMap();
292  $this->vars['selectedClearingType'] = $selectedClearingType;
293  $this->vars['selectedClearingScope'] = $selectedClearingScope;
294  $this->vars['tmpClearingType'] = $this->clearingDao->isDecisionCheck($uploadTreeId, $groupId, DecisionTypes::WIP);
295  $this->vars['bulkHistory'] = $bulkHistory;
296 
297  $noLicenseUploadTreeView = new UploadTreeProxy($uploadId,
298  array(UploadTreeProxy::OPT_SKIP_THESE => "noLicense",
299  UploadTreeProxy::OPT_GROUP_ID => $groupId),
300  $uploadTreeTableName,
301  'no_license_uploadtree' . $uploadId);
302  $filesOfInterest = $noLicenseUploadTreeView->count();
303 
304  $nonClearedUploadTreeView = new UploadTreeProxy($uploadId,
305  array(UploadTreeProxy::OPT_SKIP_THESE => "alreadyCleared",
306  UploadTreeProxy::OPT_GROUP_ID => $groupId),
307  $uploadTreeTableName,
308  'already_cleared_uploadtree' . $uploadId);
309  $filesToBeCleared = $nonClearedUploadTreeView->count();
310 
311  $filesAlreadyCleared = $filesOfInterest - $filesToBeCleared;
312  $this->vars['message'] = _("Cleared").": $filesAlreadyCleared/$filesOfInterest";
313  if ($filesOfInterest == 0) {
314  $percentage = 100;
315  } else {
316  $percentage = ($filesAlreadyCleared / $filesOfInterest) * 100;
317  $percentage = ($percentage > 11) ? $percentage : 11;
318  }
319  $this->vars['progressBar'] = "width:".$percentage."%";
320 
321  return $this->render("ui-clearing-view.html.twig");
322  }
323 
324  /*
325  * \brief Customize submenus.
326  */
327  function RegisterMenus()
328  {
329  $menuText="Licenses";
330  $menuPosition = 58;
331  $uri = $this->Name . Traceback_parm_keep(array("upload", "item", "show"));
332  $tooltipText = _("Set the concluded licenses for this upload");
333  $this->microMenu->insert(array(MicroMenu::VIEW, MicroMenu::VIEW_META), $menuText, $menuPosition, $this->Name, $uri, $tooltipText );
334 
335  if (GetParm("mod", PARM_STRING) != $this->Name) {
336  menu_insert("Browse-Pfile::$menuText", 0, $this->Name, $tooltipText);
337  }
338  return 0;
339  }
340 
348  public function updateLastItem($userId, $groupId, $lastItem, $currentUploadtreeId)
349  {
350  $type = GetParm("clearingTypes", PARM_INTEGER);
351  $global = GetParm("globalDecision", PARM_STRING) === "on" ? 1 : 0;
352  $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($lastItem);
353  $itemBounds = $this->uploadDao->getItemTreeBounds($lastItem, $uploadTreeTableName);
354  if ($global) {
355  $isDecisionWip = $this->clearingDao->isDecisionCheck($currentUploadtreeId, $groupId, DecisionTypes::WIP);
356  $hasChangedClearingType = $this->clearingDao->isDecisionCheck($currentUploadtreeId, $groupId, '');
357  if ($isDecisionWip) {
358  $this->clearingDecisionEventProcessor->makeDecisionFromLastEvents($itemBounds, $userId, $groupId, $type, $global);
359  } else if (empty($hasChangedClearingType['scope'])
360  || ($hasChangedClearingType['decision_type'] != $type)
361  ) {
362  $this->clearingDecisionEventProcessor->makeDecisionFromLastEvents($itemBounds, $userId, $groupId, $type, $global);
363  } else {
364  return;
365  }
366  } else {
367  $this->clearingDecisionEventProcessor->makeDecisionFromLastEvents($itemBounds, $userId, $groupId, $type, $global);
368  }
369  }
370 }
371 
372 $NewPlugin = new ClearingView;
getSelectedHighlighting(ItemTreeBounds $itemTreeBounds, $licenseId, $selectedAgentId, $highlightId, $clearingId, $uploadId)
RegisterMenus()
While menus can be added to any time at or after the PostInitialize phase, this is the standard locat...
OutputOpen()
This function is called when user output is requested. This function is responsible for assigning hea...
updateLastItem($userId, $groupId, $lastItem, $currentUploadtreeId)
__construct()
base constructor. Most plugins will just use this
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:57
Output()
This function is called when user output is requested. This function is responsible for content....
Definition: FO_Plugin.php:399
render($templateName, $vars=null)
Definition: FO_Plugin.php:434
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
Various utility functions to filter ClearingDecision.
Utility functions to process ClearingDecision.
Wrapper class for license map.
Definition: LicenseMap.php:19
Definition: state.hpp:16
Isdir($mode)
Definition: common-dir.php:20
Iscontainer($mode)
Definition: common-dir.php:38
Dir2Browse($Mod, $UploadtreePk, $LinkLast=NULL, $ShowBox=1, $ShowMicro=NULL, $Enumerate=-1, $PreText='', $PostText='', $uploadtree_tablename="uploadtree")
Get an html linked string of a file browse path.
Definition: common-dir.php:263
menu_insert($Path, $LastOrder=0, $URI=NULL, $Title=NULL, $Target=NULL, $HTML=NULL)
Given a Path, order level for the last item, and optional plugin name, insert the menu item.
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
const PARM_INTEGER
Definition: common-parm.php:14
const PARM_STRING
Definition: common-parm.php:18
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:46
Traceback_parm_keep($List)
Create a new URI, keeping only these items.
#define PLUGIN_DB_WRITE
Plugin requires write permission on DB.
Definition: libfossology.h:38