Skip to content

Commit

Permalink
Add custom event handler to be triggered by some actions affecting ti…
Browse files Browse the repository at this point in the history
…ckets/quotes/invoices/contacts
  • Loading branch information
wrongecho committed Oct 2, 2024
1 parent c352f6e commit 1c404b9
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 17 deletions.
10 changes: 8 additions & 2 deletions cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@

// DOMAINS EXPIRING

if($config_enable_alert_domain_expire == 1){
if ($config_enable_alert_domain_expire == 1) {

$domainAlertArray = [1,7,14,30,90];

Expand Down Expand Up @@ -247,7 +247,7 @@

$tickets_pending_assignment = mysqli_num_rows($sql_tickets_pending_assignment);

if($tickets_pending_assignment > 0){
if ($tickets_pending_assignment > 0) {

mysqli_query($mysqli, "INSERT INTO notifications SET notification_type = 'Pending Tickets', notification = 'There are $tickets_pending_assignment new tickets pending assignment', notification_action = 'tickets.php?status=New'");

Expand Down Expand Up @@ -295,6 +295,8 @@
// Logging
mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Ticket', log_action = 'Create', log_description = 'System created recurring scheduled $frequency ticket - $subject', log_client_id = $client_id, log_user_id = $created_id");

customAction('ticket_create', $id);

// Notifications

// Get client/contact/ticket details
Expand Down Expand Up @@ -415,6 +417,8 @@
//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");

customAction('ticket_close', $ticket_id);

//TODO: Add client notifs if $config_ticket_client_general_notifications is on
}

Expand Down Expand Up @@ -569,6 +573,8 @@

mysqli_query($mysqli, "INSERT INTO notifications SET notification_type = 'Recurring Sent', notification = 'Recurring Invoice $config_invoice_prefix$new_invoice_number for $client_name Sent', notification_action = 'invoice.php?invoice_id=$new_invoice_id', notification_client_id = $client_id, notification_entity_id = $new_invoice_id");

customAction('invoice_create', $new_invoice_id);

//Update recurring dates

mysqli_query($mysqli, "UPDATE recurring SET recurring_last_sent = CURDATE(), recurring_next_date = DATE_ADD(CURDATE(), INTERVAL 1 $recurring_frequency) WHERE recurring_id = $recurring_id");
Expand Down
10 changes: 7 additions & 3 deletions cron_ticket_email_parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ function addTicket($contact_id, $contact_name, $contact_email, $client_id, $date
mysqli_query($mysqli, "INSERT INTO tickets SET ticket_prefix = '$ticket_prefix_esc', ticket_number = $ticket_number, ticket_subject = '$subject_esc', ticket_details = '$message_esc', ticket_priority = 'Low', ticket_status = 1, ticket_created_by = 0, ticket_contact_id = $contact_id, ticket_url_key = '$url_key', ticket_client_id = $client_id_esc");
$id = mysqli_insert_id($mysqli);

echo "Created new ticket.<br>";
mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Ticket', log_action = 'Create', log_description = 'Email parser: Client contact $contact_email_esc created ticket $ticket_prefix_esc$ticket_number ($subject_esc) ($id)', log_client_id = $client_id_esc");

mkdirMissing('uploads/tickets/');
Expand Down Expand Up @@ -172,6 +171,9 @@ function addTicket($contact_id, $contact_name, $contact_email, $client_id, $date

addToMailQueue($mysqli, $data);

// Custom action/notif handler
customAction('ticket_create', $id);

return true;
}

Expand Down Expand Up @@ -308,10 +310,12 @@ function addReply($from_email, $date, $subject, $ticket_number, $message, $attac

mysqli_query($mysqli, "UPDATE tickets SET ticket_status = 2, ticket_resolved_at = NULL 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_esc updated ticket $config_ticket_prefix$ticket_number_esc ($subject)', log_client_id = $client_id");

customAction('ticket_reply_client', $ticket_id);

return true;

} else {
return false;
}
Expand Down Expand Up @@ -469,8 +473,8 @@ function getInboxFolder($client, $inboxNames) {
mysqli_query($mysqli, "INSERT INTO contacts SET contact_name = '".mysqli_real_escape_string($mysqli, $contact_name)."', contact_email = '".mysqli_real_escape_string($mysqli, $contact_email)."', contact_notes = 'Added automatically via email parsing.', contact_password_hash = '$password', contact_client_id = $client_id");
$contact_id = mysqli_insert_id($mysqli);

echo "Created new contact.<br>";
mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Contact', log_action = 'Create', log_description = 'Email parser: created contact ".mysqli_real_escape_string($mysqli, $contact_name)."', log_client_id = $client_id");
customAction('contact_create', $ticket_id);

if (addTicket($contact_id, $contact_name, $contact_email, $client_id, $date, $subject, $message_body, $message->getAttachments(), $original_message_file)) {
$email_processed = true;
Expand Down
8 changes: 8 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1304,10 +1304,18 @@ function enforceUserPermission($module, $check_access_level = 1) {
}
}

// TODO: Probably remove this
function enforceAdminPermission() {
global $session_is_admin;
if (!isset($session_is_admin) || !$session_is_admin) {
exit(WORDING_ROLECHECK_FAILED . "<br>Tell your admin: Your role does not have admin access.");
}
return true;
}

function customAction($trigger, $entity) {
chdir(dirname(__FILE__));
if (file_exists(__DIR__ . "/xcustom/xcustom_action_handler.php")) {
include_once __DIR__ . "/xcustom/xcustom_action_handler.php";
}
}
1 change: 1 addition & 0 deletions guest_pay_invoice_stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@

// Notify
mysqli_query($mysqli, "INSERT INTO notifications SET notification_type = 'Invoice Paid', notification = 'Invoice $invoice_prefix$invoice_number has been paid by $client_name - $ip - $os - $browser', notification_action = 'invoice.php?invoice_id=$invoice_id', notification_client_id = $pi_client_id");
customAction('invoice_pay', $invoice_id);

// Logging
$extended_log_desc = '';
Expand Down
10 changes: 10 additions & 0 deletions guest_post.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

mysqli_query($mysqli, "INSERT INTO history SET history_status = 'Accepted', history_description = 'Client accepted Quote!', history_quote_id = $quote_id");

customAction('quote_accept', $quote_id);

$_SESSION['alert_message'] = "Quote Accepted";

header("Location: " . $_SERVER["HTTP_REFERER"]);
Expand All @@ -45,6 +47,8 @@

mysqli_query($mysqli, "INSERT INTO history SET history_status = 'Declined', history_description = 'Client declined Quote!', history_quote_id = $quote_id");

customAction('quote_decline', $quote_id);

$_SESSION['alert_type'] = "danger";
$_SESSION['alert_message'] = "Quote Declined";

Expand Down Expand Up @@ -73,6 +77,8 @@
//Logging
mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Ticket', log_action = 'Replied', log_description = '$ticket_id reopened by client (guest)', log_ip = '$session_ip', log_user_agent = '$session_user_agent'");

customAction('ticket_update', $ticket_id);

$_SESSION['alert_message'] = "Ticket reopened";
header("Location: " . $_SERVER["HTTP_REFERER"]);

Expand Down Expand Up @@ -100,6 +106,8 @@
//Logging
mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Ticket', log_action = 'Replied', log_description = '$ticket_id closed by client (guest)', log_ip = '$session_ip', log_user_agent = '$session_user_agent'");

customAction('ticket_close', $ticket_id);

$_SESSION['alert_message'] = "Ticket closed";
header("Location: " . $_SERVER["HTTP_REFERER"]);

Expand Down Expand Up @@ -130,6 +138,8 @@
$_SESSION['alert_message'] = "Feedback recorded - thank you";
header("Location: " . $_SERVER["HTTP_REFERER"]);

customAction('ticket_feedback', $ticket_id);

} else {
echo "Invalid!!";
}
Expand Down
31 changes: 28 additions & 3 deletions portal/portal_post.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
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 = 1, ticket_created_by = 0, ticket_contact_id = $contact, ticket_url_key = '$url_key', ticket_client_id = $client_id");
$id = mysqli_insert_id($mysqli);
$ticket_id = mysqli_insert_id($mysqli);

// Notify agent DL of the new ticket, if populated with a valid email
if ($config_ticket_new_ticket_notification_email) {
Expand All @@ -46,7 +46,7 @@
$details = removeEmoji($details);

$email_subject = "ITFlow - New Ticket - $client_name: $subject";
$email_body = "Hello, <br><br>This is a notification that a new ticket has been raised in ITFlow. <br>Client: $client_name<br>Priority: $priority<br>Link: https://$config_base_url/ticket.php?ticket_id=$id <br><br><b>$subject</b><br>$details";
$email_body = "Hello, <br><br>This is a notification that a new ticket has been raised in ITFlow. <br>Client: $client_name<br>Priority: $priority<br>Link: https://$config_base_url/ticket.php?ticket_id=$ticket_id <br><br><b>$subject</b><br>$details";

// Queue Mail
$data = [
Expand All @@ -62,10 +62,13 @@
addToMailQueue($mysqli, $data);
}

// Custom action/notif handler
customAction('ticket_create', $ticket_id);

// Logging
mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Ticket', log_action = 'Create', log_description = 'Client contact $session_contact_name created ticket $subject', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_client_id = $client_id");

header("Location: ticket.php?id=" . $id);
header("Location: ticket.php?id=" . $ticket_id);

}

Expand Down Expand Up @@ -162,6 +165,9 @@
}
}

// Custom action/notif handler
customAction('ticket_reply_client', $ticket_id);

// Redirect back to original page
header("Location: " . $_SERVER["HTTP_REFERER"]);

Expand All @@ -187,6 +193,9 @@
mysqli_query($mysqli, "INSERT INTO notifications SET notification_type = 'Feedback', notification = '$session_contact_name rated ticket ID $ticket_id as bad', notification_client_id = $session_client_id");
}

// Custom action/notif handler
customAction('ticket_feedback', $ticket_id);

// Redirect
header("Location: " . $_SERVER["HTTP_REFERER"]);
} else {
Expand All @@ -212,7 +221,12 @@
//Logging
mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Ticket', log_action = 'Resolved', log_description = '$ticket_id resolved by client', log_ip = '$session_ip', log_user_agent = '$session_user_agent'");

// Custom action/notif handler
customAction('ticket_resolve', $ticket_id);
exit;

header("Location: ticket.php?id=" . $ticket_id);

} else {
// The client does not have access to this ticket - send them home
header("Location: index.php");
Expand All @@ -235,7 +249,11 @@
//Logging
mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Ticket', log_action = 'Replied', log_description = '$ticket_id reopened by client', log_ip = '$session_ip', log_user_agent = '$session_user_agent'");

// Custom action/notif handler
customAction('ticket_update', $ticket_id);

header("Location: ticket.php?id=" . $ticket_id);

} else {
// The client does not have access to this ticket - send them home
header("Location: index.php");
Expand All @@ -258,6 +276,9 @@
//Logging
mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Ticket', log_action = 'Closed', log_description = '$ticket_id closed by client', log_ip = '$session_ip', log_user_agent = '$session_user_agent'");

// Custom action/notif handler
customAction('ticket_close', $ticket_id);

header("Location: ticket.php?id=" . $ticket_id);
} else {
// The client does not have access to this ticket - send them home
Expand Down Expand Up @@ -303,6 +324,8 @@

$_SESSION['alert_message'] = "Contact updated";
header('Location: contacts.php');

customAction('contact_update', $ticket_id);
}

