Skip to content

Commit

Permalink
Ticket Statuses from DB / use left join instead of a function, hard-c…
Browse files Browse the repository at this point in the history
…ode system statuses
  • Loading branch information
wrongecho committed Mar 29, 2024
1 parent a961b8a commit 7df1d41
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 105 deletions.
10 changes: 5 additions & 5 deletions client_tickets.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
LEFT JOIN assets ON ticket_asset_id = asset_id
LEFT JOIN locations ON ticket_location_id = location_id
LEFT JOIN vendors ON ticket_vendor_id = vendor_id
LEFT JOIN ticket_statuses ON ticket_status = ticket_status_id
WHERE ticket_client_id = $client_id
AND $ticket_status_snippet
AND $ticket_billable_snippet
Expand Down Expand Up @@ -140,8 +141,9 @@
$ticket_number = nullable_htmlentities($row['ticket_number']);
$ticket_subject = nullable_htmlentities($row['ticket_subject']);
$ticket_priority = nullable_htmlentities($row['ticket_priority']);
$ticket_status_id = intval($row['ticket_status']);
$ticket_status_name = sanitizeInput(getTicketStatusName($row['ticket_status']));
$ticket_status_id = intval($row['ticket_status_id']);
$ticket_status_name = nullable_htmlentities($row['ticket_status_name']);
$ticket_status_color = nullable_htmlentities($row['ticket_status_color']);
$ticket_billable = intval($row['ticket_billable']);
$ticket_created_at = nullable_htmlentities($row['ticket_created_at']);
$ticket_created_at_time_ago = timeAgo($row['ticket_created_at']);
Expand All @@ -158,8 +160,6 @@
}
$ticket_closed_at = nullable_htmlentities($row['ticket_closed_at']);

$ticket_status_color = getTicketStatusColor($ticket_status_id);

if ($ticket_priority == "High") {
$ticket_priority_display = "<span class='p-2 badge badge-danger'>$ticket_priority</span>";
} elseif ($ticket_priority == "Medium") {
Expand All @@ -180,7 +180,7 @@
} else {
$ticket_assigned_to_display = nullable_htmlentities($row['user_name']);
}

$project_id = intval($row['ticket_project_id']);

$contact_name = nullable_htmlentities($row['contact_name']);
Expand Down
16 changes: 6 additions & 10 deletions cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@
$config_ticket_autoclose = intval($row['config_ticket_autoclose']);
$config_ticket_autoclose_hours = intval($row['config_ticket_autoclose_hours']);
$config_ticket_new_ticket_notification_email = sanitizeInput($row['config_ticket_new_ticket_notification_email']);
$config_ticket_status_id_new = intval($row['config_ticket_status_id_new']);
$config_ticket_status_id_open = intval($row['config_ticket_status_id_open']);
$config_ticket_status_id_autoclose = intval($row['config_ticket_status_id_autoclose']);
$config_ticket_status_id_closed = intval($row['config_ticket_status_id_closed']);

// Get Config for Telemetry
$config_theme = $row['config_theme'];
Expand Down Expand Up @@ -229,7 +225,7 @@

// Notify of New Tickets
// Get Ticket Pending Assignment
$sql_tickets_pending_assignment = mysqli_query($mysqli,"SELECT ticket_id FROM tickets WHERE ticket_status = '$config_ticket_status_id_new'");
$sql_tickets_pending_assignment = mysqli_query($mysqli,"SELECT ticket_id FROM tickets WHERE ticket_status = 1");

$tickets_pending_assignment = mysqli_num_rows($sql_tickets_pending_assignment);

Expand Down Expand Up @@ -260,9 +256,9 @@
$contact_id = intval($row['scheduled_ticket_contact_id']);
$asset_id = intval($row['scheduled_ticket_asset_id']);

$ticket_status = $config_ticket_status_id_new; // Default
$ticket_status = 1; // Default
if ($assigned_id > 0) {
$ticket_status = $config_ticket_status_id_open; // Set to open if we've auto-assigned an agent
$ticket_status = 2; // Set to open if we've auto-assigned an agent
}

// Assign this new ticket the next ticket number
Expand Down Expand Up @@ -383,7 +379,7 @@
$sql_tickets_to_chase = mysqli_query(
$mysqli,
"SELECT * FROM tickets
WHERE ticket_status = '$config_ticket_status_id_autoclose'
WHERE ticket_status = 4
AND ticket_updated_at < NOW() - INTERVAL $config_ticket_autoclose_hours HOUR"
);

