wpdb; $where=$admin?'1=1':'r.is_active=1 AND r.is_visible=1 AND r.is_bookable=1'; $values=[\ABeCe\Booking\Infrastructure\Database\Tables::name( $wpdb, 'resources' ),\ABeCe\Booking\Infrastructure\Database\Tables::name( $wpdb, 'sports' )]; if($sport){$where.=' AND r.sport_id=%d';$values[]=$sport;} return $this->rows($wpdb->prepare('SELECT r.id,r.name,r.sport_id,s.name AS sport_name,r.surface,r.capacity,r.booking_step_minutes FROM %i r LEFT JOIN %i s ON s.id=r.sport_id WHERE '.$where.' ORDER BY r.sort_order,r.name,r.id',...$values)); } public function bookings(array $resources,string $from,string $to,string $now): array { $wpdb = $this->wpdb; if(!$resources)return []; // Only placeholder tokens form the SQL; identifiers and every value are bound together. $placeholders=implode(',',array_fill(0,count($resources),'%d')); $values=[\ABeCe\Booking\Infrastructure\Database\Tables::name( $wpdb, 'bookings' ),\ABeCe\Booking\Infrastructure\Database\Tables::name( $wpdb, 'members' ),$wpdb->users,...array_map('intval',$resources),$to,$from,$now]; return $this->rows($wpdb->prepare("SELECT b.id,b.series_id,b.booking_reference,b.resource_id,b.starts_at,b.ends_at,b.status,b.customer_user_id,b.booking_source,b.external_customer_label,b.admin_note,b.customer_note,m.first_name,m.last_name,u.display_name FROM %i b LEFT JOIN %i m ON m.id=b.customer_member_id LEFT JOIN %i u ON u.ID=b.customer_user_id WHERE b.resource_id IN (".$placeholders.") AND b.starts_at<%s AND b.ends_at>%s AND b.status NOT IN ('cancelled','voided') AND (b.status<>'no_show' OR b.ends_at<=%s) ORDER BY b.starts_at,b.id",...$values)); } public function blocks(array $resources,string $from,string $to): array { $wpdb = $this->wpdb; if(!$resources)return []; $placeholders=implode(',',array_fill(0,count($resources),'%d')); $values=[\ABeCe\Booking\Infrastructure\Database\Tables::name( $wpdb, 'resource_blocks' ),...array_map('intval',$resources),$to,$from]; return $this->rows($wpdb->prepare('SELECT id,resource_id,starts_at,ends_at,block_type,reason,public_title FROM %i WHERE (resource_id IS NULL OR resource_id IN ('.$placeholders.')) AND starts_at<%s AND ends_at>%s ORDER BY starts_at,id',...$values)); } private function rows(string $sql): array { $wpdb = $this->wpdb; $rows=$wpdb->get_results($sql,ARRAY_A); if($wpdb->last_error)throw new CatalogException('abece_booking_calendar_database_error',__('The calendar could not be loaded. Please try again.','abece-booking'),500); return $rows; } }