FOSSology  4.4.0
Open Source License Compliance by Open Source Software
TextFindingsAjax.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2019 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
8 
14 use Symfony\Component\HttpFoundation\JsonResponse;
16 
22 {
30  private $dbManager;
34  private $uploadDao;
38  private $copyrightDao;
43 
44  function __construct($uploadTreeTableName)
45  {
46  global $container;
47  $this->dataTablesUtility = $container->get('utils.data_tables_utility');
48  $this->uploadDao = $container->get('dao.upload');
49  $this->dbManager = $container->get('db.manager');
50  $this->copyrightDao = $container->get('dao.copyright');
51  $this->uploadtree_tablename = $uploadTreeTableName;
52  }
53 
64  public function doGetData($type, $upload, $activated = true)
65  {
66  $item = GetParm("item", PARM_INTEGER);
67  $filter = GetParm("filter", PARM_STRING);
68  $listPage = $this->getViewName($type);
69 
70  list ($aaData, $iTotalRecords, $iTotalDisplayRecords) = $this->getTableData(
71  $upload, $item, $type, $listPage, $filter, $activated);
72  return new JsonResponse(
73  array(
74  'sEcho' => intval($_GET['sEcho']),
75  'aaData' => $aaData,
76  'iTotalRecords' => $iTotalRecords,
77  'iTotalDisplayRecords' => $iTotalDisplayRecords
78  ));
79  }
80 
92  private function getTableData($upload, $item, $type, $listPage, $filter,
93  $activated = true)
94  {
95  list ($rows, $iTotalDisplayRecords, $iTotalRecords) = $this->getTextFindings(
96  $upload, $item, $type, $this->uploadtree_tablename, $filter, $activated);
97  $aaData = array();
98  if (! empty($rows)) {
99  $rw = $this->uploadDao->isEditable($upload, Auth::getGroupId());
100  foreach ($rows as $row) {
101  $aaData[] = $this->fillTableRow($row, $upload, $item, $type, $listPage,
102  $activated, $rw);
103  }
104  }
105 
106  return array(
107  $aaData,
108  $iTotalRecords,
109  $iTotalDisplayRecords
110  );
111  }
112 
123  protected function getTextFindings($upload_pk, $item, $type,
124  $uploadTreeTableName, $filter, $activated = true)
125  {
126  $offset = GetParm('iDisplayStart', PARM_INTEGER);
127  $limit = GetParm('iDisplayLength', PARM_INTEGER);
128 
129  $tableName = $this->getTableName($type);
130  $orderString = $this->getOrderString();
131 
132  list ($left, $right) = $this->uploadDao->getLeftAndRight($item,
133  $uploadTreeTableName);
134 
135  if ($filter == "") {
136  $filter = "none";
137  }
138 
139  $sql_upload = "";
140  if ('uploadtree_a' == $uploadTreeTableName) {
141  $sql_upload = " AND UT.upload_fk = $upload_pk ";
142  }
143 
144  $join = "";
145  $filterQuery = "";
146  if ($filter == "nolic") {
147  $noLicStr = "No_license_found";
148  $voidLicStr = "Void";
149  $join = " INNER JOIN license_file AS LF on cp.pfile_fk = LF.pfile_fk ";
150  $filterQuery = " AND LF.rf_fk IN (" . "SELECT rf_pk FROM license_ref " .
151  "WHERE rf_shortname IN ('$noLicStr','$voidLicStr')) ";
152  }
153 
154  $params = array(
155  $left,
156  $right
157  );
158 
159  $filterParms = $params;
160  $searchFilter = $this->addSearchFilter($filterParms);
161  $unorderedQuery = "FROM $tableName AS cp " .
162  "INNER JOIN $uploadTreeTableName AS UT ON cp.pfile_fk = UT.pfile_fk " .
163  $join . "WHERE cp.textfinding != '' " .
164  "AND ( UT.lft BETWEEN $1 AND $2 ) " . "AND cp.is_enabled = " .
165  ($activated ? 'true' : 'false') . $sql_upload;
166  $totalFilter = $filterQuery . " " . $searchFilter;
167 
168  $grouping = " GROUP BY hash ";
169 
170  $countQuery = "SELECT count(*) FROM (SELECT hash $unorderedQuery $totalFilter $grouping) as K";
171  $iTotalDisplayRecordsRow = $this->dbManager->getSingleRow($countQuery,
172  $filterParms,
173  __METHOD__ . $tableName . ".count" . ($activated ? '' : '_deactivated'));
174  $iTotalDisplayRecords = $iTotalDisplayRecordsRow['count'];
175 
176  $countAllQuery = "SELECT count(*) FROM (SELECT hash $unorderedQuery$grouping) as K";
177  $iTotalRecordsRow = $this->dbManager->getSingleRow($countAllQuery, $params,
178  __METHOD__, $tableName . "count.all" . ($activated ? '' : '_deactivated'));
179  $iTotalRecords = $iTotalRecordsRow['count'];
180 
181  $range = "";
182  $filterParms[] = $offset;
183  $range .= ' OFFSET $' . count($filterParms);
184  $filterParms[] = $limit;
185  $range .= ' LIMIT $' . count($filterParms);
186 
187  $sql = "SELECT textfinding, hash, count(*) as textfinding_count " .
188  $unorderedQuery . $totalFilter .
189  " GROUP BY textfinding, hash " . $orderString .
190  $range;
191  $statement = __METHOD__ . $filter . $tableName . $uploadTreeTableName .
192  ($activated ? '' : '_deactivated');
193  $this->dbManager->prepare($statement, $sql);
194  $result = $this->dbManager->execute($statement, $filterParms);
195  $rows = $this->dbManager->fetchAll($result);
196  $this->dbManager->freeResult($result);
197 
198  return array(
199  $rows,
200  $iTotalDisplayRecords,
201  $iTotalRecords
202  );
203  }
204 
209  private function getOrderString()
210  {
211  $columnNamesInDatabase = array(
212  'textfinding_count',
213  'textfinding'
214  );
215 
216  $defaultOrder = CopyrightHistogram::returnSortOrder();
217 
218  return $this->dataTablesUtility->getSortingString($_GET,
219  $columnNamesInDatabase, $defaultOrder);
220  }
221 
227  private function addSearchFilter(&$filterParams)
228  {
229  $searchPattern = GetParm('sSearch', PARM_STRING);
230  if (empty($searchPattern)) {
231  return '';
232  }
233  $filterParams[] = "%$searchPattern%";
234  return ' AND CP.content ilike $' . count($filterParams) . ' ';
235  }
236 
246  private function getTableRowAction($hash, $upload, $type, $activated = true,
247  $rw = true)
248  {
249  $ajaxType = $this->getDecisionTypeName($type);
250  if ($rw) {
251  $act = "<img";
252  if (! $activated) {
253  $act .= " hidden='true'";
254  }
255  $act .= " id='deleteHashDecision$ajaxType$hash' " .
256  "onClick='event.preventDefault();deleteHashDecision(\"$hash\",$upload,\"" .
257  $ajaxType . "\");' class=\"delete\" src=\"images/space_16.png\">";
258  $act .= "<span";
259  if ($activated) {
260  $act .= " hidden='true'";
261  }
262  $act .= " id='undoDeleteHashDecision$ajaxType$hash'> " .
263  "deactivated [<a href=\"#\" class='undo$type' " .
264  "onClick='event.preventDefault();undoHashDecision(\"$hash\",$upload,\"" .
265  $ajaxType . "\");return false;'>Undo</a>]</span>";
266  return $act;
267  }
268  if (! $activated) {
269  return "deactivated";
270  }
271  return "";
272  }
273 
285  private function fillTableRow($row, $upload, $item, $type, $listPage,
286  $activated = true, $rw = true)
287  {
288  $hash = $row['hash'];
289  $sql = "SELECT pfile_fk FROM " . $this->getTableName($type) .
290  " WHERE hash = $1;";
291  $statement = __METHOD__ . ".getPfiles";
292  $decisions = $this->dbManager->getRows($sql, [$hash], $statement);
293  $pfileIds = [];
294  foreach ($decisions as $decision) {
295  $pfileIds[] = $decision['pfile_fk'];
296  }
297  $output = array(
298  'DT_RowId' => $this->getDecisionTypeName($type) . ",$hash"
299  );
300 
301  $link = "<a href='";
302  $link .= Traceback_uri();
303  $link .= "?mod=$listPage&agent=-1&item=$item" .
304  "&hash=$hash&type=$type&filter=all";
305  $link .= "'>". intval($row['textfinding_count']) . "</a>";
306  $output['0'] = $link;
307  $output['1'] = convertToUTF8($row['textfinding']);
308  $output['2'] = $this->getTableRowAction($hash, $upload, $type, $activated,
309  $rw);
310  if ($rw && $activated) {
311  $output['3'] = "<input type='checkbox' class='deleteBySelect$type' " .
312  "id='deleteBySelectfinding$hash' value='$hash,$upload," .
313  $this->getDecisionTypeName($type) . "'>";
314  } else {
315  $output['3'] = "<input type='checkbox' class='undoBySelect$type' " .
316  "id='undoBySelectfinding$hash' value='$hash,$upload," .
317  $this->getDecisionTypeName($type) . "'>";
318  }
319  return $output;
320  }
321 
329  private function getTableName($type)
330  {
331  return "copyright_decision";
332  }
333 
341  private function getDecisionTypeName($type)
342  {
343  return "copyright";
344  }
345 
353  private function getViewName($type)
354  {
355  return "copyright-list";
356  }
357 }
char * uploadtree_tablename
upload.uploadtree_tablename
Definition: adj2nest.c:100
Create histogram plugin for copyright.
Handles Ajax requests for text findings.
getOrderString()
Create sorting string for database query.
getViewName($type)
Get name of view for links.
getTableRowAction($hash, $upload, $type, $activated=true, $rw=true)
Helper to create action column for results.
getTableData($upload, $item, $type, $listPage, $filter, $activated=true)
Get the text finding data and fill in expected format.
getDecisionTypeName($type)
Get type name for ajax calls based.
getTableName($type)
Get table name based on decision type.
addSearchFilter(&$filterParams)
Add filter on content.
getTextFindings($upload_pk, $item, $type, $uploadTreeTableName, $filter, $activated=true)
Get results from database and format for JSON.
fillTableRow($row, $upload, $item, $type, $listPage, $activated=true, $rw=true)
Fill table content for JSON response.
doGetData($type, $upload, $activated=true)
Handles GET request and create a JSON response.
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
static getGroupId()
Get the current user's group id.
Definition: Auth.php:80
static returnSortOrder()
Get sorting orders.
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
if(! defined('ENT_SUBSTITUTE')) convertToUTF8($content, $toHTML=true)
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
Namespace for Copyright agent's UI components.