= 2; $can_delete = lookupUserPermission('module_client') >= 3 && !empty($config_destructive_deletes_enable); $can_support = lookupUserPermission('module_support') >= 1; $client = ''; if (!$client_url) { // Client Filter if (!empty($_GET['client'])) { $client = intval($_GET['client']); $client_query = "AND location_client_id = $client"; } } // Tags Filter - a location 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 locations.location_id IN (SELECT location_id FROM location_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 = ['location_name', 'location_address', 'location_city', 'location_phone', 'location_hours', 'contact_name', 'client_name', 'location_created_at']; if (!in_array($sort, $sortable_columns, true)) { $sort = 'location_name'; } $sql = mysqli_query( $mysqli, "SELECT SQL_CALC_FOUND_ROWS locations.location_id, location_name, location_description, location_address, location_city, location_state, location_zip, location_country, location_phone, location_phone_country_code, location_phone_extension, location_fax, location_fax_country_code, location_hours, location_photo, location_primary, location_archived_at, location_client_id, location_contact_id, client_id, client_name, contact_name, contact_photo, contact_title, contact_archived_at FROM locations LEFT JOIN clients ON client_id = location_client_id LEFT JOIN contacts ON contact_id = location_contact_id AND contact_client_id = location_client_id WHERE $archive_query $tag_query AND (location_name LIKE '%$q%' OR location_description LIKE '%$q%' OR location_address LIKE '%$q%' OR location_city LIKE '%$q%' OR location_state LIKE '%$q%' OR location_zip LIKE '%$q%' OR location_country LIKE '%$q%' OR location_phone LIKE '%$phone_query%' OR location_notes LIKE '%$q%' OR contact_name LIKE '%$q%' OR client_name LIKE '%$q%' OR locations.location_id IN (SELECT location_id FROM location_tags JOIN tags USING (tag_id) WHERE tag_name LIKE '%$q%')) " . clientScopeSql('location_client_id') . " $client_query ORDER BY location_primary DESC, $sort $order, location_name ASC LIMIT $record_from, $record_to" ); $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()")); $locations = []; while ($row = mysqli_fetch_assoc($sql)) { $locations[intval($row['location_id'])] = $row; } // Tags and what is on site for the whole page at once, not per row $locations_tags = tagsForRecords('location', array_keys($locations)); $locations_counts = locationCounts(array_keys($locations)); ?>