FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
UploadBrowseProxy.php
1 <?php
2 /*
3 Copyright (C) 2015, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
19 namespace Fossology\Lib\Proxy;
20 
24 
26 {
27  const PRIO_COLUMN = 'priority';
28 
29  protected $groupId;
30  protected $userPerm;
32  protected $dbManager;
33 
34  public function __construct($groupId, $userPerm, DbManager $dbManager, $doSanity=true)
35  {
36  $this->groupId = $groupId;
37  $this->userPerm = $userPerm;
38  $this->dbManager = $dbManager;
39  if ($doSanity) {
40  $this->sanity();
41  }
42  }
43 
44  public function sanity()
45  {
46  $params = array($this->groupId, UploadStatus::OPEN, Auth::PERM_READ);
47  $sql = 'INSERT INTO upload_clearing (upload_fk,group_fk,status_fk,'.self::PRIO_COLUMN.') '
48  . ' SELECT upload_pk,$1,$2,upload_pk as '.self::PRIO_COLUMN
49  . ' FROM upload LEFT JOIN upload_clearing ON upload_pk=upload_fk AND group_fk=$1'
50  . ' WHERE upload_clearing.upload_fk IS NULL'
51  . ' AND (public_perm>=$3 OR EXISTS(SELECT * FROM perm_upload WHERE perm_upload.upload_fk = upload_pk AND group_fk=$1))';
52  $this->dbManager->getSingleRow($sql, $params);
53  }
54 
55 
56  public function updateTable($columnName, $uploadId, $value)
57  {
58  if ($columnName == 'status_fk') {
59  $this->changeStatus($uploadId, $value);
60  } else if ($columnName == 'assignee' && $this->userPerm) {
61  $sql = "UPDATE upload_clearing SET assignee=$1 WHERE group_fk=$2 AND upload_fk=$3";
62  $this->dbManager->getSingleRow($sql, array($value, $this->groupId, $uploadId), $sqlLog = __METHOD__);
63  } else {
64  throw new \Exception('invalid column');
65  }
66  }
67 
68  protected function changeStatus($uploadId, $newStatus)
69  {
70  if ($newStatus == UploadStatus::REJECTED && $this->userPerm) {
71  $this->setStatusAndComment($uploadId, $newStatus, $commentText = '');
72  } else if ($newStatus == UploadStatus::REJECTED) {
73  throw new \Exception('missing permission');
74  } else if ($this->userPerm) {
75  $sql = "UPDATE upload_clearing SET status_fk=$1 WHERE group_fk=$2 AND upload_fk=$3";
76  $this->dbManager->getSingleRow($sql, array($newStatus, $this->groupId, $uploadId), __METHOD__ . '.advisor');
77  } else {
78  $sql = "UPDATE upload_clearing SET status_fk=$1 WHERE group_fk=$2 AND upload_fk=$3 AND status_fk<$4";
79  $params = array($newStatus, $this->groupId, $uploadId, UploadStatus::REJECTED);
80  $this->dbManager->getSingleRow($sql, $params, __METHOD__ . '.user');
81  }
82  }
83 
84  public function setStatusAndComment($uploadId, $statusId, $commentText)
85  {
86  $sql = "UPDATE upload_clearing SET status_fk=$1, status_comment=$2 WHERE group_fk=$3 AND upload_fk=$4";
87  $this->dbManager->getSingleRow($sql, array($statusId, $commentText, $this->groupId, $uploadId), __METHOD__);
88  }
89 
90  public function moveUploadToInfinity($uploadId, $top)
91  {
92  $fun = $top ? 'MAX('.self::PRIO_COLUMN.')+1' : 'MIN('.self::PRIO_COLUMN.')-1';
93  $sql = "UPDATE upload_clearing SET ".self::PRIO_COLUMN."=(SELECT $fun FROM upload_clearing WHERE group_fk=$1)"
94  . " WHERE group_fk=$1 AND upload_fk=$2";
95  $this->dbManager->getSingleRow($sql,
96  array($this->groupId,$uploadId),
97  __METHOD__.($top?'+':'-'));
98  }
99 
100  public function moveUploadBeyond($moveUpload, $beyondUpload)
101  {
102  $this->dbManager->begin();
103  $this->dbManager->prepare($stmt = __METHOD__ . '.get.single.Upload',
104  $sql='SELECT upload_fk,'.self::PRIO_COLUMN.' FROM upload_clearing WHERE group_fk=$1 AND upload_fk=$2');
105  $movePoint = $this->dbManager->getSingleRow($sql, array($this->groupId,$moveUpload), $stmt);
106  $beyondPoint = $this->dbManager->getSingleRow($sql, array($this->groupId,$beyondUpload), $stmt);
107 
108  if ($movePoint[self::PRIO_COLUMN] > $beyondPoint[self::PRIO_COLUMN]) {
109  $farPoint = $this->dbManager->getSingleRow("SELECT MAX(".self::PRIO_COLUMN.") m FROM upload_clearing WHERE group_fk=$1 AND ".self::PRIO_COLUMN."<$2",
110  array($this->groupId,$beyondPoint[self::PRIO_COLUMN]), 'get.upload.with.lower.priority');
111  $farPrio = $farPoint['m']!==null ? $farPoint['m'] : $beyondPoint[self::PRIO_COLUMN]-1;
112  } else {
113  $farPoint = $this->dbManager->getSingleRow("SELECT MIN(".self::PRIO_COLUMN.") m FROM upload_clearing WHERE group_fk=$1 AND ".self::PRIO_COLUMN.">$2",
114  array($this->groupId,$beyondPoint[self::PRIO_COLUMN]), 'get.upload.with.higher.priority');
115  $farPrio = $farPoint['m']!==null ? $farPoint['m'] : $beyondPoint[self::PRIO_COLUMN]+1;
116  }
117 
118  $newPriority = ($farPrio + $beyondPoint[self::PRIO_COLUMN]) / 2;
119  $this->dbManager->getSingleRow('UPDATE upload_clearing SET '.self::PRIO_COLUMN.'=$1 WHERE group_fk=$2 AND upload_fk=$3',
120  array($newPriority, $this->groupId, $moveUpload),
121  __METHOD__.'.update.priority');
122  $this->dbManager->commit();
123  }
124 
129  public function getFolderPartialQuery(& $params)
130  {
131  if (count($params)!=1) {
132  throw new \Exception('expected argument to be array with exactly one element for folderId');
133  }
134  $params[] = $this->groupId;
135  $params[] = Auth::PERM_READ;
136  $partQuery = 'upload
137  INNER JOIN upload_clearing ON upload_pk = upload_clearing.upload_fk AND group_fk=$2
138  INNER JOIN uploadtree ON upload_pk = uploadtree.upload_fk AND upload.pfile_fk = uploadtree.pfile_fk
139  WHERE upload_pk IN (SELECT child_id FROM foldercontents WHERE foldercontents_mode&2 != 0 AND parent_fk = $1 )
140  AND (public_perm>=$3
141  OR EXISTS(SELECT * FROM perm_upload WHERE perm_upload.upload_fk = upload_pk AND group_fk=$2))
142  AND parent IS NULL
143  AND lft IS NOT NULL';
144  return $partQuery;
145  }
146 
152  public function getStatus($uploadId)
153  {
154  $row = $this->dbManager->getSingleRow("SELECT status_fk FROM upload_clearing WHERE upload_fk=$1 AND group_fk=$2",
155  array($uploadId, $this->groupId));
156  if (false === $row) {
157  throw new \Exception("cannot find uploadId=$uploadId");
158  }
159  return $row['status_fk'];
160  }
161 }
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28