FOSSology  4.5.1
Open Source License Compliance by Open Source Software
admin-folder-size.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2023 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
10 
11 define("TITLE_SIZE_DASHBOARD", _("Folder and upload dashboard"));
12 
14 {
15 
17  private $dbManager;
18 
22  private $uploadDao;
23 
24  function __construct()
25  {
26  $this->Name = "size_dashboard";
27  $this->Title = TITLE_SIZE_DASHBOARD;
28  $this->MenuList = "Admin::Dashboards::Folder/Upload Proportions";
29  $this->Dependency = array();
30  $this->DBaccess = PLUGIN_DB_WRITE;
31  parent::__construct();
32  $this->dbManager = $GLOBALS['container']->get('db.manager');
33  $this->uploadDao = $GLOBALS['container']->get('dao.upload');
34  }
35 
41  function getFolderAndUploadSize($folderId)
42  {
43  $sql = 'INNER JOIN upload ON upload.pfile_fk=pfile.pfile_pk '.
44  'INNER JOIN foldercontents ON upload.upload_pk=foldercontents.child_id '.
45  'INNER JOIN upload_clearing ON upload.upload_pk=upload_clearing.upload_fk '.
46  'WHERE parent_fk=$1;';
47  $statementName = __METHOD__."GetFolderSize";
48  $folderSizesql = 'SELECT SUM(pfile_size) FROM pfile '.$sql;
49  $row = $this->dbManager->getSingleRow($folderSizesql,array($folderId),$statementName);
50  $folderSize = HumanSize($row['sum']);
51 
52  $statementName = __METHOD__."GetEachUploadSize";
53  $dispSql = "SELECT upload_pk, upload_filename, pfile_size, " .
54  "to_char(upload_ts, 'YYYY-MM-DD HH24:MI:SS') AS upload_ts, status_fk FROM pfile " . $sql;
55  $results = $this->dbManager->getRows($dispSql, [$folderId], $statementName);
56  $var = '';
57  foreach ($results as $result) {
58  $clearingDuration = $this->uploadDao->getClearingDuration($result["upload_pk"]);
59  $var .= "<tr><td align='left'>" . $result['upload_pk'] .
60  "</td><td align='left'>" . $result['upload_filename'] .
61  "</td><td align='left' data-order='{$result['pfile_size']}'>" .
62  HumanSize($result['pfile_size']) .
63  "</td><td align='left' data-order='{$clearingDuration[1]}'>$clearingDuration[0]</td>
64  <td align='left'>{$result['upload_ts']}</td><td align='left'>" . $this->ConvertStatusToString($result['status_fk']) .
65  "</td></tr>";
66  }
67  return [$var, $folderSize];
68  }
69 
75  private function generateExportData($folderId, $format)
76  {
77  $results = $this->dbManager->getRows(
78  "SELECT upload_pk, upload_filename, pfile_size, to_char(upload_ts, 'YYYY-MM-DD HH24:MI:SS') AS upload_ts " .
79  "FROM pfile " .
80  "INNER JOIN upload ON upload.pfile_fk=pfile.pfile_pk " .
81  "INNER JOIN foldercontents ON upload.upload_pk=foldercontents.child_id " .
82  "INNER JOIN upload_clearing ON upload.upload_pk=upload_clearing.upload_fk ".
83  "WHERE parent_fk=$1",
84  [$folderId],
85  __METHOD__."ExportData"
86  );
87 
88  $data = [];
89  foreach ($results as $row) {
90  $clearingDuration = $this->uploadDao->getClearingDuration($row["upload_pk"]);
91  $data[] = [
92  'uploadid' => $row['upload_pk'],
93  'name' => $row['upload_filename'],
94  'size' => $row['pfile_size'],
95  'duration' => $clearingDuration[1],
96  'date' => $row['upload_ts'],
97  'status' => $this->ConvertStatusToString($row['status_fk'])
98  ];
99  }
100 
101  switch ($format) {
102  case 'csv':
103  $this->outputCSV($data);
104  break;
105  case 'json':
106  $this->outputJSON($data);
107  break;
108  }
109  }
110 
115  private function ConvertStatusToString($status)
116  {
117  $statusString = 'Open';
118  if ($status == 2) {
119  $statusString = 'In progress';
120  } else if ($status == 3) {
121  $statusString = 'Closed';
122  } else if ($status == 4) {
123  $statusString = 'Rejected';
124  }
125 
126  return $statusString;
127  }
128 
133  private function outputCSV($data)
134  {
135  header('Content-Type: text/csv');
136  header('Content-Disposition: attachment; filename="folder_export.csv"');
137 
138  $output = fopen('php://output', 'w');
139  fputcsv($output, ['Upload id', 'Upload name', 'Size (bytes)', 'Clearing duration (seconds)', 'Upload date', 'Upload status']);
140 
141  foreach ($data as $row) {
142  fputcsv($output, [
143  $row['uploadid'],
144  $row['name'],
145  $row['size'],
146  $row['duration'],
147  $row['date'],
148  $row['status']
149  ]);
150  }
151  fclose($output);
152  exit;
153  }
154 
159  private function outputJSON($data)
160  {
161  header('Content-Type: application/json');
162  header('Content-Disposition: attachment; filename="folder_export.json"');
163  echo json_encode($data, JSON_PRETTY_PRINT);
164  exit;
165  }
166 
170  public function Output()
171  {
172  $exportFormat = GetParm('export', PARM_STRING);
173  if (!empty($exportFormat) && in_array($exportFormat, ['csv', 'json'])) {
174  $folderId = GetParm('folder', PARM_INTEGER);
175  $this->generateExportData($folderId, $exportFormat);
176  }
177  /* If this is a POST, then process the request. */
178  $folderId = GetParm('selectfolderid', PARM_INTEGER);
179  if (empty($folderId)) {
180  $folderId = FolderGetTop();
181  }
182  list($tableVars, $wholeFolderSize) = $this->getFolderAndUploadSize($folderId);
183 
184  /* Display the form */
185  $formVars["onchangeURI"] = Traceback_uri() . "?mod=" . $this->Name . "&selectfolderid=";
186  $formVars["folderListOption"] = FolderListOption(-1, 0, 1, $folderId);
187  $formVars["tableVars"] = $tableVars;
188  $formVars["wholeFolderSize"] = $wholeFolderSize;
189  $formVars["currentFolderId"] = $folderId;
190  $formVars["pluginName"] = $this->Name;
191  return $this->renderString("admin-folder-size-form.html.twig", $formVars);
192  }
193 }
194 $NewPlugin = new size_dashboard;
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:57
renderString($templateName, $vars=null)
Definition: FO_Plugin.php:414
getFolderAndUploadSize($folderId)
Given a folder's ID function will get size of the folder and uploads under it.
__construct()
base constructor. Most plugins will just use this
Output()
Generate the text for this plugin.
outputJSON($data)
Outputs data in JSON format.
ConvertStatusToString($status)
convert numaric status into string.
generateExportData($folderId, $format)
Generate export data in CSV or JSON format for a given folder ID.
outputCSV($data)
Outputs data in CSV format.
FolderListOption($ParentFolder, $Depth, $IncludeTop=1, $SelectId=-1, $linkParent=false, $OldParent=0)
Create the folder tree, using OPTION tags.
FolderGetTop()
DEPRECATED! Find the top-of-tree folder_pk for the current user.
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
const PARM_INTEGER
Definition: common-parm.php:14
const PARM_STRING
Definition: common-parm.php:18
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:46
HumanSize( $bytes)
Translate a byte number to a proper type, xxx bytes to xxx B/KB/MB/GB/TB/PB.
Definition: common-ui.php:100
#define PLUGIN_DB_WRITE
Plugin requires write permission on DB.
Definition: libfossology.h:38
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16