FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
agent-foreport.php
Go to the documentation of this file.
1 <?php
2 /*
3  Copyright (C) 2017, 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  */
28 
34 {
35  const NAME = 'agent_founifiedreport';
36 
37  function __construct()
38  {
39  parent::__construct(self::NAME, array(
40  self::TITLE => _("Unified Report generation"),
41  self::PERMISSION => Auth::PERM_WRITE,
42  self::REQUIRES_LOGIN => TRUE
43  ));
44  }
45 
50  protected function handle(Request $request)
51  {
52  $groupId = Auth::getGroupId();
53  $uploadId = intval($request->get('upload'));
54  try {
55  $upload = $this->getUpload($uploadId, $groupId);
56  } catch(Exception $e) {
57  return $this->flushContent($e->getMessage());
58  }
59 
60  list($jobId, $jobQueueId, $error) = $this->scheduleAgent($groupId, $upload);
61 
62  if ($jobQueueId < 0) {
63  return $this->flushContent(_('Cannot schedule').": $error");
64  }
65 
66  $vars = array('jqPk' => $jobQueueId,
67  'downloadLink' => Traceback_uri(). "?mod=download&report=".$jobId,
68  'reportType' => "Unified");
69  $text = sprintf(_("Generating new report for '%s'"), $upload->getFilename());
70  $vars['content'] = "<h2>".$text."</h2>";
71  $content = $this->renderer->loadTemplate("report.html.twig")->render($vars);
72  $message = '<h3 id="jobResult"></h3>';
73  $request->duplicate(array('injectedMessage'=>$message,'injectedFoot'=>$content,'mod'=>'showjobs'))->overrideGlobals();
74  $showJobsPlugin = \plugin_find('showjobs');
75  $showJobsPlugin->OutputOpen();
76  return $showJobsPlugin->getResponse();
77  }
78 
86  protected function getUpload($uploadId, $groupId)
87  {
88  if ($uploadId <= 0) {
89  throw new Exception(_("parameter error"));
90  }
92  $uploadDao = $this->getObject('dao.upload');
93  if (!$uploadDao->isAccessible($uploadId, $groupId)) {
94  throw new Exception(_("permission denied"));
95  }
97  $upload = $uploadDao->getUpload($uploadId);
98  if ($upload === null) {
99  throw new Exception(_('cannot find uploadId'));
100  }
101  return $upload;
102  }
103 
108  function preInstall()
109  {
110  $text = _("Generate Report");
111  menu_insert("Browse-Pfile::Export&nbsp;Unified&nbsp;Report", 0, self::NAME, $text);
112  }
113 
121  public function scheduleAgent($groupId, $upload)
122  {
123  $reportGenAgent = plugin_find('agent_unifiedreport');
124  $userId = Auth::getUserId();
125  $uploadId = $upload->getId();
126  $jobId = JobAddJob($userId, $groupId, $upload->getFilename(), $uploadId);
127  $error = "";
128  $jobQueueId = $reportGenAgent->AgentAdd($jobId, $uploadId, $error, array(), tracebackTotalUri());
129  return array($jobId, $jobQueueId, $error);
130  }
131 }
132 
133 register_plugin(new FoUnifiedReportGenerator());
Traceback_uri()
Get the URI without query to this location.
tracebackTotalUri()
Get the total url without query.
scheduleAgent($groupId, $upload)
const NAME
Plugin mod name.
Unified report generator UI plugin.
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.
handle(Request $request)
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...
#define PERM_WRITE
Read-Write permission.
Definition: libfossology.h:45