FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
delete-helper.php
1 <?php
2 
3 include_once dirname(dirname(__DIR__)) . "/lib/php/common.php";
4 include_once "DeleteResponse.php";
5 include_once "DeleteMessages.php";
8 
17 function TryToDelete($uploadpk, $user_pk, $group_pk, $uploadDao)
18 {
19  if (! $uploadDao->isEditable($uploadpk, $group_pk)) {
20  $returnMessage = DeleteMessages::NO_PERMISSION;
21  return new DeleteResponse($returnMessage);
22  }
23 
24  $rc = DeleteUpload(intval($uploadpk), $user_pk, $group_pk, $uploadDao);
25 
26  if (! empty($rc)) {
27  $returnMessage = DeleteMessages::SCHEDULING_FAILED;
28  return new DeleteResponse($returnMessage);
29  }
30 
31  /* Need to refresh the screen */
32  $URL = Traceback_uri() . "?mod=showjobs&upload=$uploadpk ";
33  $LinkText = _("View Jobs");
34  $returnMessage = DeleteMessages::SUCCESS;
35  return new DeleteResponse($returnMessage,
36  " <a href=$URL>$LinkText</a>");
37 }
38 
47 function DeleteUpload($uploadpk, $user_pk, $group_pk, $Depends = NULL)
48 {
49  /* Prepare the job: job "Delete" */
50  $jobpk = JobAddJob($user_pk, $group_pk, "Delete", $uploadpk);
51  if (empty($jobpk) || ($jobpk < 0)) {
52  $text = _("Failed to create job record");
53  return ($text);
54  }
55  /* Add job: job "Delete" has jobqueue item "delagent" */
56  $jqargs = "DELETE UPLOAD $uploadpk";
57  $jobqueuepk = JobQueueAdd($jobpk, "delagent", $jqargs, NULL, NULL);
58  if (empty($jobqueuepk)) {
59  $text = _("Failed to place delete in job queue");
60  return ($text);
61  }
62 
63  /* Tell the scheduler to check the queue. */
64  $success = fo_communicate_with_scheduler("database", $output, $error_msg);
65  if (!$success) {
66  $error_msg = _("Is the scheduler running? Your jobs have been added to job queue.");
67  $URL = Traceback_uri() . "?mod=showjobs&upload=$uploadpk ";
68  $LinkText = _("View Jobs");
69  $msg = "$error_msg <a href=$URL>$LinkText</a>";
70  return $msg;
71  }
72  return (null);
73 } // Delete()
74 
80 function initDeletion($uploadpks)
81 {
82  if (sizeof($uploadpks) <= 0) {
83  return DisplayMessage("No uploads selected");
84  }
85 
86  $V = "";
87  $errorMessages = [];
88  $deleteResponse = null;
89  for ($i=0; $i < sizeof($uploadpks); $i++) {
90  $deleteResponse = TryToDelete(intval($uploadpks[$i]));
91 
92  if ($deleteResponse->getDeleteMessageCode() != DeleteMessages::SUCCESS) {
93  $errorMessages[] = $deleteResponse;
94  }
95  }
96 
97  if (sizeof($uploadpks) == 1) {
98  $V .= DisplayMessage($deleteResponse->getDeleteMessageString().$deleteResponse->getAdditionalMessage());
99  } else {
100  $displayMessage = "";
101 
102  if (in_array(DeleteMessages::SCHEDULING_FAILED, $errorMessages)) {
103  $displayMessage .= "<br/>Scheduling failed for " .
104  array_count_values($errorMessages)[DeleteMessages::SCHEDULING_FAILED] . " uploads<br/>";
105  }
106 
107  if (in_array(DeleteMessages::NO_PERMISSION, $errorMessages)) {
108  $displayMessage .= "No permission to delete " .
109  array_count_values($errorMessages)[DeleteMessages::NO_PERMISSION]. " uploads<br/>";
110  }
111 
112  $displayMessage .= "Deletion of " .
113  (sizeof($uploadpks)-sizeof($errorMessages)) . " projects queued";
114  $V .= DisplayMessage($displayMessage);
115  }
116  return $V;
117 }
Traceback_uri()
Get the URI without query to this location.
JobQueueAdd($job_pk, $jq_type, $jq_args, $jq_runonpfile, $Depends, $host=NULL, $jq_cmd_args=NULL)
Insert a jobqueue + jobdepends records.
Definition: common-job.php:159
Handle response from delagent.
fo_communicate_with_scheduler($input, &$output, &$error_msg)
Communicate with scheduler, send commands to the scheduler, then get the output.