if (isset($_POST['add_contact'])) {
Expand All @@ -317,6 +340,8 @@
// Logging
mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Contact', log_action = 'Create', log_description = 'Client $session_contact_name created contact $contact_name', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_client_id = $session_client_id");

customAction('contact_create', $ticket_id);

$_SESSION['alert_message'] = "Contact created";
header('Location: contacts.php');
}
7 changes: 6 additions & 1 deletion post/user/contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

require_once 'post/user/contact_model.php';


// Set password
if (!empty($_POST['contact_password'])) {
$password_hash = password_hash(trim($_POST['contact_password']), PASSWORD_DEFAULT);
Expand Down Expand Up @@ -58,6 +57,8 @@
//Logging
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Contact', log_action = 'Create', log_description = '$session_name created contact $name', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_client_id = $client_id, log_user_id = $session_user_id, log_entity_id = $contact_id");

customAction('contact_create', $contact_id);

$_SESSION['alert_message'] = "Contact <strong>$name</strong> created";

header("Location: " . $_SERVER["HTTP_REFERER"]);
Expand Down Expand Up @@ -173,6 +174,8 @@
//Logging
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Contact', log_action = 'Modify', log_description = '$session_name modified contact $name', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_client_id = $client_id, log_user_id = $session_user_id, log_entity_id = $contact_id");

customAction('contact_update', $contact_id);

$_SESSION['alert_message'] = "Contact <strong>$name</strong> updated";

header("Location: " . $_SERVER["HTTP_REFERER"]);
Expand Down Expand Up @@ -313,6 +316,8 @@
//Logging
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Contact', log_action = 'Modify', log_description = '$session_name updated $contact_name role', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_client_id = $client_id, log_user_id = $session_user_id, log_entity_id = $contact_id");

customAction('contact_update', $contact_id);

} // End Assign Location Loop