Expand All @@ -397,7 +393,7 @@
$ticket_assigned_to = sanitizeInput($row['ticket_assigned_to']);
$client_id = intval($row['ticket_client_id']);

mysqli_query($mysqli,"UPDATE tickets SET ticket_status = '$config_ticket_status_id_closed', ticket_closed_at = NOW(), ticket_closed_by = $ticket_assigned_to WHERE ticket_id = $ticket_id");
mysqli_query($mysqli,"UPDATE tickets SET ticket_status = 5, ticket_closed_at = NOW(), ticket_closed_by = $ticket_assigned_to WHERE ticket_id = $ticket_id");

//Logging
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Ticket', log_action = 'Closed', log_description = '$ticket_prefix$ticket_number auto closed', log_entity_id = $ticket_id");
Expand All @@ -412,7 +408,7 @@
"SELECT contact_name, contact_email, ticket_id, ticket_prefix, ticket_number, ticket_subject, ticket_status, ticket_client_id FROM tickets
LEFT JOIN clients ON ticket_client_id = client_id
LEFT JOIN contacts ON ticket_contact_id = contact_id
WHERE ticket_status = '$config_ticket_status_id_autoclose'
WHERE ticket_status = 4
AND ticket_updated_at < NOW() - INTERVAL 48 HOUR"
);

