FOSSology  4.4.0
Open Source License Compliance by Open Source Software
agent-foreport.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2017 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
14 namespace Fossology\UnifiedReport\UI;
15 
19 use Symfony\Component\HttpFoundation\Request;
20 
26 {
27  const NAME = 'agent_founifiedreport';
28 
29  function __construct()
30  {
31  parent::__construct(self::NAME, array(
32  self::TITLE => _("Unified Report generation"),
33  self::PERMISSION => Auth::PERM_WRITE,
34  self::REQUIRES_LOGIN => TRUE
35  ));
36  }
37 
42  protected function handle(Request $request)
43  {
44  $groupId = Auth::getGroupId();
45  $uploadId = intval($request->get('upload'));
46  try {
47  $upload = $this->getUpload($uploadId, $groupId);
48  } catch(Exception $e) {
49  return $this->flushContent($e->getMessage());
50  }
51 
52  list($jobId, $jobQueueId, $error) = $this->scheduleAgent($groupId, $upload);
53 
54  if ($jobQueueId < 0) {
55  return $this->flushContent(_('Cannot schedule').": $error");
56  }
57 
58  $vars = array('jqPk' => $jobQueueId,
59  'downloadLink' => Traceback_uri(). "?mod=download&report=".$jobId,
60  'reportType' => "Unified");
61  $text = sprintf(_("Generating new report for '%s'"), $upload->getFilename());
62  $vars['content'] = "<h2>".$text."</h2>";
63  $content = $this->renderer->load("report.html.twig")->render($vars);
64  $message = '<h3 id="jobResult"></h3>';
65  $request->duplicate(array('injectedMessage'=>$message,'injectedFoot'=>$content,'mod'=>'showjobs'))->overrideGlobals();
66  $showJobsPlugin = \plugin_find('showjobs');
67  $showJobsPlugin->OutputOpen();
68  return $showJobsPlugin->getResponse();
69  }
70 
78  protected function getUpload($uploadId, $groupId)
79  {
80  if ($uploadId <= 0) {
81  throw new Exception(_("parameter error"));
82  }
84  $uploadDao = $this->getObject('dao.upload');
85  if (!$uploadDao->isAccessible($uploadId, $groupId)) {
86  throw new Exception(_("permission denied"));
87  }
89  $upload = $uploadDao->getUpload($uploadId);
90  if ($upload === null) {
91  throw new Exception(_('cannot find uploadId'));
92  }
93  return $upload;
94  }
95 
100  function preInstall()
101  {
102  $text = _("Generate Report");
103  menu_insert("Browse-Pfile::Export&nbsp;Unified&nbsp;Report", 0, self::NAME, $text);
104  }
105 
113  public function scheduleAgent($groupId, $upload)
114  {
115  $reportGenAgent = plugin_find('agent_unifiedreport');
116  $userId = Auth::getUserId();
117  $uploadId = $upload->getId();
118  $jobId = JobAddJob($userId, $groupId, $upload->getFilename(), $uploadId);
119  $error = "";
120  $url = tracebackTotalUri();
121  $url = preg_replace("/api\/.*/i", "", $url); // Remove api/v1/report
122  $jobQueueId = $reportGenAgent->AgentAdd($jobId, $uploadId, $error, array(), $url);
123  return array($jobId, $jobQueueId, $error);
124  }
125 }
126 
127 register_plugin(new FoUnifiedReportGenerator());
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
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
tracebackTotalUri()
Get the total url without query.
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Definition: nomos.h:308