FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
ui-clearing-view.php
1 <?php
2 /*
3  Copyright (C) 2014-2017, Siemens AG
4  Author: Daniele Fognini, Johannes Najjar
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License
8  version 2 as published by the Free Software Foundation.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
41 use Monolog\Logger;
44 
45 define("TITLE_CLEARINGVIEW", _("Change concluded License "));
46 
47 class ClearingView extends FO_Plugin
48 {
50  private $uploadDao;
52  private $licenseDao;
54  private $clearingDao;
56  private $agentsDao;
58  private $logger;
60  private $highlightDao;
62  private $highlightProcessor;
64  private $highlightRenderer;
66  private $clearingDecisionEventProcessor;
68  private $clearingDecisionFilter;
70  private $invalidParm = false;
72  private $decisionTypes;
73 
74  function __construct()
75  {
76  $this->Name = "view-license";
77  $this->Title = TITLE_CLEARINGVIEW;
78  $this->DBaccess = PLUGIN_DB_WRITE;
79  $this->Dependency = array("view");
80  $this->LoginFlag = 0;
81  $this->NoMenu = 0;
82  parent::__construct();
83 
84  global $container;
85  $this->licenseDao = $container->get('dao.license');
86  $this->uploadDao = $container->get('dao.upload');
87  $this->clearingDao = $container->get('dao.clearing');
88  $this->agentsDao = $container->get('dao.agent');
89  $this->logger = $container->get("logger");
90  $this->highlightDao = $container->get("dao.highlight");
91  $this->highlightRenderer = $container->get("view.highlight_renderer");
92  $this->highlightProcessor = $container->get("view.highlight_processor");
93 
94  $this->decisionTypes = $container->get('decision.types');
95 
96  $this->clearingDecisionEventProcessor = $container->get(
97  'businessrules.clearing_decision_processor');
98  $this->clearingDecisionFilter = $container->get(
99  'businessrules.clearing_decision_filter');
100  }
101 
102 
112  private function getSelectedHighlighting(ItemTreeBounds $itemTreeBounds, $licenseId, $selectedAgentId, $highlightId, $clearingId, $uploadId)
113  {
114  $unmaskAgents = $selectedAgentId;
115  if (empty($selectedAgentId)) {
116  $scanJobProxy = new ScanJobProxy($this->agentsDao,$uploadId);
117  $scanJobProxy->createAgentStatus(array_keys(AgentRef::AGENT_LIST));
118  $unmaskAgents = $scanJobProxy->getLatestSuccessfulAgentIds();
119  }
120  $highlightEntries = $this->highlightDao->getHighlightEntries($itemTreeBounds,
121  $licenseId, $unmaskAgents, $highlightId, $clearingId);
122  $groupId = Auth::getGroupId();
123  if (($selectedAgentId > 0) || ($clearingId > 0)) {
124  $this->highlightProcessor->addReferenceTexts($highlightEntries, $groupId);
125  } else {
126  $this->highlightProcessor->flattenHighlights($highlightEntries, array("K", "K "));
127  }
128  return $highlightEntries;
129  }
130 
131  public function execute()
132  {
133  $openOutput = $this->OutputOpen();
134  if ($openOutput instanceof RedirectResponse) {
135  $response = $openOutput;
136  } else {
137  $response = $this->getResponse();
138  }
139  $response->prepare($this->getRequest());
140  $response->send();
141  }
142 
143  function OutputOpen()
144  {
145  if ($this->State != PLUGIN_STATE_READY) {
146  return (0);
147  }
148  $uploadId = GetParm("upload", PARM_INTEGER);
149  if (empty($uploadId)) {
150  return;
151  }
152 
153  $uploadTreeId = GetParm("item", PARM_INTEGER);
154  if (empty($uploadTreeId)) {
155  $parent = $this->uploadDao->getUploadParent($uploadId);
156  if (!isset($parent)) {
157  $this->invalidParm = true;
158  return;
159  }
160 
161  $item = $this->uploadDao->getNextItem($uploadId, $parent);
162  if ($item === UploadDao::NOT_FOUND) {
163  $this->invalidParm = true;
164  return;
165  }
166  $uploadTreeId = $item->getId();
167  return new RedirectResponse(Traceback_uri() . '?mod=' . $this->Name
168  . Traceback_parm_keep(array("upload", "show")) . "&item=$uploadTreeId");
169  }
170 
171  $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($uploadId);
172  $uploadEntry = $this->uploadDao->getUploadEntry($uploadTreeId, $uploadTreeTableName);
173  if (Isdir($uploadEntry['ufile_mode']) || Iscontainer($uploadEntry['ufile_mode'])) {
174  $parent = $this->uploadDao->getUploadParent($uploadId);
175  if (!isset($parent)) {
176  $this->invalidParm = true;
177  return;
178  }
179 
180  $item = $this->uploadDao->getNextItem($uploadId, $parent);
181  if ($item === UploadDao::NOT_FOUND) {
182  $this->invalidParm = true;
183  return;
184  }
185  $uploadTreeId = $item->getId();
186  return new RedirectResponse(Traceback_uri() . '?mod=' . $this->Name
187  . Traceback_parm_keep(array("upload", "show")) . "&item=$uploadTreeId");
188  }
189 
190  return parent::OutputOpen();
191  }
192 
193 
197  function Output()
198  {
199  if ($this->invalidParm) {
200  $this->vars['content'] = 'This upload contains no files!<br><a href="'
201  . Traceback_uri() . '?mod=browse">Go back to browse view</a>';
202  return $this->render("include/base.html.twig");
203  }
204 
205  $uploadId = GetParm("upload", PARM_INTEGER);
206  if (empty($uploadId)) {
207  return new Response("", Response::HTTP_BAD_REQUEST);
208  }
209  $uploadTreeId = GetParm("item", PARM_INTEGER);
210  if (empty($uploadTreeId)) {
211  return new Response("", Response::HTTP_BAD_REQUEST);
212  }
213 
214  $userId = Auth::getUserId();
215  $groupId = Auth::getGroupId();
216 
217  $lastItem = GetParm("lastItem", PARM_INTEGER);
218 
219  if (!empty($lastItem)) {
220  $this->updateLastItem($userId, $groupId,$lastItem);
221  }
222 
223  $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($uploadId);
224  $itemTreeBounds = $this->uploadDao->getItemTreeBounds($uploadTreeId, $uploadTreeTableName);
225 
226  $this->vars['micromenu'] = Dir2Browse('license', $uploadTreeId, NULL,
227  $showBox = 0, "View", -1, '', '', $uploadTreeTableName);
228 
229  global $Plugins;
231  $view = &$Plugins[plugin_find_id("view")];
232 
233  $licenseId = GetParm("licenseId", PARM_INTEGER);
234  $selectedAgentId = GetParm("agentId", PARM_INTEGER);
235  $highlightId = GetParm("highlightId", PARM_INTEGER);
236  $clearingId = GetParm("clearingId", PARM_INTEGER);
237 
238  if ($clearingId !== null) {
239  $highlightId = -1;
240  } else if ($highlightId !== null) {
241  $clearingId = -1;
242  }
243 
244  $baseUri = Traceback_uri();
245  $this->vars['baseuri'] = $baseUri;
246  $this->vars['uri'] = $baseUri . "?mod=" . $this->Name . Traceback_parm_keep(array('upload', 'folder'));
247  $this->vars['bulkHistoryHighlightUri'] = $this->vars['uri'];
248  $this->vars['optionName'] = "skipFile";
249  $this->vars['formName'] = "uiClearingForm";
250  $this->vars['ajaxAction'] = "setNextPrev";
251  $highlights = $this->getSelectedHighlighting($itemTreeBounds, $licenseId,
252  $selectedAgentId, $highlightId, $clearingId, $uploadId);
253 
254  $isSingleFile = !$itemTreeBounds->containsFiles();
255  $hasWritePermission = $this->uploadDao->isEditable($uploadId, $groupId);
256 
257  $clearingDecisions = null;
258  if ($isSingleFile || $hasWritePermission) {
259  $clearingDecisions = $this->clearingDao->getFileClearings($itemTreeBounds, $groupId, false);
260  }
261 
262  if ($isSingleFile && $hasWritePermission) {
263  $this->vars['bulkUri'] = Traceback_uri() . "?mod=popup-license";
264  $licenseArray = $this->licenseDao->getLicenseArray($groupId);
265  list($addedResults, $removedResults) = $this->clearingDecisionEventProcessor->getCurrentClearings($itemTreeBounds, $groupId, LicenseMap::CONCLUSION);
266  if (count($addedResults)+count($removedResults)>0) {
267  array_unshift($licenseArray, array('id'=>0,'fullname'=>'','shortname'=>'------'));
268  }
270  foreach ($removedResults as $result) {
271  array_unshift($licenseArray, array( 'id'=>$result->getLicenseId() ,'fullname'=>$result->getLicenseFullName() ,'shortname'=>$result->getLicenseShortName()));
272  }
274  foreach ($addedResults as $result) {
275  array_unshift($licenseArray, array( 'id'=>$result->getLicenseId() ,'fullname'=>$result->getLicenseFullName() ,'shortname'=>$result->getLicenseShortName()));
276  }
277  $this->vars['licenseArray'] = $licenseArray;
278  } elseif ($isSingleFile) {
279  $this->vars['auditDenied'] = true;
280  }
281 
282  $selectedClearingType = false;
283  $selectedClearingScope = false;
284  if (!empty($clearingDecisions)) {
285  $selectedClearingType = $clearingDecisions[0]->getType();
286  $selectedClearingScope = $clearingDecisions[0]->getScope();
287  }
288  $bulkHistory = $this->clearingDao->getBulkHistory($itemTreeBounds, $groupId);
289 
290  $ModBack = GetParm("modback", PARM_STRING) ?: "license";
291  list($pageMenu, $textView) = $view->getView(NULL, $ModBack, 0, "", $highlights, false, true);
292 
293  $this->vars['uploadId'] = $uploadId;
294  $this->vars['itemId'] = $uploadTreeId;
295  $this->vars['pageMenu'] = $pageMenu;
296  $this->vars['textView'] = $textView;
297  $this->vars['legendData'] = $this->highlightRenderer->getLegendData($selectedAgentId || $clearingId);
298  $this->vars['clearingTypes'] = $this->decisionTypes->getMap();
299  $this->vars['selectedClearingType'] = $selectedClearingType;
300  $this->vars['selectedClearingScope'] = $selectedClearingScope;
301  $this->vars['tmpClearingType'] = $this->clearingDao->isDecisionWip($uploadTreeId, $groupId);
302  $this->vars['bulkHistory'] = $bulkHistory;
303 
304  $filesOfInterest = $this->clearingDao->getTotalDecisionCount($uploadId,
305  $groupId);
306  $filesCleared = $this->clearingDao->getClearingDecisionsCount($uploadId,
307  $groupId);
308 
309  $this->vars['message'] = _("Cleared").": $filesCleared/$filesOfInterest";
310 
311  return $this->render("ui-clearing-view.html.twig");
312  }
313 
314  /*
315  * \brief Customize submenus.
316  */
317  function RegisterMenus()
318  {
319  $menuText="Licenses";
320  $menuPosition = 58;
321  $uri = $this->Name . Traceback_parm_keep(array("upload", "item", "show"));
322  $tooltipText = _("Set the concluded licenses for this upload");
323  $this->microMenu->insert(array(MicroMenu::VIEW, MicroMenu::VIEW_META), $menuText, $menuPosition, $this->Name, $uri, $tooltipText );
324 
325  if (GetParm("mod", PARM_STRING) != $this->Name) {
326  menu_insert("Browse-Pfile::$menuText", 0, $this->Name, $tooltipText);
327  }
328  return 0;
329  }
330 
337  protected function updateLastItem($userId, $groupId, $lastItem)
338  {
339  $type = GetParm("clearingTypes", PARM_INTEGER);
340  $global = GetParm("globalDecision", PARM_STRING) === "on" ? 1 : 0;
341  $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($lastItem);
342  $itemBounds = $this->uploadDao->getItemTreeBounds($lastItem, $uploadTreeTableName);
343  $this->clearingDecisionEventProcessor->makeDecisionFromLastEvents($itemBounds, $userId, $groupId, $type, $global);
344  }
345 }
346 
347 $NewPlugin = new ClearingView;
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:274
Traceback_uri()
Get the URI without query to this location.
getSelectedHighlighting(ItemTreeBounds $itemTreeBounds, $licenseId, $selectedAgentId, $highlightId, $clearingId, $uploadId)
Iscontainer($mode)
Definition: common-dir.php:49
updateLastItem($userId, $groupId, $lastItem)
Definition: state.hpp:26
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:57
const PARM_STRING
Definition: common-parm.php:29
#define PLUGIN_DB_WRITE
Plugin requires write permission on DB.
Definition: libfossology.h:50
const PARM_INTEGER
Definition: common-parm.php:25
Isdir($mode)
Definition: common-dir.php:31
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:67
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_parm_keep($List)
Create a new URI, keeping only these items.
Output()
This function is called when user output is requested. This function is responsible for content...
Definition: FO_Plugin.php:407
render($templateName, $vars=null)
Definition: FO_Plugin.php:442