Skip to content

Commit

Permalink
Merge pull request #1006 from itflow-org/bulk-merge
Browse files Browse the repository at this point in the history
Add bulk merge ticket feature
  • Loading branch information
johnnyq authored Aug 25, 2024
2 parents 442681a + f964d82 commit c0c8107
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 7 deletions.
60 changes: 60 additions & 0 deletions post/ticket.php
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,65 @@
header("Location: " . $_SERVER["HTTP_REFERER"]);
}

if (isset($_POST['bulk_merge_tickets'])) {

// Role check
validateTechRole();

$ticket_count = count($_POST['ticket_ids']); // Get a ticket count
$merge_into_ticket_number = intval($_POST['merge_into_ticket_number']); // Parent ticket *number*
$merge_comment = sanitizeInput($_POST['merge_comment']); // Merge comment

// NEW PARENT ticket details
// Get merge into ticket id (as it may differ from the number)
$sql = mysqli_query($mysqli, "SELECT ticket_id FROM tickets WHERE ticket_number = $merge_into_ticket_number");
if (mysqli_num_rows($sql) == 0) {
$_SESSION['alert_message'] = "Cannot merge into that ticket.";
header("Location: " . $_SERVER["HTTP_REFERER"]);
exit();
}
$merge_row = mysqli_fetch_array($sql);
$merge_into_ticket_id = intval($merge_row['ticket_id']); // Parent ticket ID

// Update & Close the selected tickets
if (!empty($_POST['ticket_ids'])) {
foreach ($_POST['ticket_ids'] as $ticket_id) {
$ticket_id = intval($ticket_id);

if ($ticket_id !== $merge_into_ticket_id) {

$sql = mysqli_query($mysqli, "SELECT * FROM tickets WHERE ticket_id = $ticket_id");
$row = mysqli_fetch_array($sql);

$ticket_prefix = sanitizeInput($row['ticket_prefix']);
$ticket_number = intval($row['ticket_number']);
$ticket_subject = sanitizeInput($row['ticket_subject']);
$ticket_details = mysqli_escape_string($mysqli, $row['ticket_details']);
$current_ticket_priority = sanitizeInput($row['ticket_priority']);
$client_id = intval($row['ticket_client_id']);

// Update current ticket
mysqli_query($mysqli, "INSERT INTO ticket_replies SET ticket_reply = 'Ticket $ticket_prefix$ticket_number bulk merged into $ticket_prefix$merge_into_ticket_number. Comment: $merge_comment', ticket_reply_time_worked = '00:01:00', ticket_reply_type = '$ticket_reply_type', ticket_reply_by = $session_user_id, ticket_reply_ticket_id = $ticket_id");
mysqli_query($mysqli, "UPDATE tickets SET ticket_status = '5', ticket_resolved_at = NOW(), ticket_closed_at = NOW() WHERE ticket_id = $ticket_id") or die(mysqli_error($mysqli));

//Update new parent ticket
mysqli_query($mysqli, "INSERT INTO ticket_replies SET ticket_reply = 'Ticket $ticket_prefix$ticket_number was bulk merged into this ticket with comment: $merge_comment.<br><br><b>$ticket_subject</b><br>$ticket_details', ticket_reply_time_worked = '00:01:00', ticket_reply_type = 'Internal', ticket_reply_by = $session_user_id, ticket_reply_ticket_id = $merge_into_ticket_id");

// Logging
mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Ticket', log_action = 'Merged', log_description = 'Merged ticket $ticket_prefix$ticket_number into $ticket_prefix$merge_into_ticket_number', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id");

}
} // End For Each Ticket ID Loop
}

mysqli_query($mysqli, "UPDATE tickets SET ticket_updated_at = NOW() WHERE ticket_id = $merge_into_ticket_id");

$_SESSION['alert_message'] = "$ticket_count tickets merged into $ticket_prefix$merge_into_ticket_number";

header("Location: " . $_SERVER["HTTP_REFERER"]);

}

