mirror of
https://github.com/itflow-org/itflow
synced 2026-11-23 21:07:22 +00:00
Add phone system integration with FusionPBX: Admin Phone Systems with setup lines for mod_xml_cdr and mod_cidlookup, domains linked to clients with suggestions, a Calls page per client and contact with calls matched to contacts, Add to Ticket with talk time as time worked, and caller ID names from ITFlow on desk phones
This commit is contained in:
@@ -6,7 +6,7 @@ This file documents all notable changes made to ITFlow.
|
||||
|
||||
### Upgrading
|
||||
|
||||
Update from Maintenance > Update — Queue Update hands the job to cron, which updates the files and applies this release's two database changes (4.1.1 to 4.1.3) in the same pass. They add a column to RMM providers for the asset type each kind of device comes in as, and three to rack items for devices side by side in a holder, and change nothing that is there. Nothing else is required.
|
||||
Update from Maintenance > Update — Queue Update hands the job to cron, which updates the files and applies this release's three database changes (4.1.1 to 4.1.4) in the same pass. They add a column to RMM providers for the asset type each kind of device comes in as, three to rack items for devices side by side in a holder, and the tables for phone systems, and change nothing that is there. Nothing else is required.
|
||||
|
||||
### New Features & Updates
|
||||
|
||||
@@ -18,6 +18,9 @@ Update from Maintenance > Update — Queue Update hands the job to cron, which u
|
||||
- API: assets read through the API come with asset_rmm - each of the asset's devices in an RMM, with its status, when it was last seen, who is signed in, its IPs, and whether it needs a reboot, has patches waiting or has checks failing.
|
||||
- RMM Providers: most of a client's devices gone from the RMM at once - three or more, and at least half of them - are kept as they were for a day instead of being marked gone straight away. That is far more often the RMM, or a change to what its API key can see, than the devices, and marking them gone dropped them from managed device counts on recurring invoices. The provider's row names the client and how many, and the sync shows as failed so it is noticed. Still missing after a day, they are marked gone. A client deleted in the RMM is treated the same way, and agents reinstalled all at once are still matched to their assets.
|
||||
- Racks: more than one device on a rack row - two Tiny PCs in a 1U bracket, eight PLCs on a shelf, the cards in a media converter chassis. Turn on Holds devices side by side on a rack item - a shelf, bracket or chassis, an asset or not - and say how many fit across. What is in it is drawn side by side in it, and past that many it wraps onto another row, for stacks. Click an empty slot to add something there, or pick the holder under Goes in when you add it. Drag an asset from Not racked yet or an item from the rack onto an empty slot, or a device to another slot, with Undo. Moving the holder moves what is in it, and removing it takes what is in it out of the rack too, after asking - the assets stay. An asset's page and the assets list show its slot, the page with a link to the holder when that is an asset - a server node to its chassis - and a holder's own page has Devices in it, listing what is in each slot. The client export and the API have slots too.
|
||||
- Phone Systems: Admin > Phone Systems takes FusionPBX - without its API or its database. FreeSWITCH, underneath it, sends ITFlow each call's record as it hangs up and asks ITFlow for caller ID names; the system's Setup window gives the lines to paste, with its own key in them. Each of its domains is linked to the client it is, with the client it looks like suggested. A call on a client's domain is that client's; on your own company's domain it goes to whoever called, from any client.
|
||||
- Calls: a new Calls page, on the menu and on each client, lists the calls with who they were with - matched to a contact by their number, or to a client by its main number - the extension and agent that took them, how long they talked, and whether they were missed or went to voicemail, with a link to the call's page in FusionPBX and its recording. A contact's page has a Calls tab, and the menu counts the calls missed today that aren't on a ticket. Add to Ticket puts a call on one of the client's open tickets, or a new one, as an internal note with its talk time as time worked.
|
||||
- Phone Systems: as a call rings, desk phones show the contact and their client - Marla Whitfield - Ravenwood Dental - when ITFlow knows the number, and the carrier's name otherwise.
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
@@ -50,6 +53,9 @@ Update from Maintenance > Update — Queue Update hands the job to cron, which u
|
||||
- `reportQuarters($year, $current_year)` in `functions/reports.php` gives a year's quarters - label, months, first and last month, `done`, `now` or `later`, and what each is compared with - and `reportQuarterSum($months, $quarter)` a quarter's share of a year of months. A report puts its `rows` (`label`, `now`, `prev`, `good`, `strong`) on each and `reportQuarterCards($quarters, $whole)` draws them, in the `.itflow-report-stats` strip, laid out by a container query on its own width: four across, two by two, or one a row. `agent/reports/profit_loss.php` and `agent/reports/tax_summary.php` show them on Quarterly in place of the chart, the latter adding up last year's tax paid on purchases for net tax. `reportChartColumns($months, $columns, $months_shown)` sums a year's months into a report's columns for a chart, null for a column still to come.
|
||||
- Database 4.1.3: `rack_units.unit_slots` (how many a holder takes side by side, 0 for any other item), `unit_parent_id` (the holder a device is in - a foreign key to `rack_units` that cascades on delete) and `unit_position` (its slot, 1 the first, counting across and then a row on). Re-runnable. A device in a holder keeps its holder's units, face and depth in its own columns - `rackSyncChildren($holder_id)` after the holder moves or changes - so whatever reads where an asset is racked reads it as before, and `rackOccupancy()` and `rackUnitConflict()` skip devices in a holder. `rackPositionForAsset()` adds `unit_id`, `unit_slots`, `unit_position`, `holder_name` and `holder_asset_id`. `rackSlotHtml()` links the holder from an asset's page, and `rackHolderChildren($unit_id)` lists what is in a holder for its own page, from `rackUnitRows($where)`, which `rackLoadUnits()` now uses too.
|
||||
- `rackPlacement($rack_id, $unit_id, $want)` in `functions/rack.php` checks where an item goes - in the rack (bounds, overlaps, and that a holder with devices in it still holds devices) or in a slot (the holder is in the same rack and holds devices, the item holds none, the slot is free and at most a row past the last one used, `rackSlotLimit()`) - and `rackPlacementSet()` writes it. The rack item form, the drag (`move_rack_unit` with `holder` and `slot`), Undo (`['rack_place', ...]`) and the API's `rack_units` endpoints (`unit_slots`, `unit_parent_id`, `unit_position`) all go through it. `rackNestItems()` and `rackHolderCells()` lay a holder out for `agent/includes/rack_elevation.php`, its rows counting the way the rack's units do.
|
||||
- Database 4.1.4: `phone_systems`, `phone_domains`, `phone_calls`, `phone_devices`, `phone_events` and `user_settings.user_config_phone_line`. Re-runnable. `functions/phone.php` has them; nothing in ITFlow connects to a phone system - it sends to `api/phone/`, keyed by the address's `key` or the HTTP Basic password (`phoneSystemByKey()`), 404 for an unknown key and 503 for a system turned off.
|
||||
- `api/phone/cdr.php` takes mod_xml_cdr's post - the `cdr` field URL-encoded or base64, or the body - through `phoneCdrParse()` (its variables URL-decoded, the rest from the first caller profile) and `phoneCallFromCdr()` (FusionPBX's `call_direction`, else worked out from the numbers). A second leg (`originating_leg_uuid`, or `uuid` starting `b_`) is set aside, and a call is kept once per uuid. `api/phone/cid.php` answers mod_cidlookup with `phoneCallerName()`.
|
||||
- `phoneWho($number, $domain_client_id)` matches a number with `smsContactsFor()` - a mobile before an office number many share - then clients' location phones; a number more than one client has is nobody's. On a client's domain only that client counts; on your own company's (`config_internal_client_id`) or one not linked, every client does. `phoneDomainLink()` matches a domain's calls again. `phoneCallToTicket()` adds the internal note, its talk time as `ticket_reply_time_worked` with the work start and end, and `add_ticket` takes `phone_call_id` (ticket source Phone). `phoneCleanup()` runs nightly: calls over 400 days old that aren't on a ticket go. Client delete and merge take the phone tables.
|
||||
|
||||
## [26.12]
|
||||
|
||||
|
||||
127
admin/database_updates/4.1.4.php
Normal file
127
admin/database_updates/4.1.4.php
Normal file
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* ITFlow - Database update to version 4.1.4 (from 4.1.3)
|
||||
* Included by admin/database_updates.php - do not access directly
|
||||
*
|
||||
* Phone systems (functions/phone.php) - a PBX, FusionPBX the first kind, that sends ITFlow what
|
||||
* happens on it: nothing is asked of its API or its database. Its call records come in as
|
||||
* FreeSWITCH posts them, its caller ID asks ITFlow who is calling, and its phones say when they
|
||||
* start, register and ring.
|
||||
*
|
||||
* phone_systems a PBX - its kind, name, address (for links into it) and the key in the
|
||||
* addresses it sends to
|
||||
* phone_domains each of its domains (tenants) ITFlow has heard from, and the client it is
|
||||
* phone_calls a call, from its call record - the other party (matched to a contact),
|
||||
* the extension on this side, how it ended, how long, and the ticket it went on
|
||||
* phone_devices a phone, by its MAC address, as it reports itself - and the asset it is
|
||||
* phone_events a phone ringing, answered or hung up, for the agent whose desk phone it is
|
||||
* user_settings.user_config_phone_line an agent's desk phone - its extension@domain
|
||||
*
|
||||
* Re-runnable: it only adds what isn't there.
|
||||
*/
|
||||
|
||||
defined('FROM_DB_UPDATER') || die("Direct file access is not allowed");
|
||||
|
||||
mysqli_query($mysqli, "CREATE TABLE IF NOT EXISTS `phone_systems` (
|
||||
`phone_system_id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`phone_system_kind` varchar(50) NOT NULL,
|
||||
`phone_system_name` varchar(100) NOT NULL,
|
||||
`phone_system_web_url` varchar(500) NOT NULL DEFAULT '',
|
||||
`phone_system_key` varchar(100) NOT NULL,
|
||||
`phone_system_active` tinyint(1) NOT NULL DEFAULT 1,
|
||||
`phone_system_heard_at` datetime DEFAULT NULL,
|
||||
`phone_system_created_at` datetime NOT NULL DEFAULT current_timestamp(),
|
||||
`phone_system_updated_at` datetime DEFAULT NULL ON UPDATE current_timestamp(),
|
||||
PRIMARY KEY (`phone_system_id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci");
|
||||
|
||||
mysqli_query($mysqli, "CREATE TABLE IF NOT EXISTS `phone_domains` (
|
||||
`phone_domain_id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`phone_domain_system_id` int(11) NOT NULL,
|
||||
`phone_domain_name` varchar(255) NOT NULL,
|
||||
`phone_domain_client_id` int(11) NOT NULL DEFAULT 0,
|
||||
`phone_domain_seen_at` datetime DEFAULT NULL,
|
||||
`phone_domain_created_at` datetime NOT NULL DEFAULT current_timestamp(),
|
||||
PRIMARY KEY (`phone_domain_id`),
|
||||
UNIQUE KEY `phone_domain_name` (`phone_domain_system_id`,`phone_domain_name`),
|
||||
KEY `phone_domain_client_id` (`phone_domain_client_id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci");
|
||||
|
||||
mysqli_query($mysqli, "CREATE TABLE IF NOT EXISTS `phone_calls` (
|
||||
`phone_call_id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`phone_call_system_id` int(11) NOT NULL,
|
||||
`phone_call_uuid` varchar(64) NOT NULL,
|
||||
`phone_call_domain_id` int(11) NOT NULL DEFAULT 0,
|
||||
`phone_call_client_id` int(11) NOT NULL DEFAULT 0,
|
||||
`phone_call_contact_id` int(11) NOT NULL DEFAULT 0,
|
||||
`phone_call_direction` varchar(10) NOT NULL DEFAULT '',
|
||||
`phone_call_number` varchar(32) NOT NULL DEFAULT '',
|
||||
`phone_call_name` varchar(100) NOT NULL DEFAULT '',
|
||||
`phone_call_dialed` varchar(32) NOT NULL DEFAULT '',
|
||||
`phone_call_extension` varchar(32) NOT NULL DEFAULT '',
|
||||
`phone_call_user_id` int(11) NOT NULL DEFAULT 0,
|
||||
`phone_call_status` varchar(20) NOT NULL DEFAULT '',
|
||||
`phone_call_started_at` datetime NOT NULL,
|
||||
`phone_call_answered_at` datetime DEFAULT NULL,
|
||||
`phone_call_seconds` int(11) NOT NULL DEFAULT 0,
|
||||
`phone_call_talk_seconds` int(11) NOT NULL DEFAULT 0,
|
||||
`phone_call_hangup_cause` varchar(50) NOT NULL DEFAULT '',
|
||||
`phone_call_recording` varchar(500) DEFAULT NULL,
|
||||
`phone_call_ticket_id` int(11) NOT NULL DEFAULT 0,
|
||||
`phone_call_created_at` datetime NOT NULL DEFAULT current_timestamp(),
|
||||
PRIMARY KEY (`phone_call_id`),
|
||||
UNIQUE KEY `phone_call_uuid` (`phone_call_system_id`,`phone_call_uuid`),
|
||||
KEY `phone_call_client_id` (`phone_call_client_id`,`phone_call_started_at`),
|
||||
KEY `phone_call_contact_id` (`phone_call_contact_id`,`phone_call_started_at`),
|
||||
KEY `phone_call_started_at` (`phone_call_started_at`),
|
||||
KEY `phone_call_ticket_id` (`phone_call_ticket_id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci");
|
||||
|
||||
mysqli_query($mysqli, "CREATE TABLE IF NOT EXISTS `phone_devices` (
|
||||
`phone_device_id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`phone_device_system_id` int(11) NOT NULL,
|
||||
`phone_device_mac` varchar(12) NOT NULL,
|
||||
`phone_device_vendor` varchar(30) NOT NULL DEFAULT '',
|
||||
`phone_device_model` varchar(100) NOT NULL DEFAULT '',
|
||||
`phone_device_firmware` varchar(100) NOT NULL DEFAULT '',
|
||||
`phone_device_ip` varchar(64) NOT NULL DEFAULT '',
|
||||
`phone_device_line` varchar(100) NOT NULL DEFAULT '',
|
||||
`phone_device_domain_id` int(11) NOT NULL DEFAULT 0,
|
||||
`phone_device_client_id` int(11) NOT NULL DEFAULT 0,
|
||||
`phone_device_asset_id` int(11) NOT NULL DEFAULT 0,
|
||||
`phone_device_status` varchar(20) NOT NULL DEFAULT '',
|
||||
`phone_device_status_at` datetime DEFAULT NULL,
|
||||
`phone_device_heard_at` datetime DEFAULT NULL,
|
||||
`phone_device_created_at` datetime NOT NULL DEFAULT current_timestamp(),
|
||||
PRIMARY KEY (`phone_device_id`),
|
||||
UNIQUE KEY `phone_device_mac` (`phone_device_system_id`,`phone_device_mac`),
|
||||
KEY `phone_device_asset_id` (`phone_device_asset_id`),
|
||||
KEY `phone_device_client_id` (`phone_device_client_id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci");
|
||||
|
||||
mysqli_query($mysqli, "CREATE TABLE IF NOT EXISTS `phone_events` (
|
||||
`phone_event_id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`phone_event_system_id` int(11) NOT NULL,
|
||||
`phone_event_domain_id` int(11) NOT NULL DEFAULT 0,
|
||||
`phone_event_line` varchar(100) NOT NULL DEFAULT '',
|
||||
`phone_event_user_id` int(11) NOT NULL DEFAULT 0,
|
||||
`phone_event_kind` varchar(20) NOT NULL,
|
||||
`phone_event_call` varchar(100) NOT NULL DEFAULT '',
|
||||
`phone_event_number` varchar(32) NOT NULL DEFAULT '',
|
||||
`phone_event_name` varchar(100) NOT NULL DEFAULT '',
|
||||
`phone_event_contact_id` int(11) NOT NULL DEFAULT 0,
|
||||
`phone_event_client_id` int(11) NOT NULL DEFAULT 0,
|
||||
`phone_event_at` datetime NOT NULL DEFAULT current_timestamp(),
|
||||
PRIMARY KEY (`phone_event_id`),
|
||||
KEY `phone_event_user_id` (`phone_event_user_id`,`phone_event_id`),
|
||||
KEY `phone_event_at` (`phone_event_at`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci");
|
||||
|
||||
$itflow_column = mysqli_query($mysqli, "SELECT 1 FROM information_schema.COLUMNS
|
||||
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'user_settings' AND COLUMN_NAME = 'user_config_phone_line' LIMIT 1");
|
||||
if ($itflow_column && mysqli_num_rows($itflow_column) === 0) {
|
||||
mysqli_query($mysqli, "ALTER TABLE `user_settings` ADD `user_config_phone_line` varchar(255) DEFAULT NULL AFTER `user_config_reminder_email`");
|
||||
}
|
||||
|
||||
unset($itflow_column);
|
||||
@@ -146,6 +146,13 @@
|
||||
<p>RMM Providers</p>
|
||||
</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a href="/admin/phone_systems.php"
|
||||
class="nav-link <?= (in_array(basename($_SERVER['PHP_SELF']), ['phone_systems.php', 'phone_domains.php']) ? 'active' : '') ?>">
|
||||
<i class="nav-icon fas fa-phone-volume"></i>
|
||||
<p>Phone Systems</p>
|
||||
</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a href="/admin/custom_links.php" class="nav-link <?= (basename($_SERVER['PHP_SELF']) == 'custom_links.php' ? 'active' : '') ?>">
|
||||
<i class="nav-icon fas fa-external-link-alt"></i>
|
||||
|
||||
55
admin/modals/phone/phone_domain_link.php
Normal file
55
admin/modals/phone/phone_domain_link.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Admin > Phone Systems > a system's domains - link one to a client, or to another one. The
|
||||
* client it looks like is picked to start with. Saved by link_phone_domain (admin/post/phone.php),
|
||||
* which matches its calls again.
|
||||
*/
|
||||
|
||||
require_once '../../includes/modal_header.php';
|
||||
|
||||
$row = phoneDomainRow($_GET['id'] ?? 0);
|
||||
if (!$row) {
|
||||
echo json_encode(['error' => 'That domain is gone.']);
|
||||
exit;
|
||||
}
|
||||
$phone_domain_id = intval($row['phone_domain_id']);
|
||||
$current = intval($row['phone_domain_client_id']);
|
||||
$picked = $current ?: (phoneDomainSuggestions(intval($row['phone_system_id']))[$phone_domain_id] ?? 0);
|
||||
$own = intval($config_internal_client_id ?? 0);
|
||||
|
||||
$clients = mysqli_query($mysqli, "SELECT client_id, client_name, client_abbreviation FROM clients WHERE (client_archived_at IS NULL OR client_id = $current)" . clientNotLeadSql($current) . " ORDER BY client_name ASC");
|
||||
|
||||
ob_start();
|
||||
|
||||
?>
|
||||
<div class="modal-header bg-dark">
|
||||
<h5 class="modal-title"><i class="fas fa-fw fa-link me-2"></i>Link <strong class="font-monospace"><?= escapeHtml($row['phone_domain_name']) ?></strong></h5>
|
||||
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</div>
|
||||
<form action="post.php" method="post" autocomplete="off">
|
||||
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
|
||||
<input type="hidden" name="phone_domain_id" value="<?= $phone_domain_id ?>">
|
||||
|
||||
<div class="modal-body">
|
||||
<p class="small text-body-secondary">Calls on a client's domain are that client's, with the contacts matched among theirs. Link your own company's domain to <?= $own ? '<strong>' . escapeHtml((string) getFieldById('clients', $own, 'client_name')) . '</strong>, your own client record' : 'your own client record (Admin > Settings > Company)' ?>: its calls go to whoever is calling, from any client. Its calls so far are matched again.</p>
|
||||
|
||||
<label class="form-label">ITFlow client <strong class="text-danger">*</strong></label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fas fa-fw fa-user"></i></span>
|
||||
<select class="form-select select2" name="client_id" required>
|
||||
<option value="">- Select a client -</option>
|
||||
<?php while ($client = mysqli_fetch_assoc($clients)) {
|
||||
$client_id = intval($client['client_id']); ?>
|
||||
<option value="<?= $client_id ?>" <?= $client_id === $picked ? 'selected' : '' ?><?= selectOptionClient($client_id, $client['client_name'], $client['client_abbreviation'] ?? '') ?>><?= escapeHtml($client['client_name']) ?><?= $client_id === $own ? ' (your company)' : '' ?></option>
|
||||
<?php } ?>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="submit" name="link_phone_domain" class="btn btn-primary fw-bold"><i class="fas fa-link me-2"></i>Link</button>
|
||||
<button type="button" class="btn btn-light" data-bs-dismiss="modal"><i class="fas fa-times me-2"></i>Cancel</button>
|
||||
</div>
|
||||
</form>
|
||||
<?php
|
||||
require_once '../../../includes/modal_footer.php';
|
||||
9
admin/modals/phone/phone_system_add.php
Normal file
9
admin/modals/phone/phone_system_add.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Admin > Phone Systems - Add. The same window as Edit
|
||||
* (phone_system_edit.php), with no phone system yet.
|
||||
*/
|
||||
|
||||
$_GET['id'] = 0;
|
||||
require __DIR__ . '/phone_system_edit.php';
|
||||
88
admin/modals/phone/phone_system_edit.php
Normal file
88
admin/modals/phone/phone_system_edit.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Admin > Phone Systems - one phone system's window: Add (no id) or Edit. Which kind it is is
|
||||
* picked when it is added and stays. Its address is only for links into it - ITFlow never
|
||||
* connects to it; it sends ITFlow what happens, to the addresses its Setup window shows. Saved by
|
||||
* save_phone_system (admin/post/phone.php).
|
||||
*/
|
||||
|
||||
require_once '../../includes/modal_header.php';
|
||||
|
||||
$phone_system_id = intval($_GET['id'] ?? 0);
|
||||
$row = $phone_system_id ? phoneSystemRow($phone_system_id) : null;
|
||||
if ($phone_system_id && !$row) {
|
||||
echo json_encode(['error' => 'That phone system no longer exists.']);
|
||||
exit;
|
||||
}
|
||||
if ($row && !phoneKind($row)) {
|
||||
echo json_encode(['error' => 'This ITFlow doesn\'t know that phone system\'s kind.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$kinds = phoneSystemKinds();
|
||||
$kind_key = $row ? (string) $row['phone_system_kind'] : (string) array_key_first($kinds);
|
||||
$kind = $kinds[$kind_key];
|
||||
|
||||
ob_start();
|
||||
|
||||
?>
|
||||
<div class="modal-header bg-dark">
|
||||
<h5 class="modal-title"><i class="<?= escapeHtml($kind['icon']) ?> fa-fw me-2"></i><?= $row ? '<strong>' . escapeHtml(phoneSystemName($row)) . '</strong>' : 'New Phone System' ?></h5>
|
||||
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</div>
|
||||
<form action="post.php" method="post" autocomplete="off">
|
||||
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
|
||||
<input type="hidden" name="phone_system_id" value="<?= $phone_system_id ?>">
|
||||
|
||||
<div class="modal-body">
|
||||
|
||||
<?php if ($row) { ?>
|
||||
<div class="mb-3">
|
||||
<div class="form-check form-switch">
|
||||
<input type="checkbox" class="form-check-input" name="active" value="1" id="phoneSystemActive" <?= intval($row['phone_system_active']) ? 'checked' : '' ?>>
|
||||
<label class="form-check-label" for="phoneSystemActive">On - what it sends is kept</label>
|
||||
</div>
|
||||
<div class="form-text ps-5">Off, calls aren't kept and caller ID gets no names from ITFlow.</div>
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
<div class="mb-3">
|
||||
<label class="form-label" for="phoneSystemKind">Phone system <?= $row ? '' : '<strong class="text-danger">*</strong>' ?></label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fas fa-fw fa-shapes"></i></span>
|
||||
<select class="form-select select2" name="kind" id="phoneSystemKind" <?= $row ? 'disabled' : '' ?>>
|
||||
<?php foreach ($kinds as $key => $info) { ?>
|
||||
<option value="<?= escapeHtml($key) ?>" <?= $key === $kind_key ? 'selected' : '' ?><?= selectOptionData(['icon' => $info['icon'], 'description' => $info['about']]) ?>><?= escapeHtml($info['name']) ?></option>
|
||||
<?php } ?>
|
||||
</select>
|
||||
</div>
|
||||
<?php if ($row) { ?><div class="form-text">Which kind it is stays - add another phone system for another one.</div><?php } ?>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label class="form-label" for="phoneSystemName">Name <strong class="text-danger">*</strong></label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fas fa-fw fa-tag"></i></span>
|
||||
<input type="text" class="form-control" name="name" id="phoneSystemName" maxlength="100" required value="<?= escapeHtml($row ? $row['phone_system_name'] : $kind['name']) ?>" placeholder="What you call it">
|
||||
</div>
|
||||
<div class="form-text">What calls and the audit log call it. Two of the same kind need names of their own.</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label class="form-label" for="phoneSystemWebUrl">Address</label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fas fa-fw fa-globe"></i></span>
|
||||
<input type="url" class="form-control" name="web_url" id="phoneSystemWebUrl" maxlength="500" spellcheck="false" placeholder="<?= escapeHtml($kind['web_placeholder']) ?>" value="<?= escapeHtml($row['phone_system_web_url'] ?? '') ?>">
|
||||
</div>
|
||||
<div class="form-text">Where you sign in to it, for links to it and to each call's page there with its recording. ITFlow never connects to it.</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="submit" name="save_phone_system" class="btn btn-primary fw-bold"><i class="fas fa-check me-2"></i><?= $row ? 'Save' : 'Create' ?></button>
|
||||
<button type="button" class="btn btn-light" data-bs-dismiss="modal"><i class="fas fa-times me-2"></i>Cancel</button>
|
||||
</div>
|
||||
</form>
|
||||
<?php
|
||||
require_once '../../../includes/modal_footer.php';
|
||||
77
admin/modals/phone/phone_system_setup.php
Normal file
77
admin/modals/phone/phone_system_setup.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Admin > Phone Systems - how to set a phone system up to send ITFlow what happens on it: the
|
||||
* lines to put in its configuration, with this system's addresses and key in them, each with Copy.
|
||||
* Nothing in ITFlow connects to it - it sends, to api/phone/ (functions/phone.php).
|
||||
*/
|
||||
|
||||
require_once '../../includes/modal_header.php';
|
||||
|
||||
$row = phoneSystemRow($_GET['id'] ?? 0);
|
||||
if (!$row || !phoneKind($row)) {
|
||||
echo json_encode(['error' => $row ? 'This ITFlow doesn\'t know that phone system\'s kind.' : 'That phone system no longer exists.']);
|
||||
exit;
|
||||
}
|
||||
$phone_system_id = intval($row['phone_system_id']);
|
||||
$name = phoneSystemName($row);
|
||||
$kind = phoneKind($row);
|
||||
$heard = $row['phone_system_heard_at'];
|
||||
|
||||
// What goes in FreeSWITCH's XML - & written as & there
|
||||
$xml = function (string $url) {
|
||||
return htmlspecialchars($url, ENT_QUOTES | ENT_XML1, 'UTF-8');
|
||||
};
|
||||
$cdr_url = phoneUrl($row, 'cdr');
|
||||
$cid_url = phoneUrl($row, 'cid', 'number=${caller_id_number}&domain=${domain_name}');
|
||||
|
||||
$snippets = [
|
||||
'cdr' => '<param name="url" value="' . $xml($cdr_url) . "\"/>\n<param name=\"log-http-and-disk\" value=\"true\"/>\n<param name=\"retries\" value=\"2\"/>\n<param name=\"delay\" value=\"5\"/>",
|
||||
'cid' => '<param name="url" value="' . $xml($cid_url) . "\"/>\n<param name=\"cache\" value=\"false\"/>",
|
||||
'dialplan' => "<extension name=\"itflow_caller_id\" continue=\"true\">\n <condition field=\"\${caller_id_number}\" expression=\"^\\+?\\d{7,15}\$\">\n <action application=\"set\" data=\"itflow_cid=\${cidlookup(\${caller_id_number})}\" inline=\"true\"/>\n </condition>\n <condition field=\"\${itflow_cid}\" expression=\"^(?!UNKNOWN\$|-ERR).+\$\">\n <action application=\"set\" data=\"effective_caller_id_name=\${itflow_cid}\"/>\n </condition>\n</extension>",
|
||||
];
|
||||
|
||||
$copy_block = function (string $id, string $text) {
|
||||
return '<div class="position-relative">'
|
||||
. '<pre class="itflow-phone-snippet small mb-0" id="' . $id . '">' . escapeHtml($text) . '</pre>'
|
||||
. '<button type="button" class="btn btn-sm btn-light border position-absolute top-0 end-0 m-1 clipboardjs" data-clipboard-target="#' . $id . '" title="Copy"><i class="far fa-fw fa-copy"></i></button>'
|
||||
. '</div>';
|
||||
};
|
||||
|
||||
ob_start();
|
||||
|
||||
?>
|
||||
<div class="modal-header bg-dark">
|
||||
<h5 class="modal-title"><i class="fas fa-fw fa-plug me-2"></i>Set up <strong><?= escapeHtml($name) ?></strong></h5>
|
||||
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</div>
|
||||
|
||||
<div class="modal-body">
|
||||
|
||||
<p class="small mb-2">ITFlow doesn't connect to <?= escapeHtml($kind['short']) ?> - nothing is asked of its API or its database. Instead FreeSWITCH, underneath it, is set to send ITFlow what happens, to addresses with this system's key in them. Keep them to yourself - if one gets out, New Key in its Domains page's menu changes them.</p>
|
||||
<div class="small mb-3" data-phone-heard="<?= $heard ? 'yes' : 'never' ?>">
|
||||
<?php if ($heard) { ?>
|
||||
<i class="fas fa-fw fa-check-circle text-success me-1"></i>Heard from <span title="<?= formatDateTime($heard) ?>"><?= escapeHtml(timeAgo($heard)) ?></span>.
|
||||
<?php } else { ?>
|
||||
<i class="fas fa-fw fa-hourglass-half text-secondary me-1"></i>Not heard from yet - this says when it is.
|
||||
<?php } ?>
|
||||
</div>
|
||||
|
||||
<h6 class="fw-semibold"><i class="fas fa-fw fa-history text-secondary me-1"></i>Calls</h6>
|
||||
<p class="small mb-2">Each call's record comes to ITFlow as it hangs up, and is kept with the contact it was with. In <?= escapeHtml($kind['short']) ?>'s <strong>Advanced > XML Editor</strong>, or on the server, open <code>autoload_configs/xml_cdr.conf.xml</code> and set these in its <code><settings></code>, leaving its <code>log-dir</code> as it is so <?= escapeHtml($kind['short']) ?> goes on importing its own records:</p>
|
||||
<?= $copy_block('phoneSnippetCdr', $snippets['cdr']) ?>
|
||||
<p class="small text-body-secondary mt-2 mb-3">Then run <code>fs_cli -x 'reload mod_xml_cdr'</code>. If its <code>url</code> still points at <?= escapeHtml($kind['short']) ?>'s own <code>xml_cdr_import.php</code>, switch it to importing from files first - each record goes to one address only.</p>
|
||||
|
||||
<h6 class="fw-semibold"><i class="fas fa-fw fa-id-badge text-secondary me-1"></i>Caller ID</h6>
|
||||
<p class="small mb-2">Calls coming in show the contact and their client on the phones - "Marla Whitfield - Ravenwood Dental". In <code>autoload_configs/cidlookup.conf.xml</code>, set:</p>
|
||||
<?= $copy_block('phoneSnippetCid', $snippets['cid']) ?>
|
||||
<p class="small mt-2 mb-2">Load it with <code>fs_cli -x 'reload mod_cidlookup'</code> (add <code>mod_cidlookup</code> to <code>modules.conf.xml</code> if it isn't). Then, in <strong>Dialplan > Dialplan Manager</strong>, add this to the <code>public</code> context, ordered before the inbound routes - the name is only set when ITFlow knows the number, so the carrier's stays otherwise:</p>
|
||||
<?= $copy_block('phoneSnippetDialplan', $snippets['dialplan']) ?>
|
||||
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<a class="btn btn-outline-secondary me-auto" href="phone_domains.php?phone_system_id=<?= $phone_system_id ?>"><i class="fas fa-globe me-2"></i>Domains</a>
|
||||
<button type="button" class="btn btn-light" data-bs-dismiss="modal"><i class="fas fa-times me-2"></i>Close</button>
|
||||
</div>
|
||||
<?php
|
||||
require_once '../../../includes/modal_footer.php';
|
||||
277
admin/phone_domains.php
Normal file
277
admin/phone_domains.php
Normal file
@@ -0,0 +1,277 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Admin > Phone Systems > a system's domains (functions/phone.php). Each domain (a FusionPBX
|
||||
* tenant) it has sent anything about is linked to the client it is, or not linked yet - with the
|
||||
* client it looks like suggested. A call on a client's domain is that client's; on your own
|
||||
* company's, or one not linked, it is the caller's. Until the system has been heard from, the
|
||||
* page says how to set it up (modals/phone/phone_system_setup.php). The handlers are
|
||||
* admin/post/phone.php.
|
||||
*/
|
||||
|
||||
// Default Column Sortby Filter
|
||||
$sort = "phone_domain_name";
|
||||
$order = "ASC";
|
||||
|
||||
require_once "includes/inc_all_admin.php";
|
||||
|
||||
$system = phoneTablesReady() ? phoneSystemRow($_GET['phone_system_id'] ?? 0) : null;
|
||||
$kind = $system ? phoneKind($system) : null;
|
||||
|
||||
if (!$system || !$kind) { ?>
|
||||
<div class="alert alert-secondary"><i class="fas fa-fw fa-question-circle me-2"></i><?= $system ? 'This ITFlow doesn\'t know that phone system\'s kind.' : 'That phone system is gone - it may have been deleted.' ?> <a href="phone_systems.php" class="alert-link">Phone Systems</a></div>
|
||||
<?php
|
||||
require_once "../includes/footer.php";
|
||||
exit;
|
||||
}
|
||||
|
||||
$phone_system_id = intval($system['phone_system_id']);
|
||||
$name = phoneSystemName($system);
|
||||
$short = $kind['short'];
|
||||
$on = intval($system['phone_system_active']) === 1;
|
||||
$csrf = $_SESSION['csrf_token'];
|
||||
$edit_url = "modals/phone/phone_system_edit.php?id=$phone_system_id";
|
||||
$setup_url = "modals/phone/phone_system_setup.php?id=$phone_system_id";
|
||||
$suggestions = phoneDomainSuggestions($phone_system_id);
|
||||
$web = phoneWebUrl($system);
|
||||
|
||||
// The views - every domain it has sent, the ones not linked yet, and the linked ones
|
||||
$shows = [
|
||||
'' => ['label' => 'All', 'icon' => 'fa-list', 'sql' => '1 = 1', 'title' => "Every domain $name has sent"],
|
||||
'unlinked' => ['label' => 'Not linked', 'icon' => 'fa-unlink', 'sql' => 'phone_domain_client_id = 0', 'title' => 'Their calls go to whoever is calling'],
|
||||
'linked' => ['label' => 'Linked', 'icon' => 'fa-link', 'sql' => 'phone_domain_client_id > 0', 'title' => 'Their calls are their client\'s'],
|
||||
];
|
||||
$show = isset($shows[$_GET['show'] ?? '']) ? (string) ($_GET['show'] ?? '') : '';
|
||||
$view_counts = [];
|
||||
foreach ($shows as $key => $view) {
|
||||
$view_counts[$key] = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FROM phone_domains WHERE phone_domain_system_id = $phone_system_id AND {$view['sql']}"))[0]);
|
||||
}
|
||||
$filter_url = function (array $change) {
|
||||
$get = array_merge($_GET, $change);
|
||||
unset($get['page'], $get['setup']);
|
||||
$get = array_filter($get, function ($value) {
|
||||
return $value !== null && $value !== '';
|
||||
});
|
||||
return '?' . http_build_query($get);
|
||||
};
|
||||
|
||||
// Only these sort - an unknown column would take the page down
|
||||
$sortable_columns = [
|
||||
'phone_domain_name' => "phone_domain_name $order",
|
||||
'calls_month' => "calls_month $order, phone_domain_name ASC",
|
||||
'client_name' => "client_name IS NULL, client_name $order, phone_domain_name ASC",
|
||||
'phone_domain_seen_at' => "phone_domain_seen_at IS NULL, phone_domain_seen_at $order, phone_domain_name ASC",
|
||||
];
|
||||
if (!isset($sortable_columns[$sort])) {
|
||||
$sort = 'phone_domain_name';
|
||||
}
|
||||
|
||||
$sql = mysqli_query($mysqli, "SELECT SQL_CALC_FOUND_ROWS phone_domains.*, client_name, client_abbreviation, client_archived_at,
|
||||
(SELECT COUNT(*) FROM phone_calls WHERE phone_call_domain_id = phone_domains.phone_domain_id AND phone_call_started_at >= NOW() - INTERVAL 30 DAY) AS calls_month
|
||||
FROM phone_domains
|
||||
LEFT JOIN clients ON client_id = phone_domain_client_id AND phone_domain_client_id > 0
|
||||
WHERE phone_domain_system_id = $phone_system_id AND {$shows[$show]['sql']}
|
||||
AND (phone_domain_name LIKE '%$q%' OR client_name LIKE '%$q%')
|
||||
ORDER BY {$sortable_columns[$sort]}, phone_domain_id ASC
|
||||
LIMIT $record_from, $record_to");
|
||||
|
||||
$num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
|
||||
|
||||
// The clients the suggestions name
|
||||
$suggested_names = [];
|
||||
if ($suggestions) {
|
||||
$suggested_sql = mysqli_query($mysqli, "SELECT client_id, client_name FROM clients WHERE client_id IN (" . implode(', ', array_map('intval', $suggestions)) . ")");
|
||||
while ($row = mysqli_fetch_assoc($suggested_sql)) {
|
||||
$suggested_names[intval($row['client_id'])] = $row['client_name'];
|
||||
}
|
||||
}
|
||||
|
||||
$calls_total = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FROM phone_calls WHERE phone_call_system_id = $phone_system_id"))[0]);
|
||||
$own_client = intval($config_internal_client_id ?? 0);
|
||||
|
||||
?>
|
||||
|
||||
<div class="card">
|
||||
<div class="card-header bg-dark py-2">
|
||||
<h3 class="card-title mt-2"><i class="<?= escapeHtml($kind['icon']) ?> fa-fw me-2"></i><?= escapeHtml($name) ?> <span class="fw-normal opacity-75">· Domains</span>
|
||||
<?php if (!$on) { ?><span class="badge rounded-pill text-bg-secondary ms-2 fw-normal" data-phone-system-state="off" title="What it sends is turned away">Off</span><?php } ?>
|
||||
</h3>
|
||||
<div class="card-tools">
|
||||
<a class="btn btn-outline-light me-1" href="phone_systems.php" title="Back to Phone Systems"><i class="fas fa-fw fa-arrow-left"></i><span class="d-none d-lg-inline ms-2">Phone Systems</span></a>
|
||||
<?php if ($web !== '') { ?>
|
||||
<a class="btn btn-outline-light me-1" href="<?= escapeHtml($web) ?>" target="_blank" rel="noopener noreferrer" title="<?= escapeHtml($name) ?>"><i class="fas fa-fw fa-external-link-alt"></i><span class="d-none d-lg-inline ms-2">Open <?= escapeHtml($short) ?></span></a>
|
||||
<?php } ?>
|
||||
<button type="button" class="btn btn-primary ajax-modal" data-modal-size="lg" data-modal-url="<?= $setup_url ?>" data-phone-setup><i class="fas fa-fw fa-plug"></i><span class="d-none d-lg-inline ms-2">Setup</span></button>
|
||||
<div class="dropdown dropstart d-inline-block ms-1">
|
||||
<button class="btn btn-outline-light" title="More actions" type="button" data-bs-toggle="dropdown"><i class="fas fa-fw fa-ellipsis-v"></i></button>
|
||||
<div class="dropdown-menu">
|
||||
<a class="dropdown-item ajax-modal" href="#" data-modal-url="<?= $edit_url ?>"><i class="fas fa-fw fa-edit text-secondary me-2"></i>Edit</a>
|
||||
<?php if ($suggestions) { ?>
|
||||
<a class="dropdown-item" href="post.php?link_phone_suggested=all&phone_system_id=<?= $phone_system_id ?>&csrf_token=<?= $csrf ?>"><i class="fas fa-fw fa-link text-secondary me-2"></i>Link the <?= count($suggestions) ?> suggested</a>
|
||||
<?php } ?>
|
||||
<a class="dropdown-item confirm-link" href="post.php?new_phone_system_key=<?= $phone_system_id ?>&csrf_token=<?= $csrf ?>"
|
||||
data-confirm-title="Make a new key?" data-confirm-button="New key"
|
||||
data-confirm-text="Its addresses change, and the ones in <?= escapeHtml($short) ?> now stop working until you put in the new ones.">
|
||||
<i class="fas fa-fw fa-key text-secondary me-2"></i>New Key
|
||||
</a>
|
||||
<?php if ($on) { ?>
|
||||
<a class="dropdown-item confirm-link" href="post.php?turn_phone_system_off=<?= $phone_system_id ?>&csrf_token=<?= $csrf ?>"
|
||||
data-confirm-title="Turn <?= escapeHtml($name) ?> off?" data-confirm-button="Turn off"
|
||||
data-confirm-text="What it sends is turned away - calls aren't kept and caller ID gets no names - until it is on again.">
|
||||
<i class="fas fa-fw fa-toggle-off text-secondary me-2"></i>Turn Off
|
||||
</a>
|
||||
<?php } else { ?>
|
||||
<a class="dropdown-item" href="post.php?turn_phone_system_on=<?= $phone_system_id ?>&csrf_token=<?= $csrf ?>"><i class="fas fa-fw fa-toggle-on text-secondary me-2"></i>Turn On</a>
|
||||
<?php } ?>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card-header py-2 small" data-phone-heard="<?= $system['phone_system_heard_at'] ? 'yes' : 'never' ?>">
|
||||
<div class="d-flex flex-wrap align-items-center column-gap-3 row-gap-1">
|
||||
<?php if (!$system['phone_system_heard_at']) { ?>
|
||||
<span class="text-body-secondary"><i class="fas fa-fw fa-info-circle me-1"></i>Not heard from yet - <a class="ajax-modal" href="#" data-modal-size="lg" data-modal-url="<?= $setup_url ?>">set <?= escapeHtml($short) ?> up</a> to send ITFlow its calls. Its domains show here as they come in.</span>
|
||||
<?php } else { ?>
|
||||
<span><i class="fas fa-fw fa-check-circle text-success me-1"></i>Heard from <span title="<?= formatDateTime($system['phone_system_heard_at']) ?>"><?= escapeHtml(timeAgo($system['phone_system_heard_at'])) ?></span></span>
|
||||
<span class="text-body-secondary"><a class="text-reset" href="../agent/calls.php"><?= number_format($calls_total) ?> call<?= $calls_total === 1 ? '' : 's' ?></a> in ITFlow</span>
|
||||
<?php } ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card-header py-3">
|
||||
<form autocomplete="off">
|
||||
<input type="hidden" name="phone_system_id" value="<?= $phone_system_id ?>">
|
||||
<input type="hidden" name="show" value="<?= escapeHtml($show) ?>">
|
||||
<div class="row g-2 align-items-center">
|
||||
<div class="col-lg-5 col-md-6">
|
||||
<div class="input-group">
|
||||
<input type="search" class="form-control" name="q" value="<?php if (isset($q)) { echo stripslashes(escapeHtml($q)); } ?>" placeholder="Search domains..." title="The domain, or your client">
|
||||
<button class="btn btn-dark" title="Search"><i class="fas fa-search"></i></button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-lg-7 col-md-6 small text-body-secondary text-md-end">
|
||||
Link each client's domain to them - your own company's domain to your own client record.
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
<nav class="nav nav-pills itflow-asset-tabs mt-2 flex-nowrap overflow-auto">
|
||||
<?php foreach ($shows as $key => $view) { ?>
|
||||
<a class="nav-link text-nowrap <?= $show === $key ? 'active' : '' ?>" href="<?= escapeHtml($filter_url(['show' => $key === '' ? null : $key])) ?>" title="<?= escapeHtml($view['title']) ?>">
|
||||
<i class="fas fa-fw <?= $view['icon'] ?> me-1"></i><?= escapeHtml($view['label']) ?> <span class="badge rounded-pill ms-1"><?= number_format(intval($view_counts[$key])) ?></span>
|
||||
</a>
|
||||
<?php } ?>
|
||||
</nav>
|
||||
</div>
|
||||
|
||||
<div class="table-responsive">
|
||||
<table class="table table-hover align-middle mb-0">
|
||||
<thead class="table-light text-nowrap <?php if ($num_rows[0] == 0) { echo "d-none"; } ?>">
|
||||
<tr>
|
||||
<th class="ps-3"><?= sortLink('phone_domain_name', 'Domain') ?></th>
|
||||
<th class="d-none d-md-table-cell"><?= sortLink('calls_month', 'Calls, 30 days') ?></th>
|
||||
<th><?= sortLink('client_name', 'ITFlow client') ?></th>
|
||||
<th class="d-none d-lg-table-cell"><?= sortLink('phone_domain_seen_at', 'Last heard from') ?></th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$phone_domain_id = intval($row['phone_domain_id']);
|
||||
$client_id = intval($row['phone_domain_client_id']);
|
||||
$linked = $client_id && $row['client_name'] !== null;
|
||||
$suggested = $suggestions[$phone_domain_id] ?? 0;
|
||||
$link_url = "modals/phone/phone_domain_link.php?id=$phone_domain_id";
|
||||
?>
|
||||
<tr data-phone-domain-id="<?= $phone_domain_id ?>">
|
||||
<td class="ps-3">
|
||||
<div class="fw-semibold font-monospace small"><?= escapeHtml($row['phone_domain_name']) ?></div>
|
||||
<div class="d-md-none small text-body-secondary"><?= number_format(intval($row['calls_month'])) ?> calls</div>
|
||||
</td>
|
||||
<td class="d-none d-md-table-cell text-nowrap">
|
||||
<?php if ($linked && intval($row['calls_month'])) { ?>
|
||||
<?php // Your own company's calls are their callers' clients' - all of them ?>
|
||||
<a href="../agent/calls.php<?= $client_id === $own_client ? '' : "?client_id=$client_id" ?>"><?= number_format(intval($row['calls_month'])) ?></a>
|
||||
<?php } else { ?>
|
||||
<?= number_format(intval($row['calls_month'])) ?>
|
||||
<?php } ?>
|
||||
</td>
|
||||
<td>
|
||||
<?php if ($linked) { ?>
|
||||
<div class="d-flex align-items-center gap-2">
|
||||
<span class="itflow-client-avatar d-none d-sm-inline-flex bg-<?= clientInitialsTone($client_id) ?>-subtle text-<?= clientInitialsTone($client_id) ?>-emphasis" aria-hidden="true"><?= escapeHtml(clientInitials($row['client_abbreviation'] ?? '', $row['client_name'])) ?></span>
|
||||
<a class="text-body" href="../agent/client_overview.php?client_id=<?= $client_id ?>"><?= escapeHtml($row['client_name']) ?></a>
|
||||
<?php if ($client_id === $own_client) { ?><span class="badge rounded-pill text-bg-light border fw-normal" title="Your own company - its calls go to whoever is calling">Your company</span><?php } ?>
|
||||
<?php if ($row['client_archived_at'] !== null) { ?><span class="badge rounded-pill text-bg-secondary fw-normal">Archived</span><?php } ?>
|
||||
</div>
|
||||
<?php } elseif ($client_id) { ?>
|
||||
<span class="text-warning-emphasis" title="Its client was deleted in ITFlow - link it again"><i class="fas fa-fw fa-exclamation-triangle me-1"></i>Its client was deleted</span>
|
||||
<?php } else { ?>
|
||||
<div class="d-flex flex-wrap align-items-center column-gap-2 row-gap-1">
|
||||
<span class="text-body-secondary">Not linked</span>
|
||||
<?php if ($suggested && isset($suggested_names[$suggested])) { ?>
|
||||
<a class="btn btn-sm btn-outline-primary" href="post.php?link_phone_suggested=<?= $phone_domain_id ?>&phone_system_id=<?= $phone_system_id ?>&csrf_token=<?= $csrf ?>" data-phone-suggest="<?= $suggested ?>" title="Link it to the client it looks like">
|
||||
<i class="fas fa-fw fa-link me-1"></i><?= escapeHtml($suggested_names[$suggested]) ?>
|
||||
</a>
|
||||
<?php } ?>
|
||||
<a class="btn btn-sm btn-light ajax-modal" href="#" data-modal-url="<?= $link_url ?>"><i class="fas fa-fw fa-search me-1"></i>Pick</a>
|
||||
</div>
|
||||
<?php } ?>
|
||||
</td>
|
||||
<td class="d-none d-lg-table-cell small text-nowrap">
|
||||
<?php if ($row['phone_domain_seen_at']) { ?>
|
||||
<span title="<?= formatDateTime($row['phone_domain_seen_at']) ?>"><?= escapeHtml(timeAgo($row['phone_domain_seen_at'])) ?></span>
|
||||
<?php } else { ?>
|
||||
<span class="text-body-secondary">-</span>
|
||||
<?php } ?>
|
||||
</td>
|
||||
<td class="text-end pe-3">
|
||||
<div class="dropdown dropstart">
|
||||
<button class="btn btn-sm btn-light" title="More actions" type="button" data-bs-toggle="dropdown"><i class="fas fa-fw fa-ellipsis-v"></i></button>
|
||||
<div class="dropdown-menu">
|
||||
<a class="dropdown-item ajax-modal" href="#" data-modal-url="<?= $link_url ?>">
|
||||
<i class="fas fa-fw fa-link text-secondary me-2"></i><?= $client_id ? 'Link to another client' : 'Link to a client' ?>
|
||||
</a>
|
||||
<?php if ($client_id) { ?>
|
||||
<div class="dropdown-divider"></div>
|
||||
<a class="dropdown-item text-danger confirm-link" href="post.php?unlink_phone_domain=<?= $phone_domain_id ?>&csrf_token=<?= $csrf ?>"
|
||||
data-confirm-title="Unlink <?= escapeHtml($row['phone_domain_name']) ?>?" data-confirm-button="Unlink"
|
||||
data-confirm-text="Its calls go to whoever was calling, when ITFlow knows them.">
|
||||
<i class="fas fa-fw fa-unlink me-2"></i>Unlink
|
||||
</a>
|
||||
<?php } ?>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<?php } ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<?php
|
||||
$empty_state = [
|
||||
'thing' => 'domains',
|
||||
'icon' => 'fa-globe',
|
||||
'title' => escapeHtml($show === 'unlinked' ? 'Every domain is linked' : 'No domains yet'),
|
||||
'text' => $show === 'unlinked' ? '' : escapeHtml($system['phone_system_heard_at'] ? "$name hasn't named a domain yet." : "Once $short is set up, each domain shows here with its first call."),
|
||||
'actions' => !$system['phone_system_heard_at'] ? [['label' => 'Setup', 'modal' => $setup_url, 'size' => 'lg', 'icon' => 'fa-plug']] : [],
|
||||
'filtered' => $q !== '',
|
||||
];
|
||||
?>
|
||||
<?php require_once "../includes/filter_footer.php"; ?>
|
||||
</div>
|
||||
|
||||
<?php if (!empty($_GET['setup'])) { ?>
|
||||
<script>
|
||||
// Just added, or a new key - the setup window opens on its own
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
var button = document.querySelector('[data-phone-setup]');
|
||||
if (button) {
|
||||
button.click();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
<?php } ?>
|
||||
|
||||
<?php
|
||||
require_once "../includes/footer.php";
|
||||
181
admin/phone_systems.php
Normal file
181
admin/phone_systems.php
Normal file
@@ -0,0 +1,181 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Admin > Phone Systems - the PBXs that send ITFlow their calls (functions/phone.php), FusionPBX
|
||||
* the first kind. Each row says which it is, how many of its domains are linked to clients, how
|
||||
* many calls came in the last 30 days, and when it was last heard from. Its domains, and how to
|
||||
* set it up, are on its Domains page (phone_domains.php). Add and Edit are one window
|
||||
* (modals/phone/phone_system_edit.php); the rest are admin/post/phone.php.
|
||||
*/
|
||||
|
||||
// Default Column Sortby Filter
|
||||
$sort = "phone_system_name";
|
||||
$order = "ASC";
|
||||
|
||||
require_once "includes/inc_all_admin.php";
|
||||
|
||||
// The tables arrive with the 4.1.4 database update
|
||||
if (!phoneTablesReady()) { ?>
|
||||
<div class="alert alert-warning"><i class="fas fa-fw fa-exclamation-triangle me-2"></i>Phone Systems arrive with a database update - run it in <a href="update.php">Maintenance > Update</a> first.</div>
|
||||
<?php
|
||||
require_once "../includes/footer.php";
|
||||
exit;
|
||||
}
|
||||
|
||||
$kinds = phoneSystemKinds();
|
||||
|
||||
// Only these sort - an unknown column would take the page down
|
||||
$sortable_columns = [
|
||||
'phone_system_name' => "phone_system_name $order",
|
||||
'phone_system_kind' => "phone_system_kind $order, phone_system_name ASC",
|
||||
'domains_linked' => "domains_linked $order, phone_system_name ASC",
|
||||
'calls_month' => "calls_month $order, phone_system_name ASC",
|
||||
'phone_system_heard_at' => "phone_system_heard_at IS NULL, phone_system_heard_at $order, phone_system_name ASC",
|
||||
];
|
||||
if (!isset($sortable_columns[$sort])) {
|
||||
$sort = 'phone_system_name';
|
||||
}
|
||||
|
||||
$sql = mysqli_query($mysqli, "SELECT SQL_CALC_FOUND_ROWS phone_systems.*,
|
||||
(SELECT COUNT(*) FROM phone_domains WHERE phone_domain_system_id = phone_system_id) AS domains_total,
|
||||
(SELECT COUNT(*) FROM phone_domains WHERE phone_domain_system_id = phone_system_id AND phone_domain_client_id > 0) AS domains_linked,
|
||||
(SELECT COUNT(*) FROM phone_calls WHERE phone_call_system_id = phone_system_id AND phone_call_started_at >= NOW() - INTERVAL 30 DAY) AS calls_month
|
||||
FROM phone_systems
|
||||
WHERE (phone_system_name LIKE '%$q%' OR phone_system_web_url LIKE '%$q%' OR phone_system_kind LIKE '%$q%')
|
||||
ORDER BY {$sortable_columns[$sort]}, phone_system_id ASC
|
||||
LIMIT $record_from, $record_to");
|
||||
|
||||
$num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
|
||||
|
||||
$add_url = 'modals/phone/phone_system_add.php';
|
||||
$csrf = $_SESSION['csrf_token'];
|
||||
|
||||
?>
|
||||
|
||||
<div class="card">
|
||||
<div class="card-header bg-dark py-2">
|
||||
<h3 class="card-title mt-2"><i class="fas fa-fw fa-phone-volume me-2"></i>Phone Systems</h3>
|
||||
<div class="card-tools">
|
||||
<button type="button" class="btn btn-primary ajax-modal" data-modal-url="<?= $add_url ?>"><i class="fas fa-plus"></i><span class="d-none d-lg-inline ms-2">New Phone System</span></button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card-header py-3">
|
||||
<form autocomplete="off">
|
||||
<div class="row g-2 align-items-center">
|
||||
<div class="col-lg-5 col-md-6">
|
||||
<div class="input-group">
|
||||
<input type="search" class="form-control" name="q" value="<?php if (isset($q)) { echo stripslashes(escapeHtml($q)); } ?>" placeholder="Search phone systems..." title="Name, kind or address">
|
||||
<button class="btn btn-dark" title="Search"><i class="fas fa-search"></i></button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<div class="table-responsive">
|
||||
<table class="table table-hover align-middle mb-0">
|
||||
<thead class="table-light text-nowrap <?php if ($num_rows[0] == 0) { echo "d-none"; } ?>">
|
||||
<tr>
|
||||
<th class="ps-3"><?= sortLink('phone_system_name', 'Phone system') ?></th>
|
||||
<th class="d-none d-lg-table-cell"><?= sortLink('phone_system_kind', 'Kind') ?></th>
|
||||
<th class="d-none d-md-table-cell"><?= sortLink('domains_linked', 'Domains') ?></th>
|
||||
<th class="d-none d-md-table-cell"><?= sortLink('calls_month', 'Calls, 30 days') ?></th>
|
||||
<th class="d-none d-sm-table-cell"><?= sortLink('phone_system_heard_at', 'Last heard from') ?></th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$phone_system_id = intval($row['phone_system_id']);
|
||||
$name = phoneSystemName($row);
|
||||
$kind = phoneKind($row);
|
||||
$on = intval($row['phone_system_active']) === 1;
|
||||
$domains_url = "phone_domains.php?phone_system_id=$phone_system_id";
|
||||
$edit_url = "modals/phone/phone_system_edit.php?id=$phone_system_id";
|
||||
$setup_url = "modals/phone/phone_system_setup.php?id=$phone_system_id";
|
||||
$linked = intval($row['domains_linked']);
|
||||
$total = intval($row['domains_total']);
|
||||
$suggested = count(phoneDomainSuggestions($phone_system_id));
|
||||
$calls = intval($row['calls_month']);
|
||||
$host = (string) parse_url((string) $row['phone_system_web_url'], PHP_URL_HOST);
|
||||
$web = phoneWebUrl($row);
|
||||
?>
|
||||
<tr data-phone-system-id="<?= $phone_system_id ?>" class="<?= $on ? '' : 'text-body-secondary' ?>">
|
||||
<td class="ps-3">
|
||||
<div class="d-flex align-items-center gap-3">
|
||||
<span class="d-none d-sm-inline-flex itflow-asset-icon itflow-asset-tone-other" aria-hidden="true"><i class="<?= escapeHtml($kind['icon'] ?? 'fas fa-phone-volume') ?>"></i></span>
|
||||
<div class="min-w-0">
|
||||
<a class="fw-semibold text-body" href="<?= $domains_url ?>"><?= escapeHtml($name) ?></a>
|
||||
<?php if (!$kind) { ?>
|
||||
<span class="badge rounded-pill text-bg-warning ms-1" title="This ITFlow doesn't know its kind (<?= escapeHtml($row['phone_system_kind']) ?>) - what it sends is turned away">Unknown kind</span>
|
||||
<?php } elseif (!$on) { ?>
|
||||
<span class="badge rounded-pill text-bg-secondary ms-1" data-phone-system-state="off" title="What it sends is turned away">Off</span>
|
||||
<?php } ?>
|
||||
<div class="small text-body-secondary text-truncate"><span class="d-lg-none"><?= escapeHtml($kind['name'] ?? $row['phone_system_kind']) ?><?= $host !== '' ? ' · ' : '' ?></span><?= escapeHtml($host) ?></div>
|
||||
<div class="d-md-none small text-body-secondary"><?= $linked ?> of <?= $total ?> domains linked · <?= number_format($calls) ?> calls</div>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
<td class="d-none d-lg-table-cell small"><?= escapeHtml($kind['name'] ?? $row['phone_system_kind']) ?></td>
|
||||
<td class="d-none d-md-table-cell small text-nowrap">
|
||||
<a class="text-reset" href="<?= $domains_url ?>"><?= $linked ?> of <?= $total ?> linked</a>
|
||||
<?php if ($suggested) { ?>
|
||||
<div><a class="text-body-secondary" href="<?= $domains_url ?>&show=unlinked" data-phone-suggested="<?= $suggested ?>" title="Your clients they look like - link them on its Domains page"><?= $suggested ?> to link</a></div>
|
||||
<?php } ?>
|
||||
</td>
|
||||
<td class="d-none d-md-table-cell small text-nowrap"><?= number_format($calls) ?></td>
|
||||
<td class="d-none d-sm-table-cell small" data-phone-heard="<?= $row['phone_system_heard_at'] ? 'yes' : 'never' ?>">
|
||||
<?php if ($row['phone_system_heard_at']) { ?>
|
||||
<span class="text-nowrap" title="<?= formatDateTime($row['phone_system_heard_at']) ?>"><i class="fas fa-fw fa-check-circle text-success me-1"></i><?= escapeHtml(timeAgo($row['phone_system_heard_at'])) ?></span>
|
||||
<?php } else { ?>
|
||||
<span class="text-body-secondary">Not yet</span>
|
||||
<div><a class="ajax-modal" href="#" data-modal-size="lg" data-modal-url="<?= $setup_url ?>">Set it up</a></div>
|
||||
<?php } ?>
|
||||
</td>
|
||||
<td class="text-end pe-3">
|
||||
<div class="dropdown dropstart">
|
||||
<button class="btn btn-sm btn-light" title="More actions" type="button" data-bs-toggle="dropdown"><i class="fas fa-fw fa-ellipsis-v"></i></button>
|
||||
<div class="dropdown-menu">
|
||||
<a class="dropdown-item" href="<?= $domains_url ?>"><i class="fas fa-fw fa-globe text-secondary me-2"></i>Domains</a>
|
||||
<?php if ($kind) { ?>
|
||||
<a class="dropdown-item ajax-modal" href="#" data-modal-size="lg" data-modal-url="<?= $setup_url ?>"><i class="fas fa-fw fa-plug text-secondary me-2"></i>Setup</a>
|
||||
<a class="dropdown-item ajax-modal" href="#" data-modal-url="<?= $edit_url ?>"><i class="fas fa-fw fa-edit text-secondary me-2"></i>Edit</a>
|
||||
<?php } ?>
|
||||
<?php if ($web !== '') { ?>
|
||||
<a class="dropdown-item" href="<?= escapeHtml($web) ?>" target="_blank" rel="noopener noreferrer"><i class="fas fa-fw fa-external-link-alt text-secondary me-2"></i>Open <?= escapeHtml($kind['short'] ?? $name) ?></a>
|
||||
<?php } ?>
|
||||
<?php if ($on) { ?>
|
||||
<a class="dropdown-item confirm-link" href="post.php?turn_phone_system_off=<?= $phone_system_id ?>&csrf_token=<?= $csrf ?>"
|
||||
data-confirm-title="Turn <?= escapeHtml($name) ?> off?" data-confirm-button="Turn off"
|
||||
data-confirm-text="What it sends is turned away - calls aren't kept and caller ID gets no names - until it is on again.">
|
||||
<i class="fas fa-fw fa-toggle-off text-secondary me-2"></i>Turn Off
|
||||
</a>
|
||||
<?php } else { ?>
|
||||
<a class="dropdown-item" href="post.php?turn_phone_system_on=<?= $phone_system_id ?>&csrf_token=<?= $csrf ?>"><i class="fas fa-fw fa-toggle-on text-secondary me-2"></i>Turn On</a>
|
||||
<?php } ?>
|
||||
<div class="dropdown-divider"></div>
|
||||
<a class="dropdown-item text-danger confirm-link" href="post.php?delete_phone_system=<?= $phone_system_id ?>&csrf_token=<?= $csrf ?>"
|
||||
data-confirm-title="Delete <?= escapeHtml($name) ?>?" data-confirm-button="Delete"
|
||||
data-confirm-text="Its domains and the calls it sent go with it. Notes already on tickets stay.">
|
||||
<i class="fas fa-fw fa-trash me-2"></i>Delete
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<?php } ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<?php
|
||||
$empty_state = [
|
||||
'thing' => 'phone systems',
|
||||
'icon' => 'fa-phone-volume',
|
||||
'text' => 'Add ' . escapeHtml(implode(' or ', array_column($kinds, 'name'))) . ' and set it up to tell ITFlow about its calls: each call is kept with the contact it was with, and its desk phones show who is calling - nothing is asked of its API or database.',
|
||||
'actions' => [['label' => 'New Phone System', 'modal' => $add_url]],
|
||||
];
|
||||
?>
|
||||
<?php require_once "../includes/filter_footer.php"; ?>
|
||||
</div>
|
||||
|
||||
<?php
|
||||
require_once "../includes/footer.php";
|
||||
167
admin/post/phone.php
Normal file
167
admin/post/phone.php
Normal file
@@ -0,0 +1,167 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Admin > Phone Systems (functions/phone.php) - Add and Edit, turning one off and on, a new key
|
||||
* for its addresses, Delete, and on its Domains page linking its domains to clients - one at a
|
||||
* time, the suggested ones, and unlinking. What changed is in the audit log - never the key.
|
||||
*/
|
||||
|
||||
defined('FROM_POST_HANDLER') || die("Direct file access is not allowed");
|
||||
|
||||
if (isset($_POST['save_phone_system'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
$phone_system_id = intval($_POST['phone_system_id'] ?? 0);
|
||||
$before = $phone_system_id ? phoneSystemRow($phone_system_id) : null;
|
||||
if ($phone_system_id && !$before) {
|
||||
flashAlert('That phone system no longer exists.', 'error');
|
||||
redirect('phone_systems.php');
|
||||
}
|
||||
|
||||
[$after, $errors] = phoneSystemCheck($_POST, $before);
|
||||
if ($errors) {
|
||||
flashAlert('Not saved - ' . escapeHtml(reset($errors)), 'error');
|
||||
redirect();
|
||||
}
|
||||
|
||||
$name = $after['phone_system_name'];
|
||||
if ($before) {
|
||||
$lines = phoneSystemChangeLines($before, $after);
|
||||
if (!$lines) {
|
||||
flashAlert('Nothing changed on <strong>' . escapeHtml($name) . '</strong>', 'info');
|
||||
redirect();
|
||||
}
|
||||
phoneSystemWrite($after, $phone_system_id);
|
||||
logAudit("Phone System", "Edit", "$session_name changed phone system " . escapeSql(phoneSystemName($before) . ': ' . implode('; ', $lines)), 0, $phone_system_id);
|
||||
flashAlert('<strong>' . escapeHtml($name) . '</strong> saved');
|
||||
redirect();
|
||||
}
|
||||
|
||||
$phone_system_id = phoneSystemWrite($after);
|
||||
$kind = phoneSystemKinds()[$after['phone_system_kind']];
|
||||
logAudit("Phone System", "Create", "$session_name added phone system " . escapeSql($name . ($name !== $kind['name'] ? " ({$kind['name']})" : '')), 0, $phone_system_id);
|
||||
flashAlert('<strong>' . escapeHtml($name) . '</strong> created - now set it up to send ITFlow its calls');
|
||||
redirect("phone_domains.php?phone_system_id=$phone_system_id&setup=1");
|
||||
|
||||
}
|
||||
|
||||
// Turn it off - what it sends is turned away - or on again
|
||||
if (isset($_GET['turn_phone_system_off']) || isset($_GET['turn_phone_system_on'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
$on = isset($_GET['turn_phone_system_on']);
|
||||
$system = phoneSystemRow($on ? $_GET['turn_phone_system_on'] : $_GET['turn_phone_system_off']);
|
||||
if (!$system) {
|
||||
flashAlert('That phone system no longer exists.', 'error');
|
||||
redirect('phone_systems.php');
|
||||
}
|
||||
$phone_system_id = intval($system['phone_system_id']);
|
||||
mysqli_query($mysqli, "UPDATE phone_systems SET phone_system_active = " . ($on ? 1 : 0) . " WHERE phone_system_id = $phone_system_id");
|
||||
logAudit("Phone System", "Edit", "$session_name turned phone system " . escapeSql(phoneSystemName($system)) . ($on ? ' on' : ' off'), 0, $phone_system_id);
|
||||
flashAlert('<strong>' . escapeHtml(phoneSystemName($system)) . '</strong> turned ' . ($on ? 'on' : 'off - what it sends is turned away'), $on ? 'success' : 'error');
|
||||
redirect();
|
||||
|
||||
}
|
||||
|
||||
// A new key - its addresses change, and the old ones stop working
|
||||
if (isset($_GET['new_phone_system_key'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
$system = phoneSystemRow($_GET['new_phone_system_key']);
|
||||
if (!$system) {
|
||||
flashAlert('That phone system no longer exists.', 'error');
|
||||
redirect('phone_systems.php');
|
||||
}
|
||||
$phone_system_id = intval($system['phone_system_id']);
|
||||
mysqli_query($mysqli, "UPDATE phone_systems SET phone_system_key = '" . escapeSql(phoneSystemNewKey()) . "' WHERE phone_system_id = $phone_system_id");
|
||||
logAudit("Phone System", "Edit", "$session_name made a new key for phone system " . escapeSql(phoneSystemName($system)), 0, $phone_system_id);
|
||||
flashAlert('<strong>' . escapeHtml(phoneSystemName($system)) . '</strong> has a new key - put its new addresses in FusionPBX; the old ones no longer work', 'warning');
|
||||
redirect("phone_domains.php?phone_system_id=$phone_system_id&setup=1");
|
||||
|
||||
}
|
||||
|
||||
if (isset($_GET['delete_phone_system'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
$system = phoneSystemRow($_GET['delete_phone_system']);
|
||||
if (!$system) {
|
||||
flashAlert('That phone system no longer exists.', 'error');
|
||||
redirect('phone_systems.php');
|
||||
}
|
||||
$phone_system_id = intval($system['phone_system_id']);
|
||||
$calls = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FROM phone_calls WHERE phone_call_system_id = $phone_system_id"))[0]);
|
||||
foreach (['phone_calls' => 'phone_call_system_id', 'phone_devices' => 'phone_device_system_id', 'phone_events' => 'phone_event_system_id', 'phone_domains' => 'phone_domain_system_id'] as $table => $column) {
|
||||
mysqli_query($mysqli, "DELETE FROM $table WHERE $column = $phone_system_id");
|
||||
}
|
||||
mysqli_query($mysqli, "DELETE FROM phone_systems WHERE phone_system_id = $phone_system_id");
|
||||
logAudit("Phone System", "Delete", "$session_name deleted phone system " . escapeSql(phoneSystemName($system)) . " and its $calls call" . ($calls === 1 ? '' : 's'), 0, $phone_system_id);
|
||||
flashAlert('<strong>' . escapeHtml(phoneSystemName($system)) . '</strong> deleted', 'error');
|
||||
redirect('phone_systems.php');
|
||||
|
||||
}
|
||||
|
||||
// A domain linked to a client - from its window
|
||||
if (isset($_POST['link_phone_domain'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
$domain = phoneDomainRow($_POST['phone_domain_id'] ?? 0);
|
||||
$client_id = intval($_POST['client_id'] ?? 0);
|
||||
$client_name = $client_id ? (string) getFieldById('clients', $client_id, 'client_name') : '';
|
||||
if (!$domain || $client_name === '') {
|
||||
flashAlert(!$domain ? 'That domain is gone.' : 'Pick a client.', 'error');
|
||||
redirect();
|
||||
}
|
||||
phoneDomainLink(intval($domain['phone_domain_id']), $client_id);
|
||||
logAudit("Phone System", "Edit", "$session_name linked " . escapeSql(phoneSystemName($domain)) . " domain " . escapeSql($domain['phone_domain_name']) . " to client " . escapeSql($client_name), $client_id, intval($domain['phone_system_id']));
|
||||
flashAlert('<strong>' . escapeHtml($domain['phone_domain_name']) . '</strong> linked to <strong>' . escapeHtml($client_name) . '</strong>');
|
||||
redirect();
|
||||
|
||||
}
|
||||
|
||||
// The suggested link for one domain, or for all of them
|
||||
if (isset($_GET['link_phone_suggested'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
$system = phoneSystemRow($_GET['phone_system_id'] ?? 0);
|
||||
if (!$system) {
|
||||
flashAlert('That phone system no longer exists.', 'error');
|
||||
redirect('phone_systems.php');
|
||||
}
|
||||
$suggestions = phoneDomainSuggestions(intval($system['phone_system_id']));
|
||||
$which = (string) $_GET['link_phone_suggested'];
|
||||
if ($which !== 'all') {
|
||||
$suggestions = array_intersect_key($suggestions, [intval($which) => true]);
|
||||
}
|
||||
foreach ($suggestions as $domain_id => $client_id) {
|
||||
$domain = phoneDomainRow($domain_id);
|
||||
phoneDomainLink($domain_id, $client_id);
|
||||
logAudit("Phone System", "Edit", "$session_name linked " . escapeSql(phoneSystemName($system)) . " domain " . escapeSql($domain['phone_domain_name']) . " to client " . escapeSql((string) getFieldById('clients', $client_id, 'client_name')), $client_id, intval($system['phone_system_id']));
|
||||
}
|
||||
$count = count($suggestions);
|
||||
flashAlert($count ? "$count domain" . ($count === 1 ? '' : 's') . " linked" : 'Nothing to link', $count ? 'success' : 'info');
|
||||
redirect();
|
||||
|
||||
}
|
||||
|
||||
if (isset($_GET['unlink_phone_domain'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
$domain = phoneDomainRow($_GET['unlink_phone_domain']);
|
||||
if (!$domain) {
|
||||
flashAlert('That domain is gone.', 'error');
|
||||
redirect();
|
||||
}
|
||||
$client_id = intval($domain['phone_domain_client_id']);
|
||||
phoneDomainLink(intval($domain['phone_domain_id']), 0);
|
||||
logAudit("Phone System", "Edit", "$session_name unlinked " . escapeSql(phoneSystemName($domain)) . " domain " . escapeSql($domain['phone_domain_name']) . " from client " . escapeSql((string) $domain['client_name']), $client_id, intval($domain['phone_system_id']));
|
||||
flashAlert('<strong>' . escapeHtml($domain['phone_domain_name']) . '</strong> unlinked', 'error');
|
||||
redirect();
|
||||
|
||||
}
|
||||
257
agent/calls.php
Normal file
257
agent/calls.php
Normal file
@@ -0,0 +1,257 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Calls - every call the phone systems sent (functions/phone.php, Admin > Phone Systems), or one
|
||||
* client's, or one contact's (contact_id). Each row is who it was with - the contact, else the
|
||||
* caller ID name or number - the extension and agent on this side, when, how long they talked and
|
||||
* how it ended, and the ticket it went on. Add to Ticket puts it on one of the client's open
|
||||
* tickets, or a new one, as an internal note with its talk time
|
||||
* (modals/phone/phone_call_ticket.php, agent/post/phone.php).
|
||||
*/
|
||||
|
||||
// Default Column Sortby Filter
|
||||
$sort = "phone_call_started_at";
|
||||
$order = "DESC";
|
||||
|
||||
// If client_id is in URI then show client Side Bar and client header
|
||||
if (isset($_GET['client_id'])) {
|
||||
require_once "includes/inc_all_client.php";
|
||||
$client_query = "AND phone_call_client_id = $client_id";
|
||||
$client_url = "client_id=$client_id&";
|
||||
} else {
|
||||
require_once "includes/inc_all.php";
|
||||
$client_query = '';
|
||||
$client_url = '';
|
||||
}
|
||||
|
||||
enforceUserPermission('module_support');
|
||||
|
||||
$page_title = 'Calls';
|
||||
|
||||
if (!phoneSystemsExist()) { ?>
|
||||
<div class="card">
|
||||
<div class="card-body text-center text-body-secondary py-5">
|
||||
<i class="fas fa-phone-volume fa-2x mb-2 d-block opacity-50"></i>
|
||||
No phone system is set up - an admin adds one in Admin > Integrations > Phone Systems.
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
require_once "../includes/footer.php";
|
||||
exit;
|
||||
}
|
||||
|
||||
$can_edit = lookupUserPermission('module_support') >= 2;
|
||||
$statuses = phoneCallStatuses();
|
||||
$directions = phoneCallDirections();
|
||||
|
||||
// One contact's calls - from their page
|
||||
$contact_filter = intval($_GET['contact_id'] ?? 0);
|
||||
$contact_filter_name = '';
|
||||
if ($contact_filter) {
|
||||
$contact_row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT contact_name, contact_client_id FROM contacts WHERE contact_id = $contact_filter"));
|
||||
if ($contact_row && hasClientAccess($contact_row['contact_client_id'])) {
|
||||
$contact_filter_name = (string) $contact_row['contact_name'];
|
||||
} else {
|
||||
$contact_filter = 0;
|
||||
}
|
||||
}
|
||||
$contact_query = $contact_filter ? "AND phone_call_contact_id = $contact_filter" : '';
|
||||
|
||||
// The views - all of them, each way, missed ones, and callers nobody knows
|
||||
$shows = [
|
||||
'' => ['label' => 'All', 'icon' => 'fa-list', 'sql' => '1 = 1'],
|
||||
'inbound' => ['label' => 'Inbound', 'icon' => 'fa-arrow-down', 'sql' => "phone_call_direction = 'inbound'"],
|
||||
'outbound' => ['label' => 'Outbound', 'icon' => 'fa-arrow-up', 'sql' => "phone_call_direction = 'outbound'"],
|
||||
'missed' => ['label' => 'Missed', 'icon' => 'fa-phone-slash', 'sql' => "phone_call_direction = 'inbound' AND phone_call_status IN ('missed', 'voicemail')"],
|
||||
'unknown' => ['label' => 'Unknown', 'icon' => 'fa-question', 'sql' => "phone_call_direction <> 'local' AND phone_call_contact_id = 0", 'title' => 'With numbers that aren\'t a contact\'s'],
|
||||
];
|
||||
$show = isset($shows[$_GET['show'] ?? '']) ? (string) ($_GET['show'] ?? '') : '';
|
||||
|
||||
$scope = clientScopeSql('phone_call_client_id') . " $client_query $contact_query";
|
||||
$view_counts = [];
|
||||
foreach ($shows as $key => $view) {
|
||||
$view_counts[$key] = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FROM phone_calls WHERE {$view['sql']} $scope"))[0]);
|
||||
}
|
||||
$filter_url = function (array $change) {
|
||||
$get = array_merge($_GET, $change);
|
||||
unset($get['page']);
|
||||
$get = array_filter($get, function ($value) {
|
||||
return $value !== null && $value !== '';
|
||||
});
|
||||
return '?' . http_build_query($get);
|
||||
};
|
||||
|
||||
// Only these sort - an unknown column would take the page down
|
||||
$sortable_columns = [
|
||||
'phone_call_started_at' => "phone_call_started_at $order",
|
||||
'who' => "COALESCE(contact_name, NULLIF(phone_call_name, ''), phone_call_number) $order, phone_call_started_at DESC",
|
||||
'client_name' => "client_name IS NULL, client_name $order, phone_call_started_at DESC",
|
||||
'phone_call_extension' => "phone_call_extension = '', phone_call_extension $order, phone_call_started_at DESC",
|
||||
'phone_call_talk_seconds' => "phone_call_talk_seconds $order, phone_call_started_at DESC",
|
||||
];
|
||||
if (!isset($sortable_columns[$sort])) {
|
||||
$sort = 'phone_call_started_at';
|
||||
}
|
||||
|
||||
$phone_digits = preg_replace('/\D/', '', (string) $q);
|
||||
$number_search = strlen($phone_digits) >= 3 ? " OR phone_call_number LIKE '%" . escapeSql($phone_digits) . "%' OR phone_call_dialed LIKE '%" . escapeSql($phone_digits) . "%'" : '';
|
||||
|
||||
$sql = mysqli_query($mysqli, "SELECT SQL_CALC_FOUND_ROWS phone_calls.*, phone_system_kind, phone_system_name, phone_system_web_url,
|
||||
contact_name, client_name, ticket_prefix, ticket_number, ticket_subject, user_name
|
||||
FROM phone_calls
|
||||
LEFT JOIN phone_systems ON phone_system_id = phone_call_system_id
|
||||
LEFT JOIN contacts ON contact_id = phone_call_contact_id AND phone_call_contact_id > 0
|
||||
LEFT JOIN clients ON client_id = phone_call_client_id AND phone_call_client_id > 0
|
||||
LEFT JOIN tickets ON ticket_id = phone_call_ticket_id AND phone_call_ticket_id > 0
|
||||
LEFT JOIN users ON users.user_id = phone_call_user_id AND phone_call_user_id > 0
|
||||
WHERE {$shows[$show]['sql']} $scope
|
||||
AND (contact_name LIKE '%$q%' OR phone_call_name LIKE '%$q%' OR client_name LIKE '%$q%' OR phone_call_extension LIKE '%$q%' OR user_name LIKE '%$q%'$number_search)
|
||||
ORDER BY {$sortable_columns[$sort]}, phone_call_id DESC
|
||||
LIMIT $record_from, $record_to");
|
||||
|
||||
$num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
|
||||
|
||||
?>
|
||||
|
||||
<div class="card">
|
||||
<div class="card-header py-2">
|
||||
<h3 class="card-title mt-2"><i class="fas fa-fw fa-phone-volume me-2"></i>Calls
|
||||
<?php if ($contact_filter_name !== '') { ?>
|
||||
<span class="small text-body-secondary fw-normal ms-1">with <?= escapeHtml($contact_filter_name) ?></span>
|
||||
<a class="small ms-2" href="<?= escapeHtml($filter_url(['contact_id' => null])) ?>" title="Every call"><i class="fas fa-times"></i></a>
|
||||
<?php } ?>
|
||||
</h3>
|
||||
</div>
|
||||
<div class="card-header py-3">
|
||||
<form autocomplete="off">
|
||||
<?php if ($client_url) { ?><input type="hidden" name="client_id" value="<?= $client_id ?>"><?php } ?>
|
||||
<?php if ($contact_filter) { ?><input type="hidden" name="contact_id" value="<?= $contact_filter ?>"><?php } ?>
|
||||
<input type="hidden" name="show" value="<?= escapeHtml($show) ?>">
|
||||
<div class="row g-2 align-items-center">
|
||||
<div class="col-lg-5 col-md-6">
|
||||
<div class="input-group">
|
||||
<input type="search" class="form-control" name="q" value="<?php if (isset($q)) { echo stripslashes(escapeHtml($q)); } ?>" placeholder="Search calls..." title="Contact, caller ID name, number, client, extension or agent">
|
||||
<button class="btn btn-dark" title="Search"><i class="fas fa-search"></i></button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
<nav class="nav nav-pills itflow-asset-tabs mt-2 flex-nowrap overflow-auto">
|
||||
<?php foreach ($shows as $key => $view) { ?>
|
||||
<a class="nav-link text-nowrap <?= $show === $key ? 'active' : '' ?>" href="<?= escapeHtml($filter_url(['show' => $key === '' ? null : $key])) ?>"<?= isset($view['title']) ? ' title="' . escapeHtml($view['title']) . '"' : '' ?>>
|
||||
<i class="fas fa-fw <?= $view['icon'] ?> me-1"></i><?= escapeHtml($view['label']) ?> <span class="badge rounded-pill ms-1"><?= number_format(intval($view_counts[$key])) ?></span>
|
||||
</a>
|
||||
<?php } ?>
|
||||
</nav>
|
||||
</div>
|
||||
|
||||
<div class="table-responsive">
|
||||
<table class="table table-hover align-middle mb-0">
|
||||
<thead class="table-light text-nowrap <?php if ($num_rows[0] == 0) { echo "d-none"; } ?>">
|
||||
<tr>
|
||||
<th class="ps-3"><?= sortLink('who', 'Call') ?></th>
|
||||
<?php if (!$client_url) { ?><th class="d-none d-md-table-cell"><?= sortLink('client_name', 'Client') ?></th><?php } ?>
|
||||
<th class="d-none d-xl-table-cell"><?= sortLink('phone_call_extension', 'Taken by') ?></th>
|
||||
<th><?= sortLink('phone_call_started_at', 'When') ?></th>
|
||||
<th class="d-none d-sm-table-cell text-end"><?= sortLink('phone_call_talk_seconds', 'Talked') ?></th>
|
||||
<th class="d-none d-lg-table-cell">Ticket</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$phone_call_id = intval($row['phone_call_id']);
|
||||
$call_client_id = intval($row['phone_call_client_id']);
|
||||
$contact_id = intval($row['phone_call_contact_id']);
|
||||
[$status_label, $status_icon, $status_tone] = $statuses[$row['phone_call_status']] ?? [ucfirst((string) $row['phone_call_status']), 'fa-phone', 'secondary'];
|
||||
[$direction_label, $direction_icon] = $directions[$row['phone_call_direction']] ?? ['Call', 'fa-phone'];
|
||||
$who = phoneCallWho($row);
|
||||
$number = phoneNumberText($row['phone_call_number']);
|
||||
$answered = $row['phone_call_status'] === 'answered';
|
||||
$call_web = $row['phone_system_kind'] !== null ? phoneCallWebUrl($row, (string) $row['phone_call_uuid']) : '';
|
||||
$ticket_id = intval($row['phone_call_ticket_id']);
|
||||
?>
|
||||
<tr data-phone-call-id="<?= $phone_call_id ?>">
|
||||
<td class="ps-3">
|
||||
<div class="d-flex align-items-center gap-3">
|
||||
<span class="itflow-phone-call-icon text-<?= $answered ? 'success' : $status_tone ?>-emphasis bg-<?= $answered ? 'success' : $status_tone ?>-subtle" title="<?= escapeHtml("$direction_label - $status_label") ?>">
|
||||
<i class="fas fa-fw <?= $answered ? $direction_icon : $status_icon ?>"></i>
|
||||
</span>
|
||||
<div class="min-w-0">
|
||||
<?php if ($contact_id && $row['contact_name'] !== null) { ?>
|
||||
<a class="fw-semibold" href="contact.php?client_id=<?= $call_client_id ?>&contact_id=<?= $contact_id ?>"><?= escapeHtml($who) ?></a>
|
||||
<?php } else { ?>
|
||||
<span class="fw-semibold"><?= escapeHtml($who) ?></span>
|
||||
<?php } ?>
|
||||
<div class="small text-body-secondary text-truncate">
|
||||
<?= escapeHtml($direction_label) ?><?php if ($number !== '' && $number !== $who) { ?> · <?= escapeHtml($number) ?><?php } ?>
|
||||
<?php if (!$answered) { ?>· <span class="text-<?= $status_tone ?>-emphasis"><?= escapeHtml($status_label) ?></span><?php } ?>
|
||||
<?php if ($row['phone_call_extension'] !== '') { ?><span class="d-xl-none"> · <?= escapeHtml($row['user_name'] ?? phoneNumberText($row['phone_call_extension'])) ?></span><?php } ?>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
<?php if (!$client_url) { ?>
|
||||
<td class="d-none d-md-table-cell small">
|
||||
<?php if ($call_client_id && $row['client_name'] !== null) { ?>
|
||||
<a class="text-reset d-block text-truncate" style="max-width: 14rem" href="calls.php?client_id=<?= $call_client_id ?>" title="<?= escapeHtml($row['client_name']) ?>"><?= escapeHtml($row['client_name']) ?></a>
|
||||
<?php } else { ?>
|
||||
<span class="text-body-secondary">-</span>
|
||||
<?php } ?>
|
||||
</td>
|
||||
<?php } ?>
|
||||
<td class="d-none d-xl-table-cell small">
|
||||
<?php if ($row['user_name'] !== null) { ?>
|
||||
<div><?= escapeHtml($row['user_name']) ?></div>
|
||||
<?php } ?>
|
||||
<div class="text-body-secondary"><?= $row['phone_call_extension'] !== '' ? escapeHtml(phoneNumberText($row['phone_call_extension'])) : '-' ?></div>
|
||||
</td>
|
||||
<td class="small text-nowrap">
|
||||
<span title="<?= formatDateTime($row['phone_call_started_at']) ?>"><?= escapeHtml(timeAgo($row['phone_call_started_at'])) ?></span>
|
||||
<div class="text-body-secondary"><?= escapeHtml(date('M j, g:i a', strtotime($row['phone_call_started_at']))) ?></div>
|
||||
</td>
|
||||
<td class="d-none d-sm-table-cell small text-end text-nowrap"><?= $answered ? escapeHtml(phoneSecondsText($row['phone_call_talk_seconds'])) : '<span class="text-body-secondary">-</span>' ?></td>
|
||||
<td class="d-none d-lg-table-cell small text-nowrap">
|
||||
<?php if ($ticket_id && $row['ticket_number'] !== null) { ?>
|
||||
<a href="ticket.php?client_id=<?= $call_client_id ?>&ticket_id=<?= $ticket_id ?>" title="<?= escapeHtml((string) $row['ticket_subject']) ?>"><?= escapeHtml($row['ticket_prefix'] . $row['ticket_number']) ?></a>
|
||||
<?php } else { ?>
|
||||
<span class="text-body-secondary">-</span>
|
||||
<?php } ?>
|
||||
</td>
|
||||
<td class="text-end pe-3">
|
||||
<?php if (($can_edit && $row['phone_call_direction'] !== 'local') || $call_web !== '') { ?>
|
||||
<div class="dropdown dropstart">
|
||||
<button class="btn btn-sm btn-light" title="More actions" type="button" data-bs-toggle="dropdown"><i class="fas fa-fw fa-ellipsis-v"></i></button>
|
||||
<div class="dropdown-menu">
|
||||
<?php if ($can_edit && $row['phone_call_direction'] !== 'local') { ?>
|
||||
<a class="dropdown-item ajax-modal" href="#" data-modal-url="modals/phone/phone_call_ticket.php?id=<?= $phone_call_id ?>"><i class="fas fa-fw fa-life-ring text-secondary me-2"></i>Add to Ticket</a>
|
||||
<?php } ?>
|
||||
<?php if ($call_web !== '') { ?>
|
||||
<a class="dropdown-item" href="<?= escapeHtml($call_web) ?>" target="_blank" rel="noopener noreferrer"><i class="fas fa-fw fa-external-link-alt text-secondary me-2"></i>Open in <?= escapeHtml(phoneKind($row)['short'] ?? 'the phone system') ?></a>
|
||||
<?php } ?>
|
||||
</div>
|
||||
</div>
|
||||
<?php } ?>
|
||||
</td>
|
||||
</tr>
|
||||
<?php } ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<?php
|
||||
$empty_state = [
|
||||
'thing' => 'calls',
|
||||
'icon' => 'fa-phone-volume',
|
||||
'title' => $show === 'missed' ? 'No missed calls' : null,
|
||||
'text' => $show === '' ? 'Calls come in as the phone system sends them - each one with the contact it was with.' : '',
|
||||
'filtered' => $q !== '',
|
||||
];
|
||||
$empty_state = array_filter($empty_state, function ($value) {
|
||||
return $value !== null;
|
||||
});
|
||||
?>
|
||||
<?php require_once "../includes/filter_footer.php"; ?>
|
||||
</div>
|
||||
|
||||
<?php
|
||||
require_once "../includes/footer.php";
|
||||
@@ -176,10 +176,14 @@ while ($row = mysqli_fetch_assoc($sql_note_type_icons)) {
|
||||
// Its history, from the audit log
|
||||
$history = auditLogHistory('Contact', $contact_id);
|
||||
|
||||
// Their calls from the phone systems (functions/phone.php) - the latest, with how many there are
|
||||
$contact_calls = $can_support && phoneSystemsExist() ? phoneCallsFor("phone_call_contact_id = $contact_id", 15) : null;
|
||||
|
||||
$contact_tabs = [
|
||||
'assets' => ['label' => 'Assets', 'icon' => 'fa-desktop', 'count' => $asset_count],
|
||||
'credentials' => ['label' => 'Credentials', 'icon' => 'fa-key', 'count' => $credential_count],
|
||||
'tickets' => ['label' => 'Tickets', 'icon' => 'fa-life-ring', 'count' => $ticket_count, 'badge' => $ticket_open_count],
|
||||
'calls' => ['label' => 'Calls', 'icon' => 'fa-phone-volume', 'count' => $contact_calls['count'] ?? 0],
|
||||
'recurring' => ['label' => 'Recurring', 'icon' => 'fa-redo-alt', 'count' => $recurring_ticket_count],
|
||||
'licenses' => ['label' => 'Licenses', 'icon' => 'fa-cube', 'count' => $software_count],
|
||||
'documents' => ['label' => 'Documents', 'icon' => 'fa-file-alt', 'count' => $document_count],
|
||||
@@ -191,6 +195,9 @@ $contact_tabs = [
|
||||
if (!$can_credentials) {
|
||||
unset($contact_tabs['credentials']);
|
||||
}
|
||||
if ($contact_calls === null) {
|
||||
unset($contact_tabs['calls']);
|
||||
}
|
||||
if (!$can_support) {
|
||||
// Assets and documentation are Support's too
|
||||
unset($contact_tabs['assets'], $contact_tabs['tickets'], $contact_tabs['recurring'], $contact_tabs['licenses'],
|
||||
@@ -617,6 +624,23 @@ if (!$can_support) {
|
||||
<?php } ?>
|
||||
</div>
|
||||
|
||||
<?php if ($contact_calls !== null) { ?>
|
||||
<!-- Calls from the phone systems -->
|
||||
<div class="tab-pane fade" id="contact-pane-calls" role="tabpanel">
|
||||
<div class="itflow-asset-pane-bar">
|
||||
<span class="text-body-secondary small">Calls with them, newest first.</span>
|
||||
<?php if ($contact_calls['count'] > count($contact_calls['rows'])) { ?>
|
||||
<a class="btn btn-sm btn-light" href="calls.php?client_id=<?= $client_id ?>&contact_id=<?= $contact_id ?>"><i class="fas fa-list me-2"></i>All <?= number_format($contact_calls['count']) ?></a>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<?php if ($contact_calls['rows']) { ?>
|
||||
<?= phoneCallsTableHtml($contact_calls['rows'], $can_support_edit) ?>
|
||||
<?php } else { ?>
|
||||
<div class="itflow-asset-empty">No calls yet.</div>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
<!-- Recurring tickets -->
|
||||
<div class="tab-pane fade" id="contact-pane-recurring" role="tabpanel">
|
||||
<div class="itflow-asset-pane-bar">
|
||||
|
||||
@@ -202,6 +202,21 @@
|
||||
</a>
|
||||
</li>
|
||||
|
||||
<?php // Calls from the phone systems (functions/phone.php) - once there are some, or while open ?>
|
||||
<?php if (!empty($num_phone_calls) || basename($_SERVER["PHP_SELF"]) == "calls.php") { ?>
|
||||
<li class="nav-item">
|
||||
<a href="/agent/calls.php?client_id=<?= $client_id ?>" class="nav-link <?php if (basename($_SERVER["PHP_SELF"]) == "calls.php") { echo "active"; } ?>">
|
||||
<i class="nav-icon fas fa-phone-volume"></i>
|
||||
<p>
|
||||
Calls
|
||||
<?php if (!empty($num_phone_calls)) { ?>
|
||||
<span class="right badge rounded-pill text-light" title="Calls in the last 30 days"><?= number_format(intval($num_phone_calls)) ?></span>
|
||||
<?php } ?>
|
||||
</p>
|
||||
</a>
|
||||
</li>
|
||||
<?php } ?>
|
||||
|
||||
<?php } ?>
|
||||
|
||||
<?php if ($config_module_enable_itdoc == 1) { ?>
|
||||
|
||||
@@ -279,6 +279,9 @@ if (isset($_GET['client_id'])) {
|
||||
// The trips this agent may see - all of them with Financial access, else their own
|
||||
[$num_trips] = tripTotalsFor('trip_client_id', $client_id);
|
||||
|
||||
// Calls from the phone systems in the last 30 days (functions/phone.php) - the menu shows Calls once there are any
|
||||
$num_phone_calls = phoneSystemsExist() ? intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FROM phone_calls WHERE phone_call_client_id = $client_id AND phone_call_started_at >= NOW() - INTERVAL 30 DAY"))[0]) : 0;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -131,6 +131,20 @@
|
||||
</a>
|
||||
</li>
|
||||
<?php } ?>
|
||||
<?php if (phoneSystemsExist()) { ?>
|
||||
<li class="nav-item">
|
||||
<a href="/agent/calls.php" class="nav-link <?php if (basename($_SERVER["PHP_SELF"]) == "calls.php") { echo "active"; } ?>">
|
||||
<i class="nav-icon fas fa-phone-volume"></i>
|
||||
<p>
|
||||
Calls
|
||||
<?php $nav_calls_missed = phoneMissedToday(); ?>
|
||||
<?php if ($nav_calls_missed) { ?>
|
||||
<span class="right badge rounded-pill text-bg-danger" title="Missed today and not on a ticket" data-nav-calls-missed><?= number_format($nav_calls_missed) ?></span>
|
||||
<?php } ?>
|
||||
</p>
|
||||
</a>
|
||||
</li>
|
||||
<?php } ?>
|
||||
<li class="nav-item">
|
||||
<a href="/agent/recurring_tickets.php" class="nav-link <?php if (basename($_SERVER["PHP_SELF"]) == "recurring_tickets.php") { echo "active"; } ?>">
|
||||
<i class="nav-icon fas fa-redo-alt"></i>
|
||||
|
||||
109
agent/modals/phone/phone_call_ticket.php
Normal file
109
agent/modals/phone/phone_call_ticket.php
Normal file
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Calls - Add to Ticket: one of the call's client's open tickets (the contact's first), with an
|
||||
* internal note saying what the call was and, ticked, its talk time as the time worked - rounded
|
||||
* up to the minute (phoneCallToTicket(), functions/phone.php; saved by phone_call_ticket in
|
||||
* agent/post/phone.php). New Ticket opens New Ticket with the call filled in, and puts the call on
|
||||
* the ticket it makes.
|
||||
*/
|
||||
|
||||
require_once '../../../includes/modal_header.php';
|
||||
|
||||
enforceUserPermission('module_support', 2);
|
||||
|
||||
$call = phoneCallRow($_GET['id'] ?? 0);
|
||||
if (!$call || (phoneCallClientFor($call) && !hasClientAccess($call['phone_call_client_id']))) {
|
||||
echo json_encode(['error' => 'That call is gone.']);
|
||||
exit;
|
||||
}
|
||||
$phone_call_id = intval($call['phone_call_id']);
|
||||
// A caller nobody knows can go on any client's ticket
|
||||
$client_id = phoneCallClientFor($call);
|
||||
$contact_id = intval($call['phone_call_contact_id']);
|
||||
$talk = $call['phone_call_status'] === 'answered' ? intval($call['phone_call_talk_seconds']) : 0;
|
||||
$sentence = phoneCallSentence($call);
|
||||
|
||||
// The open tickets it could go on - the client's, the contact's first; with no client, any the agent can reach
|
||||
$tickets = [];
|
||||
$where = $client_id ? "ticket_client_id = $client_id" : "1 = 1" . clientScopeSql('ticket_client_id');
|
||||
$sql = mysqli_query($mysqli, "SELECT ticket_id, ticket_prefix, ticket_number, ticket_subject, ticket_contact_id, ticket_updated_at, ticket_created_at, client_name
|
||||
FROM tickets
|
||||
LEFT JOIN clients ON client_id = ticket_client_id
|
||||
WHERE $where AND ticket_closed_at IS NULL AND ticket_resolved_at IS NULL
|
||||
ORDER BY ticket_contact_id = $contact_id DESC, COALESCE(ticket_updated_at, ticket_created_at) DESC
|
||||
LIMIT 200");
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$tickets[] = $row;
|
||||
}
|
||||
$picked = intval($call['phone_call_ticket_id']) ?: (($tickets && $contact_id && intval($tickets[0]['ticket_contact_id']) === $contact_id) ? intval($tickets[0]['ticket_id']) : 0);
|
||||
|
||||
// New Ticket, with the call in it
|
||||
$who = phoneCallWho($call);
|
||||
$new_ticket_url = 'modals/ticket/ticket_add.php?' . http_build_query(array_filter([
|
||||
'client_id' => $client_id ?: null,
|
||||
'contact_id' => $contact_id ?: null,
|
||||
'subject' => ($call['phone_call_direction'] === 'outbound' ? 'Call to ' : 'Call from ') . $who,
|
||||
'details' => $sentence,
|
||||
'phone_call_id' => $phone_call_id,
|
||||
]));
|
||||
|
||||
ob_start();
|
||||
|
||||
?>
|
||||
<div class="modal-header bg-dark">
|
||||
<h5 class="modal-title"><i class="fas fa-fw fa-life-ring me-2"></i>Add Call to Ticket</h5>
|
||||
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</div>
|
||||
<form action="post.php" method="post" autocomplete="off">
|
||||
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
|
||||
<input type="hidden" name="phone_call_id" value="<?= $phone_call_id ?>">
|
||||
|
||||
<div class="modal-body">
|
||||
<p class="mb-3"><i class="fas fa-fw fa-phone-volume text-secondary me-1"></i><?= escapeHtml($sentence) ?> <span class="small text-body-secondary"><?= formatDateTime($call['phone_call_started_at']) ?></span></p>
|
||||
|
||||
<?php if (intval($call['phone_call_ticket_id']) && $call['ticket_number'] !== null) { ?>
|
||||
<div class="alert alert-info small py-2"><i class="fas fa-fw fa-info-circle me-1"></i>It is on <?= escapeHtml($call['ticket_prefix'] . $call['ticket_number']) ?> already - adding it again puts another note there.</div>
|
||||
<?php } ?>
|
||||
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Ticket <strong class="text-danger">*</strong></label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fas fa-fw fa-life-ring"></i></span>
|
||||
<select class="form-select select2" name="ticket_id" required>
|
||||
<option value="">- Select an open ticket -</option>
|
||||
<?php foreach ($tickets as $ticket) {
|
||||
$ticket_id = intval($ticket['ticket_id']); ?>
|
||||
<option value="<?= $ticket_id ?>" <?= $ticket_id === $picked ? 'selected' : '' ?><?= selectOptionData(['icon' => 'fas fa-life-ring', 'description' => ($client_id ? '' : $ticket['client_name'] . ' - ') . (intval($ticket['ticket_contact_id']) === $contact_id && $contact_id ? 'their ticket' : 'updated ' . timeAgo($ticket['ticket_updated_at'] ?: $ticket['ticket_created_at']))]) ?>><?= escapeHtml($ticket['ticket_prefix'] . $ticket['ticket_number'] . ' - ' . $ticket['ticket_subject']) ?></option>
|
||||
<?php } ?>
|
||||
</select>
|
||||
</div>
|
||||
<?php if (!$tickets) { ?>
|
||||
<div class="form-text"><?= $client_id ? 'This client has no open tickets' : 'There are no open tickets' ?> - New Ticket makes one with the call on it.</div>
|
||||
<?php } ?>
|
||||
</div>
|
||||
|
||||
<?php if ($talk > 0) { ?>
|
||||
<div class="form-check form-switch mb-3">
|
||||
<input type="checkbox" class="form-check-input" name="count_time" value="1" id="phoneCallCountTime" checked>
|
||||
<label class="form-check-label" for="phoneCallCountTime">Count the call as time worked <span class="text-body-secondary">- <?= escapeHtml(phoneSecondsText($talk)) ?>, as <?= escapeHtml(formatDuration(phoneTimeWorked($talk))) ?></span></label>
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
<div>
|
||||
<label class="form-label" for="phoneCallNote">Note</label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fas fa-fw fa-sticky-note"></i></span>
|
||||
<textarea class="form-control" name="note" id="phoneCallNote" rows="3" maxlength="5000" placeholder="What the call was about"></textarea>
|
||||
</div>
|
||||
<div class="form-text">An internal note on the ticket says what the call was - this goes with it.</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-outline-primary me-auto ajax-modal" data-modal-size="lg" data-modal-url="<?= escapeHtml($new_ticket_url) ?>"><i class="fas fa-plus me-2"></i>New Ticket</button>
|
||||
<button type="submit" name="phone_call_ticket" class="btn btn-primary fw-bold"><i class="fas fa-check me-2"></i>Add to Ticket</button>
|
||||
<button type="button" class="btn btn-light" data-bs-dismiss="modal"><i class="fas fa-times me-2"></i>Cancel</button>
|
||||
</div>
|
||||
</form>
|
||||
<?php
|
||||
require_once '../../../includes/modal_footer.php';
|
||||
@@ -40,6 +40,13 @@ $prefill_subject = mb_substr(trim((string) ($_GET['subject'] ?? '')), 0, 500);
|
||||
$prefill_details = trim((string) ($_GET['details'] ?? ''));
|
||||
$prefill_details_html = $prefill_details !== '' ? '<p>' . nl2br(escapeHtml($prefill_details)) . '</p>' : '';
|
||||
|
||||
// From a call's Add to Ticket (functions/phone.php) - the call goes on the ticket it makes
|
||||
$phone_call_id = intval($_GET['phone_call_id'] ?? 0);
|
||||
$phone_call = $phone_call_id && function_exists('phoneCallRow') ? phoneCallRow($phone_call_id) : null;
|
||||
if ($phone_call && phoneCallClientFor($phone_call) && !hasClientAccess($phone_call['phone_call_client_id'])) {
|
||||
$phone_call = null;
|
||||
}
|
||||
|
||||
if ($client_id) {
|
||||
enforceClientAccess();
|
||||
}
|
||||
@@ -114,6 +121,9 @@ ob_start();
|
||||
<input type="hidden" name="client_id" id="clientIdHidden" value="<?= $client_id ?>">
|
||||
<?php } ?>
|
||||
<input type="hidden" name="billable" value="0">
|
||||
<?php if ($phone_call) { ?>
|
||||
<input type="hidden" name="phone_call_id" value="<?= intval($phone_call['phone_call_id']) ?>">
|
||||
<?php } ?>
|
||||
|
||||
<div class="modal-body">
|
||||
|
||||
|
||||
@@ -453,6 +453,12 @@ if (isset($_GET['delete_client'])) {
|
||||
}
|
||||
mysqli_query($mysqli, "DELETE FROM tickets WHERE ticket_client_id = $client_id");
|
||||
mysqli_query($mysqli, "DELETE FROM trips WHERE trip_client_id = $client_id");
|
||||
// Their calls and phones go, and their phone system domains are no longer linked (functions/phone.php)
|
||||
if (phoneTablesReady()) {
|
||||
mysqli_query($mysqli, "DELETE FROM phone_calls WHERE phone_call_client_id = $client_id");
|
||||
mysqli_query($mysqli, "DELETE FROM phone_devices WHERE phone_device_client_id = $client_id");
|
||||
mysqli_query($mysqli, "UPDATE phone_domains SET phone_domain_client_id = 0 WHERE phone_domain_client_id = $client_id");
|
||||
}
|
||||
mysqli_query($mysqli, "DELETE FROM contracts WHERE contract_client_id = $client_id");
|
||||
mysqli_query($mysqli, "DELETE FROM vendors WHERE vendor_client_id = $client_id");
|
||||
|
||||
|
||||
45
agent/post/phone.php
Normal file
45
agent/post/phone.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* ITFlow - calls from a phone system (functions/phone.php): Add to Ticket - the call on one of the
|
||||
* client's open tickets, as an internal note with its talk time. New Ticket puts it on the ticket
|
||||
* it makes from add_ticket (agent/post/ticket.php).
|
||||
*/
|
||||
|
||||
defined('FROM_POST_HANDLER') || die("Direct file access is not allowed");
|
||||
|
||||
if (isset($_POST['phone_call_ticket'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
enforceUserPermission('module_support', 2);
|
||||
|
||||
$call = phoneCallRow($_POST['phone_call_id'] ?? 0);
|
||||
if (!$call || (phoneCallClientFor($call) && !hasClientAccess($call['phone_call_client_id']))) {
|
||||
flashAlert('That call is gone.', 'error');
|
||||
redirect();
|
||||
}
|
||||
$ticket = ticketRow($_POST['ticket_id'] ?? 0);
|
||||
if (!$ticket || !hasClientAccess($ticket['ticket_client_id'])) {
|
||||
flashAlert('Pick one of the open tickets.', 'error');
|
||||
redirect();
|
||||
}
|
||||
$client_id = intval($ticket['ticket_client_id']);
|
||||
if (phoneCallClientFor($call) && phoneCallClientFor($call) !== $client_id) {
|
||||
flashAlert('That ticket is another client\'s.', 'error');
|
||||
redirect();
|
||||
}
|
||||
|
||||
$count_time = intval($_POST['count_time'] ?? 0) === 1;
|
||||
phoneCallToTicket($call, $ticket, intval($session_user_id), $count_time, (string) ($_POST['note'] ?? ''));
|
||||
|
||||
$reference = ticketReference($ticket);
|
||||
$talk = $call['phone_call_status'] === 'answered' ? intval($call['phone_call_talk_seconds']) : 0;
|
||||
logTicketHistory(intval($ticket['ticket_id']), escapeSql("$session_name added a phone call: " . phoneCallSentence($call)));
|
||||
logAudit("Ticket", "Edit", "$session_name added a phone call to ticket " . escapeSql($reference . ' - ' . phoneCallSentence($call)), $client_id, intval($ticket['ticket_id']));
|
||||
|
||||
flashAlert('Call added to <strong>' . escapeHtml($reference) . '</strong>'
|
||||
. ($count_time && $talk > 0 ? ' with ' . escapeHtml(formatDuration(phoneTimeWorked($talk))) . ' worked' : ''));
|
||||
redirect();
|
||||
|
||||
}
|
||||
@@ -146,6 +146,14 @@ if (isset($_POST['add_ticket'])) {
|
||||
}
|
||||
}
|
||||
|
||||
// Made from a call (Calls, Add to Ticket) - the call on it, with its talk time (functions/phone.php)
|
||||
$phone_call = intval($_POST['phone_call_id'] ?? 0) ? phoneCallRow($_POST['phone_call_id']) : null;
|
||||
if ($phone_call && in_array(phoneCallClientFor($phone_call), [0, $client_id], true)) {
|
||||
mysqli_query($mysqli, "UPDATE tickets SET ticket_source = 'Phone' WHERE ticket_id = $ticket_id");
|
||||
phoneCallToTicket($phone_call, ticketRow($ticket_id), intval($session_user_id), true);
|
||||
logTicketHistory($ticket_id, escapeSql("Opened from a phone call: " . phoneCallSentence($phone_call)));
|
||||
}
|
||||
|
||||
// E-mail the client and the watchers - a quick ticket's contact only when its setting says so
|
||||
if (intval($config_ticket_client_general_notifications) === 1) {
|
||||
if (!$quick_ticket || quickTicketSettings()['email_client']) {
|
||||
|
||||
27
api/phone/cdr.php
Normal file
27
api/phone/cdr.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* ITFlow - call records from a phone system. FreeSWITCH's mod_xml_cdr posts each call's record
|
||||
* here as it hangs up - xml_cdr.conf.xml's url, as the system's setup window in Admin > Phone
|
||||
* Systems shows it, with log-http-and-disk so FusionPBX still imports its own. Each call is kept
|
||||
* once (phoneCallSave(), functions/phone.php): a record sent again puts it right. A second leg of a
|
||||
* call is taken and set aside - its first leg is the call. Anything that isn't a call record is
|
||||
* turned down (400), so FreeSWITCH keeps it in its error folder.
|
||||
*/
|
||||
|
||||
require_once __DIR__ . '/phone_system.php';
|
||||
|
||||
$phone_cdr = phoneCdrParse(phoneCdrFromRequest());
|
||||
if (!$phone_cdr) {
|
||||
$phone_reply(400, 'No call record');
|
||||
}
|
||||
|
||||
// mod_xml_cdr names a leg with a_ or b_ in uuid when prefix-a-leg is on
|
||||
$phone_leg = (string) ($_REQUEST['uuid'] ?? '');
|
||||
if (str_starts_with($phone_leg, 'b_')) {
|
||||
$phone_cdr['b_leg'] = true;
|
||||
}
|
||||
|
||||
$phone_result = phoneCallSave($phone_system, $phone_cdr);
|
||||
|
||||
$phone_reply(200, $phone_result['saved'] ? 'OK' : 'OK - ' . $phone_result['why']);
|
||||
21
api/phone/cid.php
Normal file
21
api/phone/cid.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* ITFlow - caller ID names from a phone system's contacts. FreeSWITCH's mod_cidlookup asks here
|
||||
* as a call comes in (cidlookup.conf.xml's url, as the system's setup window shows it) and the
|
||||
* phone shows what it gets back: the contact and their client, the client for its main number,
|
||||
* nothing for a number nobody has (phoneCallerName(), functions/phone.php). With the call's
|
||||
* domain, a client's own domain only matches that client's contacts.
|
||||
*/
|
||||
|
||||
require_once __DIR__ . '/phone_system.php';
|
||||
|
||||
$phone_number = phoneNumber($_GET['number'] ?? '');
|
||||
$phone_domain = phoneDomainName($_GET['domain'] ?? '');
|
||||
$phone_domain_client_id = 0;
|
||||
if ($phone_domain !== '') {
|
||||
$phone_domain_row = phoneDomain($phone_system, $phone_domain);
|
||||
$phone_domain_client_id = intval($phone_domain_row['phone_domain_client_id'] ?? 0);
|
||||
}
|
||||
|
||||
$phone_reply(200, $phone_number !== '' ? phoneCallerName($phone_number, $phone_domain_client_id) : '');
|
||||
41
api/phone/phone_system.php
Normal file
41
api/phone/phone_system.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* ITFlow - what every address under api/phone/ starts with: the phone system whose key came with
|
||||
* the request (functions/phone.php) as $phone_system. The key is the key field of the address, or
|
||||
* the password of HTTP Basic sign-in (mod_xml_cdr's cred, user:key). Without a known key there is
|
||||
* nothing here (404); a system that is turned off answers 503, so its sender knows.
|
||||
*/
|
||||
|
||||
require_once __DIR__ . '/../../config.php';
|
||||
require_once __DIR__ . '/../../functions.php';
|
||||
require_once __DIR__ . '/../../includes/inc_set_timezone.php';
|
||||
require_once __DIR__ . '/../../includes/load_global_settings.php';
|
||||
|
||||
header('Cache-Control: no-store');
|
||||
|
||||
$phone_reply = function (int $status, string $text) {
|
||||
http_response_code($status);
|
||||
header('Content-Type: text/plain; charset=utf-8');
|
||||
echo $text;
|
||||
exit;
|
||||
};
|
||||
|
||||
// logAudit() reads these
|
||||
$session_ip = escapeSql(getIP());
|
||||
$session_user_agent = escapeSql($_SERVER['HTTP_USER_AGENT'] ?? '');
|
||||
$session_user_id = 0;
|
||||
$session_name = 'Phone system';
|
||||
|
||||
$phone_key = (string) ($_GET['key'] ?? '');
|
||||
if ($phone_key === '' && isset($_SERVER['PHP_AUTH_PW'])) {
|
||||
$phone_key = (string) $_SERVER['PHP_AUTH_PW'];
|
||||
}
|
||||
$phone_system = phoneSystemByKey($phone_key);
|
||||
if (!$phone_system || !phoneKind($phone_system)) {
|
||||
$phone_reply(404, 'Not found');
|
||||
}
|
||||
if (!intval($phone_system['phone_system_active'])) {
|
||||
$phone_reply(503, 'This phone system is turned off in ITFlow');
|
||||
}
|
||||
$session_name = escapeSql(phoneSystemName($phone_system));
|
||||
@@ -174,6 +174,9 @@ ticketReadsCleanup();
|
||||
// RMM alerts cleared half a year ago (functions/rmm.php)
|
||||
rmmAlertsCleanup();
|
||||
|
||||
// Calls over a year old that aren't on a ticket, and phones ringing two days ago (functions/phone.php)
|
||||
phoneCleanup();
|
||||
|
||||
// Clean-up mail queue - a failed or cancelled email with a link or code that opens something is cleared after a day, everything is deleted after 90
|
||||
mailQueueClearSensitive();
|
||||
mailQueueImageCleanup();
|
||||
|
||||
@@ -4313,6 +4313,28 @@ button.itflow-rack-cell-body:hover {
|
||||
max-width: 14rem;
|
||||
}
|
||||
|
||||
/* Calls (agent/calls.php) - each one's direction, or how it ended, in a round badge */
|
||||
.itflow-phone-call-icon {
|
||||
display: inline-flex;
|
||||
flex: 0 0 auto;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 2rem;
|
||||
height: 2rem;
|
||||
border-radius: 50%;
|
||||
font-size: .8rem;
|
||||
}
|
||||
|
||||
/* Phone systems - the lines their setup window gives to copy (admin/modals/phone/phone_system_setup.php) */
|
||||
.itflow-phone-snippet {
|
||||
padding: .6rem 2.75rem .6rem .75rem;
|
||||
border: 1px solid var(--bs-border-color);
|
||||
border-radius: var(--bs-border-radius);
|
||||
background-color: var(--bs-tertiary-bg);
|
||||
white-space: pre-wrap;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
|
||||
/* Listing cards: the whole card opens the rack, the menu sits above that link */
|
||||
.itflow-rack-card {
|
||||
transition: box-shadow .15s ease-in-out, border-color .15s ease-in-out;
|
||||
|
||||
136
db.sql
136
db.sql
@@ -2612,6 +2612,141 @@ CREATE TABLE `payments` (
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||
|
||||
--
|
||||
-- Table structure for table `phone_calls`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `phone_calls`;
|
||||
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
||||
/*!40101 SET character_set_client = utf8mb4 */;
|
||||
CREATE TABLE `phone_calls` (
|
||||
`phone_call_id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`phone_call_system_id` int(11) NOT NULL,
|
||||
`phone_call_uuid` varchar(64) NOT NULL,
|
||||
`phone_call_domain_id` int(11) NOT NULL DEFAULT 0,
|
||||
`phone_call_client_id` int(11) NOT NULL DEFAULT 0,
|
||||
`phone_call_contact_id` int(11) NOT NULL DEFAULT 0,
|
||||
`phone_call_direction` varchar(10) NOT NULL DEFAULT '',
|
||||
`phone_call_number` varchar(32) NOT NULL DEFAULT '',
|
||||
`phone_call_name` varchar(100) NOT NULL DEFAULT '',
|
||||
`phone_call_dialed` varchar(32) NOT NULL DEFAULT '',
|
||||
`phone_call_extension` varchar(32) NOT NULL DEFAULT '',
|
||||
`phone_call_user_id` int(11) NOT NULL DEFAULT 0,
|
||||
`phone_call_status` varchar(20) NOT NULL DEFAULT '',
|
||||
`phone_call_started_at` datetime NOT NULL,
|
||||
`phone_call_answered_at` datetime DEFAULT NULL,
|
||||
`phone_call_seconds` int(11) NOT NULL DEFAULT 0,
|
||||
`phone_call_talk_seconds` int(11) NOT NULL DEFAULT 0,
|
||||
`phone_call_hangup_cause` varchar(50) NOT NULL DEFAULT '',
|
||||
`phone_call_recording` varchar(500) DEFAULT NULL,
|
||||
`phone_call_ticket_id` int(11) NOT NULL DEFAULT 0,
|
||||
`phone_call_created_at` datetime NOT NULL DEFAULT current_timestamp(),
|
||||
PRIMARY KEY (`phone_call_id`),
|
||||
UNIQUE KEY `phone_call_uuid` (`phone_call_system_id`,`phone_call_uuid`),
|
||||
KEY `phone_call_client_id` (`phone_call_client_id`,`phone_call_started_at`),
|
||||
KEY `phone_call_contact_id` (`phone_call_contact_id`,`phone_call_started_at`),
|
||||
KEY `phone_call_started_at` (`phone_call_started_at`),
|
||||
KEY `phone_call_ticket_id` (`phone_call_ticket_id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||
|
||||
--
|
||||
-- Table structure for table `phone_devices`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `phone_devices`;
|
||||
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
||||
/*!40101 SET character_set_client = utf8mb4 */;
|
||||
CREATE TABLE `phone_devices` (
|
||||
`phone_device_id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`phone_device_system_id` int(11) NOT NULL,
|
||||
`phone_device_mac` varchar(12) NOT NULL,
|
||||
`phone_device_vendor` varchar(30) NOT NULL DEFAULT '',
|
||||
`phone_device_model` varchar(100) NOT NULL DEFAULT '',
|
||||
`phone_device_firmware` varchar(100) NOT NULL DEFAULT '',
|
||||
`phone_device_ip` varchar(64) NOT NULL DEFAULT '',
|
||||
`phone_device_line` varchar(100) NOT NULL DEFAULT '',
|
||||
`phone_device_domain_id` int(11) NOT NULL DEFAULT 0,
|
||||
`phone_device_client_id` int(11) NOT NULL DEFAULT 0,
|
||||
`phone_device_asset_id` int(11) NOT NULL DEFAULT 0,
|
||||
`phone_device_status` varchar(20) NOT NULL DEFAULT '',
|
||||
`phone_device_status_at` datetime DEFAULT NULL,
|
||||
`phone_device_heard_at` datetime DEFAULT NULL,
|
||||
`phone_device_created_at` datetime NOT NULL DEFAULT current_timestamp(),
|
||||
PRIMARY KEY (`phone_device_id`),
|
||||
UNIQUE KEY `phone_device_mac` (`phone_device_system_id`,`phone_device_mac`),
|
||||
KEY `phone_device_asset_id` (`phone_device_asset_id`),
|
||||
KEY `phone_device_client_id` (`phone_device_client_id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||
|
||||
--
|
||||
-- Table structure for table `phone_domains`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `phone_domains`;
|
||||
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
||||
/*!40101 SET character_set_client = utf8mb4 */;
|
||||
CREATE TABLE `phone_domains` (
|
||||
`phone_domain_id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`phone_domain_system_id` int(11) NOT NULL,
|
||||
`phone_domain_name` varchar(255) NOT NULL,
|
||||
`phone_domain_client_id` int(11) NOT NULL DEFAULT 0,
|
||||
`phone_domain_seen_at` datetime DEFAULT NULL,
|
||||
`phone_domain_created_at` datetime NOT NULL DEFAULT current_timestamp(),
|
||||
PRIMARY KEY (`phone_domain_id`),
|
||||
UNIQUE KEY `phone_domain_name` (`phone_domain_system_id`,`phone_domain_name`),
|
||||
KEY `phone_domain_client_id` (`phone_domain_client_id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||
|
||||
--
|
||||
-- Table structure for table `phone_events`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `phone_events`;
|
||||
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
||||
/*!40101 SET character_set_client = utf8mb4 */;
|
||||
CREATE TABLE `phone_events` (
|
||||
`phone_event_id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`phone_event_system_id` int(11) NOT NULL,
|
||||
`phone_event_domain_id` int(11) NOT NULL DEFAULT 0,
|
||||
`phone_event_line` varchar(100) NOT NULL DEFAULT '',
|
||||
`phone_event_user_id` int(11) NOT NULL DEFAULT 0,
|
||||
`phone_event_kind` varchar(20) NOT NULL,
|
||||
`phone_event_call` varchar(100) NOT NULL DEFAULT '',
|
||||
`phone_event_number` varchar(32) NOT NULL DEFAULT '',
|
||||
`phone_event_name` varchar(100) NOT NULL DEFAULT '',
|
||||
`phone_event_contact_id` int(11) NOT NULL DEFAULT 0,
|
||||
`phone_event_client_id` int(11) NOT NULL DEFAULT 0,
|
||||
`phone_event_at` datetime NOT NULL DEFAULT current_timestamp(),
|
||||
PRIMARY KEY (`phone_event_id`),
|
||||
KEY `phone_event_user_id` (`phone_event_user_id`,`phone_event_id`),
|
||||
KEY `phone_event_at` (`phone_event_at`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||
|
||||
--
|
||||
-- Table structure for table `phone_systems`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `phone_systems`;
|
||||
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
||||
/*!40101 SET character_set_client = utf8mb4 */;
|
||||
CREATE TABLE `phone_systems` (
|
||||
`phone_system_id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`phone_system_kind` varchar(50) NOT NULL,
|
||||
`phone_system_name` varchar(100) NOT NULL,
|
||||
`phone_system_web_url` varchar(500) NOT NULL DEFAULT '',
|
||||
`phone_system_key` varchar(100) NOT NULL,
|
||||
`phone_system_active` tinyint(1) NOT NULL DEFAULT 1,
|
||||
`phone_system_heard_at` datetime DEFAULT NULL,
|
||||
`phone_system_created_at` datetime NOT NULL DEFAULT current_timestamp(),
|
||||
`phone_system_updated_at` datetime DEFAULT NULL ON UPDATE current_timestamp(),
|
||||
PRIMARY KEY (`phone_system_id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||
|
||||
--
|
||||
-- Table structure for table `po_items`
|
||||
--
|
||||
@@ -5118,6 +5253,7 @@ CREATE TABLE `user_settings` (
|
||||
`user_config_compact_rows` tinyint(1) NOT NULL DEFAULT 0,
|
||||
`user_config_booking_reminder` int(11) DEFAULT NULL,
|
||||
`user_config_reminder_email` tinyint(1) NOT NULL DEFAULT 0,
|
||||
`user_config_phone_line` varchar(255) DEFAULT NULL,
|
||||
PRIMARY KEY (`user_id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||
|
||||
@@ -127,6 +127,7 @@ require_once __DIR__ . '/functions/weekly_summary.php';
|
||||
require_once __DIR__ . '/functions/sms.php';
|
||||
require_once __DIR__ . '/functions/rmm.php';
|
||||
require_once __DIR__ . '/functions/rmm_tactical.php';
|
||||
require_once __DIR__ . '/functions/phone.php';
|
||||
require_once __DIR__ . '/functions/client_onboarding.php';
|
||||
require_once __DIR__ . '/functions/ticket_mention.php';
|
||||
require_once __DIR__ . '/functions/ticket_reply_recipients.php';
|
||||
|
||||
@@ -118,6 +118,7 @@ function auditLogKinds() {
|
||||
'AI Provider' => ['icon' => 'fa-robot'],
|
||||
'AI Model' => ['icon' => 'fa-brain'],
|
||||
'RMM Provider' => ['icon' => 'fa-laptop-medical'],
|
||||
'Phone System' => ['icon' => 'fa-phone-volume'],
|
||||
'Starter Content' => ['icon' => 'fa-seedling'],
|
||||
'Demo Data' => ['icon' => 'fa-flask'],
|
||||
'Demo' => ['icon' => 'fa-flask'],
|
||||
|
||||
@@ -80,6 +80,7 @@ function clientMergeTables() {
|
||||
['kb_article_feedback', 'kb_article_feedback_client_id'], ['order_items', 'order_item_client_id'], ['po_items', 'po_item_client_id'],
|
||||
['expense_import_rules', 'import_rule_client_id'], ['income_import_rules', 'import_rule_client_id'],
|
||||
['rmm_clients', 'rmm_client_client_id'], ['rmm_devices', 'rmm_device_client_id'], ['rmm_alerts', 'rmm_alert_client_id'],
|
||||
['phone_domains', 'phone_domain_client_id'], ['phone_calls', 'phone_call_client_id'], ['phone_devices', 'phone_device_client_id'], ['phone_events', 'phone_event_client_id'],
|
||||
['managed_instances', 'managed_instance_client_id'], ['ai_usage', 'ai_usage_client_id'],
|
||||
['logs', 'log_client_id'], ['notifications', 'notification_client_id'],
|
||||
];
|
||||
|
||||
902
functions/phone.php
Normal file
902
functions/phone.php
Normal file
@@ -0,0 +1,902 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Phone systems - a PBX that tells ITFlow what happens on it (Admin > Phone Systems, admin/phone_systems.php).
|
||||
* FusionPBX is the first kind. Nothing is asked of its API or its database: it is set up to send
|
||||
* ITFlow things, to addresses under api/phone/ that carry the system's key -
|
||||
*
|
||||
* cdr.php each call's record, as FreeSWITCH posts it at hangup (mod_xml_cdr) - a phone_calls row,
|
||||
* the other party matched to a contact (phoneWho()) and the extension to an agent
|
||||
* cid.php the name for a caller ID, asked for as a call comes in (mod_cidlookup) - so desk
|
||||
* phones show the contact and their client
|
||||
*
|
||||
* Each domain (a FusionPBX tenant) it names is a phone_domains row, linked on the system's Domains
|
||||
* page (admin/phone_domains.php) to the client it is. A call on a client's domain is that client's,
|
||||
* with the contact matched among theirs. A call on your own company's domain (Admin > Settings >
|
||||
* Company, your client record) or one not linked yet is the caller's: their number is matched
|
||||
* among every client's contacts, then the clients' locations.
|
||||
*/
|
||||
|
||||
/** How long calls are kept - those on a ticket stay */
|
||||
const PHONE_CALLS_KEEP_DAYS = 400;
|
||||
/** The longest a call record from FreeSWITCH is taken to be */
|
||||
const PHONE_CDR_MAX_BYTES = 4194304;
|
||||
|
||||
/** The kinds of phone system ITFlow knows */
|
||||
function phoneSystemKinds(): array
|
||||
{
|
||||
return [
|
||||
'fusionpbx' => [
|
||||
'name' => 'FusionPBX',
|
||||
'short' => 'FusionPBX',
|
||||
'icon' => 'fas fa-phone-volume',
|
||||
'about' => 'FusionPBX on FreeSWITCH - its call records, caller ID and phones',
|
||||
'web_placeholder' => 'https://pbx.example.com',
|
||||
// A call's own page there, by its uuid - with its recording
|
||||
'call_path' => '/app/xml_cdr/xml_cdr_details.php?id=',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/** The kind of a phone_systems row, or null when this ITFlow doesn't know it */
|
||||
function phoneKind(array $row): ?array
|
||||
{
|
||||
return phoneSystemKinds()[(string) ($row['phone_system_kind'] ?? '')] ?? null;
|
||||
}
|
||||
|
||||
/** Whether the 4.1.4 database update is in - the phone tables */
|
||||
function phoneTablesReady(): bool
|
||||
{
|
||||
global $mysqli;
|
||||
static $ready = null;
|
||||
if ($ready === null) {
|
||||
try {
|
||||
mysqli_query($mysqli, "SELECT 1 FROM phone_calls LIMIT 1");
|
||||
$ready = true;
|
||||
} catch (Throwable $e) {
|
||||
$ready = false;
|
||||
}
|
||||
}
|
||||
return $ready;
|
||||
}
|
||||
|
||||
/** Whether any phone system is set up - what the Calls menu items wait for */
|
||||
function phoneSystemsExist(): bool
|
||||
{
|
||||
global $mysqli;
|
||||
static $exist = null;
|
||||
if ($exist === null) {
|
||||
$exist = phoneTablesReady() && intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FROM phone_systems"))[0]) > 0;
|
||||
}
|
||||
return $exist;
|
||||
}
|
||||
|
||||
/** A phone system by id - raw, or null */
|
||||
function phoneSystemRow($phone_system_id): ?array
|
||||
{
|
||||
global $mysqli;
|
||||
$phone_system_id = intval($phone_system_id);
|
||||
if (!$phone_system_id || !phoneTablesReady()) {
|
||||
return null;
|
||||
}
|
||||
return mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT * FROM phone_systems WHERE phone_system_id = $phone_system_id")) ?: null;
|
||||
}
|
||||
|
||||
/** What a phone system is called */
|
||||
function phoneSystemName(array $row): string
|
||||
{
|
||||
$name = trim((string) ($row['phone_system_name'] ?? ''));
|
||||
return $name !== '' ? $name : (phoneKind($row)['name'] ?? 'Phone system');
|
||||
}
|
||||
|
||||
/** The phone system whose key this is - one that is set up in ITFlow - or null */
|
||||
function phoneSystemByKey(string $key): ?array
|
||||
{
|
||||
global $mysqli;
|
||||
if (strlen($key) < 32 || !phoneTablesReady()) {
|
||||
return null;
|
||||
}
|
||||
$sql = mysqli_query($mysqli, "SELECT * FROM phone_systems");
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
if ((string) $row['phone_system_key'] !== '' && hash_equals((string) $row['phone_system_key'], $key)) {
|
||||
return $row;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** A new key for a phone system's addresses */
|
||||
function phoneSystemNewKey(): string
|
||||
{
|
||||
return randomString(40);
|
||||
}
|
||||
|
||||
/**
|
||||
* An address under api/phone/ for a phone system - with its key, then $query as it is (the
|
||||
* system fills in what is in it, so it isn't encoded here).
|
||||
*/
|
||||
function phoneUrl(array $system, string $endpoint, string $query = ''): string
|
||||
{
|
||||
global $config_base_url;
|
||||
return 'https://' . rtrim((string) $config_base_url, '/') . "/api/phone/$endpoint.php?key=" . rawurlencode((string) $system['phone_system_key']) . ($query !== '' ? "&$query" : '');
|
||||
}
|
||||
|
||||
/** A page of the phone system itself - its address and $path - or '' when it has no address */
|
||||
function phoneWebUrl(array $system, string $path = ''): string
|
||||
{
|
||||
$base = rtrim(trim((string) ($system['phone_system_web_url'] ?? '')), '/');
|
||||
return $base !== '' && preg_match('#^https?://#i', $base) ? $base . $path : '';
|
||||
}
|
||||
|
||||
/** A call's own page on its phone system (with its recording) - or '' */
|
||||
function phoneCallWebUrl(array $system, string $uuid): string
|
||||
{
|
||||
$kind = phoneKind($system);
|
||||
return $kind && isset($kind['call_path']) && $uuid !== '' ? phoneWebUrl($system, $kind['call_path'] . rawurlencode($uuid)) : '';
|
||||
}
|
||||
|
||||
/** A phone system's row as Add or Edit sent it, checked - [$after, $errors by field] */
|
||||
function phoneSystemCheck(array $in, ?array $before): array
|
||||
{
|
||||
$errors = [];
|
||||
$kind = $before ? (string) $before['phone_system_kind'] : (string) ($in['kind'] ?? '');
|
||||
if (!isset(phoneSystemKinds()[$kind])) {
|
||||
$errors['kind'] = 'Pick the kind of phone system.';
|
||||
}
|
||||
$name = trim((string) ($in['name'] ?? ''));
|
||||
if ($name === '') {
|
||||
$errors['name'] = 'Give it a name.';
|
||||
}
|
||||
$web_url = rtrim(trim((string) ($in['web_url'] ?? '')), '/');
|
||||
if ($web_url !== '' && (!preg_match('#^https?://[^\s/]+#i', $web_url) || strlen($web_url) > 500)) {
|
||||
$errors['web_url'] = 'Its address starts with https:// - or leave it empty.';
|
||||
}
|
||||
return [[
|
||||
'phone_system_kind' => $kind,
|
||||
'phone_system_name' => mb_substr($name, 0, 100),
|
||||
'phone_system_web_url' => $web_url,
|
||||
'phone_system_active' => $before ? (intval($in['active'] ?? 0) === 1 ? 1 : 0) : 1,
|
||||
'phone_system_key' => $before ? (string) $before['phone_system_key'] : phoneSystemNewKey(),
|
||||
], $errors];
|
||||
}
|
||||
|
||||
/** Save a phone system's row - a new one when $phone_system_id is 0. Its id */
|
||||
function phoneSystemWrite(array $after, int $phone_system_id = 0): int
|
||||
{
|
||||
global $mysqli;
|
||||
$set = implode(', ', array_map(function ($column, $value) {
|
||||
return "`$column` = '" . escapeSql((string) $value) . "'";
|
||||
}, array_keys($after), $after));
|
||||
if ($phone_system_id) {
|
||||
mysqli_query($mysqli, "UPDATE phone_systems SET $set WHERE phone_system_id = $phone_system_id");
|
||||
return $phone_system_id;
|
||||
}
|
||||
mysqli_query($mysqli, "INSERT INTO phone_systems SET $set");
|
||||
return intval(mysqli_insert_id($mysqli));
|
||||
}
|
||||
|
||||
/** What changed on a phone system, for the audit log - its key is never written down */
|
||||
function phoneSystemChangeLines(array $before, array $after): array
|
||||
{
|
||||
$labels = ['phone_system_name' => 'name', 'phone_system_web_url' => 'address', 'phone_system_active' => 'on'];
|
||||
$lines = [];
|
||||
foreach ($labels as $column => $label) {
|
||||
if ((string) ($before[$column] ?? '') !== (string) ($after[$column] ?? '')) {
|
||||
$lines[] = $column === 'phone_system_active'
|
||||
? ($after[$column] ? 'turned it on' : 'turned it off')
|
||||
: "$label: " . ((string) ($before[$column] ?? '') === '' ? '(none)' : $before[$column]) . ' to ' . ((string) $after[$column] === '' ? '(none)' : $after[$column]);
|
||||
}
|
||||
}
|
||||
return $lines;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ Domains */
|
||||
|
||||
/** A domain as a PBX names it, tidied - lower case, no port - or '' when it can't be one */
|
||||
function phoneDomainName($name): string
|
||||
{
|
||||
$name = strtolower(trim((string) $name));
|
||||
$name = preg_replace('/:\d+$/', '', $name);
|
||||
return preg_match('/^[a-z0-9_]([a-z0-9._-]{0,253}[a-z0-9_])?$/', $name) ? $name : '';
|
||||
}
|
||||
|
||||
/** A phone_domains row by id - with its system - or null */
|
||||
function phoneDomainRow($phone_domain_id): ?array
|
||||
{
|
||||
global $mysqli;
|
||||
$phone_domain_id = intval($phone_domain_id);
|
||||
if (!$phone_domain_id || !phoneTablesReady()) {
|
||||
return null;
|
||||
}
|
||||
return mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT phone_domains.*, phone_systems.*, client_name FROM phone_domains
|
||||
JOIN phone_systems ON phone_system_id = phone_domain_system_id
|
||||
LEFT JOIN clients ON client_id = phone_domain_client_id AND phone_domain_client_id > 0
|
||||
WHERE phone_domain_id = $phone_domain_id")) ?: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The phone_domains row a system's domain is - made the first time it is heard of, and said to be
|
||||
* heard from now. Null when $name can't be a domain.
|
||||
*/
|
||||
function phoneDomain(array $system, $name): ?array
|
||||
{
|
||||
global $mysqli;
|
||||
$name = phoneDomainName($name);
|
||||
if ($name === '') {
|
||||
return null;
|
||||
}
|
||||
$system_id = intval($system['phone_system_id']);
|
||||
$name_sql = escapeSql($name);
|
||||
mysqli_query($mysqli, "INSERT INTO phone_domains SET phone_domain_system_id = $system_id, phone_domain_name = '$name_sql', phone_domain_seen_at = NOW()
|
||||
ON DUPLICATE KEY UPDATE phone_domain_seen_at = NOW()");
|
||||
return mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT * FROM phone_domains WHERE phone_domain_system_id = $system_id AND phone_domain_name = '$name_sql'")) ?: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The clients a system's domains not linked yet look like - [phone_domain_id => client_id]. A
|
||||
* domain is a client's when it is one of the client's own domains or under one
|
||||
* (voice.ravenwooddental.com), when its first part is the client's abbreviation (rdg.pbx.example.com),
|
||||
* or when the client's name starts with its first part (ravenwood.pbx.example.com) - and only
|
||||
* one client fits.
|
||||
*/
|
||||
function phoneDomainSuggestions(int $phone_system_id): array
|
||||
{
|
||||
global $mysqli;
|
||||
$domains = [];
|
||||
$sql = mysqli_query($mysqli, "SELECT phone_domain_id, phone_domain_name FROM phone_domains WHERE phone_domain_system_id = $phone_system_id AND phone_domain_client_id = 0");
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$domains[intval($row['phone_domain_id'])] = $row['phone_domain_name'];
|
||||
}
|
||||
if (!$domains) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$clients = [];
|
||||
$sql = mysqli_query($mysqli, "SELECT client_id, client_name, client_abbreviation FROM clients WHERE client_archived_at IS NULL AND client_lead = 0");
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$clients[intval($row['client_id'])] = [
|
||||
'name' => preg_replace('/[^a-z0-9]/', '', strtolower((string) $row['client_name'])),
|
||||
'abbreviation' => strtolower(trim((string) $row['client_abbreviation'])),
|
||||
'domains' => [],
|
||||
];
|
||||
}
|
||||
try {
|
||||
$sql = mysqli_query($mysqli, "SELECT domain_client_id, domain_name FROM domains WHERE domain_archived_at IS NULL AND domain_client_id > 0");
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
if (isset($clients[intval($row['domain_client_id'])])) {
|
||||
$clients[intval($row['domain_client_id'])]['domains'][] = strtolower(trim((string) $row['domain_name']));
|
||||
}
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
// Without the client's domains it goes by names
|
||||
}
|
||||
|
||||
$suggestions = [];
|
||||
foreach ($domains as $domain_id => $domain) {
|
||||
$first = explode('.', $domain)[0];
|
||||
$fits = [];
|
||||
foreach ($clients as $client_id => $client) {
|
||||
foreach ($client['domains'] as $own) {
|
||||
if ($own !== '' && ($domain === $own || str_ends_with($domain, ".$own"))) {
|
||||
$fits[$client_id] = 3;
|
||||
}
|
||||
}
|
||||
if (!isset($fits[$client_id]) && $client['abbreviation'] !== '' && $first === $client['abbreviation']) {
|
||||
$fits[$client_id] = 2;
|
||||
}
|
||||
if (!isset($fits[$client_id]) && strlen($first) >= 4 && str_starts_with($client['name'], preg_replace('/[^a-z0-9]/', '', $first))) {
|
||||
$fits[$client_id] = 1;
|
||||
}
|
||||
}
|
||||
if (!$fits) {
|
||||
continue;
|
||||
}
|
||||
// The best kind of fit, when only one client has it
|
||||
$best = max($fits);
|
||||
$top = array_keys(array_filter($fits, function ($fit) use ($best) {
|
||||
return $fit === $best;
|
||||
}));
|
||||
if (count($top) === 1) {
|
||||
$suggestions[$domain_id] = $top[0];
|
||||
}
|
||||
}
|
||||
return $suggestions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Link a domain to a client (0 unlinks it). Its phones become the client's, and its calls are
|
||||
* matched again by the new rule - on a client's domain they are the client's; on your own
|
||||
* company's, or unlinked, the caller's. A call already on a ticket keeps who it was with.
|
||||
*/
|
||||
function phoneDomainLink(int $phone_domain_id, int $client_id): void
|
||||
{
|
||||
global $mysqli, $config_internal_client_id;
|
||||
mysqli_query($mysqli, "UPDATE phone_domains SET phone_domain_client_id = $client_id WHERE phone_domain_id = $phone_domain_id");
|
||||
mysqli_query($mysqli, "UPDATE phone_devices SET phone_device_client_id = $client_id WHERE phone_device_domain_id = $phone_domain_id");
|
||||
|
||||
$own = $client_id && $client_id !== intval($config_internal_client_id ?? 0);
|
||||
$who = [];
|
||||
$sql = mysqli_query($mysqli, "SELECT phone_call_id, phone_call_number, phone_call_direction FROM phone_calls
|
||||
WHERE phone_call_domain_id = $phone_domain_id AND phone_call_ticket_id = 0
|
||||
ORDER BY phone_call_id DESC LIMIT 5000");
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$number = (string) $row['phone_call_number'];
|
||||
if (!isset($who[$number])) {
|
||||
$who[$number] = $row['phone_call_direction'] !== 'local' ? phoneWho($number, $client_id) : ['contact_id' => 0, 'client_id' => 0];
|
||||
}
|
||||
$call_client = $own ? $client_id : ($who[$number]['client_id'] ?: $client_id);
|
||||
mysqli_query($mysqli, "UPDATE phone_calls SET phone_call_client_id = $call_client, phone_call_contact_id = " . intval($who[$number]['contact_id']) . "
|
||||
WHERE phone_call_id = " . intval($row['phone_call_id']));
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ Numbers */
|
||||
|
||||
/**
|
||||
* A number as a phone system gives it - "+14125550100", "4125550100", "sip:101@pbx.example.com" -
|
||||
* as digits: E.164 without the + for a phone number (a 10-digit North American number given its 1),
|
||||
* as it is for an extension. '' when there are no digits.
|
||||
*/
|
||||
function phoneNumber($raw): string
|
||||
{
|
||||
$raw = trim(rawurldecode((string) $raw));
|
||||
if (preg_match('/^sips?:([^@;]+)/i', $raw, $m)) {
|
||||
$raw = $m[1];
|
||||
}
|
||||
$digits = preg_replace('/\D/', '', $raw);
|
||||
if ($digits === '') {
|
||||
return '';
|
||||
}
|
||||
if (phoneIsExtension($digits)) {
|
||||
return $digits;
|
||||
}
|
||||
return mb_substr(smsInboundNumber($digits) ?: $digits, 0, 32);
|
||||
}
|
||||
|
||||
/** Whether digits are an extension - six or fewer - rather than a phone number */
|
||||
function phoneIsExtension(string $digits): bool
|
||||
{
|
||||
return strlen($digits) <= 6;
|
||||
}
|
||||
|
||||
/** A number for reading - "+1 (412) 555-0100", "Ext 101" */
|
||||
function phoneNumberText($number): string
|
||||
{
|
||||
$number = (string) $number;
|
||||
if ($number === '') {
|
||||
return '';
|
||||
}
|
||||
return phoneIsExtension($number) ? "Ext $number" : smsNumberText($number);
|
||||
}
|
||||
|
||||
/**
|
||||
* Who a number is - ['contact_id', 'client_id', 'contact_name', 'client_name']: a contact, else a
|
||||
* client whose location has the number, else nobody (all 0 and ''). On a client's domain only that
|
||||
* client's contacts and locations count; on your own company's domain, or one not linked
|
||||
* ($domain_client_id 0), every client's do. A number more than one client has is nobody's.
|
||||
*/
|
||||
function phoneWho($number, int $domain_client_id): array
|
||||
{
|
||||
global $mysqli, $config_internal_client_id;
|
||||
$nobody = ['contact_id' => 0, 'client_id' => 0, 'contact_name' => '', 'client_name' => ''];
|
||||
$number = (string) $number;
|
||||
if ($number === '' || phoneIsExtension($number) || !smsNumberIsPhone($number)) {
|
||||
return $nobody;
|
||||
}
|
||||
$everyone = !$domain_client_id || $domain_client_id === intval($config_internal_client_id ?? 0);
|
||||
|
||||
$contacts = array_values(array_filter(smsContactsFor($number), function ($contact) use ($everyone, $domain_client_id) {
|
||||
return intval($contact['contact_client_id']) > 0 && ($everyone || intval($contact['contact_client_id']) === $domain_client_id);
|
||||
}));
|
||||
if ($contacts) {
|
||||
// Someone's own mobile says more than an office number many share
|
||||
$mobiles = array_values(array_filter($contacts, function ($contact) use ($number) {
|
||||
return smsNumber($contact['contact_mobile'], $contact['contact_mobile_country_code']) === $number;
|
||||
}));
|
||||
foreach ([$mobiles, $contacts] as $matches) {
|
||||
if ($matches && count(array_unique(array_map('intval', array_column($matches, 'contact_client_id')))) === 1) {
|
||||
return [
|
||||
'contact_id' => intval($matches[0]['contact_id']),
|
||||
'client_id' => intval($matches[0]['contact_client_id']),
|
||||
'contact_name' => (string) $matches[0]['contact_name'],
|
||||
'client_name' => (string) $matches[0]['client_name'],
|
||||
];
|
||||
}
|
||||
}
|
||||
return $nobody;
|
||||
}
|
||||
|
||||
// A client's main number - one of its locations'
|
||||
$tail = escapeSql(substr($number, -7));
|
||||
$scope = $everyone ? '' : " AND location_client_id = $domain_client_id";
|
||||
$sql = mysqli_query($mysqli, "SELECT location_client_id, location_phone, location_phone_country_code, client_name FROM locations
|
||||
JOIN clients ON client_id = location_client_id
|
||||
WHERE location_archived_at IS NULL AND client_archived_at IS NULL AND REGEXP_REPLACE(location_phone, '[^0-9]', '') LIKE '%$tail'$scope
|
||||
LIMIT 20");
|
||||
$clients = [];
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
if (smsNumber($row['location_phone'], $row['location_phone_country_code']) === $number) {
|
||||
$clients[intval($row['location_client_id'])] = (string) $row['client_name'];
|
||||
}
|
||||
}
|
||||
if (count($clients) === 1) {
|
||||
return ['contact_id' => 0, 'client_id' => (int) array_key_first($clients), 'contact_name' => '', 'client_name' => reset($clients)];
|
||||
}
|
||||
return $nobody;
|
||||
}
|
||||
|
||||
/** The name a phone shows for a caller - "Marla Whitfield - Ravenwood Dental", the client's name for its main number, '' for nobody */
|
||||
function phoneCallerName($number, int $domain_client_id): string
|
||||
{
|
||||
$who = phoneWho($number, $domain_client_id);
|
||||
if ($who['contact_name'] !== '') {
|
||||
// On a client's own domain the client goes without saying
|
||||
$name = $who['contact_name'] . ($who['client_name'] !== '' && $who['client_id'] !== $domain_client_id ? ' - ' . $who['client_name'] : '');
|
||||
} else {
|
||||
$name = $who['client_name'];
|
||||
}
|
||||
// One line, no characters a phone would choke on
|
||||
return mb_substr(trim(preg_replace('/[\x00-\x1F\x7F]+/', ' ', $name)), 0, 60);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ Agents */
|
||||
|
||||
/** The agent whose desk phone an extension on a domain is (user_config_phone_line, extension@domain) - 0 for nobody */
|
||||
function phoneUserForLine(string $extension, string $domain_name): int
|
||||
{
|
||||
global $mysqli;
|
||||
if ($extension === '' || $domain_name === '') {
|
||||
return 0;
|
||||
}
|
||||
$line = escapeSql(strtolower("$extension@$domain_name"));
|
||||
$row = mysqli_fetch_row(mysqli_query($mysqli, "SELECT user_settings.user_id FROM user_settings
|
||||
JOIN users ON users.user_id = user_settings.user_id
|
||||
WHERE LOWER(user_config_phone_line) = '$line' AND user_archived_at IS NULL
|
||||
LIMIT 1"));
|
||||
return intval($row[0] ?? 0);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ Calls */
|
||||
|
||||
/** How a call ended - [label, icon, tone] by status */
|
||||
function phoneCallStatuses(): array
|
||||
{
|
||||
return [
|
||||
'answered' => ['Answered', 'fa-phone', 'success'],
|
||||
'missed' => ['Missed', 'fa-phone-slash', 'danger'],
|
||||
'voicemail' => ['Voicemail', 'fa-voicemail', 'warning'],
|
||||
'busy' => ['Busy', 'fa-phone-slash', 'secondary'],
|
||||
'no_answer' => ['No answer', 'fa-phone-slash', 'secondary'],
|
||||
'failed' => ['Didn\'t connect', 'fa-exclamation-circle', 'secondary'],
|
||||
];
|
||||
}
|
||||
|
||||
/** Which way a call went - [label, icon] by direction */
|
||||
function phoneCallDirections(): array
|
||||
{
|
||||
return [
|
||||
'inbound' => ['Inbound', 'fa-arrow-down'],
|
||||
'outbound' => ['Outbound', 'fa-arrow-up'],
|
||||
'local' => ['Internal', 'fa-exchange-alt'],
|
||||
];
|
||||
}
|
||||
|
||||
/** "4m 12s", "38s", "1h 2m" */
|
||||
function phoneSecondsText($seconds): string
|
||||
{
|
||||
$seconds = max(0, intval($seconds));
|
||||
if ($seconds < 60) {
|
||||
return "{$seconds}s";
|
||||
}
|
||||
$hours = intdiv($seconds, 3600);
|
||||
$minutes = intdiv($seconds % 3600, 60);
|
||||
return $hours ? "{$hours}h {$minutes}m" : "{$minutes}m" . ($seconds % 60 ? ' ' . ($seconds % 60) . 's' : '');
|
||||
}
|
||||
|
||||
/** A call's talk time as time worked on a ticket - 'HH:MM:SS', rounded up to the minute */
|
||||
function phoneTimeWorked($talk_seconds): string
|
||||
{
|
||||
$minutes = (int) ceil(max(0, intval($talk_seconds)) / 60);
|
||||
return sprintf('%02d:%02d:00', intdiv($minutes, 60), $minutes % 60);
|
||||
}
|
||||
|
||||
/**
|
||||
* The call record FreeSWITCH posted - mod_xml_cdr sends it as the cdr field, URL-encoded or in
|
||||
* base64 by its encode setting, or as the body itself. '' when there isn't one.
|
||||
*/
|
||||
function phoneCdrFromRequest(): string
|
||||
{
|
||||
$cdr = (string) ($_POST['cdr'] ?? '');
|
||||
if ($cdr === '') {
|
||||
$body = (string) file_get_contents('php://input', false, null, 0, PHONE_CDR_MAX_BYTES + 1);
|
||||
if (str_starts_with(ltrim($body), '<')) {
|
||||
$cdr = $body;
|
||||
} else {
|
||||
parse_str($body, $fields);
|
||||
$cdr = (string) ($fields['cdr'] ?? '');
|
||||
}
|
||||
}
|
||||
$cdr = trim($cdr);
|
||||
if ($cdr !== '' && !str_starts_with($cdr, '<')) {
|
||||
$decoded = base64_decode($cdr, true);
|
||||
$cdr = $decoded !== false && str_starts_with(ltrim($decoded), '<') ? trim($decoded) : '';
|
||||
}
|
||||
return strlen($cdr) <= PHONE_CDR_MAX_BYTES ? $cdr : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* What a FreeSWITCH XML call record says, as plain values - null when it isn't one. Its variables
|
||||
* are URL-encoded in it; a value missing from them is taken from the call's first caller profile.
|
||||
*/
|
||||
function phoneCdrParse(string $xml): ?array
|
||||
{
|
||||
if ($xml === '' || strlen($xml) > PHONE_CDR_MAX_BYTES) {
|
||||
return null;
|
||||
}
|
||||
$was = libxml_use_internal_errors(true);
|
||||
$doc = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NONET | LIBXML_COMPACT);
|
||||
libxml_clear_errors();
|
||||
libxml_use_internal_errors($was);
|
||||
if (!$doc || $doc->getName() !== 'cdr') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$vars = [];
|
||||
if (isset($doc->variables)) {
|
||||
foreach ($doc->variables->children() as $name => $value) {
|
||||
$vars[(string) $name] = rawurldecode((string) $value);
|
||||
}
|
||||
}
|
||||
// The first caller profile is the last callflow - each transfer adds one in front
|
||||
$profile = [];
|
||||
$callflows = $doc->xpath('callflow');
|
||||
if ($callflows) {
|
||||
$first = end($callflows);
|
||||
if (isset($first->caller_profile)) {
|
||||
foreach ($first->caller_profile->children() as $name => $value) {
|
||||
if (!$value->count()) {
|
||||
$profile[(string) $name] = (string) $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$get = function (...$names) use ($vars, $profile) {
|
||||
foreach ($names as $name) {
|
||||
foreach ([$vars, $profile] as $from) {
|
||||
if (isset($from[$name]) && trim((string) $from[$name]) !== '' && $from[$name] !== '_undef_') {
|
||||
return trim((string) $from[$name]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return '';
|
||||
};
|
||||
|
||||
$uuid = $get('uuid');
|
||||
if ($uuid === '') {
|
||||
return null;
|
||||
}
|
||||
$recording = '';
|
||||
if ($get('record_name') !== '') {
|
||||
$recording = ($get('record_path') !== '' ? rtrim($get('record_path'), '/') . '/' : '') . $get('record_name');
|
||||
} elseif ($get('recording_file') !== '') {
|
||||
$recording = $get('recording_file');
|
||||
}
|
||||
|
||||
return [
|
||||
'uuid' => mb_substr($uuid, 0, 64),
|
||||
// A second leg of a call - its first has the call
|
||||
'b_leg' => $get('originating_leg_uuid') !== '' && $get('originating_leg_uuid') !== $uuid,
|
||||
'domain' => $get('domain_name', 'dialed_domain', 'sip_req_host'),
|
||||
'direction' => strtolower($get('call_direction')),
|
||||
'channel_direction' => strtolower((string) ($doc->channel_data->direction ?? '') ?: $get('direction')),
|
||||
'caller_number' => $get('caller_id_number'),
|
||||
'caller_name' => $get('caller_id_name'),
|
||||
'destination' => $get('destination_number'),
|
||||
'caller_destination' => $get('caller_destination'),
|
||||
'from_user' => $get('sip_from_user', 'username'),
|
||||
'answered_by' => $get('last_sent_callee_id_number', 'dialed_user', 'dialed_extension'),
|
||||
'start' => intval($get('start_epoch')),
|
||||
'answer' => intval($get('answer_epoch')),
|
||||
'end' => intval($get('end_epoch')),
|
||||
'duration' => intval($get('duration')),
|
||||
'billsec' => intval($get('billsec')),
|
||||
'hangup_cause' => strtoupper($get('hangup_cause')),
|
||||
'last_app' => strtolower($get('last_app')),
|
||||
'missed' => in_array(strtolower($get('missed_call')), ['true', '1', 'yes'], true),
|
||||
'voicemail' => strtolower($get('last_app')) === 'voicemail' || $get('voicemail_message_len') !== '' || $get('voicemail_message_seconds') !== '',
|
||||
'recording' => mb_substr($recording, 0, 500),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* A call record as a call - direction, the other party's number and name, the number dialled,
|
||||
* the extension on this side, how it ended and when. FusionPBX says which way a call went
|
||||
* (call_direction); without that, a call from an extension to a phone number is outbound, from a
|
||||
* phone number inbound, and between extensions internal.
|
||||
*/
|
||||
function phoneCallFromCdr(array $cdr): array
|
||||
{
|
||||
$caller = phoneNumber($cdr['caller_number']);
|
||||
$destination = phoneNumber($cdr['destination']);
|
||||
$direction = $cdr['direction'];
|
||||
if (!in_array($direction, ['inbound', 'outbound', 'local'], true)) {
|
||||
$caller_internal = $caller === '' || phoneIsExtension($caller);
|
||||
$destination_internal = $destination === '' || phoneIsExtension($destination);
|
||||
$direction = $caller_internal && !$destination_internal ? 'outbound' : (!$caller_internal ? 'inbound' : 'local');
|
||||
}
|
||||
|
||||
if ($direction === 'inbound') {
|
||||
$number = $caller;
|
||||
$name = $cdr['caller_name'];
|
||||
$dialed = phoneNumber($cdr['caller_destination'] ?: $cdr['destination']);
|
||||
$extension = phoneNumber($cdr['answered_by']);
|
||||
if ($extension === '' && phoneIsExtension($destination)) {
|
||||
$extension = $destination;
|
||||
}
|
||||
} elseif ($direction === 'outbound') {
|
||||
$number = $destination;
|
||||
$name = '';
|
||||
$dialed = $destination;
|
||||
$from = phoneNumber($cdr['from_user']);
|
||||
$extension = phoneIsExtension($from) ? $from : (phoneIsExtension($caller) ? $caller : '');
|
||||
} else {
|
||||
$number = $destination;
|
||||
$name = '';
|
||||
$dialed = $destination;
|
||||
$from = phoneNumber($cdr['from_user']);
|
||||
$extension = phoneIsExtension($from) && $from !== '' ? $from : $caller;
|
||||
}
|
||||
// A caller ID name that is only the number says nothing
|
||||
if ($name !== '' && phoneNumber($name) === $number && !preg_match('/[a-z]/i', $name)) {
|
||||
$name = '';
|
||||
}
|
||||
|
||||
$talk = max(0, $cdr['billsec']);
|
||||
if ($direction === 'inbound' && $cdr['voicemail']) {
|
||||
$status = 'voicemail';
|
||||
} elseif ($talk > 0 && !($direction === 'inbound' && $cdr['missed'])) {
|
||||
$status = 'answered';
|
||||
} elseif ($direction === 'inbound') {
|
||||
$status = 'missed';
|
||||
} elseif ($cdr['hangup_cause'] === 'USER_BUSY') {
|
||||
$status = 'busy';
|
||||
} elseif (in_array($cdr['hangup_cause'], ['NO_ANSWER', 'ORIGINATOR_CANCEL', 'NO_USER_RESPONSE', 'NORMAL_CLEARING'], true)) {
|
||||
$status = 'no_answer';
|
||||
} else {
|
||||
$status = 'failed';
|
||||
}
|
||||
|
||||
$start = $cdr['start'] ?: ($cdr['end'] ? $cdr['end'] - max(0, $cdr['duration']) : time());
|
||||
$seconds = $cdr['duration'] ?: ($cdr['end'] && $cdr['start'] ? max(0, $cdr['end'] - $cdr['start']) : 0);
|
||||
|
||||
return [
|
||||
'direction' => $direction,
|
||||
'number' => mb_substr($number, 0, 32),
|
||||
'name' => mb_substr(trim(preg_replace('/[\x00-\x1F\x7F]+/', ' ', $name)), 0, 100),
|
||||
'dialed' => mb_substr($dialed, 0, 32),
|
||||
'extension' => mb_substr($extension, 0, 32),
|
||||
'status' => $status,
|
||||
'started_at' => date('Y-m-d H:i:s', $start),
|
||||
'answered_at' => $cdr['answer'] ? date('Y-m-d H:i:s', $cdr['answer']) : null,
|
||||
'seconds' => max(0, $seconds),
|
||||
'talk_seconds' => $talk,
|
||||
'hangup_cause' => mb_substr($cdr['hangup_cause'], 0, 50),
|
||||
'recording' => $cdr['recording'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Keep a call from its record - the second time the same call comes (FreeSWITCH retrying a post)
|
||||
* it is put right, not doubled. Returns ['saved' => bool, 'why' => what happened].
|
||||
*/
|
||||
function phoneCallSave(array $system, array $cdr): array
|
||||
{
|
||||
global $mysqli;
|
||||
|
||||
if ($cdr['b_leg']) {
|
||||
return ['saved' => false, 'why' => 'second leg'];
|
||||
}
|
||||
$system_id = intval($system['phone_system_id']);
|
||||
$domain = $cdr['domain'] !== '' ? phoneDomain($system, $cdr['domain']) : null;
|
||||
$domain_id = intval($domain['phone_domain_id'] ?? 0);
|
||||
$domain_client_id = intval($domain['phone_domain_client_id'] ?? 0);
|
||||
$call = phoneCallFromCdr($cdr);
|
||||
|
||||
// The other party - a contact, or a client - for calls with the outside world
|
||||
$who = $call['direction'] !== 'local' ? phoneWho($call['number'], $domain_client_id) : ['contact_id' => 0, 'client_id' => 0];
|
||||
$client_id = $domain_client_id && $domain_client_id !== intval($GLOBALS['config_internal_client_id'] ?? 0) ? $domain_client_id : ($who['client_id'] ?: $domain_client_id);
|
||||
$user_id = phoneUserForLine($call['extension'], (string) ($domain['phone_domain_name'] ?? ''));
|
||||
|
||||
$set = "phone_call_domain_id = $domain_id, phone_call_client_id = $client_id, phone_call_contact_id = " . intval($who['contact_id']) . ",
|
||||
phone_call_direction = '" . escapeSql($call['direction']) . "', phone_call_number = '" . escapeSql($call['number']) . "',
|
||||
phone_call_name = '" . escapeSql($call['name']) . "', phone_call_dialed = '" . escapeSql($call['dialed']) . "',
|
||||
phone_call_extension = '" . escapeSql($call['extension']) . "', phone_call_user_id = $user_id,
|
||||
phone_call_status = '" . escapeSql($call['status']) . "', phone_call_started_at = '" . escapeSql($call['started_at']) . "',
|
||||
phone_call_answered_at = " . ($call['answered_at'] ? "'" . escapeSql($call['answered_at']) . "'" : 'NULL') . ",
|
||||
phone_call_seconds = " . intval($call['seconds']) . ", phone_call_talk_seconds = " . intval($call['talk_seconds']) . ",
|
||||
phone_call_hangup_cause = '" . escapeSql($call['hangup_cause']) . "',
|
||||
phone_call_recording = " . ($call['recording'] !== '' ? "'" . escapeSql($call['recording']) . "'" : 'NULL');
|
||||
|
||||
mysqli_query($mysqli, "INSERT INTO phone_calls SET phone_call_system_id = $system_id, phone_call_uuid = '" . escapeSql($cdr['uuid']) . "', $set
|
||||
ON DUPLICATE KEY UPDATE $set");
|
||||
mysqli_query($mysqli, "UPDATE phone_systems SET phone_system_heard_at = NOW() WHERE phone_system_id = $system_id");
|
||||
|
||||
return ['saved' => true, 'why' => $call['status']];
|
||||
}
|
||||
|
||||
/** Inbound calls missed today that aren't on a ticket, of the clients this agent can reach - the Calls menu's count */
|
||||
function phoneMissedToday(): int
|
||||
{
|
||||
global $mysqli;
|
||||
if (!phoneTablesReady()) {
|
||||
return 0;
|
||||
}
|
||||
return intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FROM phone_calls
|
||||
WHERE phone_call_direction = 'inbound' AND phone_call_status IN ('missed', 'voicemail') AND phone_call_ticket_id = 0
|
||||
AND phone_call_started_at >= CURDATE()" . clientScopeSql('phone_call_client_id')))[0]);
|
||||
}
|
||||
|
||||
/** Calls where $where (SQL, built from ints) - ['count', 'rows' (the latest $limit, with their ticket and agent)] - of the clients this agent can reach */
|
||||
function phoneCallsFor(string $where, int $limit): array
|
||||
{
|
||||
global $mysqli;
|
||||
$scope = clientScopeSql('phone_call_client_id');
|
||||
$count = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FROM phone_calls WHERE $where$scope"))[0]);
|
||||
$rows = [];
|
||||
if ($count) {
|
||||
$sql = mysqli_query($mysqli, "SELECT phone_calls.*, phone_system_kind, phone_system_name, phone_system_web_url, contact_name, ticket_prefix, ticket_number, ticket_subject, user_name
|
||||
FROM phone_calls
|
||||
LEFT JOIN phone_systems ON phone_system_id = phone_call_system_id
|
||||
LEFT JOIN contacts ON contact_id = phone_call_contact_id AND phone_call_contact_id > 0
|
||||
LEFT JOIN tickets ON ticket_id = phone_call_ticket_id AND phone_call_ticket_id > 0
|
||||
LEFT JOIN users ON users.user_id = phone_call_user_id AND phone_call_user_id > 0
|
||||
WHERE $where$scope
|
||||
ORDER BY phone_call_started_at DESC, phone_call_id DESC
|
||||
LIMIT " . intval($limit));
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$rows[] = $row;
|
||||
}
|
||||
}
|
||||
return ['count' => $count, 'rows' => $rows];
|
||||
}
|
||||
|
||||
/** A short table of calls (phoneCallsFor() rows) - for a contact's Calls tab */
|
||||
function phoneCallsTableHtml(array $rows, bool $can_edit): string
|
||||
{
|
||||
$statuses = phoneCallStatuses();
|
||||
$directions = phoneCallDirections();
|
||||
$html = '<div class="table-responsive"><table class="table table-sm table-hover align-middle mb-0"><tbody>';
|
||||
foreach ($rows as $row) {
|
||||
[$status_label, $status_icon, $status_tone] = $statuses[$row['phone_call_status']] ?? [ucfirst((string) $row['phone_call_status']), 'fa-phone', 'secondary'];
|
||||
[$direction_label, $direction_icon] = $directions[$row['phone_call_direction']] ?? ['Call', 'fa-phone'];
|
||||
$answered = $row['phone_call_status'] === 'answered';
|
||||
$tone = $answered ? 'success' : $status_tone;
|
||||
$client_id = intval($row['phone_call_client_id']);
|
||||
$ticket_id = intval($row['phone_call_ticket_id']);
|
||||
$who = trim((string) ($row['user_name'] ?? '')) !== '' ? (string) $row['user_name'] : phoneNumberText($row['phone_call_extension']);
|
||||
$html .= '<tr data-phone-call-id="' . intval($row['phone_call_id']) . '">'
|
||||
. '<td class="ps-3" style="width: 2.5rem"><span class="itflow-phone-call-icon text-' . $tone . '-emphasis bg-' . $tone . '-subtle" title="' . escapeHtml("$direction_label - $status_label") . '"><i class="fas fa-fw ' . ($answered ? $direction_icon : $status_icon) . '"></i></span></td>'
|
||||
. '<td class="small w-100"><div>' . escapeHtml($direction_label) . ($answered ? ' · ' . escapeHtml(phoneSecondsText($row['phone_call_talk_seconds'])) : ' · <span class="text-' . $status_tone . '-emphasis">' . escapeHtml($status_label) . '</span>') . '</div>'
|
||||
. '<div class="text-body-secondary">' . escapeHtml(phoneNumberText($row['phone_call_number'])) . ($who !== '' ? ' · ' . escapeHtml($who) : '') . '</div></td>'
|
||||
. '<td class="small text-nowrap"><span title="' . formatDateTime($row['phone_call_started_at']) . '">' . escapeHtml(timeAgo($row['phone_call_started_at'])) . '</span></td>'
|
||||
. '<td class="small">' . ($ticket_id && $row['ticket_number'] !== null ? '<a href="ticket.php?client_id=' . $client_id . '&ticket_id=' . $ticket_id . '" title="' . escapeHtml((string) $row['ticket_subject']) . '">' . escapeHtml($row['ticket_prefix'] . $row['ticket_number']) . '</a>' : '') . '</td>'
|
||||
. '<td class="text-end pe-3">' . ($can_edit && $row['phone_call_direction'] !== 'local' ? '<button type="button" class="btn btn-sm btn-light ajax-modal" title="Put it on a ticket, with its talk time" data-modal-url="modals/phone/phone_call_ticket.php?id=' . intval($row['phone_call_id']) . '"><i class="fas fa-fw fa-life-ring"></i></button>' : '') . '</td>'
|
||||
. '</tr>';
|
||||
}
|
||||
return $html . '</tbody></table></div>';
|
||||
}
|
||||
|
||||
/** A call by id, with its contact, client, ticket and system - raw, or null */
|
||||
function phoneCallRow($phone_call_id): ?array
|
||||
{
|
||||
global $mysqli;
|
||||
$phone_call_id = intval($phone_call_id);
|
||||
if (!$phone_call_id || !phoneTablesReady()) {
|
||||
return null;
|
||||
}
|
||||
return mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT phone_calls.*, phone_systems.*, phone_domain_name, contact_name, client_name,
|
||||
ticket_prefix, ticket_number, ticket_subject, user_name
|
||||
FROM phone_calls
|
||||
JOIN phone_systems ON phone_system_id = phone_call_system_id
|
||||
LEFT JOIN phone_domains ON phone_domain_id = phone_call_domain_id
|
||||
LEFT JOIN contacts ON contact_id = phone_call_contact_id AND phone_call_contact_id > 0
|
||||
LEFT JOIN clients ON client_id = phone_call_client_id AND phone_call_client_id > 0
|
||||
LEFT JOIN tickets ON ticket_id = phone_call_ticket_id AND phone_call_ticket_id > 0
|
||||
LEFT JOIN users ON users.user_id = phone_call_user_id AND phone_call_user_id > 0
|
||||
WHERE phone_call_id = $phone_call_id")) ?: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The client a call belongs to for putting it on a ticket - 0 when it can go on any client's: a
|
||||
* caller nobody knows, on your own company's line or a domain not linked.
|
||||
*/
|
||||
function phoneCallClientFor(array $call): int
|
||||
{
|
||||
global $config_internal_client_id;
|
||||
$client_id = intval($call['phone_call_client_id']);
|
||||
if (!intval($call['phone_call_contact_id']) && ($client_id === 0 || $client_id === intval($config_internal_client_id ?? 0))) {
|
||||
return 0;
|
||||
}
|
||||
return $client_id;
|
||||
}
|
||||
|
||||
/** Who a call was with, for reading - the contact, the caller ID name, or the number */
|
||||
function phoneCallWho(array $call): string
|
||||
{
|
||||
if (trim((string) ($call['contact_name'] ?? '')) !== '') {
|
||||
return (string) $call['contact_name'];
|
||||
}
|
||||
if (trim((string) $call['phone_call_name']) !== '') {
|
||||
return (string) $call['phone_call_name'];
|
||||
}
|
||||
return phoneNumberText($call['phone_call_number']) ?: 'Unknown caller';
|
||||
}
|
||||
|
||||
/** A call in a sentence, for a ticket note - "Inbound call from Marla Whitfield (+1 (412) 555-0100) on ext 101, 4m 12s." Raw */
|
||||
function phoneCallSentence(array $call): string
|
||||
{
|
||||
$direction = (string) $call['phone_call_direction'];
|
||||
$who = phoneCallWho($call);
|
||||
$number = phoneNumberText($call['phone_call_number']);
|
||||
$with = $who . ($number !== '' && $number !== $who ? " ($number)" : '');
|
||||
$label = $direction === 'outbound' ? "Outbound call to $with" : ($direction === 'local' ? "Internal call with $with" : "Inbound call from $with");
|
||||
if ((string) $call['phone_call_extension'] !== '') {
|
||||
$label .= ' on ext ' . $call['phone_call_extension'];
|
||||
}
|
||||
$status = (string) $call['phone_call_status'];
|
||||
if ($status === 'answered') {
|
||||
$label .= ', ' . phoneSecondsText($call['phone_call_talk_seconds']);
|
||||
} else {
|
||||
$label .= ' - ' . strtolower(phoneCallStatuses()[$status][0] ?? $status);
|
||||
}
|
||||
return $label . '.';
|
||||
}
|
||||
|
||||
/**
|
||||
* Put a call on a ticket - an internal note saying what the call was, with its talk time as the
|
||||
* time worked (rounded up to the minute) when $count_time, and the call says which ticket it is on.
|
||||
* The note's id.
|
||||
*/
|
||||
function phoneCallToTicket(array $call, array $ticket, int $user_id, bool $count_time, string $note = ''): int
|
||||
{
|
||||
global $mysqli;
|
||||
|
||||
$ticket_id = intval($ticket['ticket_id']);
|
||||
// Time talking to someone - not a voicemail box
|
||||
$talk = $call['phone_call_status'] === 'answered' ? intval($call['phone_call_talk_seconds']) : 0;
|
||||
$time_worked = $count_time && $talk > 0 ? phoneTimeWorked($talk) : '00:00:00';
|
||||
$html = '<p>' . escapeHtml(phoneCallSentence($call)) . ' <small>(' . escapeHtml(formatDateTime($call['phone_call_started_at'])) . ')</small></p>';
|
||||
if (trim($note) !== '') {
|
||||
$html .= '<p>' . nl2br(escapeHtml(trim($note))) . '</p>';
|
||||
}
|
||||
$work_sql = '';
|
||||
if ($time_worked !== '00:00:00' && function_exists('ticketWorkTimesReady') && ticketWorkTimesReady() && $call['phone_call_answered_at']) {
|
||||
$end = date('Y-m-d H:i:s', strtotime($call['phone_call_answered_at']) + $talk);
|
||||
$work_sql = ", ticket_reply_work_start = '" . escapeSql($call['phone_call_answered_at']) . "', ticket_reply_work_end = '$end'";
|
||||
}
|
||||
|
||||
mysqli_query($mysqli, "INSERT INTO ticket_replies SET ticket_reply = '" . escapeSql($html) . "', ticket_reply_type = 'Internal',
|
||||
ticket_reply_time_worked = '$time_worked'$work_sql, ticket_reply_by = $user_id, ticket_reply_ticket_id = $ticket_id");
|
||||
$reply_id = intval(mysqli_insert_id($mysqli));
|
||||
mysqli_query($mysqli, "UPDATE tickets SET ticket_updated_at = NOW() WHERE ticket_id = $ticket_id");
|
||||
mysqli_query($mysqli, "UPDATE phone_calls SET phone_call_ticket_id = $ticket_id WHERE phone_call_id = " . intval($call['phone_call_id']));
|
||||
// A caller nobody knew is now the ticket's client - and its contact, when it has one
|
||||
if (!phoneCallClientFor($call)) {
|
||||
mysqli_query($mysqli, "UPDATE phone_calls SET phone_call_client_id = " . intval($ticket['ticket_client_id']) . ", phone_call_contact_id = " . intval($ticket['ticket_contact_id']) . "
|
||||
WHERE phone_call_id = " . intval($call['phone_call_id']));
|
||||
} elseif (!intval($call['phone_call_contact_id']) && intval($call['phone_call_client_id']) === intval($ticket['ticket_client_id'])) {
|
||||
mysqli_query($mysqli, "UPDATE phone_calls SET phone_call_contact_id = " . intval($ticket['ticket_contact_id']) . " WHERE phone_call_id = " . intval($call['phone_call_id']));
|
||||
}
|
||||
return $reply_id;
|
||||
}
|
||||
|
||||
/** Each night (cron/nightly_tasks.php): calls older than PHONE_CALLS_KEEP_DAYS that aren't on a ticket go */
|
||||
function phoneCleanup(): void
|
||||
{
|
||||
global $mysqli;
|
||||
if (!phoneTablesReady()) {
|
||||
return;
|
||||
}
|
||||
mysqli_query($mysqli, "DELETE FROM phone_calls WHERE phone_call_ticket_id = 0 AND phone_call_started_at < NOW() - INTERVAL " . PHONE_CALLS_KEEP_DAYS . " DAY");
|
||||
mysqli_query($mysqli, "DELETE FROM phone_events WHERE phone_event_at < NOW() - INTERVAL 2 DAY");
|
||||
}
|
||||
@@ -1901,7 +1901,8 @@ function ticketActivityHtml($reply, $row) {
|
||||
function ticketSourceIcon($source) {
|
||||
$icons = ['Email' => ['fa-envelope', 'Came in by email'], 'Portal' => ['fa-globe', 'Raised in the client portal'],
|
||||
'Recurring' => ['fa-redo-alt', 'Raised by a recurring ticket'], 'API' => ['fa-plug', 'Raised through the API'],
|
||||
'Project' => ['fa-project-diagram', 'From a project template'], 'Text' => ['fa-sms', 'Came in by text'], 'RMM' => ['fa-laptop-medical', 'Opened by an RMM alert']];
|
||||
'Project' => ['fa-project-diagram', 'From a project template'], 'Text' => ['fa-sms', 'Came in by text'], 'RMM' => ['fa-laptop-medical', 'Opened by an RMM alert'],
|
||||
'Phone' => ['fa-phone', 'From a phone call']];
|
||||
foreach ($icons as $match => [$icon, $title]) {
|
||||
if (stripos((string) $source, $match) !== false) {
|
||||
return '<i class="fas fa-fw ' . $icon . '" title="' . $title . '"></i>';
|
||||
|
||||
Reference in New Issue
Block a user