$_SESSION['alert_message'] = "You updated roles for <b>$contact_count</b> contacts";
Expand Down
10 changes: 9 additions & 1 deletion post/user/invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
//Logging
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Invoice', log_action = 'Create', log_description = '$config_invoice_prefix$invoice_number', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id");

customAction('invoice_create', $invoice_id);

$_SESSION['alert_message'] = "Invoice added";

header("Location: invoice.php?invoice_id=$invoice_id");
Expand Down Expand Up @@ -117,6 +119,8 @@
//Logging
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Invoice', log_action = 'Create', log_description = 'Copied Invoice', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id");

customAction('invoice_create', $new_invoice_id);

$_SESSION['alert_message'] = "Invoice copied";

header("Location: invoice.php?invoice_id=$new_invoice_id");
Expand Down Expand Up @@ -748,6 +752,8 @@
//Logging
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Payment', log_action = 'Create', log_description = '$payment_amount', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_client_id = $client_id, log_user_id = $session_user_id, log_entity_id = $payment_id");

customAction('invoice_pay', $invoice_id);

if ($email_receipt == 1) {
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Payment', log_action = 'Email', log_description = 'Payment receipt for invoice $invoice_prefix$invoice_number queued to $contact_email Email ID: $email_id', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_client_id = $client_id, log_user_id = $session_user_id, log_entity_id = $payment_id");
}
Expand Down Expand Up @@ -834,9 +840,9 @@
mysqli_query($mysqli, $add_history_query);

