diff --git a/agent/modals/ticket/ticket_link_service.php b/agent/modals/ticket/ticket_link_service.php
new file mode 100644
index 000000000..827db059f
--- /dev/null
+++ b/agent/modals/ticket/ticket_link_service.php
@@ -0,0 +1,76 @@
+ 'A ticket with no client has no services to link.']);
+ exit;
+}
+
+$linked = ticketServices($ticket_id);
+$sql = mysqli_query($mysqli, "SELECT service_id, service_name, service_description, service_category, service_importance FROM services
+ WHERE service_client_id = $client_id AND service_archived_at IS NULL
+ AND service_id NOT IN (SELECT service_id FROM service_tickets WHERE ticket_id = $ticket_id)
+ ORDER BY FIELD(service_importance, 'High', 'Medium', 'Low'), service_name");
+$candidates = [];
+while ($candidate = mysqli_fetch_assoc($sql)) {
+ $candidates[] = $candidate;
+}
+
+ob_start();
+
+?>
+
+
+
+
+$service_name linked with asset
$asset_name ");
-
- redirect();
-
-}
-
-if (isset($_GET['unlink_service_from_asset'])) {
-
- validateCSRFToken();
-
- enforceUserPermission('module_support', 2);
-
- $asset_id = intval($_GET['asset_id']);
- $service_id = intval($_GET['service_id']);
-
- // Get service Name and Client ID for logging
- $sql_service = mysqli_query($mysqli,"SELECT service_name, service_client_id FROM services WHERE service_id = $service_id");
- $row = mysqli_fetch_assoc($sql_service);
- $service_name = escapeSql($row['service_name']);
- $client_id = intval($row['service_client_id']);
-
- enforceClientAccess();
-
- // Get Asset Name for logging
- $asset_name = escapeSql(getFieldById('assets', $asset_id, 'asset_name'));
-
- mysqli_query($mysqli,"DELETE FROM service_assets WHERE asset_id = $asset_id AND service_id = $service_id");
-
- logAudit("Service", "Unlink", "$session_name unlinked asset $asset_name from service $service_name", $client_id, $service_id);
-
- flashAlert("Asset
$asset_name unlinked from service
$service_name ", 'error');
-
- redirect();
-
-}
-
// END LINKING
if (isset($_POST["import_assets_csv"])) {
diff --git a/agent/post/client.php b/agent/post/client.php
index c3961e4c7..4b2dd854c 100644
--- a/agent/post/client.php
+++ b/agent/post/client.php
@@ -1222,6 +1222,17 @@ if (isset($_POST["export_client_pdf"])) {
$sql_contacts = $pack['contacts'] ? mysqli_query($mysqli, "SELECT * FROM contacts WHERE contact_client_id = $client_id AND contact_archived_at IS NULL ORDER BY contact_name ASC") : false;
$sql_locations = $pack['locations'] ? mysqli_query($mysqli, "SELECT * FROM locations WHERE location_client_id = $client_id AND location_archived_at IS NULL ORDER BY location_name ASC") : false;
$sql_vendors = $pack['vendors'] ? mysqli_query($mysqli, "SELECT * FROM vendors WHERE vendor_client_id = $client_id AND vendor_archived_at IS NULL ORDER BY vendor_name ASC") : false;
+ // Services, most important first, with what each runs on and who supports it
+ $pack_services = [];
+ if ($pack['services']) {
+ $sql = mysqli_query($mysqli, "SELECT * FROM services WHERE service_client_id = $client_id AND service_archived_at IS NULL
+ ORDER BY FIELD(service_importance, 'High', 'Medium', 'Low'), service_name ASC");
+ while ($row = mysqli_fetch_assoc($sql)) {
+ $row['runs_on'] = implode(', ', array_column(array_filter(serviceLinked($row['service_id'], 'asset'), function ($a) { return !$a['archived_at']; }), 'name'));
+ $row['supported_by'] = implode(', ', array_column(array_filter(serviceLinked($row['service_id'], 'vendor'), function ($v) { return !$v['archived_at']; }), 'name'));
+ $pack_services[] = $row;
+ }
+ }
$sql_credentials = $pack['credentials'] ? mysqli_query($mysqli, "SELECT * FROM credentials WHERE credential_client_id = $client_id ORDER BY credential_name ASC") : false;
$sql_assets = $pack['assets'] ? mysqli_query($mysqli, "SELECT 1 FROM assets
LEFT JOIN contacts ON asset_contact_id = contact_id
@@ -1520,6 +1531,46 @@ if (isset($_POST["export_client_pdf"])) {
";
}
+ // Services Section
+ if ($pack['services'] == 1 && $pack_services) {
+ $pdf->Bookmark("Services", 0, 0, "", "B", array(0,0,0));
+ $html .= "
+
Services
+
+
+
+ Service
+ Importance
+ Runs On
+ Vendors
+ Backup and Recovery
+ Next Review
+
+
+ ";
+ foreach ($pack_services as $row) {
+ $service_name = escapeHtml($row['service_name']);
+ $service_detail = escapeHtml(implode(' - ', array_filter([$row['service_category'], $row['service_description']])));
+ $service_importance = escapeHtml($row['service_importance']);
+ $service_runs_on = escapeHtml($row['runs_on']);
+ $service_vendors = escapeHtml($row['supported_by']);
+ $service_backup = nl2br(escapeHtml($row['service_backup']));
+ $service_review = escapeHtml((string) $row['service_review_due']);
+ $html .= "
+
+ $service_name $service_detail
+ $service_importance
+ $service_runs_on
+ $service_vendors
+ $service_backup
+ $service_review
+ ";
+ }
+ $html .= "
+
+
";
+ }
+
// Credentials Section
if ($pack['credentials'] == 1 && mysqli_num_rows($sql_credentials) > 0) {
$pdf->Bookmark("Credentials", 0, 0, "", "B", array(0,0,0));
diff --git a/agent/post/contact.php b/agent/post/contact.php
index 52a163ca0..53e1b4d0c 100644
--- a/agent/post/contact.php
+++ b/agent/post/contact.php
@@ -1070,66 +1070,6 @@ if (isset($_GET['unlink_credential_from_contact'])) {
}
-if (isset($_POST['link_service_to_contact'])) {
-
- validateCSRFToken();
-
- enforceUserPermission('module_client', 2);
-
- $service_id = intval($_POST['service_id']);
- $contact_id = intval($_POST['contact_id']);
-
- // Get service Name and Client ID for logging
- $sql_service = mysqli_query($mysqli,"SELECT service_name, service_client_id FROM services WHERE service_id = $service_id");
- $row = mysqli_fetch_assoc($sql_service);
- $service_name = escapeSql($row['service_name']);
- $client_id = intval($row['service_client_id']);
-
- enforceClientAccess();
-
- // Get Contact Name for logging
- $contact_name = escapeSql(getFieldById('contacts', $contact_id, 'contact_name'));
-
- mysqli_query($mysqli,"INSERT INTO service_contacts SET contact_id = $contact_id, service_id = $service_id");
-
- logAudit("Service", "Link", "$session_name linked contact $contact_name to service $service_name", $client_id, $service_id);
-
- flashAlert("service
$service_name linked with contact
$contact_name ");
-
- redirect();
-
-}
-
-if (isset($_GET['unlink_service_from_contact'])) {
-
- validateCSRFToken();
-
- enforceUserPermission('module_client', 2);
-
- $contact_id = intval($_GET['contact_id']);
- $service_id = intval($_GET['service_id']);
-
- // Get service Name and Client ID for logging
- $sql_service = mysqli_query($mysqli,"SELECT service_name, service_client_id FROM services WHERE service_id = $service_id");
- $row = mysqli_fetch_assoc($sql_service);
- $service_name = escapeSql($row['service_name']);
- $client_id = intval($row['service_client_id']);
-
- enforceClientAccess();
-
- // Get Contact Name for logging
- $contact_name = escapeSql(getFieldById('contacts', $contact_id, 'contact_name'));
-
- mysqli_query($mysqli,"DELETE FROM service_contacts WHERE contact_id = $contact_id AND service_id = $service_id");
-
- logAudit("service", "Unlink", "$session_name unlinked contact $contact_name from service $service_name", $client_id, $service_id);
-
- flashAlert("Contact
$contact_name unlinked from service
$service_name ", 'error');
-
- redirect();
-
-}
-
if (isExportRequest('export_contacts')) {
validateCSRFToken();
diff --git a/agent/post/document.php b/agent/post/document.php
index 5c309edf6..864f97062 100644
--- a/agent/post/document.php
+++ b/agent/post/document.php
@@ -61,6 +61,8 @@ if (isset($_POST['add_document'])) {
$software_id = $software && intval($software['client_id']) === $client_id ? intval($software['id']) : 0;
$location_id = assetClientOwnedId('location', $_POST['location'] ?? 0, $client_id);
$vendor_id = assetClientOwnedId('vendor', $_POST['vendor'] ?? 0, $client_id);
+ $service = linkRecord('service', $_POST['service'] ?? 0);
+ $service_id = $service && intval($service['client_id']) === $client_id && !$service['archived_at'] ? intval($service['id']) : 0;
$client_visible = intval($_POST['client_visible'] ?? 0) ? 1 : 0;
if ($name === '') {
@@ -97,13 +99,17 @@ if (isset($_POST['add_document'])) {
if ($vendor_id) {
mysqli_query($mysqli, "INSERT IGNORE INTO vendor_documents SET vendor_id = $vendor_id, document_id = $document_id");
}
+ if ($service_id) {
+ mysqli_query($mysqli, "INSERT IGNORE INTO service_documents SET service_id = $service_id, document_id = $document_id");
+ logAudit("Service", "Link", "$session_name linked new document $name to service " . escapeSql($service['name']), $client_id, $service_id);
+ }
logAudit("Document", "Create", "$session_name created document $name", $client_id, $document_id);
flashAlert("Document
" . trim($_POST['name']) . " created");
- // From an asset, a contact, a license, a location or a vendor, back to it; otherwise to the new document
- redirect(($contact_id || $asset_id || $software_id || $location_id || $vendor_id) ? null : "document.php?client_id=$client_id&document_id=$document_id");
+ // From an asset, a contact, a license, a location, a vendor or a service, back to it; otherwise to the new document
+ redirect(($contact_id || $asset_id || $software_id || $location_id || $vendor_id || $service_id) ? null : "document.php?client_id=$client_id&document_id=$document_id");
}
diff --git a/agent/post/link.php b/agent/post/link.php
index 726806c0d..4d2be607a 100644
--- a/agent/post/link.php
+++ b/agent/post/link.php
@@ -2,8 +2,8 @@
/*
* ITFlow - links between documents, files and what they are about, the
- * devices, users, credentials and files on a license, and the documents and
- * files about a location.
+ * devices, users, credentials and files on a license, the documents and
+ * files about a location, and what a service depends on.
*
* One handler for every pair LINK_RELATIONS knows (functions/links.php):
* link_items links any number of records at once, unlink_item drops one.
@@ -83,6 +83,8 @@ $legacy_links = [
'contact_to_file' => ['file', 'file_id', 'contact', 'contact_id'],
'software_to_asset' => ['asset', 'asset_id', 'software', 'software_id'],
'software_to_contact' => ['contact', 'contact_id', 'software', 'software_id'],
+ 'service_to_asset' => ['asset', 'asset_id', 'service', 'service_id'],
+ 'service_to_contact' => ['contact', 'contact_id', 'service', 'service_id'],
];
foreach ($legacy_links as $legacy => [$from, $from_field, $to, $to_field]) {
if (isset($_POST["link_$legacy"])) {
diff --git a/agent/post/service.php b/agent/post/service.php
index 98df6de2c..53ce6f39b 100644
--- a/agent/post/service.php
+++ b/agent/post/service.php
@@ -1,7 +1,11 @@
$service_name created");
+ // What it depends on, from the form's pickers - the client's own, unarchived records only (linkAdd checks)
+ $linked = 0;
+ foreach (SERVICE_LINK_TYPES as $type) {
+ $ids = $_POST[LINK_ENTITIES[$type]['table']] ?? [];
+ if ($ids && linkCanEdit('service', $type)) {
+ $linked += count(linkAdd('service', $service_id, $type, (array) $ids));
+ }
+ }
- redirect();
+ flashAlert("Service
" . escapeHtml(trim($_POST['name'])) . " created" . ($linked ? " with
$linked linked" : ' - link what it runs on and depends on below'));
+
+ redirect(serviceUrl($service_id, $client_id));
}
@@ -93,88 +55,110 @@ if (isset($_POST['edit_service'])) {
validateCSRFToken();
- enforceUserPermission('module_support', 2);
+ $row = serviceForRequest($_POST['service_id'] ?? 0, 2);
+ $service_id = intval($row['service_id']);
- $service_id = intval($_POST['service_id']);
- $service_name = escapeSql($_POST['name']);
- $service_description = escapeSql($_POST['description']);
- $service_category = escapeSql($_POST['category']); //TODO: Needs integration with company categories
- $service_importance = escapeSql($_POST['importance']);
- $service_backup = escapeSql($_POST['backup']);
- $service_notes = escapeSql($_POST['note']);
-
- $client_id = intval(getFieldById('services', $service_id, 'service_client_id'));
-
- enforceClientAccess();
-
- // Update main service details
- mysqli_query($mysqli, "UPDATE services SET service_name = '$service_name', service_description = '$service_description', service_category = '$service_category', service_importance = '$service_importance', service_backup = '$service_backup', service_notes = '$service_notes' WHERE service_id = $service_id");
-
- // Unlink existing relations/assets
- mysqli_query($mysqli, "DELETE FROM service_contacts WHERE service_id = $service_id");
- mysqli_query($mysqli, "DELETE FROM service_vendors WHERE service_id = $service_id");
- mysqli_query($mysqli, "DELETE FROM service_documents WHERE service_id = $service_id");
- mysqli_query($mysqli, "DELETE FROM service_assets WHERE service_id = $service_id");
- mysqli_query($mysqli, "DELETE FROM service_credentials WHERE service_id = $service_id");
- mysqli_query($mysqli, "DELETE FROM service_domains WHERE service_id = $service_id");
- mysqli_query($mysqli, "DELETE FROM service_certificates WHERE service_id = $service_id");
-
- // Relink
- if (isset($_POST['contacts'])) {
- foreach($_POST['contacts'] as $contact_id) {
- $contact_id = intval($contact_id);
- mysqli_query($mysqli, "INSERT INTO service_contacts SET service_id = $service_id, contact_id = $contact_id");
- }
+ $error = '';
+ $set = serviceFormSet($_POST, $error, $row);
+ if ($set === '') {
+ flashAlert($error, 'error');
+ redirect();
}
- if (isset($_POST['vendors'])) {
- foreach($_POST['vendors'] as $vendor_id) {
- $vendor_id = intval($vendor_id);
- mysqli_query($mysqli, "INSERT INTO service_vendors SET service_id = $service_id, vendor_id = $vendor_id");
- }
- }
-
- if (isset($_POST['documents'])) {
- foreach($_POST['documents'] as $document_id) {
- $document_id = intval($document_id);
- mysqli_query($mysqli, "INSERT INTO service_documents SET service_id = $service_id, document_id = $document_id");
- }
- }
-
- if (isset($_POST['assets'])) {
- foreach($_POST['assets'] as $asset_id) {
- $asset_id = intval($asset_id);
- mysqli_query($mysqli, "INSERT INTO service_assets SET service_id = $service_id, asset_id = $asset_id");
- }
- }
-
- if (isset($_POST['credentials'])) {
- foreach($_POST['credentials'] as $credential_id) {
- $credential_id = intval($credential_id);
- // Only the client's own credentials
- mysqli_query($mysqli, "INSERT INTO service_credentials (service_id, credential_id) SELECT $service_id, credential_id FROM credentials WHERE credential_id = $credential_id AND credential_client_id = $client_id");
- }
- }
-
- if (isset($_POST['domains'])) {
- foreach($_POST['domains'] as $domain_id) {
- $domain_id = intval($domain_id);
- // Only the client's own domains
- mysqli_query($mysqli, "INSERT INTO service_domains (service_id, domain_id) SELECT $service_id, domain_id FROM domains WHERE domain_id = $domain_id AND domain_client_id = $client_id");
- }
- }
-
- if (isset($_POST['certificates'])) {
- foreach($_POST['certificates'] as $cert_id) {
- $cert_id = intval($cert_id);
- // Only the client's own certificates
- mysqli_query($mysqli, "INSERT INTO service_certificates (service_id, certificate_id) SELECT $service_id, certificate_id FROM certificates WHERE certificate_id = $cert_id AND certificate_client_id = $client_id");
- }
- }
+ mysqli_query($mysqli, "UPDATE services SET $set WHERE service_id = $service_id");
+ $service_name = escapeSql(trim($_POST['name']));
logAudit("Service", "Edit", "$session_name edited service $service_name", $client_id, $service_id);
- flashAlert("Service
$service_name edited");
+ flashAlert("Service
" . escapeHtml(trim($_POST['name'])) . " saved");
+
+ redirect();
+
+}
+
+// Notes save from the service page when you click away
+if (isset($_POST['service_set_notes'])) {
+
+ validateCSRFToken();
+
+ $row = serviceForRequest($_POST['service_id'] ?? 0, 2);
+ $notes = escapeSql((string) ($_POST['notes'] ?? ''));
+ mysqli_query($mysqli, "UPDATE services SET service_notes = '$notes' WHERE service_id = " . intval($row['service_id']));
+
+ logAudit("Service", "Edit", "$session_name updated the notes of service " . escapeSql($row['service_name']), $client_id, intval($row['service_id']));
+
+ if (!empty($_POST['ajax'])) {
+ header('Content-Type: application/json');
+ echo json_encode(['ok' => true]);
+ exit;
+ }
+
+ flashAlert("Notes saved");
+
+ redirect();
+
+}
+
+if (isset($_POST['review_service'])) {
+
+ validateCSRFToken();
+
+ $row = serviceForRequest($_POST['service_id'] ?? 0, 2);
+
+ // A new schedule can be set at the same time
+ if (isset($_POST['review_interval']) && isset(SERVICE_REVIEW_INTERVALS[intval($_POST['review_interval'])])) {
+ $row['service_review_interval'] = intval($_POST['review_interval']);
+ mysqli_query($mysqli, "UPDATE services SET service_review_interval = {$row['service_review_interval']} WHERE service_id = " . intval($row['service_id']));
+ }
+
+ $next = serviceMarkReviewed($row, trim((string) ($_POST['review_due'] ?? '')), (string) ($_POST['note'] ?? ''));
+
+ flashAlert("Service
" . escapeHtml($row['service_name']) . " marked reviewed" . ($next ? " - next review " . date('M j, Y', strtotime($next)) : ''));
+
+ redirect();
+
+}
+
+if (isset($_GET['favorite_service']) || isset($_GET['unfavorite_service'])) {
+
+ validateCSRFToken();
+
+ $favorite = isset($_GET['favorite_service']) ? 1 : 0;
+ $row = serviceForRequest($favorite ? $_GET['favorite_service'] : $_GET['unfavorite_service'], 2);
+
+ mysqli_query($mysqli, "UPDATE services SET service_favorite = $favorite WHERE service_id = " . intval($row['service_id']));
+
+ logAudit("Service", "Edit", "$session_name " . ($favorite ? 'starred' : 'unstarred') . " service " . escapeSql($row['service_name']), $client_id, intval($row['service_id']));
+
+ flashAlert("Service
" . escapeHtml($row['service_name']) . " " . ($favorite ? 'starred' : 'unstarred'));
+
+ redirect();
+
+}
+
+if (isset($_GET['archive_service'])) {
+
+ validateCSRFToken();
+
+ $row = serviceForRequest($_GET['archive_service'], 2);
+
+ serviceArchive($row);
+
+ flashAlert("Service
" . escapeHtml($row['service_name']) . " archived", 'error');
+
+ redirect();
+
+}
+
+if (isset($_GET['restore_service'])) {
+
+ validateCSRFToken();
+
+ $row = serviceForRequest($_GET['restore_service'], 2);
+
+ serviceRestore($row);
+
+ flashAlert("Service
" . escapeHtml($row['service_name']) . " restored");
redirect();
@@ -184,24 +168,350 @@ if (isset($_GET['delete_service'])) {
validateCSRFToken();
- enforceUserPermission('module_support', 3);
+ $row = serviceForRequest($_GET['delete_service'], 3);
- $service_id = intval($_GET['delete_service']);
+ serviceDelete($row);
- // Get Service Details
- $sql = mysqli_query($mysqli,"SELECT service_name, service_client_id FROM services WHERE service_id = $service_id");
- $row = mysqli_fetch_assoc($sql);
- $service_name = escapeSql($row['service_name']);
- $client_id = intval($row['service_client_id']);
+ flashAlert("Service
" . escapeHtml($row['service_name']) . " deleted", 'error');
- enforceClientAccess();
+ // Not back to the page of a service that is gone
+ $referer = $_SERVER['HTTP_REFERER'] ?? '';
+ redirect(strpos($referer, 'service.php') !== false ? 'services.php?client_id=' . intval($row['service_client_id']) : null);
- // Delete service
- mysqli_query($mysqli, "DELETE FROM services WHERE service_id = $service_id");
+}
- logAudit("Service", "Delete", "$session_name deleted service $service_name", $client_id);
+// Tickets raised about a service - from its page, or from the ticket
+if (isset($_POST['link_service_tickets'])) {
- flashAlert("Service
$service_name deleted", 'error');
+ validateCSRFToken();
+
+ $row = serviceForRequest($_POST['service_id'] ?? 0, 2);
+
+ $added = serviceLinkTickets($row['service_id'], $client_id, (array) ($_POST['ticket_ids'] ?? []));
+ if ($added) {
+ logAudit("Service", "Link", "$session_name linked $added ticket(s) to service " . escapeSql($row['service_name']), $client_id, intval($row['service_id']));
+ flashAlert("Linked
$added ticket" . ($added === 1 ? '' : 's') . " to
" . escapeHtml($row['service_name']) . " ");
+ } else {
+ flashAlert("Nothing new to link to
" . escapeHtml($row['service_name']) . " .", 'warning');
+ }
+
+ redirect();
+
+}
+
+if (isset($_POST['link_ticket_services'])) {
+
+ validateCSRFToken();
+
+ enforceUserPermission('module_support', 2);
+
+ $ticket_id = intval($_POST['ticket_id'] ?? 0);
+ $ticket = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT ticket_id, ticket_prefix, ticket_number, ticket_client_id FROM tickets WHERE ticket_id = $ticket_id"));
+ if (!$ticket || !intval($ticket['ticket_client_id'])) {
+ flashAlert('That ticket no longer exists.', 'error');
+ redirect();
+ }
+ $client_id = intval($ticket['ticket_client_id']);
+ enforceClientAccess();
+
+ $names = [];
+ foreach (array_unique(array_map('intval', (array) ($_POST['service_ids'] ?? []))) as $service_id) {
+ $row = serviceRow($service_id);
+ if (!$row || intval($row['service_client_id']) !== $client_id || $row['service_archived_at']) {
+ continue;
+ }
+ if (serviceLinkTickets($service_id, $client_id, [$ticket_id])) {
+ $names[] = $row['service_name'];
+ logAudit("Service", "Link", "$session_name linked ticket " . escapeSql($ticket['ticket_prefix'] . $ticket['ticket_number']) . " to service " . escapeSql($row['service_name']), $client_id, $service_id);
+ }
+ }
+
+ if ($names) {
+ flashAlert("Ticket linked to
" . escapeHtml(implode(', ', $names)) . " ");
+ } else {
+ flashAlert('Nothing new to link.', 'warning');
+ }
+
+ redirect();
+
+}
+
+if (isset($_GET['unlink_service_ticket'])) {
+
+ validateCSRFToken();
+
+ $row = serviceForRequest($_GET['service_id'] ?? 0, 2);
+ $ticket_id = intval($_GET['unlink_service_ticket']);
+
+ mysqli_query($mysqli, "DELETE FROM service_tickets WHERE service_id = " . intval($row['service_id']) . " AND ticket_id = $ticket_id");
+ if (mysqli_affected_rows($mysqli) > 0) {
+ $ticket = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT ticket_prefix, ticket_number FROM tickets WHERE ticket_id = $ticket_id"));
+ $reference = $ticket ? $ticket['ticket_prefix'] . $ticket['ticket_number'] : "#$ticket_id";
+ logAudit("Service", "Unlink", "$session_name unlinked ticket " . escapeSql($reference) . " from service " . escapeSql($row['service_name']), $client_id, intval($row['service_id']));
+ flashAlert("Ticket
" . escapeHtml($reference) . " unlinked from
" . escapeHtml($row['service_name']) . " ", 'error');
+ }
+
+ redirect();
+
+}
+
+/*
+ * Bulk actions from the services list
+ */
+
+if (isset($_POST['bulk_edit_services'])) {
+
+ validateCSRFToken();
+
+ $rows = servicesForBulk(2);
+
+ // Each field only when one was picked - blank leaves it as it is
+ $sets = [];
+ $changes = [];
+ if (in_array($_POST['importance'] ?? '', SERVICE_IMPORTANCE, true)) {
+ $sets[] = "service_importance = '{$_POST['importance']}'";
+ $changes[] = 'importance ' . $_POST['importance'];
+ }
+ $category = trim((string) ($_POST['category'] ?? ''));
+ if ($category !== '') {
+ $category = mb_substr($category, 0, 50);
+ $sets[] = "service_category = '" . escapeSql($category) . "'";
+ $changes[] = 'category ' . $category;
+ }
+ $interval = (string) ($_POST['review_interval'] ?? '');
+ $set_interval = $interval !== '' && isset(SERVICE_REVIEW_INTERVALS[intval($interval)]);
+
+ if (!$sets && !$set_interval) {
+ flashAlert('Nothing picked to change.', 'warning');
+ redirect();
+ }
+
+ $count = 0;
+ foreach ($rows as $service_id => $row) {
+ $row_sets = $sets;
+ if ($set_interval) {
+ $months = intval($interval);
+ $row_sets[] = "service_review_interval = $months";
+ // A schedule needs a next date: from the last review, never in the past - and none without a schedule
+ if ($months) {
+ $due = max(serviceNextReview($months, $row['service_reviewed_at'] ?: null), date('Y-m-d'));
+ $row_sets[] = "service_review_due = '$due'";
+ } else {
+ $row_sets[] = "service_review_due = NULL";
+ }
+ }
+ mysqli_query($mysqli, "UPDATE services SET " . implode(', ', $row_sets) . " WHERE service_id = $service_id");
+ $what = $changes;
+ if ($set_interval) {
+ $what[] = 'reviews ' . strtolower(SERVICE_REVIEW_INTERVALS[intval($interval)]);
+ }
+ logAudit("Service", "Edit", "$session_name set " . escapeSql(implode(', ', $what)) . " on service " . escapeSql($row['service_name']), intval($row['service_client_id']), $service_id);
+ $count++;
+ }
+
+ flashAlert("Updated
$count service" . ($count === 1 ? '' : 's'));
+
+ redirect();
+
+}
+
+if (isset($_POST['bulk_review_services'])) {
+
+ validateCSRFToken();
+
+ $count = 0;
+ foreach (servicesForBulk(2) as $row) {
+ if ($row['service_archived_at']) {
+ continue;
+ }
+ serviceMarkReviewed($row);
+ $count++;
+ }
+
+ flashAlert("Marked
$count service" . ($count === 1 ? '' : 's') . " reviewed");
+
+ redirect();
+
+}
+
+if (isset($_POST['bulk_favorite_services']) || isset($_POST['bulk_unfavorite_services'])) {
+
+ validateCSRFToken();
+
+ $favorite = isset($_POST['bulk_favorite_services']) ? 1 : 0;
+ $count = 0;
+ foreach (servicesForBulk(2) as $service_id => $row) {
+ mysqli_query($mysqli, "UPDATE services SET service_favorite = $favorite WHERE service_id = $service_id");
+ $count++;
+ }
+
+ flashAlert(($favorite ? 'Starred' : 'Unstarred') . "
$count service" . ($count === 1 ? '' : 's'));
+
+ redirect();
+
+}
+
+if (isset($_POST['bulk_archive_services'])) {
+
+ validateCSRFToken();
+
+ $count = 0;
+ foreach (servicesForBulk(2) as $row) {
+ if (!$row['service_archived_at']) {
+ serviceArchive($row);
+ $count++;
+ }
+ }
+
+ flashAlert("Archived
$count service" . ($count === 1 ? '' : 's'), 'error');
+
+ redirect();
+
+}
+
+if (isset($_POST['bulk_restore_services'])) {
+
+ validateCSRFToken();
+
+ $count = 0;
+ foreach (servicesForBulk(2) as $row) {
+ if ($row['service_archived_at']) {
+ serviceRestore($row);
+ $count++;
+ }
+ }
+
+ flashAlert("Restored
$count service" . ($count === 1 ? '' : 's'));
+
+ redirect();
+
+}
+
+if (isset($_POST['bulk_delete_services'])) {
+
+ validateCSRFToken();
+
+ $count = 0;
+ foreach (servicesForBulk(3) as $row) {
+ serviceDelete($row);
+ $count++;
+ }
+
+ flashAlert("Deleted
$count service" . ($count === 1 ? '' : 's'), 'error');
+
+ redirect();
+
+}
+
+if (isExportRequest('export_services')) {
+
+ validateCSRFToken();
+
+ enforceUserPermission('module_support');
+
+ $format = resolveExportFormat($_POST['export_services']);
+ $filter_summary = [];
+
+ $client_id = intval($_POST['client_id'] ?? 0);
+ $where = [clientScopeSql('service_client_id')];
+ if ($client_id) {
+ enforceClientAccess();
+ $where[] = "AND service_client_id = $client_id";
+ $file_name_prepend = getFieldById('clients', $client_id, 'client_name') . '-';
+ $filter_summary['Client'] = getFieldById('clients', $client_id, 'client_name');
+ } else {
+ $file_name_prepend = "$session_company_name-";
+ $where[] = 'AND clients.client_archived_at IS NULL';
+ if (intval($_POST['client'] ?? 0)) {
+ $where[] = 'AND service_client_id = ' . intval($_POST['client']);
+ $filter_summary['Client'] = getFieldById('clients', intval($_POST['client']), 'client_name');
+ }
+ }
+
+ $views = serviceViews();
+ $show = (string) ($_POST['show'] ?? '');
+ if (!isset($views[$show])) {
+ $show = '';
+ }
+ if ($views[$show]['where'] !== '') {
+ $where[] = 'AND ' . $views[$show]['where'];
+ }
+ $filter_summary['Showing'] = $views[$show]['label'];
+
+ if (in_array($_POST['importance'] ?? '', SERVICE_IMPORTANCE, true)) {
+ $where[] = "AND service_importance = '{$_POST['importance']}'";
+ $filter_summary['Importance'] = $_POST['importance'];
+ }
+ $category = trim((string) ($_POST['category'] ?? ''));
+ if ($category !== '') {
+ $where[] = "AND service_category = '" . escapeSql($category) . "'";
+ $filter_summary['Category'] = $category;
+ }
+
+ $q = escapeSql($_POST['q'] ?? '');
+ if ($q !== '') {
+ $filter_summary['Search'] = $_POST['q'];
+ }
+
+ $sql = mysqli_query($mysqli, "SELECT services.*, client_name FROM services
+ LEFT JOIN clients ON client_id = service_client_id
+ WHERE (service_name LIKE '%$q%' OR service_description LIKE '%$q%' OR service_category LIKE '%$q%' OR client_name LIKE '%$q%')
+ " . implode(' ', $where) . "
+ ORDER BY client_name ASC, FIELD(service_importance, 'High', 'Medium', 'Low'), service_name ASC");
+
+ $rows = [];
+ while ($row = mysqli_fetch_assoc($sql)) {
+ $rows[intval($row['service_id'])] = $row;
+ }
+ $num_rows = count($rows);
+
+ if ($num_rows > 0) {
+
+ guardExportPdfRowCount($format, $num_rows);
+
+ $ids = array_keys($rows);
+ $in = implode(',', $ids);
+ $counts = serviceCounts($ids);
+ $issues = serviceIssues($ids);
+
+ // The names of what each one depends on, for the columns that list them
+ $names = [];
+ foreach (['asset', 'vendor', 'contact', 'domain'] as $type) {
+ $relation = linkRelation('service', $type);
+ $entity = LINK_ENTITIES[$type];
+ $sql = mysqli_query($mysqli, "SELECT l.service_id, t.{$entity['name']} FROM {$relation['table']} l
+ JOIN {$entity['table']} t ON t.{$entity['id']} = l.{$entity['id']} AND t.{$entity['archived']} IS NULL
+ WHERE l.service_id IN ($in) ORDER BY t.{$entity['name']}");
+ while ($name_row = mysqli_fetch_row($sql)) {
+ $names[intval($name_row[0])][$type][] = $name_row[1];
+ }
+ }
+
+ $export = beginExport('services', $format, $file_name_prepend . 'Services', 'Services', summarizeExportFilters($filter_summary));
+
+ foreach ($rows as $service_id => $row) {
+ $review = serviceReview($row);
+ $row['service_assets'] = implode(', ', $names[$service_id]['asset'] ?? []);
+ $row['service_vendors'] = implode(', ', $names[$service_id]['vendor'] ?? []);
+ $row['service_contacts'] = implode(', ', $names[$service_id]['contact'] ?? []);
+ $row['service_domains'] = implode(', ', $names[$service_id]['domain'] ?? []);
+ $row['service_attention'] = implode('; ', array_column($issues[$service_id], 'text'));
+ $row['service_review'] = $review['date'] ? $review['date'] . ($review['state'] === 'overdue' ? ' (overdue)' : '') : '';
+ $row['service_reviewed'] = $row['service_reviewed_at'] ? substr($row['service_reviewed_at'], 0, 10) : '';
+ $row['service_review_every'] = intval($row['service_review_interval']) ? (SERVICE_REVIEW_INTERVALS[intval($row['service_review_interval'])] ?? '') : '';
+ $row['open_tickets'] = $counts[$service_id]['open_tickets'];
+ $row['service_state'] = $row['service_archived_at'] ? 'Archived' : 'Active';
+ addExportRow($export, $row);
+ }
+
+ finishExport($export);
+
+ logAudit("Service", "Export", "$session_name exported $num_rows service(s) to a " . strtoupper($format) . " file", $client_id);
+
+ exit;
+ }
+
+ flashAlert('No services match - nothing to export.', 'error');
redirect();
diff --git a/agent/post/ticket.php b/agent/post/ticket.php
index ff23fd131..b20a32efa 100644
--- a/agent/post/ticket.php
+++ b/agent/post/ticket.php
@@ -163,6 +163,15 @@ if (isset($_POST['add_ticket'])) {
ticketAddWatchers($ticket_id, ticketWatcherEmails($_POST['watchers'] ?? []));
ticketSetOtherAssets($ticket_id, $other_asset_ids);
+ // Raised about a service - from the service page's "Report a problem"
+ foreach (array_unique(array_map('intval', (array) ($_POST['service_ids'] ?? []))) as $service_id) {
+ $service = serviceRow($service_id);
+ if ($service && intval($service['service_client_id']) === $client_id && !$service['service_archived_at']) {
+ serviceLinkTickets($service_id, $client_id, [$ticket_id]);
+ logAudit("Service", "Link", "$session_name raised ticket $ticket_prefix$ticket_number about service " . escapeSql($service['service_name']), $client_id, $service_id);
+ }
+ }
+
// E-mail the client and the watchers
if (intval($config_ticket_client_general_notifications) === 1) {
ticketMailCreated($ticket_id, 'Ticket Created', $emailable_attachments['send']);
diff --git a/agent/service.php b/agent/service.php
new file mode 100644
index 000000000..ca6146c49
--- /dev/null
+++ b/agent/service.php
@@ -0,0 +1,716 @@
+
Nothing to see here Go Back";
+
+} else {
+
+ $client_id = intval($service['service_client_id']);
+ enforceClientAccess();
+ $client_url = "client_id=$client_id&";
+
+ $service_name = escapeHtml($service['service_name']);
+ $service_archived = !empty($service['service_archived_at']);
+ $is_favorite = intval($service['service_favorite']) === 1;
+
+ $can_edit = serviceCan(2) && !$service_archived;
+ $can_restore = serviceCan(2);
+ $can_delete = serviceCan(3) && !empty($config_destructive_deletes_enable);
+ $can_credentials = lookupUserPermission('module_credential') >= 1;
+ $can_link_credentials = $can_edit && linkCanEdit('service', 'credential');
+
+ // Override Tab Title // No Sanitizing needed as this var will only be used in the tab title
+ $page_title = $service['service_name'];
+
+ $counts = serviceCounts([$service_id])[$service_id];
+ $issues = serviceIssues([$service_id])[$service_id];
+ $review = serviceReview($service);
+ $urls = serviceUrls($service_id, $can_credentials);
+
+ // What it runs on - each with its address, network, site and warranty
+ $assets = [];
+ $sql = mysqli_query($mysqli, "SELECT assets.asset_id, asset_name, asset_type, asset_make, asset_model, asset_status, asset_warranty_expire, asset_archived_at,
+ asset_contact_id, asset_location_id, interface_ip, network_name, network_vlan, location_name, contact_name, contact_photo
+ FROM service_assets
+ JOIN assets ON assets.asset_id = service_assets.asset_id
+ LEFT JOIN asset_interfaces ON interface_asset_id = assets.asset_id AND interface_primary = 1
+ LEFT JOIN networks ON network_id = interface_network_id
+ LEFT JOIN locations ON location_id = asset_location_id
+ LEFT JOIN contacts ON contact_id = asset_contact_id
+ WHERE service_assets.service_id = $service_id
+ ORDER BY asset_archived_at IS NOT NULL, asset_name ASC");
+ while ($row = mysqli_fetch_assoc($sql)) {
+ $assets[] = $row;
+ }
+ $networks = array_unique(array_filter(array_column(array_filter($assets, function ($a) { return !$a['asset_archived_at']; }), 'network_name')));
+ $sites = array_unique(array_filter(array_column(array_filter($assets, function ($a) { return !$a['asset_archived_at']; }), 'location_name')));
+
+ // Its logins, and the ones on the assets it runs on
+ $credentials = $can_credentials ? credentialRows("credential_id IN (SELECT credential_id FROM service_credentials WHERE service_id = $service_id)") : [];
+ $asset_credentials = $can_credentials ? credentialRows("credential_client_id = $client_id AND credential_asset_id IN (SELECT asset_id FROM service_assets WHERE service_id = $service_id)
+ AND credential_id NOT IN (SELECT credential_id FROM service_credentials WHERE service_id = $service_id)") : [];
+
+ $domains = serviceLinked($service_id, 'domain');
+ $certificates = serviceLinked($service_id, 'certificate');
+ $documents = serviceLinked($service_id, 'document');
+ $contacts = serviceLinked($service_id, 'contact');
+ $vendors = serviceLinked($service_id, 'vendor');
+ $archived_credentials = $can_credentials ? array_values(array_filter(serviceLinked($service_id, 'credential'), function ($row) { return $row['archived_at']; })) : [];
+
+ // Who to call - the vendors' and people's details for the call
+ $vendor_details = [];
+ if ($vendors) {
+ $sql = mysqli_query($mysqli, "SELECT vendor_id, vendor_account_number, vendor_code, vendor_email, vendor_website, vendor_hours, vendor_sla FROM vendors
+ WHERE vendor_id IN (" . implode(',', array_map('intval', array_column($vendors, 'id'))) . ")");
+ while ($row = mysqli_fetch_assoc($sql)) {
+ $vendor_details[intval($row['vendor_id'])] = $row;
+ }
+ }
+ $contact_details = [];
+ if ($contacts) {
+ $sql = mysqli_query($mysqli, "SELECT contact_id, contact_email, contact_phone, contact_phone_country_code, contact_extension, contact_mobile, contact_mobile_country_code FROM contacts
+ WHERE contact_id IN (" . implode(',', array_map('intval', array_column($contacts, 'id'))) . ")");
+ while ($row = mysqli_fetch_assoc($sql)) {
+ $contact_details[intval($row['contact_id'])] = $row;
+ }
+ }
+
+ $tickets = ticketRows(serviceTicketsSql($service_id), 100);
+ $linked_ticket_ids = [];
+ $sql = mysqli_query($mysqli, "SELECT ticket_id FROM service_tickets WHERE service_id = $service_id");
+ while ($row = mysqli_fetch_row($sql)) {
+ $linked_ticket_ids[intval($row[0])] = true;
+ }
+ $open_tickets = count(array_filter($tickets, 'ticketIsOpen'));
+ $history = serviceHistory($service_id, 100);
+
+ $service_tabs = [
+ 'assets' => ['label' => 'Runs on', 'icon' => 'fa-desktop', 'count' => count($assets)],
+ 'logins' => ['label' => 'Logins', 'icon' => 'fa-key', 'count' => count($credentials) + count($asset_credentials) + count($archived_credentials)],
+ 'web' => ['label' => 'Domains & SSL', 'icon' => 'fa-globe', 'count' => count($domains) + count($certificates)],
+ 'documents' => ['label' => 'Documents', 'icon' => 'fa-file-alt', 'count' => count($documents)],
+ 'tickets' => ['label' => 'Tickets', 'icon' => 'fa-life-ring', 'count' => count($tickets), 'alarm' => $open_tickets],
+ 'history' => ['label' => 'History', 'icon' => 'fa-history', 'count' => count($history)],
+ ];
+ if (!$can_credentials) {
+ unset($service_tabs['logins']);
+ }
+
+ $link_url = "modals/link/link.php?from=service&id=$service_id&to=";
+ $unlink_url = function ($to, $other_id) use ($service_id) {
+ return "post.php?unlink_item&from=service&id=$service_id&to=$to&other=" . intval($other_id) . "&csrf_token=" . $_SESSION['csrf_token'];
+ };
+ $report_url = 'modals/ticket/ticket_add.php?' . http_build_query(['client_id' => $client_id, 'service_id' => $service_id]);
+ $has_danger = in_array('danger', array_column($issues, 'tone'), true);
+
+ /** An archived record still linked - struck through, with a way to unlink it. */
+ $archived_row = function ($to, $row) use ($can_edit, $unlink_url) {
+ $entity = LINK_ENTITIES[$to];
+ return '
'
+ . '' . escapeHtml($row['name']) . '
'
+ . '
Archived - unlink it, or link what replaced it
'
+ . ($can_edit ? ' ' : '')
+ . '';
+ };
+
+ ?>
+
+
+
+ = escapeHtml($service['client_name']) ?>
+
+
+ Services
+
+ = $service_name ?>
+
+
+
+
+
+ = serviceTile($service['service_category'], 'lg') ?>
+
+
+
+ = $service_name ?>
+
+ Archived
+ = serviceImportancePill($service['service_importance'], true) ?>
+ = serviceReviewPill($review, true) ?>
+
+
+ = escapeHtml($service['service_category']) ?>
+ = escapeHtml($service['service_description']) ?>
+
+
+
+
+
+
+ Report a problem
+
+
+ Link
+
+
+
+ Mark reviewed
+
+
+ Edit
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
= count($issues) ?> thing= count($issues) === 1 ? '' : 's' ?> it depends on need= count($issues) === 1 ? 's' : '' ?> attention
+ = serviceIssuesHtml($issues) ?>
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Importance
+ = $can_edit ? quickPick('service_importance', $service_id, $service['service_importance'], serviceImportancePill($service['service_importance'])) : serviceImportancePill($service['service_importance']) ?>
+ Review
+
+
+ = escapeHtml(date('M j, Y', strtotime($review['date']))) ?>
+ = escapeHtml($review['state'] === 'scheduled' ? ucfirst(timeAgo($review['date'])) : $review['text']) ?>
+
+ Not scheduled
+
+
+ = escapeHtml(SERVICE_REVIEW_INTERVALS[intval($service['service_review_interval'])] ?? '') ?>= $service['service_reviewed_at'] ? ' · last ' . escapeHtml(date('M j, Y', strtotime($service['service_reviewed_at']))) : ' · never reviewed' ?>
+
+
+
+ Opens at
+
+
+
+
+
+
+
+ Where
+
+ = escapeHtml(implode(', ', $sites)) ?>
+ = escapeHtml(implode(', ', $networks)) ?>
+
+
+ Added = timeAgo($service['service_created_at']) ?>
+
+ Updated = timeAgo($service['service_updated_at']) ?>
+
+
+
+
+
+
+
+
+
+
= escapeHtml($service['service_backup']) ?>
+
+
+ How it is backed up and how it comes back is not written down.
+
Add it
+
+
+
+
+
+
+
+
+
+
+
+
No vendor or person linked - who gets the call when it breaks?
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ "FIELD(service_importance, 'High', 'Medium', 'Low') $order, service_name ASC",
+ 'service_name' => "service_name $order",
+ 'service_category' => "service_category = '' $order, service_category $order, service_name ASC",
+ 'client_name' => "client_name $order, service_name ASC",
+ 'service_review_due' => "service_review_due IS NULL, service_review_due $order, service_name ASC",
+ 'service_updated_at' => "COALESCE(service_updated_at, service_created_at) $order",
+];
+if (!isset($sortable_columns[$sort])) {
+ $sort = 'service_importance';
+}
+
+$sql = mysqli_query($mysqli, "SELECT SQL_CALC_FOUND_ROWS services.*, client_name FROM services $joins
+ WHERE $filters $show_query
+ ORDER BY services.service_favorite DESC, {$sortable_columns[$sort]}, service_id DESC
+ LIMIT $record_from, $record_to");
$num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
+$services = [];
+while ($row = mysqli_fetch_assoc($sql)) {
+ $services[intval($row['service_id'])] = $row;
+}
+$counts = serviceCounts(array_keys($services));
+$issues = serviceIssues(array_keys($services));
+
+// How many of each, for the pills
+$count_sql = [];
+foreach ($shows as $key => $s) {
+ $count_sql[] = $s['where'] === '' ? 'COUNT(*)' : "COALESCE(SUM({$s['where']}), 0)";
+}
+$row = mysqli_fetch_row(mysqli_query($mysqli, "SELECT " . implode(', ', $count_sql) . " FROM services $joins WHERE $filters"));
+$view_counts = [];
+foreach (array_keys($shows) as $i => $key) {
+ $view_counts[$key] = intval($row[$i]);
+}
+
+// Categories in use in this scope, for the filter
+$categories = [];
+$sql_categories = mysqli_query($mysqli, "SELECT DISTINCT service_category FROM services $joins
+ WHERE service_category <> '' " . clientScopeSql('service_client_id') . ($client_url ? " AND service_client_id = $client_id" : '') . " ORDER BY service_category");
+while ($row = mysqli_fetch_row($sql_categories)) {
+ $categories[] = $row[0];
+}
+
+$filter_url = function ($changes) {
+ $params = array_merge($_GET, $changes);
+ unset($params['page']);
+ return '?' . http_build_query(array_filter($params, function ($v) { return $v !== '' && $v !== null; }));
+};
+
+$sort_link = function ($column, $label) use ($url_query_strings_sort, $sort, $disp, $order_icon) {
+ return '
'
+ . $label . ($sort === $column ? " $order_icon" : '') . ' ';
+};
+
+$link_icons = ['asset' => ['fa-desktop', 'asset', 'assets'], 'credential' => ['fa-key', 'login', 'logins'], 'domain' => ['fa-globe', 'domain', 'domains'],
+ 'certificate' => ['fa-lock', 'certificate', 'certificates'], 'contact' => ['fa-user', 'person', 'people'], 'vendor' => ['fa-building', 'vendor', 'vendors'],
+ 'document' => ['fa-file-alt', 'document', 'documents']];
+
?>
-
-