if (isset($_POST['bulk_resolve_tickets'])) {

// Role check
Expand Down Expand Up @@ -1335,6 +1394,7 @@

//Update new parent ticket
mysqli_query($mysqli, "INSERT INTO ticket_replies SET ticket_reply = 'Ticket $ticket_prefix$ticket_number was merged into this ticket with comment: $merge_comment.<br><br><b>$ticket_subject</b><br>$ticket_details', ticket_reply_time_worked = '00:01:00', ticket_reply_type = '$ticket_reply_type', ticket_reply_by = $session_user_id, ticket_reply_ticket_id = $merge_into_ticket_id");
mysqli_query($mysqli, "UPDATE tickets SET ticket_updated_at = NOW() WHERE ticket_id = $merge_into_ticket_id");

// Logging
mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Ticket', log_action = 'Merged', log_description = 'Merged ticket $ticket_prefix$ticket_number into $ticket_prefix$merge_into_ticket_number', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id");
Expand Down
69 changes: 69 additions & 0 deletions ticket_bulk_merge_modal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<div class="modal" id="bulkMergeTicketModal" tabindex="-1">
<div class="modal-dialog modal-md">
<div class="modal-content bg-dark">
<div class="modal-header">
<h5 class="modal-title"><i class="fa fa-fw fa-clone mr-2"></i>Bulk merge & close tickets</h5>
<button type="button" class="close text-white" data-dismiss="modal">
<span>&times;</span>
</button>
</div>

<input type="hidden" id="current_ticket_id" name="ticket_id" value="0"> <!-- Can't currently bulk check this -->
<input type="hidden" name="merge_move_replies" value="0"> <!-- Default 0 -->
<div class="modal-body bg-white">

<div class="form-group">
<label>Ticket number to merge tickets into <strong class="text-danger">*</strong></label>
<div class="input-group">
<div class="input-group-prepend">
<?php
// Show the ticket prefix, or just the tag icon
if (empty($ticket_prefix)) {
echo "<span class=\"input-group-text\"><i class=\"fa fa-fw fa-tag\"></i></span>";
} else {
echo "<div class=\"input-group-text\"> $ticket_prefix </div>";
}
?>
</div>
<input type="text" class="form-control" id="merge_into_ticket_number" name="merge_into_ticket_number" placeholder="Ticket number" required onfocusout="merge_into_number_get_details()">
<!-- Calls Javascript function merge_into_number_get_details() after leaving input field -->
</div>
</div>

<div class="form-group">
<label>Reason for merge <strong class="text-danger">*</strong></label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-fw fa-sticky-note"></i></span>
</div>
<input type="text" class="form-control" name="merge_comment" placeholder="Comments" required>
</div>
</div>

<div class="alert alert-dark" role="alert">
<i>Selected tickets will be closed once merging is complete.</i>
</div>


<hr>
<div class="form-group" id="merge_into_details_div" hidden>
<h5 id="merge_into_details_number"></h5>
<p id="merge_into_details_client"></p>
<p id="merge_into_details_subject"></p>
<p id="merge_into_details_priority"></p>
<p id="merge_into_details_status"></p>
</div>

</div>
<div class="modal-footer bg-white">
<button type="submit" id="merge_ticket_btn" name="bulk_merge_tickets" class="btn btn-primary text-bold" disabled><i class="fa fa-check mr-2"></i>Merge</button>
<button type="button" class="btn btn-light" data-dismiss="modal"><i class="fa fa-times mr-2"></i>Cancel</button>
<!-- Merge button starts disabled. Is enabled by the merge_into_number_get_details Javascript function-->
</div>

</div>
</div>
</div>

<!-- Ticket merge JS -->
<script src="js/ticket_merge.js"></script>
3 changes: 1 addition & 2 deletions ticket_merge_modal.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
</div>
<form action="post.php" method="post" autocomplete="off">
<input type="hidden" id="current_ticket_id" name="ticket_id" value="<?php echo $ticket_id; ?>">
<input type="hidden" name="merge_move_replies" value="0"> <!-- Default 0 -->
<div class="modal-body bg-white">

<div class="form-group">
Expand All @@ -26,7 +25,7 @@
?>
</div>
<input type="text" class="form-control" id="merge_into_ticket_number" name="merge_into_ticket_number" placeholder="Ticket number" required onfocusout="merge_into_number_get_details()">
<!-- Calls Javascript function merge_into_number_get_details() after leaving intput field -->
<!-- Calls Javascript function merge_into_number_get_details() after leaving input field -->
</div>
</div>

Expand Down
15 changes: 10 additions & 5 deletions tickets.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,17 @@
<i class="fas fa-fw fa-thermometer-half mr-2"></i>Update Priority
</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#" data-toggle="modal" data-target="#bulkCloseTicketsModal">
<i class="fas fa-fw fa-check mr-2"></i>Resolve
</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#" data-toggle="modal" data-target="#bulkReplyTicketModal">
<i class="fas fa-fw fa-paper-plane mr-2"></i>Bulk Update/Reply
</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#" data-toggle="modal" data-target="#bulkMergeTicketModal">
<i class="fas fa-fw fa-clone mr-2"></i>Merge
</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#" data-toggle="modal" data-target="#bulkCloseTicketsModal">
<i class="fas fa-fw fa-check mr-2"></i>Resolve
</a>
</div>
</div>

Expand Down Expand Up @@ -539,8 +543,9 @@ class="collapse
</div>
<?php require_once "ticket_bulk_assign_modal.php"; ?>
<?php require_once "ticket_bulk_edit_priority_modal.php"; ?>
<?php require_once "ticket_bulk_resolve_modal.php"; ?>
<?php require_once "ticket_bulk_reply_modal.php"; ?>
<?php require_once "ticket_bulk_merge_modal.php"; ?>
<?php require_once "ticket_bulk_resolve_modal.php"; ?>
</form>
<?php require_once "pagination.php"; ?>
</div>
Expand Down

0 comments on commit c0c8107

Please sign in to comment.