FOSSology  4.4.0
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  'WHERE parent_fk=$1;';
46  $statementName = __METHOD__."GetFolderSize";
47  $folderSizesql = 'SELECT SUM(pfile_size) FROM pfile '.$sql;
48  $row = $this->dbManager->getSingleRow($folderSizesql,array($folderId),$statementName);
49  $folderSize = HumanSize($row['sum']);
50 
51  $statementName = __METHOD__."GetEachUploadSize";
52  $dispSql = "SELECT upload_pk, upload_filename, pfile_size FROM pfile " .
53  $sql;
54  $results = $this->dbManager->getRows($dispSql, [$folderId], $statementName);
55  $var = '';
56  foreach ($results as $result) {
57  $duration = "NA";
58  $assignDate = $this->uploadDao->getAssigneeDate($result["upload_pk"]);
59  $closingDate = $this->uploadDao->getClosedDate($result["upload_pk"]);
60  $durationSort = 0;
61  if ($assignDate != null && $closingDate != null) {
62  try {
63  $closingDate = new DateTime($closingDate);
64  $assignDate = new DateTime($assignDate);
65  if ($assignDate < $closingDate) {
66  $duration = HumanDuration($closingDate->diff($assignDate));
67  $durationSort = $closingDate->getTimestamp() - $assignDate->getTimestamp();
68  }
69  } catch (Exception $_) {
70  }
71  }
72  $var .= "<tr><td align='left'>" . $result['upload_filename'] .
73  "</td><td align='left' data-order='{$result['pfile_size']}'>" .
74  HumanSize($result['pfile_size']) .
75  "</td><td align='left' data-order='{$durationSort}'>$duration</td></tr>";
76  }
77  return [$var, $folderSize];
78  }
79 
83  public function Output()
84  {
85  /* If this is a POST, then process the request. */
86  $folderId = GetParm('selectfolderid', PARM_INTEGER);
87  if (empty($folderId)) {
88  $folderId = FolderGetTop();
89  }
90  list($tableVars, $wholeFolderSize) = $this->getFolderAndUploadSize($folderId);
91 
92  /* Display the form */
93  $formVars["onchangeURI"] = Traceback_uri() . "?mod=" . $this->Name . "&selectfolderid=";
94  $formVars["folderListOption"] = FolderListOption(-1, 0, 1, $folderId);
95  $formVars["tableVars"] = $tableVars;
96  $formVars["wholeFolderSize"] = $wholeFolderSize;
97  return $this->renderString("admin-folder-size-form.html.twig", $formVars);
98  }
99 }
100 $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.
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
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
HumanDuration(DateInterval $duration)
Convert DateInterval to Human readable format.
Definition: common-ui.php:120
#define PLUGIN_DB_WRITE
Plugin requires write permission on DB.
Definition: libfossology.h:38
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16