16 use Symfony\Component\HttpFoundation\JsonResponse;
17 use Symfony\Component\HttpFoundation\Response;
19 define(
"TITLE_COPYRIGHTHISTOGRAMPROCESSPOST", _(
"Private: Browse post"));
34 private $uploadtree_tablename;
46 private $copyrightDao;
51 private $dataTablesUtility;
60 $this->Name =
"ajax-copyright-hist";
61 $this->Title = TITLE_COPYRIGHTHISTOGRAMPROCESSPOST;
63 $this->OutputType =
'JSON';
67 parent::__construct();
69 $this->dataTablesUtility = $container->get(
'utils.data_tables_utility');
70 $this->uploadDao = $container->get(
'dao.upload');
71 $this->
dbManager = $container->get(
'db.manager');
72 $this->copyrightDao = $container->get(
'dao.copyright');
83 if ($this->
State != PLUGIN_STATE_READY) {
91 if ($action==
"deletedecision" || $action==
"undodecision") {
94 } elseif ($action==
"deleteHashDecision" || $action==
"undoHashDecision") {
96 } elseif ($action==
"update" || $action==
"delete" || $action==
"undo" || $action==
"revertToOriginal") {
98 $getEachID = array_filter(explode(
",",
trim($id,
',')),
function($var) {
101 if (count($getEachID) == 4) {
102 list($upload, $item, $hash, $type) = $getEachID;
104 return new Response(
'bad request while '.$action,
105 Response::HTTP_BAD_REQUEST,
106 array(
'Content-type'=>
'text/plain')
112 if (!(($action ==
"getData" || $action ==
"getDeactivatedData") &&
113 ($this->uploadDao->isAccessible($upload, Auth::getGroupId())) ||
114 ($this->uploadDao->isEditable($upload, Auth::getGroupId())))) {
115 $permDeniedText = _(
"Permission Denied");
116 $returnValue =
"<h2>$permDeniedText</h2>";
121 if (in_array($type, $this->textFindingTypes) &&
122 ($action ==
"getData" || $action ==
"getDeactivatedData")) {
124 if ($action ==
"getData") {
125 $returnValue = $textFindingsHandler->doGetData($type, $upload);
126 } elseif ($action ==
"getDeactivatedData") {
127 $returnValue = $textFindingsHandler->doGetData($type, $upload,
false);
132 $returnValue = $this->
doGetData($upload);
134 case "getDeactivatedData":
135 $returnValue = $this->
doGetData($upload,
false);
138 $returnValue = $this->
doUpdate($item, $hash, $type);
141 $returnValue = $this->
doDelete($item, $hash, $type);
144 $returnValue = $this->
doUndo($item, $hash, $type);
146 case "revertToOriginal":
149 case "deletedecision":
155 case "deleteHashDecision":
158 case "undoHashDecision":
162 $returnValue =
"<h2>" . _(
"Unknown action") .
"</h2>";
177 protected function doGetData($upload, $activated =
true)
185 header(
'Content-type: text/json');
186 list($aaData, $iTotalRecords, $iTotalDisplayRecords) = $this->
getTableData($upload, $item, $agent_pk, $type,
$listPage, $filter, $activated);
187 return new JsonResponse(array(
188 'sEcho' => intval($_GET[
'sEcho']),
190 'iTotalRecords' => $iTotalRecords,
191 'iTotalDisplayRecords' => $iTotalDisplayRecords
211 list ($rows, $iTotalDisplayRecords, $iTotalRecords) = $this->
getCopyrights($upload, $item, $this->
uploadtree_tablename, $agent_pk, $type, $filter, $activated, $offset, $limit);
214 $rw = $this->uploadDao->isEditable($upload, Auth::getGroupId());
215 foreach ($rows as $row) {
216 $aaData [] = $this->
fillTableRow($row, $item, $upload, $agent_pk, $type,
$listPage, $filter, $activated, $rw);
220 return array($aaData, $iTotalRecords, $iTotalDisplayRecords);
235 public function getCopyrights($upload_pk, $item, $uploadTreeTableName, $agentId, $type, $filter, $activated =
true, $offset =
null , $limit =
null)
238 $tableNameEvent = $tableName.
'_event';
241 list($left, $right) = $this->uploadDao->getLeftAndRight($item, $uploadTreeTableName);
248 if (
'uploadtree_a' == $uploadTreeTableName) {
249 $sql_upload =
" AND UT.upload_fk=$5 ";
254 if (($type ==
'statement' || $type ==
'scancode_statement') && $filter ==
"nolic") {
255 $noLicStr =
"No_license_found";
256 $voidLicStr =
"Void";
257 $join =
" INNER JOIN license_file AS LF on cp.pfile_fk=LF.pfile_fk ";
258 $filterQuery =
" AND LF.rf_fk IN (SELECT rf_pk FROM license_ref WHERE rf_shortname IN ('$noLicStr','$voidLicStr')) ";
262 $params = array($left, $right, $type,
"{" . $agentId .
"}", $upload_pk);
264 $filterParms = $params;
267 $activatedClause =
"ce.is_enabled = 'false'";
269 $activatedClause =
"ce.is_enabled IS NULL OR ce.is_enabled = 'true'";
271 $unorderedQuery =
"FROM $tableName AS cp " .
272 "INNER JOIN $uploadTreeTableName AS UT ON cp.pfile_fk = UT.pfile_fk " .
273 "LEFT JOIN $tableNameEvent AS ce ON ce.".$tableName.
"_fk = cp.".$tableName.
"_pk " .
274 "AND ce.upload_fk = $5 AND ce.uploadtree_fk = UT.uploadtree_pk " .
276 "WHERE cp.content!='' " .
277 "AND ( UT.lft BETWEEN $1 AND $2 ) " .
278 "AND cp.type = $3 " .
279 "AND cp.agent_fk = ANY($4::int[]) " .
280 "AND ($activatedClause)" .
282 $grouping =
" GROUP BY mcontent ";
284 $countQuery =
"SELECT count(*) FROM (
285 SELECT mcontent AS content FROM (SELECT
286 (CASE WHEN (ce.content IS NULL OR ce.content = '') THEN cp.content ELSE ce.content END) AS mcontent
287 $unorderedQuery $filterQuery $grouping) AS k $searchFilter) AS cx";
288 $iTotalDisplayRecordsRow = $this->
dbManager->getSingleRow($countQuery,
289 $filterParms, __METHOD__.$tableName .
".count" . ($activated ?
'' :
'_deactivated'));
290 $iTotalDisplayRecords = $iTotalDisplayRecordsRow[
'count'];
292 $countAllQuery =
"SELECT count(*) FROM (SELECT
293 (CASE WHEN (ce.content IS NULL OR ce.content = '') THEN cp.content ELSE ce.content END) AS mcontent
294 $unorderedQuery$grouping) as K";
295 $iTotalRecordsRow = $this->
dbManager->getSingleRow($countAllQuery, $params, __METHOD__,$tableName .
"count.all" . ($activated ?
'' :
'_deactivated'));
296 $iTotalRecords = $iTotalRecordsRow[
'count'];
299 $filterParms[] = $offset;
300 $range .=
' OFFSET $' . count($filterParms);
301 $filterParms[] = $limit;
302 $range .=
' LIMIT $' . count($filterParms);
304 $sql =
"SELECT mcontent AS content, mhash AS hash, copyright_count FROM (SELECT
305 (CASE WHEN (ce.content IS NULL OR ce.content = '') THEN cp.content ELSE ce.content END) AS mcontent,
306 (CASE WHEN (ce.hash IS NULL OR ce.hash = '') THEN cp.hash ELSE ce.hash END) AS mhash,
307 count(*) AS copyright_count " .
308 "$unorderedQuery $filterQuery GROUP BY mcontent, mhash) AS k $searchFilter $orderString $range";
309 $statement = __METHOD__ . $filter.$tableName . $uploadTreeTableName . ($activated ?
'' :
'_deactivated');
310 $rows = $this->
dbManager->getRows($sql, $filterParms, $statement);
312 return array($rows, $iTotalDisplayRecords, $iTotalRecords);
338 $tableName =
"keyword";
342 $tableName =
"copyright";
344 case "scancode_statement":
345 $tableName =
"scancode_copyright";
347 case "user_statement":
348 $tableName =
"copyright_decision";
350 case "scancode_email":
351 case "scancode_author":
353 $tableName =
"scancode_author";
356 $tableName =
"author";
367 $columnNamesInDatabase = array(
'copyright_count',
'content');
371 return $this->dataTablesUtility->getSortingString($_GET, $columnNamesInDatabase, $defaultOrder);
394 if (empty($searchPattern)) {
397 $filterParams[] =
"%$searchPattern%";
398 if ($isInverseSearch ==
'true') {
399 return 'WHERE mcontent not ilike $'.count($filterParams).
' ';
401 return 'WHERE mcontent ilike $'.count($filterParams).
' ';
414 private function getTableRowAction($hash, $uploadTreeId, $upload, $type, $activated =
true, $rw =
true)
419 $act .=
" hidden='true'";
421 $act .=
" id='delete$type$hash' onClick='delete$type($upload,$uploadTreeId,\"$hash\",\"$type\");' class=\"delete\" src=\"images/space_16.png\">";
424 $act .=
" hidden='true'";
426 $act .=
" id='update$type$hash'>deactivated [<a href=\"#\" id='undo$type$hash' onClick='undo$type($upload,$uploadTreeId,\"$hash\",\"$type\");return false;'>Undo</a>]</span>";
430 return "deactivated";
448 private function fillTableRow($row, $uploadTreeId, $upload, $agentId, $type,
$listPage, $filter =
"", $activated =
true, $rw =
true)
450 $hash = $row[
'hash'];
451 $output = array(
'DT_RowId' =>
"$upload,$uploadTreeId,$hash,$type",
"DT_RowClass" =>
"row$hash");
455 $urlArgs =
"?mod=".$listPage.
"&agent=$agentId&item=$uploadTreeId&hash=$hash&type=$type";
456 if ((empty($filter) || $filter ==
"all")) {
460 $filter =
"inactive";
463 if (!empty($filter)) {
464 $urlArgs .=
"&filter=$filter";
466 $link .= $urlArgs .
"'>" . $row[
'copyright_count'] .
"</a>";
467 $output[
'0'] = $link;
469 $output[
'2'] = $this->
getTableRowAction($hash, $uploadTreeId, $upload, $type, $activated, $rw);
470 if ($rw && $activated) {
471 $output[
'3'] =
"<input type='checkbox' class='deleteBySelect$type' id='deleteBySelect$type$hash' value='".$upload.
",".$uploadTreeId.
",".$hash.
",".$type.
"'>";
473 $output[
'3'] =
"<input type='checkbox' class='undoBySelect$type' id='undoBySelect$type$hash' value='".$upload.
",".$uploadTreeId.
",".$hash.
",".$type.
"'>";
488 if (empty($content)) {
489 return new Response(
'empty content not allowed',
490 Response::HTTP_BAD_REQUEST ,array(
'Content-type'=>
'text/plain'));
495 $this->copyrightDao->updateTable($item, $hash, $content, Auth::getUserId(), $cpTable);
497 return new Response(
'success', Response::HTTP_OK,array(
'Content-type'=>
'text/plain'));
511 $this->copyrightDao->updateTable($item, $hash,
'', Auth::getUserId(), $cpTable,
'delete');
512 return new Response(
'Successfully deleted', Response::HTTP_OK, array(
'Content-type'=>
'text/plain'));
522 protected function doUndo($itemId, $hash, $type)
526 $this->copyrightDao->updateTable($item, $hash,
'', Auth::getUserId(), $cpTable,
'rollback');
527 return new Response(
'Successfully restored', Response::HTTP_OK, array(
'Content-type'=>
'text/plain'));
539 if (empty($newContent)) {
540 return new Response(
'empty content',
541 Response::HTTP_BAD_REQUEST, array(
'Content-type'=>
'text/plain'));
543 $newHash = md5(StringOperation::replaceUnicodeControlChar($newContent));
546 $this->copyrightDao->updateTable($item, $newHash,
'', Auth::getUserId(), $cpTable,
'revertToOriginal');
547 return new Response(
'Successfully reverted', Response::HTTP_OK, array(
'Content-type'=>
'text/plain'));
559 $this->copyrightDao->removeDecision($type.
"_decision", $pfileId, $decisionId);
560 return new JsonResponse(array(
"msg" => $decisionId .
" .. " . $pfileId .
" .. " . $type));
572 $this->copyrightDao->undoDecision($type.
"_decision", $pfileId, $decisionId);
573 return new JsonResponse(array(
"msg" => $decisionId .
" .. " . $pfileId .
" .. " . $type));
585 $tableName = $type.
"_decision";
586 $decisions = $this->copyrightDao->getDecisionsFromHash($tableName, $hash,
588 foreach ($decisions as $decision) {
589 $this->copyrightDao->removeDecision($tableName, $decision[
'pfile_fk'],
590 $decision[$tableName .
'_pk']);
592 return new JsonResponse(array(
"msg" =>
"$hash .. $upload .. $type"));
604 $tableName = $type.
"_decision";
605 $decisions = $this->copyrightDao->getDecisionsFromHash($tableName, $hash,
607 foreach ($decisions as $decision) {
608 $this->copyrightDao->undoDecision($tableName, $decision[
'pfile_fk'],
609 $decision[$tableName .
'_pk']);
611 return new JsonResponse(array(
"msg" =>
"$hash .. $upload .. $type"));
616 $NewPlugin->Initialize();
char * uploadtree_tablename
upload.uploadtree_tablename
Handles Ajax requests for copyright.
doUpdate($itemId, $hash, $type)
Update result.
getTableRowAction($hash, $uploadTreeId, $upload, $type, $activated=true, $rw=true)
Helper to create action column for results.
getTableName($type)
Get table name based on statement type.
doDeleteDecision($decisionId, $pfileId, $type)
Disable a decision.
getCopyrights($upload_pk, $item, $uploadTreeTableName, $agentId, $type, $filter, $activated=true, $offset=null, $limit=null)
Get results from database and format for JSON.
doGetData($upload, $activated=true)
Handles GET request and create a JSON response.
doRevertToOriginal($itemId, $type)
Revert an edited result back to its original scanner text.
doUndoDecision($decisionId, $pfileId, $type)
Rollback a decision.
doDelete($itemId, $hash, $type)
Disable a result.
getTableData($upload, $item, $agent_pk, $type, $listPage, $filter, $activated=true)
Get the copyright data and fill in expected format.
getAgentId($upload_pk, $copyrightType)
Get Agent ID for an upload.
__construct()
base constructor. Most plugins will just use this
fillTableRow($row, $uploadTreeId, $upload, $agentId, $type, $listPage, $filter="", $activated=true, $rw=true)
Fill table content for JSON response.
addSearchFilter(&$filterParams)
Add filter on content.
Output()
Display the loaded menu and plugins.
doUndo($itemId, $hash, $type)
Rollback a result.
doDeleteHashDecision($hash, $upload, $type)
Disable decisions for an upload which matches a hash.
doUndoHashDecision($hash, $upload, $type)
Rollback decisions for an upload which matches a hash.
getOrderString()
Create sorting string for database query.
This is the Plugin class. All plugins should:
Handles Ajax requests for text findings.
Contains the constants and helpers for authentication of user.
static returnSortOrder()
Get sorting orders.
LatestAgentpk($upload_pk, $arsTableName, $arsSuccess=false)
Given an upload_pk, find the latest enabled agent_pk with results.
Traceback_uri()
Get the URI without query to this location.
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
if(! defined('ENT_SUBSTITUTE')) convertToUTF8($content, $toHTML=true)
char * trim(char *ptext)
Trimming whitespace.
#define PLUGIN_DB_READ
Plugin requires read permission on DB.
fo_dbManager * dbManager
fo_dbManager object