0"; $client_url = "view=clients&"; } else { // Ours require_once "includes/inc_all.php"; $view = 'ours'; $client_query = "AND vendor_client_id = 0"; $client_url = ''; } // Override Filter Header Archived $archived = isset($_GET['archived']) && $_GET['archived'] == 1 ? 1 : 0; if ($view === 'clients') { $archive_query = $archived ? "(client_archived_at IS NOT NULL OR vendor_archived_at IS NOT NULL)" : "(client_archived_at IS NULL AND vendor_archived_at IS NULL)"; } else { $archive_query = $archived ? "vendor_archived_at IS NOT NULL" : "vendor_archived_at IS NULL"; } // Perms - a client's vendors are the client module's; anyone may look up ours, the financial module changes them if ($view !== 'ours') { enforceUserPermission('module_client'); } $can_edit = $view === 'ours' ? lookupUserPermission('module_financial') >= 2 : lookupUserPermission('module_client') >= 2; $can_delete = ($view === 'ours' ? lookupUserPermission('module_financial') >= 3 : lookupUserPermission('module_client') >= 3) && !empty($config_destructive_deletes_enable); $can_support = lookupUserPermission('module_support') >= 1; $can_credentials = lookupUserPermission('module_credential') >= 1; $can_financial = lookupUserPermission('module_financial') >= 1; $client = ''; if ($view === 'clients' && !empty($_GET['client'])) { // Client Filter $client = intval($_GET['client']); $client_query = "AND vendor_client_id = $client"; } // Tags Filter - a vendor with any of them if (isset($_GET['tags']) && is_array($_GET['tags']) && !empty($_GET['tags'])) { $tag_filter = implode(",", array_map('intval', $_GET['tags'])); $tag_query = "AND vendors.vendor_id IN (SELECT vendor_id FROM vendor_tags WHERE tag_id IN ($tag_filter))"; } else { $tag_filter = 0; $tag_query = ''; } // Only these sort - filter_header.php will hand through any lowercase word, and // an unknown column takes the page down with a mysqli exception $sortable_columns = ['vendor_name', 'vendor_description', 'vendor_account_number', 'vendor_phone', 'vendor_contact_name', 'vendor_hours', 'client_name', 'vendor_created_at']; if (!in_array($sort, $sortable_columns, true)) { $sort = 'vendor_name'; } $sql = mysqli_query( $mysqli, "SELECT SQL_CALC_FOUND_ROWS vendors.vendor_id, vendor_name, vendor_description, vendor_account_number, vendor_code, vendor_contact_name, vendor_contact_phone, vendor_contact_phone_country_code, vendor_contact_email, vendor_phone, vendor_phone_country_code, vendor_extension, vendor_email, vendor_website, vendor_status_page, vendor_hours, vendor_sla, vendor_favorite, vendor_archived_at, vendor_client_id, vendor_template_name, client_id, client_name FROM vendors LEFT JOIN clients ON client_id = vendor_client_id LEFT JOIN vendor_templates ON vendor_templates.vendor_template_id = vendors.vendor_template_id WHERE $archive_query $tag_query AND (vendor_name LIKE '%$q%' OR vendor_description LIKE '%$q%' OR vendor_account_number LIKE '%$q%' OR vendor_website LIKE '%$q%' OR vendor_status_page LIKE '%$q%' OR vendor_portal LIKE '%$q%' OR vendor_contact_name LIKE '%$q%' OR vendor_email LIKE '%$q%' OR vendor_contact_email LIKE '%$q%' OR vendor_notes LIKE '%$q%' OR vendor_phone LIKE '%$phone_query%' OR vendor_contact_phone LIKE '%$phone_query%'" . ($view === 'clients' ? " OR client_name LIKE '%$q%'" : '') . " OR vendors.vendor_id IN (SELECT vendor_id FROM vendor_tags JOIN tags USING (tag_id) WHERE tag_name LIKE '%$q%')) $client_query " . clientScopeSql('vendor_client_id') . " ORDER BY vendor_favorite DESC, $sort $order, vendor_name ASC LIMIT $record_from, $record_to" ); $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()")); $vendors = []; while ($row = mysqli_fetch_assoc($sql)) { $vendors[intval($row['vendor_id'])] = $row; } // Tags and what each vendor is used for, for the whole page at once, not per row $vendors_tags = tagsForRecords('vendor', array_keys($vendors)); $vendors_counts = vendorCounts($vendors); $has_templates = (bool) mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM vendor_templates WHERE vendor_template_archived_at IS NULL LIMIT 1")); $add_url = "modals/vendor/vendor_add.php?" . ($view === 'client' ? "client_id=$client_id" : ($view === 'clients' ? 'view=clients' : '')); // The vendor templates this client should have a vendor from, and hasn't $missing_templates = $view === 'client' && !$archived ? vendorTemplatesMissingFor($client_id) : []; ?>