13 namespace Fossology\DecisionImporter\UI;
22 use Symfony\Component\HttpFoundation\File\UploadedFile;
23 use Symfony\Component\HttpFoundation\Request;
24 use Symfony\Component\HttpFoundation\Response;
31 const NAME =
'ui_fodecisionimporter';
40 function __construct()
42 parent::__construct(self::NAME, array(
43 self::TITLE => _(
"Decision Dump Importer"),
45 self::REQUIRES_LOGIN => TRUE
47 $this->uploadDao = $GLOBALS[
'container']->get(
'dao.upload');
48 $this->folderDao = $GLOBALS[
'container']->get(
'dao.folder');
49 $this->userDao = $GLOBALS[
'container']->get(
'dao.user');
54 menu_insert(
"Browse-Pfile::Import FOSSology Dump", 0, self::NAME, $this->
getTitle());
64 protected function handle(Request $request): Response
66 if ($this->handleRequest($request) !==
false) {
69 $showJobsPlugin->OutputOpen();
70 return $showJobsPlugin->getResponse();
82 public function handleRequest(Request $request)
85 $uploadedFile = $request->files->get(
"report");
86 $uploadId = intval($request->get(
"uploadselect"));
87 if (empty($uploadId) ||
88 $uploadedFile ==
null ||
89 empty($uploadedFile->getSize())) {
92 return $this->runImport($uploadId, $uploadedFile, $request);
107 if (empty($folder_pk)) {
108 $folder_pk = $rootFolder->getId();
110 $vars[
'folderId'] = $folder_pk;
112 $folderUploads = $this->folderDao->getFolderUploads($folder_pk, $groupId);
113 $uploadsById = array();
114 foreach ($folderUploads as $uploadProgress) {
115 if ($uploadProgress->getGroupId() != $groupId) {
118 if (!$this->uploadDao->isEditable($uploadProgress->getId(), $groupId)) {
121 $display = $uploadProgress->getFilename() . _(
" from ") .
Convert2BrowserTime(date(
"Y-m-d H:i:s", $uploadProgress->getTimestamp()));
122 $uploadsById[$uploadProgress->getId()] = $display;
124 $vars[
'uploadList'] = $uploadsById;
127 $allUsers = $this->userDao->getAllUsers();
129 $usersById[$vars[
'userid']] =
"-- ME --";
130 foreach ($allUsers as $user) {
131 if ($user[
'user_pk'] != $vars[
'userid']) {
132 $usersById[$user[
'user_pk']] = htmlentities($user[
'user_name']);
135 $vars[
'userList'] = $usersById;
138 if (empty($uploadId)) {
140 $uploadId = key($uploadsById);
142 $vars[
'uploadId'] = $uploadId;
144 $folderStructure = $this->folderDao->getFolderStructure($rootFolder->getId());
145 $vars[
'folderStructure'] = $folderStructure;
146 $vars[
'baseUri'] =
Traceback_uri() .
"?mod=" . self::NAME .
"&folder=";
148 return $this->
render(
'AgentDecisionImporterPlugin.html.twig', $this->mergeWithDefault($vars));
159 protected function runImport(
int $uploadId, UploadedFile $report, Request $request): array
162 $decisionImportAgent =
plugin_find(
'agent_fodecisionimporter');
164 $jqCmdArgs = $decisionImportAgent->addReport($report);
165 $jqCmdArgs .= $decisionImportAgent->setAdditionalJqCmdArgs($request);
169 $dbManager = $this->
getObject(
'db.manager');
170 $sql =
'SELECT jq_pk,job_pk FROM jobqueue, job '
171 .
'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';
172 $params = array($decisionImportAgent->AgentName, $uploadId, $userId, $groupId);
173 $statementName = __METHOD__;
175 $sql .=
' AND jq_cmd_args=$5';
176 $params[] = $jqCmdArgs;
177 $statementName .=
'.args';
179 $sql .=
' AND jq_cmd_args IS NULL';
182 $scheduled = $dbManager->getSingleRow($sql, $params, $statementName);
183 if (!empty($scheduled)) {
184 return array($scheduled[
'job_pk'], $scheduled[
'jq_pk']);
187 $upload = $this->
getUpload($uploadId, $groupId);
188 $jobId = JobAddJob($userId, $groupId, $upload->getFilename(), $uploadId);
190 $jobQueueId = $decisionImportAgent->AgentAdd($jobId, $uploadId, $error, array(), $jqCmdArgs);
191 if ($jobQueueId < 0) {
192 throw new Exception(_(
"Cannot schedule") .
": " . $error);
194 return array($jobId, $jobQueueId);
206 if ($uploadId <= 0) {
207 throw new Exception(_(
"parameter error: $uploadId"));
209 if (!$this->uploadDao->isAccessible($uploadId, $groupId)) {
210 throw new Exception(_(
"permission denied"));
212 $upload = $this->uploadDao->getUpload($uploadId);
213 if ($upload ===
null) {
214 throw new Exception(_(
'cannot find uploadId'));
220 register_plugin(
new AgentDecisionImporterPlugin());
const NAME
Name of the plugin.
getUpload(int $uploadId, int $groupId)
Contains the constants and helpers for authentication of user.
static getUserId()
Get the current user's id.
static getGroupId()
Get the current user's group id.
render($templateName, $vars=null, $headers=null)
Traceback_uri()
Get the URI without query to this location.
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.
Convert2BrowserTime($server_time)
Convert the server time to browser time.