Expand Down
10 changes: 5 additions & 5 deletions cron_ticket_email_parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
function addTicket($contact_id, $contact_name, $contact_email, $client_id, $date, $subject, $message, $attachments) {

// Access global variables
global $mysqli,$config_app_name, $company_name, $company_phone, $config_ticket_prefix, $config_ticket_client_general_notifications, $config_ticket_new_ticket_notification_email, $config_ticket_status_id_new, $config_ticket_status_id_closed, $config_base_url, $config_ticket_from_name, $config_ticket_from_email, $config_smtp_host, $config_smtp_port, $config_smtp_encryption, $config_smtp_username, $config_smtp_password, $allowed_extensions;
global $mysqli,$config_app_name, $company_name, $company_phone, $config_ticket_prefix, $config_ticket_client_general_notifications, $config_ticket_new_ticket_notification_email, $config_base_url, $config_ticket_from_name, $config_ticket_from_email, $config_smtp_host, $config_smtp_port, $config_smtp_encryption, $config_smtp_username, $config_smtp_password, $allowed_extensions;

// Get the next Ticket Number and add 1 for the new ticket number
$ticket_number_sql = mysqli_fetch_array(mysqli_query($mysqli, "SELECT config_ticket_next_number FROM settings WHERE company_id = 1"));
Expand All @@ -122,7 +122,7 @@ function addTicket($contact_id, $contact_name, $contact_email, $client_id, $date
$message = nl2br($message);
$message = mysqli_escape_string($mysqli, "<i>Email from: $contact_email at $date:-</i> <br><br>$message");

mysqli_query($mysqli, "INSERT INTO tickets SET ticket_prefix = '$config_ticket_prefix', ticket_number = $ticket_number, ticket_subject = '$subject', ticket_details = '$message', ticket_priority = 'Low', ticket_status = '$config_ticket_status_id_new', ticket_created_by = 0, ticket_contact_id = $contact_id, ticket_client_id = $client_id");
mysqli_query($mysqli, "INSERT INTO tickets SET ticket_prefix = '$config_ticket_prefix', ticket_number = $ticket_number, ticket_subject = '$subject', ticket_details = '$message', ticket_priority = 'Low', ticket_status = 1, ticket_created_by = 0, ticket_contact_id = $contact_id, ticket_client_id = $client_id");
$id = mysqli_insert_id($mysqli);

// Logging
Expand Down Expand Up @@ -213,7 +213,7 @@ function addReply($from_email, $date, $subject, $ticket_number, $message, $attac
// Add email as a comment/reply to an existing ticket

// Access global variables
global $mysqli, $config_app_name, $company_name, $company_phone, $config_ticket_prefix, $config_ticket_status_id_open, $config_ticket_status_id_closed, $config_base_url, $config_ticket_from_name, $config_ticket_from_email, $config_smtp_host, $config_smtp_port, $config_smtp_encryption, $config_smtp_username, $config_smtp_password, $allowed_extensions;
global $mysqli, $config_app_name, $company_name, $company_phone, $config_ticket_prefix, $config_base_url, $config_ticket_from_name, $config_ticket_from_email, $config_smtp_host, $config_smtp_port, $config_smtp_encryption, $config_smtp_username, $config_smtp_password, $allowed_extensions;

// Set default reply type
$ticket_reply_type = 'Client';
Expand Down Expand Up @@ -243,7 +243,7 @@ function addReply($from_email, $date, $subject, $ticket_number, $message, $attac
$client_name = sanitizeInput($row['client_name']);

// Check ticket isn't closed - tickets can't be re-opened
if ($ticket_status == $config_ticket_status_id_closed) {
if ($ticket_status == 5) {
mysqli_query($mysqli, "INSERT INTO notifications SET notification_type = 'Ticket', notification = 'Email parser: $from_email attempted to re-open ticket $config_ticket_prefix$ticket_number (ID $ticket_id) - check inbox manually to see email', notification_action = 'ticket.php?ticket_id=$ticket_id', notification_client_id = $client_id");

$email_subject = "Action required: This ticket is already closed";
Expand Down Expand Up @@ -360,7 +360,7 @@ function addReply($from_email, $date, $subject, $ticket_number, $message, $attac
}

// Update Ticket Last Response Field & set ticket to open as client has replied
mysqli_query($mysqli, "UPDATE tickets SET ticket_status = '$config_ticket_status_id_open' WHERE ticket_id = $ticket_id AND ticket_client_id = $client_id LIMIT 1");
mysqli_query($mysqli, "UPDATE tickets SET ticket_status = 2 WHERE ticket_id = $ticket_id AND ticket_client_id = $client_id LIMIT 1");

echo "Updated existing ticket.<br>";
mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Ticket', log_action = 'Update', log_description = 'Email parser: Client contact $from_email updated ticket $config_ticket_prefix$ticket_number ($subject)', log_client_id = $client_id");
Expand Down
8 changes: 1 addition & 7 deletions database_updates.php
Original file line number Diff line number Diff line change
Expand Up @@ -1758,19 +1758,13 @@
PRIMARY KEY (`ticket_status_id`)
)");

// Pre-seed ticket statuses
// Pre-seed default system/built-in ticket statuses
mysqli_query($mysqli, "INSERT INTO ticket_statuses SET ticket_status_name = 'New', ticket_status_color = 'danger'"); // Default ID for new tickets is 1
mysqli_query($mysqli, "INSERT INTO ticket_statuses SET ticket_status_name = 'Open', ticket_status_color = 'primary'"); // 2
mysqli_query($mysqli, "INSERT INTO ticket_statuses SET ticket_status_name = 'On Hold', ticket_status_color = 'success'"); // 3
mysqli_query($mysqli, "INSERT INTO ticket_statuses SET ticket_status_name = 'Auto Close', ticket_status_color = 'dark'"); // 4
mysqli_query($mysqli, "INSERT INTO ticket_statuses SET ticket_status_name = 'Closed', ticket_status_color = 'dark'"); // 5

// Add default values above to settings
mysqli_query($mysqli, "ALTER TABLE `settings` ADD `config_ticket_status_id_new` int(1) NOT NULL DEFAULT '1' AFTER `config_ticket_new_ticket_notification_email`");
mysqli_query($mysqli, "ALTER TABLE `settings` ADD `config_ticket_status_id_open` int(1) NOT NULL DEFAULT '2' AFTER `config_ticket_status_id_new`");
mysqli_query($mysqli, "ALTER TABLE `settings` ADD `config_ticket_status_id_autoclose` int(1) NOT NULL DEFAULT '4' AFTER `config_ticket_status_id_open`");
mysqli_query($mysqli, "ALTER TABLE `settings` ADD `config_ticket_status_id_closed` int(1) NOT NULL DEFAULT '5' AFTER `config_ticket_status_id_autoclose`");

// Update existing tickets to use new values
mysqli_query($mysqli, "UPDATE tickets SET ticket_status = 1 WHERE ticket_status = 'New'"); // New
mysqli_query($mysqli, "UPDATE tickets SET ticket_status = 2 WHERE ticket_status = 'Open'"); // Open
Expand Down
6 changes: 1 addition & 5 deletions db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1317,10 +1317,6 @@ CREATE TABLE `settings` (
`config_ticket_autoclose` tinyint(1) NOT NULL DEFAULT 0,
`config_ticket_autoclose_hours` int(5) NOT NULL DEFAULT 72,
`config_ticket_new_ticket_notification_email` varchar(200) DEFAULT NULL,
`config_ticket_status_id_new` int(1) NOT NULL DEFAULT 1,
`config_ticket_status_id_open` int(1) NOT NULL DEFAULT 2,
`config_ticket_status_id_autoclose` int(1) NOT NULL DEFAULT 4,
`config_ticket_status_id_closed` int(1) NOT NULL DEFAULT 5,
`config_enable_cron` tinyint(1) NOT NULL DEFAULT 0,
`config_cron_key` varchar(255) DEFAULT NULL,
`config_recurring_auto_send_invoice` tinyint(1) NOT NULL DEFAULT 1,
Expand Down Expand Up @@ -1879,4 +1875,4 @@ CREATE TABLE `vendors` (
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- As of 2024 03 29
-- As of 2024 03 29
4 changes: 0 additions & 4 deletions get_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@
$config_ticket_autoclose = intval($row['config_ticket_autoclose']);
$config_ticket_autoclose_hours = intval($row['config_ticket_autoclose_hours']);
$config_ticket_new_ticket_notification_email = $row['config_ticket_new_ticket_notification_email'];
$config_ticket_status_id_new = intval($row['config_ticket_status_id_new']);
$config_ticket_status_id_open = intval($row['config_ticket_status_id_open']);
$config_ticket_status_id_autoclose = intval($row['config_ticket_status_id_autoclose']);
$config_ticket_status_id_closed = intval($row['config_ticket_status_id_closed']);

// Cron
$config_enable_cron = intval($row['config_enable_cron']);
Expand Down
6 changes: 3 additions & 3 deletions portal/portal_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ function verifyContactTicketAccess($requested_ticket_id, $expected_ticket_state)
{

// Access the global variables
global $mysqli, $session_contact_id, $session_contact_primary, $session_contact_is_technical_contact, $session_client_id, $config_ticket_status_id_closed;
global $mysqli, $session_contact_id, $session_contact_primary, $session_contact_is_technical_contact, $session_client_id;

// Setup
if ($expected_ticket_state == "Closed") {
// Closed tickets
$ticket_state_snippet = "ticket_status = 'Closed' OR ticket_status = $config_ticket_status_id_closed";
$ticket_state_snippet = "ticket_status = 5";
} else {
// Open (working/hold) tickets
$ticket_state_snippet = "ticket_status != 'Closed' or ticket_status != $config_ticket_status_id_closed";
$ticket_state_snippet = "ticket_status != 5";
}

// Verify the contact has access to the provided ticket ID
Expand Down
6 changes: 3 additions & 3 deletions portal/portal_post.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
$new_config_ticket_next_number = $config_ticket_next_number + 1;
mysqli_query($mysqli, "UPDATE settings SET config_ticket_next_number = $new_config_ticket_next_number WHERE company_id = 1");

mysqli_query($mysqli, "INSERT INTO tickets SET ticket_prefix = '$config_ticket_prefix', ticket_number = $ticket_number, ticket_subject = '$subject', ticket_details = '$details', ticket_priority = '$priority', ticket_status = $config_ticket_status_id_new, ticket_created_by = 0, ticket_contact_id = $contact, ticket_client_id = $client_id");
mysqli_query($mysqli, "INSERT INTO tickets SET ticket_prefix = '$config_ticket_prefix', ticket_number = $ticket_number, ticket_subject = '$subject', ticket_details = '$details', ticket_priority = '$priority', ticket_status = 1, ticket_created_by = 0, ticket_contact_id = $contact, ticket_client_id = $client_id");
$id = mysqli_insert_id($mysqli);

// Notify agent DL of the new ticket, if populated with a valid email
Expand Down Expand Up @@ -86,7 +86,7 @@
$ticket_reply_id = mysqli_insert_id($mysqli);

// Update Ticket Last Response Field & set ticket to open as client has replied
mysqli_query($mysqli, "UPDATE tickets SET ticket_status = $config_ticket_status_id_open WHERE ticket_id = $ticket_id AND ticket_client_id = $session_client_id LIMIT 1");
mysqli_query($mysqli, "UPDATE tickets SET ticket_status = 2 WHERE ticket_id = $ticket_id AND ticket_client_id = $session_client_id LIMIT 1");


// Get ticket details & Notify the assigned tech (if any)
Expand Down Expand Up @@ -201,7 +201,7 @@
if (verifyContactTicketAccess($ticket_id, "Open")) {

// Close ticket
mysqli_query($mysqli, "UPDATE tickets SET ticket_status = $config_ticket_status_id_closed, ticket_closed_at = NOW() WHERE ticket_id = $ticket_id AND ticket_client_id = $session_client_id");
mysqli_query($mysqli, "UPDATE tickets SET ticket_status = 5, ticket_closed_at = NOW() WHERE ticket_id = $ticket_id AND ticket_client_id = $session_client_id");

// Add reply
mysqli_query($mysqli, "INSERT INTO ticket_replies SET ticket_reply = 'Ticket closed by $session_contact_name.', ticket_reply_type = 'Client', ticket_reply_by = $session_contact_id, ticket_reply_ticket_id = $ticket_id");
Expand Down
24 changes: 18 additions & 6 deletions portal/ticket.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,21 @@

if ($session_contact_primary == 1 || $session_contact_is_technical_contact) {
// For a primary / technical contact viewing all tickets
$ticket_sql = mysqli_query($mysqli, "SELECT * FROM tickets LEFT JOIN users on ticket_assigned_to = user_id WHERE ticket_id = $ticket_id AND ticket_client_id = $session_client_id");
$ticket_sql = mysqli_query($mysqli,
"SELECT * FROM tickets
LEFT JOIN users on ticket_assigned_to = user_id
LEFT JOIN ticket_statuses ON ticket_status = ticket_status_id
WHERE ticket_id = $ticket_id AND ticket_client_id = $session_client_id"
);

} else {
// For a user viewing their own ticket
$ticket_sql = mysqli_query($mysqli, "SELECT * FROM tickets LEFT JOIN users on ticket_assigned_to = user_id WHERE ticket_id = $ticket_id AND ticket_client_id = $session_client_id AND ticket_contact_id = $session_contact_id");
$ticket_sql = mysqli_query($mysqli,
"SELECT * FROM tickets
LEFT JOIN users on ticket_assigned_to = user_id
LEFT JOIN ticket_statuses ON ticket_status = ticket_status_id
WHERE ticket_id = $ticket_id AND ticket_client_id = $session_client_id AND ticket_contact_id = $session_contact_id"
);
}

$ticket_row = mysqli_fetch_array($ticket_sql);
Expand All @@ -32,11 +43,12 @@

$ticket_prefix = nullable_htmlentities($ticket_row['ticket_prefix']);
$ticket_number = intval($ticket_row['ticket_number']);
$ticket_status = sanitizeInput(getTicketStatusName($ticket_row['ticket_status']));
$ticket_status = nullable_htmlentities($ticket_row['ticket_status_name']);
$ticket_priority = nullable_htmlentities($ticket_row['ticket_priority']);
$ticket_subject = nullable_htmlentities($ticket_row['ticket_subject']);
$ticket_details = $purifier->purify($ticket_row['ticket_details']);
$ticket_assigned_to = nullable_htmlentities($ticket_row['user_name']);
$ticket_closed_at = nullable_htmlentities($ticket_row['ticket_closed_at']);
$ticket_feedback = nullable_htmlentities($ticket_row['ticket_feedback']);

?>
Expand All @@ -56,7 +68,7 @@
<h4 class="mt-1">
Ticket <?php echo $ticket_prefix, $ticket_number ?>
<?php
if ($ticket_status !== $config_ticket_status_id_closed && $ticket_status !== "Closed") { ?>
if (empty($ticket_closed_at)) { ?>
<a href="portal_post.php?close_ticket=<?php echo $ticket_id; ?>" class="btn btn-sm btn-outline-success float-right text-white confirm-link"><i class="fas fa-fw fa-check text-success"></i> Close ticket</a>
<?php } ?>
</h4>
Expand All @@ -70,7 +82,7 @@
<br>
<strong>Priority:</strong> <?php echo $ticket_priority ?>
<br>
<?php if (!empty($ticket_assigned_to) && $ticket_status !== $config_ticket_status_id_closed && $ticket_status !== "Closed") { ?>
<?php if (empty($ticket_closed_at)) { ?>
<strong>Assigned to: </strong> <?php echo $ticket_assigned_to ?>
<?php } ?>
</p>
Expand All @@ -81,7 +93,7 @@

<!-- Either show the reply comments box, ticket smiley feedback, or thanks for feedback -->

<?php if ($ticket_status !== "Closed") { ?>
<?php if (empty($ticket_closed_at)) { ?>

<form action="portal_post.php" enctype="multipart/form-data" method="post">
<input type="hidden" name="ticket_id" value="<?php echo $ticket_id ?>">
Expand Down
Loading

0 comments on commit 7df1d41

Please sign in to comment.