15 use Symfony\Component\HttpFoundation\JsonResponse;
16 use Symfony\Component\HttpFoundation\Response;
18 define(
"TITLE_COPYRIGHTHISTOGRAMPROCESSPOST", _(
"Private: Browse post"));
33 private $uploadtree_tablename;
45 private $copyrightDao;
50 private $dataTablesUtility;
59 $this->Name =
"ajax-copyright-hist";
60 $this->Title = TITLE_COPYRIGHTHISTOGRAMPROCESSPOST;
62 $this->OutputType =
'JSON';
66 parent::__construct();
68 $this->dataTablesUtility = $container->get(
'utils.data_tables_utility');
69 $this->uploadDao = $container->get(
'dao.upload');
70 $this->
dbManager = $container->get(
'db.manager');
71 $this->copyrightDao = $container->get(
'dao.copyright');
82 if ($this->
State != PLUGIN_STATE_READY) {
90 if ($action==
"deletedecision" || $action==
"undodecision") {
93 } elseif ($action==
"deleteHashDecision" || $action==
"undoHashDecision") {
95 } elseif ($action==
"update" || $action==
"delete" || $action==
"undo") {
97 $getEachID = array_filter(explode(
",",
trim($id,
',')),
function($var) {
100 if (count($getEachID) == 4) {
101 list($upload, $item, $hash, $type) = $getEachID;
103 return new Response(
'bad request while '.$action,
104 Response::HTTP_BAD_REQUEST,
105 array(
'Content-type'=>
'text/plain')
111 if (!(($action ==
"getData" || $action ==
"getDeactivatedData") &&
112 ($this->uploadDao->isAccessible($upload, Auth::getGroupId())) ||
113 ($this->uploadDao->isEditable($upload, Auth::getGroupId())))) {
114 $permDeniedText = _(
"Permission Denied");
115 $returnValue =
"<h2>$permDeniedText</h2>";
120 if (in_array($type, $this->textFindingTypes) &&
121 ($action ==
"getData" || $action ==
"getDeactivatedData")) {
123 if ($action ==
"getData") {
124 $returnValue = $textFindingsHandler->doGetData($type, $upload);
125 } elseif ($action ==
"getDeactivatedData") {
126 $returnValue = $textFindingsHandler->doGetData($type, $upload,
false);
131 $returnValue = $this->
doGetData($upload);
133 case "getDeactivatedData":
134 $returnValue = $this->
doGetData($upload,
false);
137 $returnValue = $this->
doUpdate($item, $hash, $type);
140 $returnValue = $this->
doDelete($item, $hash, $type);
143 $returnValue = $this->
doUndo($item, $hash, $type);
145 case "deletedecision":
151 case "deleteHashDecision":
154 case "undoHashDecision":
158 $returnValue =
"<h2>" . _(
"Unknown action") .
"</h2>";
173 protected function doGetData($upload, $activated =
true)
181 header(
'Content-type: text/json');
182 list($aaData, $iTotalRecords, $iTotalDisplayRecords) = $this->
getTableData($upload, $item, $agent_pk, $type,
$listPage, $filter, $activated);
183 return new JsonResponse(array(
184 'sEcho' => intval($_GET[
'sEcho']),
186 'iTotalRecords' => $iTotalRecords,
187 'iTotalDisplayRecords' => $iTotalDisplayRecords
207 list ($rows, $iTotalDisplayRecords, $iTotalRecords) = $this->
getCopyrights($upload, $item, $this->
uploadtree_tablename, $agent_pk, $type, $filter, $activated, $offset, $limit);
210 $rw = $this->uploadDao->isEditable($upload, Auth::getGroupId());
211 foreach ($rows as $row) {
212 $aaData [] = $this->
fillTableRow($row, $item, $upload, $agent_pk, $type,
$listPage, $filter, $activated, $rw);
216 return array($aaData, $iTotalRecords, $iTotalDisplayRecords);
231 public function getCopyrights($upload_pk, $item, $uploadTreeTableName, $agentId, $type, $filter, $activated =
true, $offset =
null , $limit =
null)
234 $tableNameEvent = $tableName.
'_event';
237 list($left, $right) = $this->uploadDao->getLeftAndRight($item, $uploadTreeTableName);
244 if (
'uploadtree_a' == $uploadTreeTableName) {
245 $sql_upload =
" AND UT.upload_fk=$5 ";
250 if (($type ==
'statement' || $type ==
'scancode_statement') && $filter ==
"nolic") {
251 $noLicStr =
"No_license_found";
252 $voidLicStr =
"Void";
253 $join =
" INNER JOIN license_file AS LF on cp.pfile_fk=LF.pfile_fk ";
254 $filterQuery =
" AND LF.rf_fk IN (SELECT rf_pk FROM license_ref WHERE rf_shortname IN ('$noLicStr','$voidLicStr')) ";
258 $params = array($left, $right, $type,
"{" . $agentId .
"}", $upload_pk);
260 $filterParms = $params;
263 $activatedClause =
"ce.is_enabled = 'false'";
265 $activatedClause =
"ce.is_enabled IS NULL OR ce.is_enabled = 'true'";
267 $unorderedQuery =
"FROM $tableName AS cp " .
268 "INNER JOIN $uploadTreeTableName AS UT ON cp.pfile_fk = UT.pfile_fk " .
269 "LEFT JOIN $tableNameEvent AS ce ON ce.".$tableName.
"_fk = cp.".$tableName.
"_pk " .
270 "AND ce.upload_fk = $5 AND ce.uploadtree_fk = UT.uploadtree_pk " .
272 "WHERE cp.content!='' " .
273 "AND ( UT.lft BETWEEN $1 AND $2 ) " .
274 "AND cp.type = $3 " .
275 "AND cp.agent_fk = ANY($4::int[]) " .
276 "AND ($activatedClause)" .
278 $grouping =
" GROUP BY mcontent ";
280 $countQuery =
"SELECT count(*) FROM (
281 SELECT mcontent AS content FROM (SELECT
282 (CASE WHEN (ce.content IS NULL OR ce.content = '') THEN cp.content ELSE ce.content END) AS mcontent
283 $unorderedQuery $filterQuery $grouping) AS k $searchFilter) AS cx";
284 $iTotalDisplayRecordsRow = $this->
dbManager->getSingleRow($countQuery,
285 $filterParms, __METHOD__.$tableName .
".count" . ($activated ?
'' :
'_deactivated'));
286 $iTotalDisplayRecords = $iTotalDisplayRecordsRow[
'count'];
288 $countAllQuery =
"SELECT count(*) FROM (SELECT
289 (CASE WHEN (ce.content IS NULL OR ce.content = '') THEN cp.content ELSE ce.content END) AS mcontent
290 $unorderedQuery$grouping) as K";
291 $iTotalRecordsRow = $this->
dbManager->getSingleRow($countAllQuery, $params, __METHOD__,$tableName .
"count.all" . ($activated ?
'' :
'_deactivated'));
292 $iTotalRecords = $iTotalRecordsRow[
'count'];
295 $filterParms[] = $offset;
296 $range .=
' OFFSET $' . count($filterParms);
297 $filterParms[] = $limit;
298 $range .=
' LIMIT $' . count($filterParms);
300 $sql =
"SELECT mcontent AS content, mhash AS hash, copyright_count FROM (SELECT
301 (CASE WHEN (ce.content IS NULL OR ce.content = '') THEN cp.content ELSE ce.content END) AS mcontent,
302 (CASE WHEN (ce.hash IS NULL OR ce.hash = '') THEN cp.hash ELSE ce.hash END) AS mhash,
303 count(*) AS copyright_count " .
304 "$unorderedQuery $filterQuery GROUP BY mcontent, mhash) AS k $searchFilter $orderString $range";
305 $statement = __METHOD__ . $filter.$tableName . $uploadTreeTableName . ($activated ?
'' :
'_deactivated');
306 $rows = $this->
dbManager->getRows($sql, $filterParms, $statement);
308 return array($rows, $iTotalDisplayRecords, $iTotalRecords);
334 $tableName =
"keyword";
338 $tableName =
"copyright";
340 case "scancode_statement":
341 $tableName =
"scancode_copyright";
343 case "user_statement":
344 $tableName =
"copyright_decision";
346 case "scancode_email":
347 case "scancode_author":
349 $tableName =
"scancode_author";
352 $tableName =
"author";
363 $columnNamesInDatabase = array(
'copyright_count',
'content');
367 return $this->dataTablesUtility->getSortingString($_GET, $columnNamesInDatabase, $defaultOrder);
390 if (empty($searchPattern)) {
393 $filterParams[] =
"%$searchPattern%";
394 if ($isInverseSearch ==
'true') {
395 return 'WHERE mcontent not ilike $'.count($filterParams).
' ';
397 return 'WHERE mcontent ilike $'.count($filterParams).
' ';
410 private function getTableRowAction($hash, $uploadTreeId, $upload, $type, $activated =
true, $rw =
true)
415 $act .=
" hidden='true'";
417 $act .=
" id='delete$type$hash' onClick='delete$type($upload,$uploadTreeId,\"$hash\",\"$type\");' class=\"delete\" src=\"images/space_16.png\">";
420 $act .=
" hidden='true'";
422 $act .=
" id='update$type$hash'>deactivated [<a href=\"#\" id='undo$type$hash' onClick='undo$type($upload,$uploadTreeId,\"$hash\",\"$type\");return false;'>Undo</a>]</span>";
426 return "deactivated";
444 private function fillTableRow($row, $uploadTreeId, $upload, $agentId, $type,
$listPage, $filter =
"", $activated =
true, $rw =
true)
446 $hash = $row[
'hash'];
447 $output = array(
'DT_RowId' =>
"$upload,$uploadTreeId,$hash,$type",
"DT_RowClass" =>
"row$hash");
451 $urlArgs =
"?mod=".$listPage.
"&agent=$agentId&item=$uploadTreeId&hash=$hash&type=$type";
452 if ((empty($filter) || $filter ==
"all")) {
456 $filter =
"inactive";
459 if (!empty($filter)) {
460 $urlArgs .=
"&filter=$filter";
462 $link .= $urlArgs .
"'>" . $row[
'copyright_count'] .
"</a>";
463 $output[
'0'] = $link;
465 $output[
'2'] = $this->
getTableRowAction($hash, $uploadTreeId, $upload, $type, $activated, $rw);
466 if ($rw && $activated) {
467 $output[
'3'] =
"<input type='checkbox' class='deleteBySelect$type' id='deleteBySelect$type$hash' value='".$upload.
",".$uploadTreeId.
",".$hash.
",".$type.
"'>";
469 $output[
'3'] =
"<input type='checkbox' class='undoBySelect$type' id='undoBySelect$type$hash' value='".$upload.
",".$uploadTreeId.
",".$hash.
",".$type.
"'>";
484 if (empty($content)) {
485 return new Response(
'empty content not allowed',
486 Response::HTTP_BAD_REQUEST ,array(
'Content-type'=>
'text/plain'));
491 $this->copyrightDao->updateTable($item, $hash, $content, Auth::getUserId(), $cpTable);
493 return new Response(
'success', Response::HTTP_OK,array(
'Content-type'=>
'text/plain'));
507 $this->copyrightDao->updateTable($item, $hash,
'', Auth::getUserId(), $cpTable,
'delete');
508 return new Response(
'Successfully deleted', Response::HTTP_OK, array(
'Content-type'=>
'text/plain'));
518 protected function doUndo($itemId, $hash, $type)
522 $this->copyrightDao->updateTable($item, $hash,
'', Auth::getUserId(), $cpTable,
'rollback');
523 return new Response(
'Successfully restored', Response::HTTP_OK, array(
'Content-type'=>
'text/plain'));
535 $this->copyrightDao->removeDecision($type.
"_decision", $pfileId, $decisionId);
536 return new JsonResponse(array(
"msg" => $decisionId .
" .. " . $pfileId .
" .. " . $type));
548 $this->copyrightDao->undoDecision($type.
"_decision", $pfileId, $decisionId);
549 return new JsonResponse(array(
"msg" => $decisionId .
" .. " . $pfileId .
" .. " . $type));
561 $tableName = $type.
"_decision";
562 $decisions = $this->copyrightDao->getDecisionsFromHash($tableName, $hash,
564 foreach ($decisions as $decision) {
565 $this->copyrightDao->removeDecision($tableName, $decision[
'pfile_fk'],
566 $decision[$tableName .
'_pk']);
568 return new JsonResponse(array(
"msg" =>
"$hash .. $upload .. $type"));
580 $tableName = $type.
"_decision";
581 $decisions = $this->copyrightDao->getDecisionsFromHash($tableName, $hash,
583 foreach ($decisions as $decision) {
584 $this->copyrightDao->undoDecision($tableName, $decision[
'pfile_fk'],
585 $decision[$tableName .
'_pk']);
587 return new JsonResponse(array(
"msg" =>
"$hash .. $upload .. $type"));
592 $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.
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