FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
TextFindingsAjax.php
1 <?php
21 
29 
35 {
43  private $dbManager;
47  private $uploadDao;
51  private $copyrightDao;
56 
57  function __construct($uploadTreeTableName)
58  {
59  global $container;
60  $this->dataTablesUtility = $container->get('utils.data_tables_utility');
61  $this->uploadDao = $container->get('dao.upload');
62  $this->dbManager = $container->get('db.manager');
63  $this->copyrightDao = $container->get('dao.copyright');
64  $this->uploadtree_tablename = $uploadTreeTableName;
65  }
66 
77  public function doGetData($type, $upload, $activated = true)
78  {
79  $item = GetParm("item", PARM_INTEGER);
80  $filter = GetParm("filter", PARM_STRING);
81  $listPage = $this->getViewName($type);
82 
83  list ($aaData, $iTotalRecords, $iTotalDisplayRecords) = $this->getTableData(
84  $upload, $item, $type, $listPage, $filter, $activated);
85  return new JsonResponse(
86  array(
87  'sEcho' => intval($_GET['sEcho']),
88  'aaData' => $aaData,
89  'iTotalRecords' => $iTotalRecords,
90  'iTotalDisplayRecords' => $iTotalDisplayRecords
91  ));
92  }
93 
105  private function getTableData($upload, $item, $type, $listPage, $filter,
106  $activated = true)
107  {
108  list ($rows, $iTotalDisplayRecords, $iTotalRecords) = $this->getTextFindings(
109  $upload, $item, $type, $this->uploadtree_tablename, $filter, $activated);
110  $aaData = array();
111  if (! empty($rows)) {
112  $rw = $this->uploadDao->isEditable($upload, Auth::getGroupId());
113  foreach ($rows as $row) {
114  $aaData[] = $this->fillTableRow($row, $upload, $item, $type, $listPage,
115  $activated, $rw);
116  }
117  }
118 
119  return array(
120  $aaData,
121  $iTotalRecords,
122  $iTotalDisplayRecords
123  );
124  }
125 
136  protected function getTextFindings($upload_pk, $item, $type,
137  $uploadTreeTableName, $filter, $activated = true)
138  {
139  $offset = GetParm('iDisplayStart', PARM_INTEGER);
140  $limit = GetParm('iDisplayLength', PARM_INTEGER);
141 
142  $tableName = $this->getTableName($type);
143  $orderString = $this->getOrderString();
144 
145  list ($left, $right) = $this->uploadDao->getLeftAndRight($item,
146  $uploadTreeTableName);
147 
148  if ($filter == "") {
149  $filter = "none";
150  }
151 
152  $sql_upload = "";
153  if ('uploadtree_a' == $uploadTreeTableName) {
154  $sql_upload = " AND UT.upload_fk = $upload_pk ";
155  }
156 
157  $join = "";
158  $filterQuery = "";
159  if ($filter == "nolic") {
160  $noLicStr = "No_license_found";
161  $voidLicStr = "Void";
162  $join = " INNER JOIN license_file AS LF on cp.pfile_fk = LF.pfile_fk ";
163  $filterQuery = " AND LF.rf_fk IN (" . "SELECT rf_pk FROM license_ref " .
164  "WHERE rf_shortname IN ('$noLicStr','$voidLicStr')) ";
165  }
166 
167  $params = array(
168  $left,
169  $right
170  );
171 
172  $filterParms = $params;
173  $searchFilter = $this->addSearchFilter($filterParms);
174  $unorderedQuery = "FROM $tableName AS cp " .
175  "INNER JOIN $uploadTreeTableName AS UT ON cp.pfile_fk = UT.pfile_fk " .
176  $join . "WHERE cp.textfinding != '' " .
177  "AND ( UT.lft BETWEEN $1 AND $2 ) " . "AND cp.is_enabled = " .
178  ($activated ? 'true' : 'false') . $sql_upload;
179  $totalFilter = $filterQuery . " " . $searchFilter;
180 
181  $grouping = " GROUP BY hash ";
182 
183  $countQuery = "SELECT count(*) FROM (SELECT hash $unorderedQuery $totalFilter $grouping) as K";
184  $iTotalDisplayRecordsRow = $this->dbManager->getSingleRow($countQuery,
185  $filterParms,
186  __METHOD__ . $tableName . ".count" . ($activated ? '' : '_deactivated'));
187  $iTotalDisplayRecords = $iTotalDisplayRecordsRow['count'];
188 
189  $countAllQuery = "SELECT count(*) FROM (SELECT hash $unorderedQuery$grouping) as K";
190  $iTotalRecordsRow = $this->dbManager->getSingleRow($countAllQuery, $params,
191  __METHOD__, $tableName . "count.all" . ($activated ? '' : '_deactivated'));
192  $iTotalRecords = $iTotalRecordsRow['count'];
193 
194  $range = "";
195  $filterParms[] = $offset;
196  $range .= ' OFFSET $' . count($filterParms);
197  $filterParms[] = $limit;
198  $range .= ' LIMIT $' . count($filterParms);
199 
200  $sql = "SELECT textfinding, hash, count(*) as textfinding_count " .
201  $unorderedQuery . $totalFilter .
202  " GROUP BY textfinding, hash " . $orderString .
203  $range;
204  $statement = __METHOD__ . $filter . $tableName . $uploadTreeTableName .
205  ($activated ? '' : '_deactivated');
206  $this->dbManager->prepare($statement, $sql);
207  $result = $this->dbManager->execute($statement, $filterParms);
208  $rows = $this->dbManager->fetchAll($result);
209  $this->dbManager->freeResult($result);
210 
211  return array(
212  $rows,
213  $iTotalDisplayRecords,
214  $iTotalRecords
215  );
216  }
217 
222  private function getOrderString()
223  {
224  $columnNamesInDatabase = array(
225  'textfinding_count',
226  'textfinding'
227  );
228 
229  $defaultOrder = CopyrightHistogram::returnSortOrder();
230 
231  return $this->dataTablesUtility->getSortingString($_GET,
232  $columnNamesInDatabase, $defaultOrder);
233  }
234 
240  private function addSearchFilter(&$filterParams)
241  {
242  $searchPattern = GetParm('sSearch', PARM_STRING);
243  if (empty($searchPattern)) {
244  return '';
245  }
246  $filterParams[] = "%$searchPattern%";
247  return ' AND CP.content ilike $' . count($filterParams) . ' ';
248  }
249 
259  private function getTableRowAction($hash, $upload, $type, $activated = true,
260  $rw = true)
261  {
262  $ajaxType = $this->getDecisionTypeName($type);
263  if ($rw) {
264  $act = "<img";
265  if (! $activated) {
266  $act .= " hidden='true'";
267  }
268  $act .= " id='deleteHashDecision$ajaxType$hash' " .
269  "onClick='event.preventDefault();deleteHashDecision(\"$hash\",$upload,\"" .
270  $ajaxType . "\");' class=\"delete\" src=\"images/space_16.png\">";
271  $act .= "<span";
272  if ($activated) {
273  $act .= " hidden='true'";
274  }
275  $act .= " id='undoDeleteHashDecision$ajaxType$hash'> " .
276  "deactivated [<a href=\"#\" class='undo$type' " .
277  "onClick='event.preventDefault();undoHashDecision(\"$hash\",$upload,\"" .
278  $ajaxType . "\");return false;'>Undo</a>]</span>";
279  return $act;
280  }
281  if (! $activated) {
282  return "deactivated";
283  }
284  return "";
285  }
286 
298  private function fillTableRow($row, $upload, $item, $type, $listPage,
299  $activated = true, $rw = true)
300  {
301  $hash = $row['hash'];
302  $sql = "SELECT pfile_fk FROM " . $this->getTableName($type) .
303  " WHERE hash = $1;";
304  $statement = __METHOD__ . ".getPfiles";
305  $decisions = $this->dbManager->getRows($sql, [$hash], $statement);
306  $pfileIds = [];
307  foreach ($decisions as $decision) {
308  $pfileIds[] = $decision['pfile_fk'];
309  }
310  $output = array(
311  'DT_RowId' => $this->getDecisionTypeName($type) . ",$hash"
312  );
313 
314  $link = "<a href='";
315  $link .= Traceback_uri();
316  $link .= "?mod=$listPage&agent=-1&item=$item" .
317  "&hash=$hash&type=$type&filter=all";
318  $link .= "'>". intval($row['textfinding_count']) . "</a>";
319  $output['0'] = $link;
320  $output['1'] = convertToUTF8($row['textfinding']);
321  $output['2'] = $this->getTableRowAction($hash, $upload, $type, $activated,
322  $rw);
323  if ($rw && $activated) {
324  $output['3'] = "<input type='checkbox' class='deleteBySelect$type' " .
325  "id='deleteBySelectfinding$hash' value='$hash,$upload," .
326  $this->getDecisionTypeName($type) . "'>";
327  } else {
328  $output['3'] = "";
329  }
330  return $output;
331  }
332 
340  private function getTableName($type)
341  {
342  switch ($type) {
343  case "copyFindings":
344  $tableName = "copyright_decision";
345  break;
346  default:
347  $tableName = "copyright_decision";
348  }
349  return $tableName;
350  }
351 
359  private function getDecisionTypeName($type)
360  {
361  switch ($type) {
362  case "copyFindings":
363  $typeName = "copyright";
364  break;
365  default:
366  $typeName = "copyright";
367  }
368  return $typeName;
369  }
370 
378  private function getViewName($type)
379  {
380  switch ($type) {
381  case "copyFindings":
382  $viewName = "copyright-list";
383  break;
384  default:
385  $viewName = "copyright-list";
386  }
387  return $viewName;
388  }
389 }
Traceback_uri()
Get the URI without query to this location.
getTableName($type)
Get table name based on decision type.
Namespace for Copyright agent&#39;s UI components.
getOrderString()
Create sorting string for database query.
fillTableRow($row, $upload, $item, $type, $listPage, $activated=true, $rw=true)
Fill table content for JSON response.
getTableData($upload, $item, $type, $listPage, $filter, $activated=true)
Get the text finding data and fill in expected format.
addSearchFilter(&$filterParams)
Add filter on content.
Handles Ajax requests for text findings.
doGetData($type, $upload, $activated=true)
Handles GET request and create a JSON response.
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:57
const PARM_STRING
Definition: common-parm.php:29
getTextFindings($upload_pk, $item, $type, $uploadTreeTableName, $filter, $activated=true)
Get results from database and format for JSON.
const PARM_INTEGER
Definition: common-parm.php:25
char * uploadtree_tablename
upload.uploadtree_tablename
Definition: adj2nest.c:112
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
getTableRowAction($hash, $upload, $type, $activated=true, $rw=true)
Helper to create action column for results.
static getGroupId()
Get the current user&#39;s group id.
Definition: Auth.php:78
getViewName($type)
Get name of view for links.
if(!defined('ENT_SUBSTITUTE')) convertToUTF8($content, $toHTML=true)
returnSortOrder()
Get sorting orders.
getDecisionTypeName($type)
Get type name for ajax calls based.