'; } $tone = contactAvatarTone($contact_id); return ''; } /** * A contact wherever a page mentions one: their avatar, then their name, * linked to their quick look - or to their page with 'page' => true. * Other options: 'size' of the avatar ('xs' unless given), 'class' on the * link, 'archived' to strike the name through. * Takes values raw or already escaped. */ function contactLink($client_id, $contact_id, $name, $photo = '', $options = []) { $client_id = intval($client_id); $contact_id = intval($contact_id); $name = html_entity_decode((string) $name, ENT_QUOTES, 'UTF-8'); $label = escapeHtml($name !== '' ? $name : 'Unnamed contact'); $archived = !empty($options['archived']); $class = trim('text-truncate ' . ($options['class'] ?? '') . ($archived ? ' text-decoration-line-through text-body-secondary' : '')); $title = $archived ? ' title="Archived contact"' : ''; $link = !empty($options['page']) ? "$label" : "$label"; return '' . contactAvatar($client_id, $contact_id, $name, $photo, $options['size'] ?? 'xs') . $link . ''; } /** The quieter line under a contact in a picker: their title, and Primary or Technical. */ function contactOptionDescription($row) { $role = !empty($row['contact_primary']) ? 'Primary' : (!empty($row['contact_technical']) ? 'Technical' : ''); return implode(' ยท ', array_filter([trim(html_entity_decode((string) ($row['contact_title'] ?? ''), ENT_QUOTES, 'UTF-8')), $role])); } /** For JSON (pickers, search): what contactAvatar() draws - the photo's URL, or their initials and colour. */ function contactAvatarData($client_id, $contact_id, $name, $photo) { $photo = html_entity_decode((string) $photo, ENT_QUOTES, 'UTF-8'); if ($photo !== '') { return ['image' => '/uploads/clients/' . intval($client_id) . '/' . rawurlencode($photo)]; } return ['initials' => personInitials($name) ?: '?', 'tone' => contactAvatarTone($contact_id)]; } /** * How they log in to the client portal, or '' with no access: 'Password' * or 'Identity provider' (only through one, functions/identity_provider.php). */ function contactPortalAccess($contact_user_id, $auth_method) { if (!intval($contact_user_id) || (string) $auth_method === '') { return ''; } return $auth_method === 'azure' ? 'Identity provider' : 'Password'; } /** * Their flags as pills: Primary (or Important when not primary - a primary * contact is always important), Billing, Technical, and Portal when they can * log in to the client portal. */ function contactFlagPills($row, $with_portal = true) { $pills = []; if (!empty($row['contact_primary'])) { $pills[] = ['Primary', 'text-bg-success', 'The client\'s main contact']; } elseif (!empty($row['contact_important'])) { $pills[] = ['Important', 'bg-warning-subtle text-warning-emphasis', 'Pinned to the top of the list']; } if (!empty($row['contact_billing'])) { $pills[] = ['Billing', 'bg-secondary-subtle text-secondary-emphasis', 'Receives invoices']; } if (!empty($row['contact_technical'])) { $pills[] = ['Technical', 'bg-secondary-subtle text-secondary-emphasis', 'Technical contact']; } if ($with_portal) { $portal = contactPortalAccess($row['contact_user_id'] ?? 0, $row['user_auth_method'] ?? ''); if ($portal) { $pills[] = ['Portal', 'bg-info-subtle text-info-emphasis', "Client portal login ($portal)"]; } } $html = ''; foreach ($pills as [$label, $class, $title]) { $html .= '' . $label . ' '; } return rtrim($html); } /** A phone number as a tel: link, with its extension. '' when there is no number. */ function contactPhoneLink($phone, $country_code, $extension = '', $class = '') { $phone = (string) $phone; if ($phone === '') { return ''; } $display = escapeHtml(formatPhoneNumber($phone, $country_code)); $dial = escapeHtml(phoneDialNumber($phone, $country_code)); $extension = escapeHtml($extension); return '' . $display . '' . ($extension ? ' x' . $extension . '' : ''); } /* * Quick picker (js/quick_pick.js) for a contact's location - the same picker * the assets use, with the contact's client as the scope. */ /** The contact and their location, by id. */ function contactQuickRow($contact_id) { global $mysqli; return mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT contact_id, contact_name, contact_client_id, contact_location_id, location_name, location_archived_at FROM contacts LEFT JOIN locations ON location_id = contact_location_id WHERE contact_id = " . intval($contact_id))) ?: null; } /** What the location picker shows - the asset location pill, fed the contact's row. */ function contactLocationHtml($row, $style = 'list') { return assetQuickHtml('asset_location', [ 'asset_client_id' => $row['contact_client_id'] ?? 0, 'asset_location_id' => $row['contact_location_id'] ?? 0, 'location_name' => $row['location_name'] ?? '', 'location_archived_at' => $row['location_archived_at'] ?? null, ], $style); } /** Move a contact to one of their client's locations (0 for none), as Edit Contact would. Returns [ok, plain-text message]. */ function contactQuickSetLocation($contact_id, $value) { global $mysqli, $session_name; $row = contactQuickRow($contact_id); if (!$row) { return [false, 'That contact no longer exists.']; } $contact_id = intval($row['contact_id']); $client_id = intval($row['contact_client_id']); $location_id = assetClientOwnedId('location', $value, $client_id); if (intval($value) && !$location_id) { return [false, 'That location is not one of this client\'s.']; } if ($location_id === intval($row['contact_location_id'])) { return [true, '']; } $location_name = $location_id ? (string) mysqli_fetch_row(mysqli_query($mysqli, "SELECT location_name FROM locations WHERE location_id = $location_id"))[0] : ''; mysqli_query($mysqli, "UPDATE contacts SET contact_location_id = $location_id WHERE contact_id = $contact_id"); $contact_name = escapeSql($row['contact_name']); logAudit("Contact", "Edit", $location_id ? "$session_name set the location of contact $contact_name to " . escapeSql($location_name) : "$session_name removed the location of contact $contact_name", $client_id, $contact_id); return [true, $location_id ? "Moved to $location_name" : 'Location removed']; } /** What quick_pick.js redraws after a change. */ function contactQuickRecord($contact_id) { $row = contactQuickRow($contact_id); return [ 'current' => (string) intval($row['contact_location_id']), 'html' => ['list' => contactLocationHtml($row, 'list'), 'detail' => contactLocationHtml($row, 'detail')], ]; } /** * The + in a picker's input group that adds a contact, location or vendor there and then - it * opens the usual New Contact, New Location or New Vendor window over the form (js/quick_add.js), * and what is made there is picked in the picker - '' for an agent who can't add them. * $client_id when the window's client is fixed; otherwise the form's client_id is used. Styled * like the + beside a category picker (categoryAddButtonHtml()) - $class to match it where it differs. */ function quickAddButton($type, $client_id = 0, $class = 'btn btn-outline-secondary') { if (lookupUserPermission('module_client') < 2) { return ''; } $titles = ['contact' => 'New contact', 'location' => 'New location', 'vendor' => 'New vendor']; return ''; } /* ------------------------------------------------------------ Alternate email (4.4.1) */ /** Whether the 4.4.1 update has run - contacts have an alternate email (contact_email_alt). */ function contactEmailAltReady() { global $mysqli; static $ready = null; if ($ready === null) { $version = defined('CURRENT_DATABASE_VERSION') ? CURRENT_DATABASE_VERSION : (string) (mysqli_fetch_row(mysqli_query($mysqli, "SELECT config_current_database_version FROM settings WHERE company_id = 1"))[0] ?? '0'); $ready = version_compare($version, '4.4.1', '>='); } return $ready; } /* * SQL for a contact whose email or alternate email is $email (raw) - for finding who an email is from. $table, a table * alias in front of the columns, as "c." for c.contact_email. An empty address finds nobody. */ function contactEmailSql($email, $table = '') { $email = trim((string) $email); if ($email === '') { return '0'; } $email_sql = escapeSql($email); return contactEmailAltReady() ? "({$table}contact_email = '$email_sql' OR {$table}contact_email_alt = '$email_sql')" : "{$table}contact_email = '$email_sql'"; } /** The alternate email as a SELECT column - '' before the 4.4.1 update. */ function contactEmailAltColumn() { return contactEmailAltReady() ? 'contact_email_alt' : "'' AS contact_email_alt"; } /** " OR contact_email_alt LIKE '%...%'" for a search, $like SQL-escaped as the search's own - '' before the 4.4.1 update. */ function contactEmailAltLikeSql($like, $table = '') { return contactEmailAltReady() ? " OR {$table}contact_email_alt LIKE '%$like%'" : ''; } /** Whether $email (raw) is the contact's ($row: contact_email, contact_email_alt) - their email or their alternate one, any case. */ function contactHasEmail(array $row, $email) { $email = strtolower(trim((string) $email)); if ($email === '') { return false; } return strtolower(trim((string) ($row['contact_email'] ?? ''))) === $email || strtolower(trim((string) ($row['contact_email_alt'] ?? ''))) === $email; } /* * An alternate email as posted, trimmed - an address, at most 200 characters, and not the contact's own email ($email, in any * case) - or '' for none. */ function contactEmailAltClean($alt, $email = '') { $alt = trim((string) $alt); if ($alt === '' || mb_strlen($alt) > 200 || !filter_var($alt, FILTER_VALIDATE_EMAIL) || strtolower($alt) === strtolower(trim((string) $email))) { return ''; } return $alt; } /* * SQL for a contact with an address that $match takes - an address, or a domain and its subdomains, as an ignored sender * or an email routing rule has it - by their email or their alternate one. Any case. For the warnings on saving one. */ function contactEmailMatchSql($match) { $match = strtolower(trim((string) $match)); if ($match === '') { return '0'; } $parts = []; foreach (contactEmailAltReady() ? ['contact_email', 'contact_email_alt'] : ['contact_email'] as $column) { if (strpos($match, '@') !== false) { $parts[] = "LOWER($column) = '" . escapeSql($match) . "'"; } else { $like = escapeSql(str_replace(['\\', '%', '_'], ['\\\\', '\\%', '\\_'], $match)); $parts[] = "LOWER($column) LIKE '%@$like' OR LOWER($column) LIKE '%.$like'"; } } return '(' . implode(' OR ', $parts) . ')'; }