FOSSology  4.4.0
Open Source License Compliance by Open Source Software
admin-upload-edit.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008-2013 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2015 Siemens AG
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
14 
16 {
18  private $uploadDao;
20  private $dbManager;
22  private $folderDao;
23 
24  function __construct()
25  {
26  $this->Name = "upload_properties";
27  $this->Title = _("Edit Uploaded File Properties");
28  $this->MenuList = "Organize::Uploads::Edit Properties";
29  $this->DBaccess = PLUGIN_DB_WRITE;
30  parent::__construct();
31  $this->uploadDao = $GLOBALS['container']->get('dao.upload');
32  $this->dbManager = $GLOBALS['container']->get('db.manager');
33  $this->folderDao = $GLOBALS['container']->get('dao.folder');
34  }
35 
47  function UpdateUploadProperties($uploadId, $newName, $newDesc)
48  {
49  if (empty($newName) and empty($newDesc)) {
50  return 2;
51  }
52 
53  if (!empty($newName)) {
54  /*
55  * Use pfile_fk to select the correct entry in the upload tree, artifacts
56  * (e.g. directories of the upload do not have pfiles).
57  */
58  $row = $this->dbManager->getSingleRow(
59  "SELECT pfile_fk FROM upload WHERE upload_pk=$1",array($uploadId),__METHOD__.'.getPfileId');
60  if (empty($row)) {
61  return 0;
62  }
63  $pfileFk = $row['pfile_fk'];
64  $trimNewName = trim($newName);
65 
66  /* Always keep uploadtree.ufile_name and upload.upload_filename in sync */
67  $this->dbManager->getSingleRow(
68  "UPDATE uploadtree SET ufile_name=$3 WHERE upload_fk=$1 AND pfile_fk=$2",
69  array($uploadId, $pfileFk, $trimNewName),
70  __METHOD__ . '.updateItem');
71  $this->dbManager->getSingleRow(
72  "UPDATE upload SET upload_filename=$3 WHERE upload_pk=$1 AND pfile_fk=$2",
73  array($uploadId, $pfileFk, $trimNewName),
74  __METHOD__ . '.updateUpload.name');
75  }
76 
77  if (! empty($newDesc)) {
78  $trimNewDesc = trim($newDesc);
79  $this->dbManager->getSingleRow("UPDATE upload SET upload_desc=$2 WHERE upload_pk=$1",
80  array($uploadId, $trimNewDesc), __METHOD__ . '.updateUpload.desc');
81  }
82  return 1;
83  }
84 
85  public function Output()
86  {
87  $groupId = Auth::getGroupId();
88  $rootFolder = $this->folderDao->getRootFolder(Auth::getUserId());
89  $folderStructure = $this->folderDao->getFolderStructure($rootFolder->getId());
90 
91  $V = "";
92  $folder_pk = GetParm('folder', PARM_INTEGER);
93  if (empty($folder_pk)) {
94  $folder_pk = $rootFolder->getId();
95  }
96 
97  $NewName = GetArrayVal("newname", $_POST);
98  $NewDesc = GetArrayVal("newdesc", $_POST);
99  $upload_pk = GetArrayVal("upload_pk", $_POST);
100  if (empty($upload_pk)) {
101  $upload_pk = GetParm('upload', PARM_INTEGER);
102  }
103  /* Check Upload permission */
104  if (! empty($upload_pk) && !$this->uploadDao->isEditable($upload_pk, $groupId)) {
105  $text = _("Permission Denied");
106  return "<h2>$text</h2>";
107  }
108  $rc = $this->UpdateUploadProperties($upload_pk, $NewName, $NewDesc);
109  if ($rc == 0) {
110  $text = _("Nothing to Change");
111  $this->vars['message'] = $text;
112  } else if ($rc == 1) {
113  $text = _("Upload Properties successfully changed");
114  $this->vars['message'] = $text;
115  }
116 
117  $this->vars['folderStructure'] = $folderStructure;
118  $this->vars['folderId'] = $folder_pk;
119  $this->vars['baseUri'] = $Uri = Traceback_uri() . "?mod=" . $this->Name . "&folder=";
120 
121  $folderUploads = $this->folderDao->getFolderUploads($folder_pk, $groupId);
122  $uploadsById = array();
123  /* @var $uploadProgress UploadProgress */
124  foreach ($folderUploads as $uploadProgress) {
125  if ($uploadProgress->getGroupId() != $groupId) {
126  continue;
127  }
128  if (! $this->uploadDao->isEditable($uploadProgress->getId(), $groupId)) {
129  continue;
130  }
131  $display = $uploadProgress->getFilename() . _(" from ") . Convert2BrowserTime(date("Y-m-d H:i:s",$uploadProgress->getTimestamp()));
132  $uploadsById[$uploadProgress->getId()] = $display;
133  }
134  $this->vars['uploadList'] = $uploadsById;
135  if (empty($upload_pk)) {
136  reset($uploadsById);
137  $upload_pk = key($uploadsById);
138  }
139  $this->vars['uploadId'] = $upload_pk;
140 
141  if ($upload_pk) {
142  $upload = $this->uploadDao->getUpload($upload_pk);
143  if (empty($upload)) {
144  $this->vars['message'] = _("Missing upload.");
145  return 0;
146  }
147  } else {
148  $upload = null;
149  }
150 
151  $baseFolderUri = $this->vars['baseUri']."$folder_pk&upload=";
152  $this->vars['uploadAction'] = "onchange=\"js_url(this.value, '$baseFolderUri')\"";
153 
154  $this->vars['uploadFilename'] = $upload ? $upload->getFilename() : '';
155  $this->vars['uploadDesc'] = $upload ? $upload->getDescription() : '';
156  $this->vars['content'] = $V;
157 
158  return $this->render('admin_upload_edit.html.twig');
159  }
160 }
161 $NewPlugin = new upload_properties;
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:57
render($templateName, $vars=null)
Definition: FO_Plugin.php:434
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
Output()
This function is called when user output is requested. This function is responsible for content....
UpdateUploadProperties($uploadId, $newName, $newDesc)
Update upload properties (name, description)
__construct()
base constructor. Most plugins will just use this
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
const PARM_INTEGER
Definition: common-parm.php:14
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:46
GetArrayVal($Key, $Arr)
Get the value from a array(map)
Definition: common-ui.php:157
Convert2BrowserTime($server_time)
Convert the server time to browser time.
Definition: common-ui.php:312
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690
#define PLUGIN_DB_WRITE
Plugin requires write permission on DB.
Definition: libfossology.h:38
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16