// Add to Email Body Invoice Portion

$email_body_invoices .= "<br>Invoice <a href=\'https://$config_base_url/guest_view_invoice.php?invoice_id=$invoice_id&url_key=$invoice_url_key\'>$invoice_prefix$invoice_number</a> - Outstanding Amount: " . numfmt_format_currency($currency_format, $invoice_balance, $currency_code) . " - Payment Applied: " . numfmt_format_currency($currency_format, $payment_amount, $currency_code) . " - New Balance: " . numfmt_format_currency($currency_format, $remaining_invoice_balance, $currency_code);

customAction('invoice_pay', $invoice_id);

} // End Invoice Loop

Expand Down Expand Up @@ -1209,6 +1215,8 @@
//Logging
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Invoice', log_action = 'Create', log_description = '$session_name forced recurring invoice into an invoice', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_client_id = $client_id, log_user_id = $session_user_id, log_entity_id = $new_invoice_id");

customAction('invoice_create', $new_invoice_id);

$_SESSION['alert_message'] = "Recurring Invoice Forced";

header("Location: " . $_SERVER["HTTP_REFERER"]);
Expand Down
10 changes: 10 additions & 0 deletions post/user/quote.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
//Logging
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Quote', log_action = 'Create', log_description = '$quote_prefix$quote_number', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id");

customAction('quote_create', $quote_id);

$_SESSION['alert_message'] = "Quote added";

header("Location: quote.php?quote_id=$quote_id");
Expand Down Expand Up @@ -86,6 +88,8 @@
//Logging
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Quote', log_action = 'Create', log_description = 'Copied Quote', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_client_id = $client_id, log_user_id = $session_user_id");

customAction('quote_create', $new_quote_id);

$_SESSION['alert_message'] = "Quote copied";

header("Location: quote.php?quote_id=$new_quote_id");
Expand Down Expand Up @@ -145,6 +149,8 @@
//Logging
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Quote', log_action = 'Create', log_description = 'Quote copied to Invoice', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id");

customAction('invoice_create', $new_invoice_id);

$_SESSION['alert_message'] = "Quote copied to Invoice";

header("Location: invoice.php?invoice_id=$new_invoice_id");
Expand Down Expand Up @@ -345,6 +351,8 @@
//Logging
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Quote', log_action = 'Modify', log_description = 'Accepted Quote $quote_id', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id");

customAction('quote_accept', $quote_id);

$_SESSION['alert_message'] = "Quote accepted";

header("Location: " . $_SERVER["HTTP_REFERER"]);
Expand All @@ -361,6 +369,8 @@

mysqli_query($mysqli,"INSERT INTO history SET history_status = 'Cancelled', history_description = 'Quote declined!', history_quote_id = $quote_id");

customAction('quote_decline', $quote_id);

//Logging
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Quote', log_action = 'Modify', log_description = 'Declined Quote $quote_id', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id");

Expand Down
Loading

0 comments on commit 1c404b9

Please sign in to comment.