FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
admin-upload-edit.php
1 <?php
2 /***********************************************************
3  Copyright (C) 2008-2013 Hewlett-Packard Development Company, L.P.
4  Copyright (C) 2015 Siemens AG
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License
8  version 2 as published by the Free Software Foundation.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  ***********************************************************/
19 
25 
27 {
29  private $uploadDao;
31  private $dbManager;
33  private $folderDao;
34 
35  function __construct()
36  {
37  $this->Name = "upload_properties";
38  $this->Title = _("Edit Uploaded File Properties");
39  $this->MenuList = "Organize::Uploads::Edit Properties";
40  $this->DBaccess = PLUGIN_DB_WRITE;
41  parent::__construct();
42  $this->uploadDao = $GLOBALS['container']->get('dao.upload');
43  $this->dbManager = $GLOBALS['container']->get('db.manager');
44  $this->folderDao = $GLOBALS['container']->get('dao.folder');
45  }
46 
58  function UpdateUploadProperties($uploadId, $newName, $newDesc)
59  {
60  if (empty($newName) and empty($newDesc)) {
61  return 2;
62  }
63 
64  if (!empty($newName)) {
65  /*
66  * Use pfile_fk to select the correct entry in the upload tree, artifacts
67  * (e.g. directories of the upload do not have pfiles).
68  */
69  $row = $this->dbManager->getSingleRow(
70  "SELECT pfile_fk FROM upload WHERE upload_pk=$1",array($uploadId),__METHOD__.'.getPfileId');
71  if (empty($row)) {
72  return 0;
73  }
74  $pfileFk = $row['pfile_fk'];
75  $trimNewName = trim($newName);
76 
77  /* Always keep uploadtree.ufile_name and upload.upload_filename in sync */
78  $this->dbManager->getSingleRow(
79  "UPDATE uploadtree SET ufile_name=$3 WHERE upload_fk=$1 AND pfile_fk=$2",
80  array($uploadId, $pfileFk, $trimNewName),
81  __METHOD__ . '.updateItem');
82  $this->dbManager->getSingleRow(
83  "UPDATE upload SET upload_filename=$3 WHERE upload_pk=$1 AND pfile_fk=$2",
84  array($uploadId, $pfileFk, $trimNewName),
85  __METHOD__ . '.updateUpload.name');
86  }
87 
88  if (! empty($newDesc)) {
89  $trimNewDesc = trim($newDesc);
90  $this->dbManager->getSingleRow("UPDATE upload SET upload_desc=$2 WHERE upload_pk=$1",
91  array($uploadId, $trimNewDesc), __METHOD__ . '.updateUpload.desc');
92  }
93  return 1;
94  }
95 
96  public function Output()
97  {
98  $groupId = Auth::getGroupId();
99  $rootFolder = $this->folderDao->getRootFolder(Auth::getUserId());
100  $folderStructure = $this->folderDao->getFolderStructure($rootFolder->getId());
101 
102  $V = "";
103  $folder_pk = GetParm('folder', PARM_INTEGER);
104  if (empty($folder_pk)) {
105  $folder_pk = $rootFolder->getId();
106  }
107 
108  $NewName = GetArrayVal("newname", $_POST);
109  $NewDesc = GetArrayVal("newdesc", $_POST);
110  $upload_pk = GetArrayVal("upload_pk", $_POST);
111  if (empty($upload_pk)) {
112  $upload_pk = GetParm('upload', PARM_INTEGER);
113  }
114  /* Check Upload permission */
115  if (! empty($upload_pk) && !$this->uploadDao->isEditable($upload_pk, $groupId)) {
116  $text = _("Permission Denied");
117  return "<h2>$text</h2>";
118  }
119  $rc = $this->UpdateUploadProperties($upload_pk, $NewName, $NewDesc);
120  if ($rc == 0) {
121  $text = _("Nothing to Change");
122  $this->vars['message'] = $text;
123  } else if ($rc == 1) {
124  $text = _("Upload Properties successfully changed");
125  $this->vars['message'] = $text;
126  }
127 
128  $this->vars['folderStructure'] = $folderStructure;
129  $this->vars['folderId'] = $folder_pk;
130  $this->vars['baseUri'] = $Uri = Traceback_uri() . "?mod=" . $this->Name . "&folder=";
131 
132  $folderUploads = $this->folderDao->getFolderUploads($folder_pk, $groupId);
133  $uploadsById = array();
134  /* @var $uploadProgress UploadProgress */
135  foreach ($folderUploads as $uploadProgress) {
136  if ($uploadProgress->getGroupId() != $groupId) {
137  continue;
138  }
139  if (! $this->uploadDao->isEditable($uploadProgress->getId(), $groupId)) {
140  continue;
141  }
142  $display = $uploadProgress->getFilename() . _(" from ") . Convert2BrowserTime(date("Y-m-d H:i:s",$uploadProgress->getTimestamp()));
143  $uploadsById[$uploadProgress->getId()] = $display;
144  }
145  $this->vars['uploadList'] = $uploadsById;
146  if (empty($upload_pk)) {
147  reset($uploadsById);
148  $upload_pk = key($uploadsById);
149  }
150  $this->vars['uploadId'] = $upload_pk;
151 
152  if ($upload_pk) {
153  $upload = $this->uploadDao->getUpload($upload_pk);
154  if (empty($upload)) {
155  $this->vars['message'] = _("Missing upload.");
156  return 0;
157  }
158  } else {
159  $upload = null;
160  }
161 
162  $baseFolderUri = $this->vars['baseUri']."$folder_pk&upload=";
163  $this->vars['uploadAction'] = "onchange=\"js_url(this.value, '$baseFolderUri')\"";
164 
165  $this->vars['uploadFilename'] = $upload ? $upload->getFilename() : '';
166  $this->vars['uploadDesc'] = $upload ? $upload->getDescription() : '';
167  $this->vars['content'] = $V;
168 
169  return $this->render('admin_upload_edit.html.twig');
170  }
171 }
172 $NewPlugin = new upload_properties;
Traceback_uri()
Get the URI without query to this location.
Convert2BrowserTime($server_time)
Convert the server time to browser time.
Definition: common-ui.php:298
UpdateUploadProperties($uploadId, $newName, $newDesc)
Update upload properties (name, description)
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:57
#define PLUGIN_DB_WRITE
Plugin requires write permission on DB.
Definition: libfossology.h:50
const PARM_INTEGER
Definition: common-parm.php:25
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:67
GetArrayVal($Key, $Arr)
Get the value from a array(map)
Definition: common-ui.php:143
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:695
render($templateName, $vars=null)
Definition: FO_Plugin.php:442