FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
LicenseStdCommentDao.php
Go to the documentation of this file.
1 <?php
2 /***********************************************************
3  * Copyright (C) 2019 Siemens AG
4  * Author: Gaurav Mishra <mishra.gaurav@siemens.com>
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  ***********************************************************/
23 namespace Fossology\Lib\Dao;
24 
27 
33 {
36  private $dbManager;
37 
38  function __construct(DbManager $dbManager)
39  {
40  $this->dbManager = $dbManager;
41  }
42 
50  public function getAllComments($skipNotSet = false)
51  {
52  $where = "";
53  if ($skipNotSet) {
54  $where = "WHERE name <> 'not-set' AND is_enabled = TRUE";
55  }
56  $sql = "SELECT lsc_pk, name, comment, is_enabled " .
57  "FROM license_std_comment $where " .
58  "ORDER BY lsc_pk ASC;";
59  return $this->dbManager->getRows($sql);
60  }
61 
69  function updateComment($commentPk, $newName, $newComment)
70  {
71  if (!Auth::isAdmin()) {
72  // Only admins can update the comments.
73  return false;
74  }
75  $this->isCommentIdValid($commentPk);
76 
77  $userFk = Auth::getUserId();
78 
79  $sql = "UPDATE license_std_comment " .
80  "SET name = $2, comment = $3, updated = NOW(), user_fk = $4 " .
81  "WHERE lsc_pk = $1 " .
82  "RETURNING 1 AS updated;";
83  $row = $this->dbManager->getSingleRow($sql,
84  [$commentPk, $newName, $newComment, $userFk]);
85  return $row['updated'] == 1;
86  }
87 
96  function insertComment($name, $comment)
97  {
98  if (! Auth::isAdmin()) {
99  // Only admins can add comments.
100  return -1;
101  }
102 
103  $name = trim($name);
104  $comment = trim($comment);
105 
106  if (empty($name) || empty($comment)) {
107  // Cannot insert empty fields.
108  return -1;
109  }
110 
111  $userFk = Auth::getUserId();
112 
113  $params = [
114  'name' => $name,
115  'comment' => $comment,
116  'user_fk' => $userFk
117  ];
118  $statement = __METHOD__ . ".insertNewLicStdComment";
119  $returning = "lsc_pk";
120  $returnVal = -1;
121  try {
122  $returnVal = $this->dbManager->insertTableRow("license_std_comment",
123  $params, $statement, $returning);
124  } catch (\Exception $e) {
125  $returnVal = -2;
126  }
127  return $returnVal;
128  }
129 
140  function updateCommentFromArray($commentArray)
141  {
142  if (!Auth::isAdmin()) {
143  // Only admins can update the comments.
144  return false;
145  }
146 
147  $userFk = Auth::getUserId();
148  $updated = 0;
149 
150  foreach ($commentArray as $commentPk => $comment) {
151  if (count($comment) < 1 ||
152  (! array_key_exists("name", $comment) &&
153  ! array_key_exists("comment", $comment))) {
154  throw new \UnexpectedValueException(
155  "At least name or comment is " . "required for entry " . $commentPk);
156  }
157  $this->isCommentIdValid($commentPk);
158  $statement = __METHOD__;
159  $params = [$commentPk, $userFk];
160  $updateStatement = [];
161  if (array_key_exists("name", $comment)) {
162  $params[] = $comment["name"];
163  $updateStatement[] = "name = $" . count($params);
164  $statement .= ".name";
165  }
166  if (array_key_exists("comment", $comment)) {
167  $params[] = $comment["comment"];
168  $updateStatement[] = "comment = $" . count($params);
169  $statement .= ".comment";
170  }
171  $sql = "UPDATE license_std_comment " .
172  "SET updated = NOW(), user_fk = $2, " . join(",", $updateStatement) .
173  "WHERE lsc_pk = $1 " .
174  "RETURNING 1 AS updated;";
175  $retVal = $this->dbManager->getSingleRow($sql, $params, $statement);
176  $updated += intval($retVal);
177  }
178  return $updated;
179  }
180 
186  function getComment($commentPk)
187  {
188  $this->isCommentIdValid($commentPk);
189  $sql = "SELECT comment FROM license_std_comment " . "WHERE lsc_pk = $1;";
190  $statement = __METHOD__ . ".getComment";
191 
192  $comment = $this->dbManager->getSingleRow($sql, [$commentPk], $statement);
193  $comment = $comment['comment'];
194  if (strcasecmp($comment, "null") === 0) {
195  return null;
196  }
197  return $comment;
198  }
199 
206  function toggleComment($commentPk)
207  {
208  if (! Auth::isAdmin()) {
209  // Only admins can update the comments.
210  return false;
211  }
212  $this->isCommentIdValid($commentPk);
213 
214  $userFk = Auth::getUserId();
215 
216  $sql = "UPDATE license_std_comment " .
217  "SET is_enabled = NOT is_enabled, user_fk = $2 " .
218  "WHERE lsc_pk = $1;";
219 
220  $this->dbManager->getSingleRow($sql, [$commentPk, $userFk]);
221  return true;
222  }
223 
230  private function isCommentIdValid($commentPk)
231  {
232  if (! is_int($commentPk)) {
233  throw new \UnexpectedValueException("Inavlid comment id");
234  }
235  $sql = "SELECT count(*) AS cnt FROM license_std_comment " .
236  "WHERE lsc_pk = $1;";
237 
238  $commentCount = $this->dbManager->getSingleRow($sql, [$commentPk]);
239  if ($commentCount['cnt'] < 1) {
240  // Invalid comment id
241  throw new \UnexpectedValueException("Inavlid comment id");
242  }
243  }
244 }
static getUserId()
Get the current user&#39;s id.
Definition: Auth.php:69
static isAdmin()
Check if user is admin.
Definition: Auth.php:87
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
updateComment($commentPk, $newName, $newComment)
Fossology exception.
Definition: Exception.php:25
updateCommentFromArray($commentArray)
Update the comments based only on the values provided.
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:695