FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
ReportImportPlugin.php
1 <?php
2 /*
3  Copyright (C) 2014-2016 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 */
18 
25 
27 {
28  const NAME = 'ui_reportImport';
29 
31  private $uploadDao;
33  private $folderDao;
34 
35  function __construct()
36  {
37  parent::__construct(self::NAME, array(
38  self::TITLE => _("Report Import"),
39  self::PERMISSION => Auth::PERM_WRITE,
40  self::REQUIRES_LOGIN => TRUE
41  ));
42  $this->uploadDao = $GLOBALS['container']->get('dao.upload');
43  $this->folderDao = $GLOBALS['container']->get('dao.folder');
44  }
45 
46  function preInstall()
47  {
48  $text = _("Import Report");
49  menu_insert("Browse-Pfile::Import&nbsp;Report", 0, self::NAME, $text);
50  menu_insert("Main::Upload::Import&nbsp;Report", 0, self::NAME, $text);
51  }
52 
53  protected function handle(Request $request)
54  {
55  $uploadId = intval(GetArrayVal("uploadselect", $_POST));
56  if (empty($uploadId) ||
57  !array_key_exists('report',$_FILES) ||
58  sizeof($_FILES['report']['name']) != 1)
59  {
60  return $this->showUiToChoose();
61  }
62  else
63  {
64  $jobMetaData = $this->runImport($uploadId, $_FILES['report'], $request);
65  $showJobsPlugin = \plugin_find('showjobs');
66  $showJobsPlugin->OutputOpen();
67  return $showJobsPlugin->getResponse();
68  }
69  }
70 
71  protected function showUiToChoose()
72  {
73  $vars=array();
74  $groupId = Auth::getGroupId();
75  $vars['userIsAdmin'] = Auth::isAdmin();
76 
77  $rootFolder = $this->folderDao->getRootFolder(Auth::getUserId());
78  $folder_pk = GetParm('folder', PARM_INTEGER);
79  if (empty($folder_pk)) {
80  $folder_pk = $rootFolder->getId();
81  }
82  $vars['folderId'] = $folder_pk;
83 
84  $folderUploads = $this->folderDao->getFolderUploads($folder_pk, $groupId);
85  $uploadsById = array();
86  /* @var $uploadProgress UploadProgress */
87  foreach ($folderUploads as $uploadProgress)
88  {
89  if ($uploadProgress->getGroupId() != $groupId) {
90  continue;
91  }
92  if (!$this->uploadDao->isEditable($uploadProgress->getId(), $groupId)) {
93  continue;
94  }
95  $display = $uploadProgress->getFilename() . _(" from ") . Convert2BrowserTime(date("Y-m-d H:i:s",$uploadProgress->getTimestamp()));
96  $uploadsById[$uploadProgress->getId()] = $display;
97  }
98  $vars['uploadList'] = $uploadsById;
99 
100  $uploadId = GetParm('upload', PARM_INTEGER);
101  if (empty($uploadId))
102  {
103  reset($uploadsById);
104  $uploadId = key($uploadsById);
105  }
106  $vars['uploadId'] = $uploadId;
107 
108  $folderStructure = $this->folderDao->getFolderStructure($rootFolder->getId());
109  $vars['folderStructure'] = $folderStructure;
110  $vars['baseUri'] = $Uri = Traceback_uri() . "?mod=" . self::NAME . "&folder=";
111 
112  return $this->render('ReportImportPlugin.html.twig', $this->mergeWithDefault($vars));
113  }
114 
115  protected function runImport($uploadId, $report, $request)
116  {
117  $reportImportAgent = plugin_find('agent_reportImport');
118 
119  $jqCmdArgs = $reportImportAgent->addReport($report);
120  $jqCmdArgs .= $reportImportAgent->setAdditionalJqCmdArgs($request);
121 
122  $userId = Auth::getUserId();
123  $groupId = Auth::getGroupId();
124  $dbManager = $this->getObject('db.manager');
125  $sql = 'SELECT jq_pk,job_pk FROM jobqueue, job '
126  . 'WHERE jq_job_fk=job_pk AND jq_type=$1 AND job_group_fk=$4 AND job_user_fk=$3 AND jq_args=$2 AND jq_endtime IS NULL';
127  $params = array($reportImportAgent->AgentName,$uploadId,$userId,$groupId);
128  $statementName = __METHOD__;
129  if ($jqCmdArgs) {
130  $sql .= ' AND jq_cmd_args=$5';
131  $params[] = $jqCmdArgs;
132  $statementName .= '.args';
133  }
134  else {
135  $sql .= ' AND jq_cmd_args IS NULL';
136  }
137 
138  $scheduled = $dbManager->getSingleRow($sql,$params,$statementName);
139  if (!empty($scheduled)) {
140  return array($scheduled['job_pk'],$scheduled['jq_pk']);
141  }
142 
143  $upload = $this->getUpload($uploadId, $groupId);
144  $jobId = JobAddJob($userId, $groupId, $upload->getFilename(), $uploadId);
145  $error = "";
146  $jobQueueId = $reportImportAgent->AgentAdd($jobId, $uploadId, $error, array(), $jqCmdArgs);
147  if ($jobQueueId<0)
148  {
149  throw new Exception(_("Cannot schedule").": ".$error);
150  }
151  return array($jobId,$jobQueueId);
152  }
153 
154  protected function getUpload($uploadId, $groupId)
155  {
156  if ($uploadId <=0)
157  {
158  throw new Exception(_("parameter error: $uploadId"));
159  }
160  if (!$this->uploadDao->isAccessible($uploadId, $groupId))
161  {
162  throw new Exception(_("permission denied"));
163  }
165  $upload = $this->uploadDao->getUpload($uploadId);
166  if ($upload === null)
167  {
168  throw new Exception(_('cannot find uploadId'));
169  }
170  return $upload;
171  }
172 }
173 
174 register_plugin(new ReportImportPlugin());
Traceback_uri()
Get the URI without query to this location.
render($templateName, $vars=null, $headers=null)
Convert2BrowserTime($server_time)
Convert the server time to browser time.
Definition: common-ui.php:298
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:57
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.
const PARM_INTEGER
Definition: common-parm.php:25
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...
GetArrayVal($Key, $Arr)
Get the value from a array(map)
Definition: common-ui.php:143
#define PERM_WRITE
Read-Write permission.
Definition: libfossology.h:45