One Assets field for a ticket's assets in place of Asset and Other assets, in New Ticket, Edit Ticket, the Change assets window and New and Edit Recurring Ticket, with the first asset picked kept as the ticket's main asset and the others in ticket_assets; every asset on the ticket's Linked card can be taken off, the main one too, added and removed assets noted in the ticket history, all assets in the ticket quick look and in a copied ticket, and ticket search matching any of a ticket's assets; 26.12.6 changelog

This commit is contained in:
johnnyq
2026-12-05 02:44:17 -05:00
parent f750e3c00b
commit 2e51f18461
13 changed files with 258 additions and 214 deletions

View File

@@ -29,6 +29,7 @@ Update from Maintenance > Update as usual. This release has four database change
- Copy a Knowledge Base article's link from its **⋮** menu, on its page or beside it in the list - to send it to someone, or to link to it from another article. It offers the links that suit who can read it: **Copy Public Link** for an article for anyone, **Copy Portal Link** for one in the client portal, and **Copy Link** to its page here for one for agents only. A link that won't open yet is greyed out with the reason under it, such as **Publish it first** or **The public help center is off**. Agents who can read articles but not edit them get the menu with these links too.
- A **Vendors** permission in Admin > Roles, for your own vendors - the ones with no client: suppliers, distributors, the colo - at **None**, **Read**, **Modify** or **Full**. Set a role to **None** to keep its agents out of your suppliers' numbers, accounts and contacts: **Vendors** in the menu opens your clients' vendors instead, your own vendors' pages and their export are refused, search leaves them out, and where a client's license, network, ticket or service names one of yours, they see only its name. A client's vendors stay with the **Clients** permission. Adding, editing and deleting your own vendors moves from **Financial** to **Vendors** too.
- Link assets to each other - a label printer and its cutter, a server and its disk shelf, a desk phone and its sidecar - so a part that is swapped or sent off for repair on its own can be its own asset, with its own serial number and warranty, and still be found with what it goes on. **Link**, on the asset's new **Linked assets** card, picks from the client's other assets, of any type. A link goes both ways, each asset showing the other, and an asset can be linked to as many as it needs. Linking and unlinking are noted in both assets' History, so you can see which part was on which device and when it was swapped. A linked asset at another site says where it is, and its **⋮** menu moves either one to the other's site - nothing moves on its own. The assets list and the asset's quick look show what each asset is linked to, the client export lists the links, and assets transferred to another client together stay linked.
- A ticket's assets are one field, **Assets**, for one or several, in place of **Asset** and **Other assets** - in New Ticket, Edit, the ticket's **Change assets** window and New and Edit Recurring Ticket. The first asset picked fills in the person it's assigned to and its site, as before, and a ticket raised from an asset's page starts with that asset. On the ticket, every asset on the **Linked** card has a **×** to take it off, the first one too, the history says which assets were added and taken off, and its quick look lists them all. **Copy as a New Ticket** copies all its assets, and searching tickets finds a ticket by any of them.
### Bug Fixes
@@ -38,6 +39,7 @@ Update from Maintenance > Update as usual. This release has four database change
- A card is saved once per client, however often it's entered. A client who set up autopay on two recurring invoices with the same card - or added it again in the portal - ended up with the card saved twice, once for each, as Stripe gives the card a new reference each time it's entered. ITFlow now knows the same card when it comes back: the one saved card is kept, with the details just entered, and every recurring invoice it pays carries on with it. A card that is already saved twice is merged into one overnight - the copy that runs out last is kept and the autopay on the others moves to it - so you don't need to do anything, and its client isn't sent two expiry reminders for it.
- Email and phone links work in Knowledge Base articles and documents - a `mailto:` or `tel:` link showed as plain text on the published article, in the client portal and on the document, while the editor still had it. Those in an article or document written in Markdown were taken out when it was saved; the database update puts them back in the ones already saved, so they don't need saving again. Email and phone links in ticket replies, such as those in an email signature, work too.
- Opening one of your own vendors from a client's license or service works - it said there was nothing to see.
- A ticket's other assets show on it when its main asset was taken off - they were still on the ticket but not listed.
### Developer Updates
@@ -57,6 +59,7 @@ Update from Maintenance > Update as usual. This release has four database change
- `kbCopyLinks($row)` (`functions/kb.php`) gives an article's links for its readers - `[label, icon, url, why not, what it is]`, from `kbPublicUrl()`, `kbPortalArticleUrl()` and the new `kbAgentArticleUrl()` - with why one won't open yet (a draft, `kbPublicOn()` or `moduleOn('portal')` off); `kbCopyLinksMenuHtml()` makes them `.clipboardjs` dropdown items, or disabled ones with the reason in `.itflow-dropdown-why`. `agent/kb_article.php` and the rows of `agent/kb.php` show their ⋮ menu for `kbCan(1)` as well as `kbCan(2)` - with only these items for those who can't edit.
- Vendors permission: `module_vendor` (database 4.4.6, module 9 on a fresh install, `setup/seed_data.php` - Technician Read, Accountant Full); 4.4.6 gives every role `max(1, its module_financial level)`. In `functions/vendor.php`, `vendorPermissionReady()` (4.4.6) switches `vendorModule(0)` from `module_financial` to `module_vendor`, and `vendorCan(0, 1)` from always-yes to the permission; `vendorEnforce(0, $level)` enforces it at every level. `vendorIsOurs($vendor_id)` (one query a page) and `vendorCanOpen($vendor_id)` decide whether a mention of a vendor opens: `vendorLink()` and the new `vendorNameHtml()` show the name without a link, and `vendorRedact($row, $vendor_id)` blanks a row's `vendor_phone`, `vendor_account_number`, `vendor_code`, `vendor_email`, contacts and hours - used by `agent/network.php`, `agent/license.php`, `agent/ticket.php`, `agent/service.php` and the network, license and service quick looks. `vendorLink()` no longer puts a client's id on a page link to one of ours. The ours list (`agent/vendors.php`), its export, the vendor quick look and global search's Vendors group check it; the side nav's Vendors goes to `vendors.php?view=clients` without it. `roleModules()` lists modules in `roleModuleInfo()`'s order, then by id.
- Linked assets: `asset_links` (database 4.4.7) keeps each link as two rows, (`asset_id`, `linked_asset_id`) and the reverse, both `ON DELETE CASCADE`. `functions/asset_link.php` has `assetLinksReady()` (4.4.7), `assetLinked($asset_id)` (the client's assets, not archived), `assetLinkedNames($asset_ids)` for a list in one query, `assetLinkedTo()`, `assetLinkAdd($asset_id, $ids)` and `assetLinkRemove($asset_id, $other_id)` - each writes an `asset_history` row on both sides and `logAudit('Asset', 'Link')` or `'Unlink'` - and `assetLinkedListHtml()` for the card. The link picker (`functions/link_picker.php`) takes `asset|asset` in `LINK_PICKER_OWN`, with Support at Modify; it is kept out of `LINK_RELATIONS`, whose code assumes two different kinds. `agent/post/asset.php` adds `unlink_asset_link` and `move_linked_asset`, which sets one asset's location to the other's through `assetQuickSet()`, and `bulk_transfer_client_asset` copies the links between the assets it moves to their new copies. `clientExportLinks()` lists each pair once.
- Ticket assets: New Ticket, Edit Ticket, `agent/modals/ticket/ticket_edit_asset.php` and both recurring ticket windows post one `assets[]` multi-select, an `assets_form` marker and `asset_main`, in place of `asset_id` (`asset` in the Assets window) and `additional_assets[]`. The tables are unchanged. `ticketAssetsPosted($client_id, $main_field)` (`functions/ticket.php`) reads it - the client's own in the order posted, `asset_main` kept as `ticket_asset_id` while it is picked, else the first, the rest to `ticket_assets` - and still reads the old fields. `ticketAssetsSave()` writes both and notes the assets added and removed in `ticket_history`; `ticketOtherAssetIds()` lists the others by name; `ticketAssetsFieldHtml()` draws the field for the edit windows. `agent/js/tickets_add_modal.js` fills one `#assetSelect` and keeps the first asset picked in `#assetMain`. `remove_ticket_asset` (`delete_ticket_additional_asset` still works) takes the main asset off too, the first of the others by name taking its place. The tickets list search also looks through `ticket_assets`, and the ticket quick look (`agent/modals/ticket/ticket.php`) lists them.
## [26.12.5]

View File

@@ -38,9 +38,43 @@
}
}
// The ticket's main asset, out of the ones picked in Assets (one field for one or many): the one
// it was opened for, or else the first one picked, while it is still picked - kept in assetMain,
// which the server goes by (ticketAssetsPosted() in functions/ticket.php)
let pickedBefore = [];
function pickedAssets() {
const assets = field("assetSelect");
if (!assets) {
return [];
}
return Array.prototype.map.call(assets.selectedOptions, function (option) { return option.value; })
.filter(function (value) { return value && value !== '0'; });
}
function mainAsset() {
const picked = pickedAssets();
const main = field("assetMain");
if (main && picked.indexOf(main.value) !== -1) {
return main.value;
}
return picked.length ? picked[0] : '';
}
// After a pick: the main one stays while it is picked; else it is the first of those just
// picked - not one that happens to come first in the list
function keepMainAsset() {
const picked = pickedAssets();
const main = field("assetMain");
if (main && picked.indexOf(main.value) === -1) {
const added = picked.filter(function (value) { return pickedBefore.indexOf(value) === -1; });
main.value = added.length ? added[0] : (picked.length ? picked[0] : '');
}
pickedBefore = picked;
}
function fillFromAsset() {
const asset = field("assetSelect");
const links = asset ? assetLinks[asset.value] : null;
const links = assetLinks[mainAsset()] || null;
if (!links) {
return;
}
@@ -71,9 +105,9 @@
return;
}
const contact = field("contactSelect");
const asset = field("assetSelect");
const asset = mainAsset();
const client_id = currentClientId();
if (!client_id || ((!contact || contact.value === '0') && (!asset || !asset.value || asset.value === '0'))) {
if (!client_id || ((!contact || contact.value === '0') && !asset)) {
box.innerHTML = '';
return;
}
@@ -81,7 +115,7 @@
ticket_also_open: 1,
client_id: client_id,
contact_id: contact ? contact.value : 0,
asset_id: asset ? asset.value : 0
asset_id: asset || 0
}, function (data) {
try {
box.innerHTML = JSON.parse(data).html || '';
@@ -100,6 +134,7 @@
dropdown.addEventListener('change', function () {
touched[id] = true;
if (id === "assetSelect") {
keepMainAsset();
fillFromAsset();
} else if (id === "contactSelect") {
fillFromContact();
@@ -204,7 +239,15 @@
return;
}
dropdown.value = dropdown.dataset.selected;
// A multi-select (Assets) takes a list - "12,15"
if (dropdown.multiple) {
const wanted = dropdown.dataset.selected.split(',');
Array.prototype.forEach.call(dropdown.options, function (option) {
option.selected = wanted.indexOf(option.value) !== -1;
});
} else {
dropdown.value = dropdown.dataset.selected;
}
// What the modal was opened for counts as picked
touched[dropdown.id] = true;
@@ -343,11 +386,10 @@
);
}
// Populate client assets - feeds both the single asset picker and the additional assets
// multi-select from one request, as both need the same list
// Populate client assets - Assets, one field for one or many
function populateAssetsDropdowns(client_id) {
if (!document.getElementById("assetSelect") && !document.getElementById("additionalAssetsSelect")) {
if (!document.getElementById("assetSelect")) {
return;
}
@@ -364,13 +406,11 @@
// Access the data for assets (multiple)
const assets = response.assets || [];
const assetSelectDropdown = resetDropdown("assetSelect", '- None -', '0');
const additionalAssetsDropdown = resetDropdown("additionalAssetsSelect", null, null);
const assetSelectDropdown = resetDropdown("assetSelect", null, null);
// Assets arrive ordered by type, so a change of type starts a new group
let currentType = null;
let assetGroup = null;
let additionalAssetGroup = null;
assets.forEach(asset => {
const assetType = asset.asset_type || 'Uncategorized';
@@ -378,7 +418,6 @@
if (assetType !== currentType) {
currentType = assetType;
assetGroup = appendOptionGroup(assetSelectDropdown, assetType);
additionalAssetGroup = appendOptionGroup(additionalAssetsDropdown, assetType);
}
assetLinks[asset.asset_id] = {contact: asset.asset_contact_id, location: asset.asset_location_id};
@@ -386,10 +425,10 @@
const assetLabel = buildAssetLabel(asset);
appendGroupedOption(assetGroup, assetLabel, asset.asset_id, asset.asset_icon);
appendGroupedOption(additionalAssetGroup, assetLabel, asset.asset_id, asset.asset_icon);
});
applyPreselection(assetSelectDropdown);
pickedBefore = pickedAssets();
loaded();

View File

@@ -6,8 +6,8 @@ require_once '../../../includes/modal_header.php';
* New Recurring Ticket - what the tickets it raises will say (Details), how
* often and from when, with their priority, category and billing (Planning),
* what they are about and their tasks. Opened from a client, contact or
* asset, it starts with that filled in; picking an asset fills in the person
* it is assigned to.
* asset, it starts with that filled in. Assets is one field for one or many;
* the first asset picked fills in the person it is assigned to.
*/
enforceUserPermission('module_support', 2);
@@ -226,21 +226,14 @@ ob_start();
<div class="tab-pane fade" id="pills-add-assets">
<div class="mb-3">
<label class="form-label">Asset</label>
<label class="form-label">Assets</label>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-fw fa-desktop"></i></span>
<select class="form-select select2" name="asset_id" id="assetSelect" data-selected="<?= $asset_id ?>">
</select>
</div>
</div>
<div class="mb-3">
<label class="form-label">Other assets</label>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-fw fa-desktop"></i></span>
<select class="form-select select2" name="additional_assets[]" id="additionalAssetsSelect" data-placeholder="- Any other assets involved -" multiple>
<select class="form-select select2" name="assets[]" id="assetSelect" data-selected="<?= $asset_id ?: '' ?>" data-placeholder="- Select assets -" multiple>
</select>
</div>
<input type="hidden" name="assets_form" value="1">
<input type="hidden" name="asset_main" id="assetMain" value="<?= $asset_id ?>">
</div>
<?php if (!$client_id) { ?>

View File

@@ -230,39 +230,7 @@ ob_start();
<div class="tab-pane fade" id="pills-recurring-related">
<div class="mb-3">
<label class="form-label">Asset</label>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-fw fa-desktop"></i></span>
<select class="form-select select2" name="asset_id">
<option value="0">- None -</option>
<?php
$assets = [];
$sql_assets = mysqli_query($mysqli, "SELECT asset_id, asset_name, asset_type, asset_make, asset_model, asset_archived_at, contact_name FROM assets
LEFT JOIN contacts ON contact_id = asset_contact_id
WHERE asset_client_id = $client_id AND (asset_archived_at IS NULL OR asset_id IN (" . implode(',', array_merge([0, $asset_id], $other_asset_ids)) . "))
ORDER BY asset_name ASC");
while ($row = mysqli_fetch_assoc($sql_assets)) {
$assets[] = $row;
}
foreach ($assets as $row) {
$asset_id_select = intval($row['asset_id']); ?>
<option <?= $asset_id === $asset_id_select ? 'selected' : '' ?> value="<?= $asset_id_select ?>"<?= selectOptionAsset($row['asset_type'] ?? '', trim(($row['asset_make'] ?? '') . ' ' . ($row['asset_model'] ?? '') . ($row['contact_name'] ? ' - ' . $row['contact_name'] : ''))) ?>><?= escapeHtml($row['asset_name']) ?><?= $row['asset_archived_at'] ? ' (archived)' : '' ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="mb-3">
<label class="form-label">Other assets</label>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-fw fa-desktop"></i></span>
<select class="form-select select2" name="additional_assets[]" data-placeholder="- Any other assets involved -" multiple>
<?php foreach ($assets as $row) {
$asset_id_select = intval($row['asset_id']); ?>
<option value="<?= $asset_id_select ?>" <?= in_array($asset_id_select, $other_asset_ids, true) ? 'selected' : '' ?><?= selectOptionAsset($row['asset_type'] ?? '', trim(($row['asset_make'] ?? '') . ' ' . ($row['asset_model'] ?? ''))) ?>><?= escapeHtml($row['asset_name']) ?><?= $row['asset_archived_at'] ? ' (archived)' : '' ?></option>
<?php } ?>
</select>
</div>
<?= ticketAssetsFieldHtml($client_id, $asset_id, $other_asset_ids) ?>
</div>
</div>

View File

@@ -163,9 +163,26 @@ ob_start();
<dt>Assigned</dt>
<dd><?= formatAssignee($ticket['ticket_assigned_to'], $ticket['user_name'], $ticket['user_avatar'], $is_open) ?></dd>
<?php if (intval($ticket['ticket_asset_id']) && $ticket['asset_name'] !== null && moduleOn('itdoc')) { ?>
<dt>Asset</dt>
<dd><a class="ajax-modal" href="#" data-modal-size="lg" data-modal-url="modals/asset/asset.php?id=<?= intval($ticket['ticket_asset_id']) ?>"><i class="fas fa-desktop text-body-secondary me-1"></i><?= escapeHtml($ticket['asset_name']) ?></a></dd>
<?php
// Its assets - the main one first, then the others by name
$quick_assets = [];
if (moduleOn('itdoc')) {
if (intval($ticket['ticket_asset_id']) && $ticket['asset_name'] !== null) {
$quick_assets[] = [intval($ticket['ticket_asset_id']), $ticket['asset_name']];
}
$sql_quick_assets = mysqli_query($mysqli, "SELECT assets.asset_id, asset_name FROM ticket_assets JOIN assets ON assets.asset_id = ticket_assets.asset_id
WHERE ticket_id = $ticket_id AND ticket_assets.asset_id != " . intval($ticket['ticket_asset_id']) . " ORDER BY asset_name ASC");
while ($quick_asset = mysqli_fetch_row($sql_quick_assets)) {
$quick_assets[] = [intval($quick_asset[0]), $quick_asset[1]];
}
}
if ($quick_assets) { ?>
<dt><?= count($quick_assets) === 1 ? 'Asset' : 'Assets' ?></dt>
<dd>
<?php foreach ($quick_assets as [$quick_asset_id, $quick_asset_name]) { ?>
<div class="text-truncate"><a class="ajax-modal" href="#" data-modal-size="lg" data-modal-url="modals/asset/asset.php?id=<?= $quick_asset_id ?>"><i class="fas fa-desktop text-body-secondary me-1"></i><?= escapeHtml($quick_asset_name) ?></a></div>
<?php } ?>
</dd>
<?php } ?>
<?php if (intval($ticket['ticket_location_id']) && $ticket['location_name'] !== null) { ?>

View File

@@ -7,13 +7,14 @@ require_once '../../../includes/modal_header.php';
* due, the status it starts in, the work booked, its tags and billing
* (Planning), what it is related to, and its tasks. Opened from a client,
* contact, asset, site, vendor, project or service, it starts with that
* filled in; picking an asset fills in the person it is assigned to and its
* site. When the person or the asset already has open tickets, they show
* filled in. Assets is one field for one or many; the first asset picked fills
* in the person it is assigned to and its site. When the person or the asset
* already has open tickets, they show
* under the contact - so a second ticket for the same problem gets noticed
* before it is raised.
*
* Copy of a ticket (copy_ticket_id - its More actions > Copy as a new ticket): it starts with
* that ticket's subject, details, priority, category, billing, contact, asset, site, vendor and
* that ticket's subject, details, priority, category, billing, contact, assets, site, vendor and
* tasks, for the same client - which can be changed, and its lists follow.
*/
@@ -63,6 +64,7 @@ $prefill_priority = 'Medium';
$prefill_category_id = 0;
$prefill_billable = intval($config_ticket_default_billable) === 1;
$existing_tasks = [];
$copy_other_asset_ids = [];
if ($copy) {
$prefill_subject = (string) $copy['ticket_subject'];
$prefill_details_html = (string) $copy['ticket_details'];
@@ -75,6 +77,7 @@ if ($copy) {
if ($client_id === intval($copy['ticket_client_id']) || $copy_client_id) {
$contact_id = $contact_id ?: intval($copy['ticket_contact_id']);
$asset_id = $asset_id ?: intval($copy['ticket_asset_id']);
$copy_other_asset_ids = ticketOtherAssetIds($copy['ticket_id'], $asset_id);
$location_id = $location_id ?: intval($copy['ticket_location_id']);
$vendor_id = $vendor_id ?: intval($copy['ticket_vendor_id']);
}
@@ -366,21 +369,14 @@ ob_start();
</div>
<div class="mb-3">
<label class="form-label">Asset</label>
<label class="form-label">Assets</label>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-fw fa-desktop"></i></span>
<select class="form-select select2" name="asset_id" id="assetSelect" data-selected="<?= $asset_id ?>">
</select>
</div>
</div>
<div class="mb-3">
<label class="form-label">Other assets</label>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-fw fa-desktop"></i></span>
<select class="form-select select2" name="additional_assets[]" id="additionalAssetsSelect" data-placeholder="- Select additional assets -" multiple>
<select class="form-select select2" name="assets[]" id="assetSelect" data-selected="<?= implode(',', array_merge(array_filter([$asset_id]), $copy_other_asset_ids)) ?>" data-placeholder="- Select assets -" multiple>
</select>
</div>
<input type="hidden" name="assets_form" value="1">
<input type="hidden" name="asset_main" id="assetMain" value="<?= $asset_id ?>">
</div>
<div class="mb-3">

View File

@@ -39,11 +39,7 @@ $ticket_billable = intval($ticket['ticket_billable']);
$ticket_due_at = $ticket['ticket_due_at'] ? date('Y-m-d\TH:i', strtotime($ticket['ticket_due_at'])) : '';
$edit_tab = ($_GET['tab'] ?? '') === 'planning' ? 'planning' : 'details';
$other_asset_ids = [];
$sql_other_assets = mysqli_query($mysqli, "SELECT asset_id FROM ticket_assets WHERE ticket_id = $ticket_id");
while ($row = mysqli_fetch_row($sql_other_assets)) {
$other_asset_ids[] = intval($row[0]);
}
$other_asset_ids = ticketOtherAssetIds($ticket_id, $asset_id);
ob_start();
@@ -205,39 +201,7 @@ ob_start();
<?php if ($client_id) { ?>
<div class="mb-3">
<label class="form-label">Asset</label>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-fw fa-desktop"></i></span>
<select class="form-select select2" name="asset_id">
<option value="0">- None -</option>
<?php
$assets = [];
$sql_assets = mysqli_query($mysqli, "SELECT asset_id, asset_name, asset_type, asset_make, asset_model, asset_archived_at, contact_name FROM assets
LEFT JOIN contacts ON contact_id = asset_contact_id
WHERE asset_client_id = $client_id AND (asset_archived_at IS NULL OR asset_id = $asset_id OR asset_id IN (0" . ($other_asset_ids ? ',' . implode(',', $other_asset_ids) : '') . "))
ORDER BY asset_name ASC");
while ($row = mysqli_fetch_assoc($sql_assets)) {
$assets[] = $row;
}
foreach ($assets as $row) {
$asset_id_select = intval($row['asset_id']); ?>
<option <?= $asset_id === $asset_id_select ? 'selected' : '' ?> value="<?= $asset_id_select ?>"<?= selectOptionAsset($row['asset_type'] ?? '', trim(($row['asset_make'] ?? '') . ' ' . ($row['asset_model'] ?? '') . ($row['contact_name'] ? ' - ' . $row['contact_name'] : ''))) ?>><?= escapeHtml($row['asset_name']) ?><?= $row['asset_archived_at'] ? ' (archived)' : '' ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="mb-3">
<label class="form-label">Other assets</label>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-fw fa-desktop"></i></span>
<select class="form-select select2" name="additional_assets[]" data-placeholder="- Any other assets involved -" multiple>
<?php foreach ($assets as $row) {
$asset_id_select = intval($row['asset_id']); ?>
<option value="<?= $asset_id_select ?>" <?= in_array($asset_id_select, $other_asset_ids, true) ? 'selected' : '' ?><?= selectOptionAsset($row['asset_type'] ?? '', trim(($row['asset_make'] ?? '') . ' ' . ($row['asset_model'] ?? ''))) ?>><?= escapeHtml($row['asset_name']) ?><?= $row['asset_archived_at'] ? ' (archived)' : '' ?></option>
<?php } ?>
</select>
</div>
<?= ticketAssetsFieldHtml($client_id, $asset_id, $other_asset_ids) ?>
</div>
<div class="mb-3">

View File

@@ -20,15 +20,8 @@ if (!$client_id) {
exit;
}
// Additional Assets Selected
$additional_assets_array = array();
$sql_additional_assets = mysqli_query($mysqli, "SELECT asset_id FROM ticket_assets WHERE ticket_id = $ticket_id");
while ($row = mysqli_fetch_assoc($sql_additional_assets)) {
$additional_asset_id = intval($row['asset_id']);
$additional_assets_array[] = $additional_asset_id;
}
// What it links to now stays on offer even once archived, so saving doesn't drop it
$linked_asset_ids = implode(',', array_merge([0, $asset_id], $additional_assets_array));
// Its assets in one field, for one or many - its main one, and the others
$other_asset_ids = ticketOtherAssetIds($ticket_id, $asset_id);
ob_start();
@@ -44,49 +37,7 @@ ob_start();
<div class="modal-body">
<div class="mb-3">
<label class="form-label">Primary asset</label>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-fw fa-desktop"></i></span>
<select class="form-select select2" name="asset">
<option value="0">- No asset -</option>
<?php
$sql_assets = mysqli_query($mysqli, "SELECT asset_id, asset_name, asset_type, asset_make, asset_model, asset_archived_at, contact_name FROM assets LEFT JOIN contacts ON contact_id = asset_contact_id WHERE asset_client_id = $client_id AND (asset_archived_at IS NULL OR asset_id IN ($linked_asset_ids)) ORDER BY asset_name ASC");
while ($row = mysqli_fetch_assoc($sql_assets)) {
$asset_id_select = intval($row['asset_id']);
$asset_name_select = escapeHtml($row['asset_name']) . ($row['asset_archived_at'] ? ' (archived)' : '');
$asset_contact_name_select = escapeHtml($row['contact_name']);
?>
<option <?php if ($asset_id == $asset_id_select) { echo "selected"; } ?> value="<?= $asset_id_select ?>"<?= selectOptionAsset($row['asset_type'] ?? '', trim(($row['asset_make'] ?? '') . ' ' . ($row['asset_model'] ?? ''))) ?>><?= $asset_contact_name_select ? "$asset_name_select ($asset_contact_name_select)" : $asset_name_select ?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="mb-3">
<label class="form-label">Other assets</label>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-fw fa-desktop"></i></span>
<select class="form-select select2" name="additional_assets[]" data-placeholder="- Any other assets involved -" multiple>
<option value=""></option>
<?php
$sql_assets = mysqli_query($mysqli, "SELECT asset_id, asset_name, asset_type, asset_make, asset_model, asset_archived_at, contact_name FROM assets LEFT JOIN contacts ON contact_id = asset_contact_id WHERE asset_client_id = $client_id AND asset_id != $asset_id AND (asset_archived_at IS NULL OR asset_id IN ($linked_asset_ids)) ORDER BY asset_name ASC");
while ($row = mysqli_fetch_assoc($sql_assets)) {
$asset_id_select = intval($row['asset_id']);
$asset_name_select = escapeHtml($row['asset_name']) . ($row['asset_archived_at'] ? ' (archived)' : '');
$asset_contact_name_select = escapeHtml($row['contact_name']);
?>
<option value="<?= $asset_id_select ?>"
<?php if (in_array($asset_id_select, $additional_assets_array)) { echo "selected"; } ?>
<?= selectOptionAsset($row['asset_type'] ?? '', trim(($row['asset_make'] ?? '') . ' ' . ($row['asset_model'] ?? ''))) ?>><?= $asset_contact_name_select ? "$asset_name_select ($asset_contact_name_select)" : $asset_name_select ?></option>
<?php } ?>
</select>
</div>
<?= ticketAssetsFieldHtml($client_id, $asset_id, $other_asset_ids) ?>
</div>
</div>

View File

@@ -64,6 +64,10 @@ function recurringTicketFromPost($client_id, $current = null) {
. ", recurring_ticket_lead_mode = '$mode'";
}
// Its assets, one field for one or many - the main one on the schedule, the others beside it.
// A form without the field keeps what it has.
$assets = ticketAssetsPosted($client_id);
return [
'subject' => escapeSql(mb_substr($subject, 0, 500)),
'details' => mysqli_real_escape_string($mysqli, (string) ($_POST['details'] ?? '')),
@@ -72,7 +76,8 @@ function recurringTicketFromPost($client_id, $current = null) {
'billable' => $billable,
'assigned_to' => ticketAgentId($_POST['assigned_to'] ?? 0),
'contact_id' => ticketClientOwnedId('contact', $_POST['contact_id'] ?? 0, $client_id),
'asset_id' => ticketClientOwnedId('asset', $_POST['asset_id'] ?? 0, $client_id),
'asset_id' => $assets ? $assets[0] : intval($current['recurring_ticket_asset_id'] ?? 0),
'other_asset_ids' => $assets ? $assets[1] : null,
'category_id' => ticketCategoryId($_POST['category_id'] ?? 0),
'template_id' => $template_id,
'fill_ins' => $fill_ins,
@@ -146,7 +151,7 @@ if (isset($_POST['add_recurring_ticket'])) {
$recurring_ticket_id = mysqli_insert_id($mysqli);
recurringTicketSetOtherAssets($recurring_ticket_id, ticketClientOwnedAssetIds($_POST['additional_assets'] ?? [], $client_id, $f['asset_id']));
recurringTicketSetOtherAssets($recurring_ticket_id, $f['other_asset_ids'] ?? []);
recurringTicketSetTasks($recurring_ticket_id);
if ($f['fill_ins'] !== null) {
ticketFillInsStore('recurring_ticket', $recurring_ticket_id, $f['fill_ins']);
@@ -193,9 +198,8 @@ if (isset($_POST['edit_recurring_ticket'])) {
recurring_ticket_contact_id = {$f['contact_id']}, recurring_ticket_category = {$f['category_id']}, recurring_ticket_ticket_template_id = {$f['template_id']} {$f['ahead']} $by_day_sql
WHERE recurring_ticket_id = $recurring_ticket_id");
// The other assets go with the asset field - an empty multi-select posts nothing
if (isset($_POST['asset_id'])) {
recurringTicketSetOtherAssets($recurring_ticket_id, ticketClientOwnedAssetIds($_POST['additional_assets'] ?? [], $client_id, $f['asset_id']));
if ($f['other_asset_ids'] !== null) {
recurringTicketSetOtherAssets($recurring_ticket_id, $f['other_asset_ids']);
}
if (isset($_POST['tasks_submitted'])) {
recurringTicketSetTasks($recurring_ticket_id);

View File

@@ -93,11 +93,11 @@ if (isset($_POST['add_ticket'])) {
$primary = mysqli_fetch_row(mysqli_query($mysqli, "SELECT contact_id FROM contacts WHERE contact_client_id = $client_id AND contact_primary = 1 AND contact_archived_at IS NULL LIMIT 1"));
$contact_id = $primary ? intval($primary[0]) : $contact_id;
}
$asset_id = ticketClientOwnedId('asset', $_POST['asset_id'] ?? 0, $client_id);
// Its assets, one field for one or many - the main one is kept on the ticket, the others beside it
[$asset_id, $other_asset_ids] = ticketAssetsPosted($client_id) ?? [0, []];
$location_id = ticketClientOwnedId('location', $_POST['location_id'] ?? 0, $client_id);
$vendor_id = ticketClientOwnedId('vendor', $_POST['vendor_id'] ?? 0, $client_id);
$project_id = ticketClientOwnedId('project', $_POST['project_id'] ?? 0, $client_id);
$other_asset_ids = ticketClientOwnedAssetIds($_POST['additional_assets'] ?? [], $client_id, $asset_id);
// Atomically increment and get the new ticket number
mysqli_query($mysqli, "UPDATE settings SET config_ticket_next_number = LAST_INSERT_ID(config_ticket_next_number), config_ticket_next_number = config_ticket_next_number + 1 WHERE company_id = 1");
@@ -312,11 +312,8 @@ if (isset($_POST['edit_ticket'])) {
$contact_id = ticketClientOwnedId('contact', $_POST['contact_id'], $client_id);
$set .= ", ticket_contact_id = $contact_id";
}
$asset_posted = isset($_POST['asset_id']);
if ($asset_posted) {
$asset_id = ticketClientOwnedId('asset', $_POST['asset_id'], $client_id);
$set .= ", ticket_asset_id = $asset_id";
}
// Its assets - saved, with what changed noted, once the rest is
$assets = ticketAssetsPosted($client_id);
if (isset($_POST['location_id'])) {
$set .= ", ticket_location_id = " . ticketClientOwnedId('location', $_POST['location_id'], $client_id);
}
@@ -340,9 +337,9 @@ if (isset($_POST['edit_ticket'])) {
syncTicketSlaClock($ticket_id);
}
// An empty multi-select posts nothing, so the other assets go with the asset field
if ($asset_posted) {
ticketSetOtherAssets($ticket_id, ticketClientOwnedAssetIds($_POST['additional_assets'] ?? [], $client_id, $asset_id));
if ($assets !== null) {
$assets_before = [intval($row['ticket_asset_id']), ticketOtherAssetIds($ticket_id, $row['ticket_asset_id'])];
ticketAssetsSave($ticket_id, $assets[0], $assets[1], $assets_before);
}
// ...and the tags with their marker
@@ -539,20 +536,24 @@ if (isset($_GET['delete_ticket_cc'])) {
}
if (isset($_GET['delete_ticket_additional_asset'])) {
// Takes one asset off a ticket (the x beside it on the ticket page) - its main one too, the first
// of the others by name taking its place. delete_ticket_additional_asset is the name from before.
if (isset($_GET['remove_ticket_asset']) || isset($_GET['delete_ticket_additional_asset'])) {
validateCSRFToken();
$row = ticketForRequest($_GET['ticket_id'] ?? 0, 2);
$ticket_id = intval($row['ticket_id']);
$asset_id = intval($_GET['delete_ticket_additional_asset']);
$asset_name = (string) getFieldById('assets', $asset_id, 'asset_name');
mysqli_query($mysqli, "DELETE FROM ticket_assets WHERE ticket_id = $ticket_id AND asset_id = $asset_id");
$asset_id = intval($_GET['remove_ticket_asset'] ?? $_GET['delete_ticket_additional_asset']);
$main_asset_id = intval($row['ticket_asset_id']);
$other_asset_ids = ticketOtherAssetIds($ticket_id, $main_asset_id);
// Named only when it was on this ticket - any other id would say another client's asset name back
if (mysqli_affected_rows($mysqli)) {
logTicketHistory($ticket_id, "$session_name removed additional asset " . escapeSql($asset_name));
if ($asset_id && ($asset_id === $main_asset_id || in_array($asset_id, $other_asset_ids, true))) {
$asset_name = (string) getFieldById('assets', $asset_id, 'asset_name');
$left = array_values(array_diff($other_asset_ids, [$asset_id]));
$new_main = $asset_id === $main_asset_id ? intval(array_shift($left)) : $main_asset_id;
ticketAssetsSave($ticket_id, $new_main, $left, [$main_asset_id, $other_asset_ids]);
logAudit("Ticket", "Edit", "$session_name removed asset " . escapeSql($asset_name) . " from ticket " . escapeSql(ticketReference($row)), $client_id, $ticket_id);
flashAlert("Removed asset <strong>" . escapeHtml($asset_name) . "</strong> from the ticket", 'error');
} else {
@@ -569,26 +570,21 @@ if (isset($_POST['edit_ticket_asset'])) {
$row = ticketForRequest($_POST['ticket_id'] ?? 0, 2);
$ticket_id = intval($row['ticket_id']);
$asset_id = ticketClientOwnedId('asset', $_POST['asset'] ?? 0, $client_id);
$reference = escapeSql(ticketReference($row));
$main_asset_id = intval($row['ticket_asset_id']);
$before = [$main_asset_id, ticketOtherAssetIds($ticket_id, $main_asset_id)];
mysqli_query($mysqli, "UPDATE tickets SET ticket_asset_id = $asset_id WHERE ticket_id = $ticket_id");
// One field for one or many (a form from before has the main one as asset, and additional_assets[])
[$asset_id, $other_asset_ids] = ticketAssetsPosted($client_id, 'asset') ?? $before;
$all_ids = array_merge(array_filter([$asset_id]), $other_asset_ids);
$names = $all_ids ? array_column(mysqli_fetch_all(mysqli_query($mysqli, "SELECT asset_name FROM assets WHERE asset_id IN (" . implode(',', $all_ids) . ") ORDER BY asset_id = $asset_id DESC, asset_name ASC")), 0) : [];
// The others, when the form carried them
if (isset($_POST['additional_assets']) || isset($_POST['additional_assets_form'])) {
ticketSetOtherAssets($ticket_id, ticketClientOwnedAssetIds($_POST['additional_assets'] ?? [], $client_id, $asset_id));
} else {
// The primary asset is never also one of the others
mysqli_query($mysqli, "DELETE FROM ticket_assets WHERE ticket_id = $ticket_id AND asset_id = $asset_id");
}
ticketAssetsSave($ticket_id, $asset_id, $other_asset_ids, $before);
$asset_name = $asset_id ? (string) getFieldById('assets', $asset_id, 'asset_name') : '';
if ($asset_id !== intval($row['ticket_asset_id'])) {
logTicketHistory($ticket_id, $asset_id ? "$session_name set the asset to " . escapeSql($asset_name) : "$session_name removed the asset");
}
logAudit("Ticket", "Edit", "$session_name " . ($asset_id ? "changed asset to " . escapeSql($asset_name) : "removed the asset") . " for ticket $reference", $client_id, $ticket_id);
$names_text = implode(', ', $names);
logAudit("Ticket", "Edit", "$session_name " . ($names ? "set the assets to " . escapeSql($names_text) : "removed the assets") . " for ticket $reference", $client_id, $ticket_id);
flashAlert($asset_id ? "Ticket <strong>" . escapeHtml(ticketReference($row)) . "</strong> asset set to <strong>" . escapeHtml($asset_name) . "</strong>" : "Removed the asset from ticket <strong>" . escapeHtml(ticketReference($row)) . "</strong>");
flashAlert($names ? "Ticket <strong>" . escapeHtml(ticketReference($row)) . "</strong> assets: <strong>" . escapeHtml($names_text) . "</strong>" : "Removed the assets from ticket <strong>" . escapeHtml(ticketReference($row)) . "</strong>");
redirect();

View File

@@ -318,11 +318,12 @@ if (isset($_GET['ticket_id'])) {
$sql_ticket_ccs = mysqli_query($mysqli, "SELECT ticket_cc_email, ticket_cc_id FROM ticket_ccs WHERE ticket_cc_ticket_id = $ticket_id ORDER BY ticket_cc_email DESC");
$cc_count = mysqli_num_rows($sql_ticket_ccs);
// Get Additional Assets
// Its assets besides its main one, by name
$sql_additional_assets = mysqli_query($mysqli, "SELECT assets.asset_id, asset_name, asset_type FROM assets, ticket_assets
WHERE assets.asset_id = ticket_assets.asset_id
AND ticket_id = $ticket_id
AND assets.asset_id != $asset_id"
AND assets.asset_id != $asset_id
ORDER BY asset_name ASC"
);
// Get Tasks
@@ -1887,7 +1888,8 @@ if (isset($_GET['ticket_id'])) {
$can_link = $can_edit_ticket && !$ticket_is_closed;
// Its assets and services are IT documentation - not shown while that is switched off (Admin > Settings > Modules)
$ticket_itdoc = moduleOn('itdoc');
$ticket_asset_shown = $ticket_itdoc ? $asset_id : 0;
// Its main asset first, then the others - all one list, each can be taken off
$ticket_asset_shown = $ticket_itdoc && ($asset_id || mysqli_num_rows($sql_additional_assets));
// The services it is about - raised for them, or through its assets
$ticket_services = $client_id && $ticket_itdoc ? ticketServices($ticket_id) : [];
$has_links = $ticket_asset_shown || $vendor_id || $project_id || $cc_count || $ticket_services;
@@ -1905,7 +1907,7 @@ if (isset($_GET['ticket_id'])) {
<?php if ($client_id) { ?>
<?php if ($ticket_itdoc) { ?>
<a class="dropdown-item ajax-modal" href="#" data-modal-url="modals/ticket/ticket_edit_asset.php?id=<?= $ticket_id ?>">
<i class="fas fa-fw fa-desktop me-2"></i><?= $asset_id ? 'Change assets' : 'Add asset' ?>
<i class="fas fa-fw fa-desktop me-2"></i><?= $ticket_asset_shown ? 'Change assets' : 'Add assets' ?>
</a>
<?php } ?>
<a class="dropdown-item ajax-modal" href="#" data-modal-url="modals/ticket/ticket_edit_vendor.php?ticket_id=<?= $ticket_id ?>">
@@ -1936,11 +1938,18 @@ if (isset($_GET['ticket_id'])) {
<?php if ($ticket_asset_shown) { ?>
<div class="ticket-link-heading">Assets</div>
<div class="mt-1 text-truncate">
<a class="ajax-modal" href="#" data-modal-size="lg" data-modal-url="modals/asset/asset.php?<?= $client_url ?>&id=<?= $asset_id ?>" title="<?= $asset_name ?>">
<i class="fas fa-fw fa-<?= $asset_icon ?> text-secondary me-2"></i><?= $asset_name ?>
</a>
</div>
<?php if ($asset_id) { ?>
<div class="mt-1 d-flex align-items-center">
<a class="ajax-modal text-truncate flex-grow-1" href="#" data-modal-size="lg" data-modal-url="modals/asset/asset.php?<?= $client_url ?>&id=<?= $asset_id ?>" title="<?= $asset_name ?>">
<i class="fas fa-fw fa-<?= $asset_icon ?> text-secondary me-2"></i><?= $asset_name ?>
</a>
<?php if ($can_link) { ?>
<a class="confirm-link ms-2" href="post.php?remove_ticket_asset=<?= $asset_id ?>&ticket_id=<?= $ticket_id ?>&csrf_token=<?= $_SESSION['csrf_token'] ?>" title="Remove from this ticket">
<i class="fas fa-fw fa-times text-secondary"></i>
</a>
<?php } ?>
</div>
<?php } ?>
<?php while ($additional_asset_row = mysqli_fetch_assoc($sql_additional_assets)) {
$additional_asset_id = intval($additional_asset_row['asset_id']);
$additional_asset_name = escapeHtml($additional_asset_row['asset_name']);
@@ -1951,7 +1960,7 @@ if (isset($_GET['ticket_id'])) {
<i class="fas fa-fw fa-<?= $additional_asset_icon ?> text-secondary me-2"></i><?= $additional_asset_name ?>
</a>
<?php if ($can_link) { ?>
<a class="confirm-link ms-2" href="post.php?delete_ticket_additional_asset=<?= $additional_asset_id ?>&ticket_id=<?= $ticket_id ?>&csrf_token=<?= $_SESSION['csrf_token'] ?>" title="Remove from this ticket">
<a class="confirm-link ms-2" href="post.php?remove_ticket_asset=<?= $additional_asset_id ?>&ticket_id=<?= $ticket_id ?>&csrf_token=<?= $_SESSION['csrf_token'] ?>" title="Remove from this ticket">
<i class="fas fa-fw fa-times text-secondary"></i>
</a>
<?php } ?>

View File

@@ -188,6 +188,7 @@ if ($q !== '') {
$filter_sql[] = "AND (CONCAT(ticket_prefix, ticket_number) LIKE '%$q%' OR ticket_subject LIKE '%$q%' OR client_name LIKE '%$q%'
OR contact_name LIKE '%$q%' OR contact_email LIKE '%$q%'" . contactEmailAltLikeSql($q) . " OR ticket_status_name LIKE '%$q%' OR ticket_priority LIKE '%$q%'
OR user_name LIKE '%$q%' OR asset_name LIKE '%$q%' OR vendor_name LIKE '%$q%' OR ticket_vendor_ticket_number LIKE '%$q%' OR category_name LIKE '%$q%'
OR tickets.ticket_id IN (SELECT ticket_assets.ticket_id FROM ticket_assets JOIN assets AS searched_assets ON searched_assets.asset_id = ticket_assets.asset_id WHERE searched_assets.asset_name LIKE '%$q%')
" . tagSearchSql('ticket', $q, 'tickets.ticket_id') . ")";
}

View File

@@ -200,6 +200,109 @@ function ticketSetOtherAssets($ticket_id, array $asset_ids) {
}
}
/**
* A ticket's (or recurring ticket's) assets from a form's one Assets field - pick one or many:
* assets[], with an assets_form marker, as an empty multi-select posts nothing. Only the client's
* own, in the order posted. The ticket keeps one as its main asset - listed first, and what fills
* in the person and site: asset_main (the one it had, or the window was opened for) while it is
* still picked, else the first. A form from before posts $main_field and additional_assets[].
* Returns [main asset id or 0, [the others]], or null when the form has no assets.
*/
function ticketAssetsPosted($client_id, $main_field = 'asset_id') {
if (isset($_POST['assets_form'])) {
$posted = array_values(array_unique(array_filter(array_map('intval', (array) ($_POST['assets'] ?? [])))));
$owned = ticketClientOwnedAssetIds($posted, $client_id);
$ids = array_values(array_filter($posted, function ($id) use ($owned) { return in_array($id, $owned, true); }));
$keep = intval($_POST['asset_main'] ?? 0);
$main = in_array($keep, $ids, true) ? $keep : ($ids[0] ?? 0);
return [$main, array_values(array_diff($ids, [$main]))];
}
if (!isset($_POST[$main_field])) {
return null;
}
$main = ticketClientOwnedId('asset', $_POST[$main_field], $client_id);
return [$main, ticketClientOwnedAssetIds($_POST['additional_assets'] ?? [], $client_id, $main)];
}
/**
* The one Assets field of a window that edits a ticket's or recurring ticket's assets: the
* client's assets - its archived ones only where picked - by name, with what it has ticked. It
* posts assets[], the assets_form marker and its main one as asset_main, for ticketAssetsPosted().
*/
function ticketAssetsFieldHtml($client_id, $main_asset_id, array $other_ids) {
global $mysqli;
$client_id = intval($client_id);
$main_asset_id = intval($main_asset_id);
$picked = array_values(array_filter(array_merge([$main_asset_id], array_map('intval', $other_ids))));
$sql = mysqli_query($mysqli, "SELECT asset_id, asset_name, asset_type, asset_make, asset_model, asset_archived_at, contact_name FROM assets
LEFT JOIN contacts ON contact_id = asset_contact_id
WHERE asset_client_id = $client_id AND (asset_archived_at IS NULL OR asset_id IN (" . implode(',', array_merge([0], $picked)) . "))
ORDER BY asset_name ASC");
$options = '';
while ($row = mysqli_fetch_assoc($sql)) {
$id = intval($row['asset_id']);
$description = implode(' - ', array_filter([trim($row['asset_make'] . ' ' . $row['asset_model']), (string) $row['contact_name']]));
$options .= '<option value="' . $id . '"' . (in_array($id, $picked, true) ? ' selected' : '') . selectOptionAsset($row['asset_type'] ?? '', $description) . '>'
. escapeHtml($row['asset_name']) . ($row['asset_archived_at'] ? ' (archived)' : '') . '</option>';
}
return '<label class="form-label">Assets</label>'
. '<div class="input-group">'
. '<span class="input-group-text"><i class="fas fa-fw fa-desktop"></i></span>'
. '<select class="form-select select2" name="assets[]" data-placeholder="- Select assets -" multiple>' . $options . '</select>'
. '</div>'
. '<input type="hidden" name="assets_form" value="1">'
. '<input type="hidden" name="asset_main" value="' . $main_asset_id . '">';
}
/** A ticket's assets besides its main one, by name. */
function ticketOtherAssetIds($ticket_id, $main_asset_id = 0) {
global $mysqli;
$ids = [];
$sql = mysqli_query($mysqli, "SELECT ticket_assets.asset_id FROM ticket_assets JOIN assets ON assets.asset_id = ticket_assets.asset_id
WHERE ticket_id = " . intval($ticket_id) . " AND ticket_assets.asset_id != " . intval($main_asset_id) . " ORDER BY asset_name ASC");
while ($row = mysqli_fetch_row($sql)) {
$ids[] = intval($row[0]);
}
return $ids;
}
/**
* Save a ticket's assets - its main one and the others - and, given what it had ([main, [others]]),
* note in its history which were added and taken off. Swapping which one is main notes nothing.
* Returns the names [added, removed].
*/
function ticketAssetsSave($ticket_id, $main_asset_id, array $other_ids, array $before = null) {
global $mysqli, $session_name;
$ticket_id = intval($ticket_id);
$main_asset_id = intval($main_asset_id);
mysqli_query($mysqli, "UPDATE tickets SET ticket_asset_id = $main_asset_id WHERE ticket_id = $ticket_id");
ticketSetOtherAssets($ticket_id, array_diff(array_map('intval', $other_ids), [$main_asset_id]));
if ($before === null) {
return [[], []];
}
$was = array_filter(array_merge([intval($before[0])], array_map('intval', $before[1])));
$now = array_filter(array_merge([$main_asset_id], array_map('intval', $other_ids)));
$names = function (array $ids) use ($mysqli) {
if (!$ids) {
return [];
}
return array_column(mysqli_fetch_all(mysqli_query($mysqli, "SELECT asset_name FROM assets WHERE asset_id IN (" . implode(',', array_map('intval', $ids)) . ") ORDER BY asset_name ASC")), 0);
};
$added = $names(array_values(array_diff($now, $was)));
$removed = $names(array_values(array_diff($was, $now)));
$said = [];
if ($added) {
$said[] = 'added ' . (count($added) === 1 ? 'asset ' : 'assets ') . implode(', ', $added);
}
if ($removed) {
$said[] = 'removed ' . (count($removed) === 1 ? 'asset ' : 'assets ') . implode(', ', $removed);
}
if ($said) {
logTicketHistory($ticket_id, escapeSql(stripslashes((string) $session_name) . ' ' . implode(' and ', $said)));
}
return [$added, $removed];
}
/** An active agent's id, else 0 (unassigned). */
function ticketAgentId($user_id) {
global $mysqli;