Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leaf 2784 - comments on cancel #2097

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions LEAF_Request_Portal/ajaxIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ function customTemplate($tpl)

break;
case 'cancel':
/* This endpoint has been deprecated as of 8/31/2023 */
jampaul3 marked this conversation as resolved.
Show resolved Hide resolved
if (is_numeric($_POST['cancel']))
{
$form = new Portal\Form($db, $login);
Expand Down
2 changes: 1 addition & 1 deletion LEAF_Request_Portal/api/controllers/FormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public function post($act)
});

$this->index['POST']->register('form/[digit]/cancel', function ($args) use ($form) {
return $form->deleteRecord((int)$args[0]);
return $form->deleteRecord((int)$args[0], $_POST['comment']);
});

$this->index['POST']->register('form/[digit]/delete', function ($args) use ($form) {
Expand Down
112 changes: 69 additions & 43 deletions LEAF_Request_Portal/sources/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -710,56 +710,82 @@ public function getFormJSON($recordID)
return json_encode($json);
}

public function deleteRecord($recordID)
/**
* @param int $recordID
* @param string $comment
*
* @return int|string
*
* Created at: 8/24/2023, 2:15:39 PM (America/New_York)
*/
public function deleteRecord(int $recordID, string $comment = ''): int|string
{
if ($_POST['CSRFToken'] != $_SESSION['CSRFToken'])
{
return 0;
}
if (!$this->hasWriteAccess($recordID))
{
return 'Please contact your administrator to cancel this request to help avoid confusion in the process.';
}
if ($_POST['CSRFToken'] != $_SESSION['CSRFToken']) {
$return_value = 0;
} elseif (!$this->hasWriteAccess($recordID)) {
$return_value = 'Please contact your administrator to cancel this request to help avoid confusion in the process.';
} else {
// only allow admins to delete resolved requests
$vars = array(':recordID' => $recordID);
$sql = 'SELECT `recordID`, `submitted`, `stepID`
FROM `records`
LEFT JOIN `records_workflow_state` USING (`recordID`)
WHERE `recordID` = :recordID
AND `submitted` > 0';

// only allow admins to delete resolved requests
$vars = array(':recordID' => $recordID);
$res = $this->db->prepared_query('SELECT recordID, submitted, stepID FROM records
LEFT JOIN records_workflow_state USING (recordID)
WHERE recordID=:recordID
AND submitted > 0', $vars);
if (isset($res[0])
&& $res[0]['stepID'] == null
&& !$this->login->checkGroup(1))
{
return 'Cannot cancel resolved request.';
}
$res = $this->db->prepared_query($sql, $vars);

$vars = array(':recordID' => $recordID,
':time' => time(), );
$res = $this->db->prepared_query('UPDATE records SET
deleted=:time
WHERE recordID=:recordID', $vars);
if (
isset($res[0])
&& $res[0]['stepID'] == null
&& !$this->login->checkGroup(1)
) {
$return_value = 'Cannot cancel resolved request.';
} else {
$vars = array(':recordID' => $recordID,
':time' => time());
$sql = 'UPDATE `records`
SET `deleted` = :time
WHERE `recordID` = :recordID';

// actionID 4 = delete
$vars = array(':recordID' => $recordID,
':userID' => $this->login->getUserID(),
':dependencyID' => 0,
':actionType' => 'deleted',
':actionTypeID' => 4,
':time' => time(), );
$res = $this->db->prepared_query('INSERT INTO action_history (recordID, userID, dependencyID, actionType, actionTypeID, time)
VALUES (:recordID, :userID, :dependencyID, :actionType, :actionTypeID, :time)', $vars);
$res = $this->db->prepared_query($sql, $vars);

// delete state
$vars = array(':recordID' => $recordID);
$this->db->prepared_query('DELETE FROM records_workflow_state
WHERE recordID=:recordID', $vars);
// actionID 4 = delete
$vars = array(':recordID' => $recordID,
':userID' => $this->login->getUserID(),
':dependencyID' => 0,
':actionType' => 'deleted',
':actionTypeID' => 4,
':time' => time(),
':comment' => \Leaf\XSSHelpers::xscrub($comment));
$sql = 'INSERT INTO `action_history`
(`recordID`, `userID`, `dependencyID`, `actionType`, `actionTypeID`, `time`, `comment`)
VALUES
(:recordID, :userID, :dependencyID, :actionType, :actionTypeID, :time, :comment)';

// delete tags
$vars = array(':recordID' => $recordID);
$res = $this->db->prepared_query('DELETE FROM tags WHERE recordID=:recordID', $vars);
$res = $this->db->prepared_query($sql, $vars);

return 1;
// delete state
$vars = array(':recordID' => $recordID);
$sql = 'DELETE
FROM `records_workflow_state`
WHERE `recordID` = :recordID';

$this->db->prepared_query($sql, $vars);

// delete tags
$vars = array(':recordID' => $recordID);
$sql = 'DELETE
FROM `tags`
WHERE `recordID` = :recordID';

$res = $this->db->prepared_query($sql, $vars);
}

$return_value = 1;
}

return $return_value;
}

public function restoreRecord($recordID)
Expand Down
5 changes: 2 additions & 3 deletions LEAF_Request_Portal/templates/form.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,8 @@ function cancelRequest() {
dialog_confirm.setSaveHandler(function() {
$.ajax({
type: 'POST',
url: 'ajaxIndex.php?a=cancel',
data: {cancel: <!--{$recordID}-->,
CSRFToken: '<!--{$CSRFToken}-->'},
url: './api/form/<!--{$recordID}-->/cancel',
data: {CSRFToken: '<!--{$CSRFToken}-->'},
success: function(response) {
if(response > 0) {
window.location.href="index.php?a=cancelled_request&cancelled=<!--{$recordID}-->";
Expand Down
8 changes: 6 additions & 2 deletions LEAF_Request_Portal/templates/print_form.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -662,14 +662,17 @@ function doSubmit(recordID) {

function cancelRequest() {
dialog_confirm.setContent(
'<img src="dynicons/?img=process-stop.svg&amp;w=48" alt="Cancel Request" style="float: left; padding-right: 24px" /> Are you sure you want to cancel this request?'
'<img src="dynicons/?img=process-stop.svg&amp;w=48" alt="Cancel Request" style="float: left; padding-right: 24px" /> Are you sure you want to cancel this request?<br /><textarea id="cancel_comment" cols=30 rows=3 placeholder="Enter Comment"></textarea>'
);

dialog_confirm.setSaveHandler(function() {
let comment = $('#cancel_comment').val();

$.ajax({
type: 'POST',
url: 'api/form/<!--{$recordID|strip_tags|escape}-->/cancel',
data: {CSRFToken: '<!--{$CSRFToken}-->'},
data: {CSRFToken: '<!--{$CSRFToken}-->',
comment: comment},
success: function(response) {
if (response == 1) {
window.location.href="index.php?a=cancelled_request&cancelled=<!--{$recordID|strip_tags}-->";
Expand All @@ -682,6 +685,7 @@ function doSubmit(recordID) {
});
});
dialog_confirm.show();
$('#cancel_comment').focus();
}

function changeTitle() {
Expand Down