// ========================================================================= // 29. SHORTCODE: Special Coach Profile /coach-profile/ // ========================================================================= add_shortcode('otg_coach_profile', 'otg_render_coach_profile'); function otg_render_coach_profile($atts) { global $wpdb; $a = shortcode_atts(array('id' => 0), $atts, 'otg_coach_profile'); $coach_id = isset($_GET['coach_id']) ? intval($_GET['coach_id']) : intval($a['id']); if (!$coach_id) return "Coach ID required."; $c = $wpdb->get_row($wpdb->prepare("SELECT * FROM otg_coach WHERE CoachID = %d", $coach_id)); if (!$c) return "Coach not found."; // --- Coach Rank Calculation (Joining otg_coach with otg_coach_statistics for NYR only) --- // Groups coaching stints by Season first, then CoachID. $ranked_stints = $wpdb->get_results( "SELECT CoachID, Season FROM otg_coach_statistics WHERE Team = 'New York Rangers' GROUP BY Season, CoachID ORDER BY Season ASC, CoachID ASC" ); // Group stints by season to handle multiple coaches in the same season $season_to_coaches = []; foreach ($ranked_stints as $stint) { $c_id = intval($stint->CoachID); $season_str = $stint->Season; $season_to_coaches[$season_str][$c_id] = true; } // Get all unique seasons sorted chronologically first $all_seasons = array_keys($season_to_coaches); sort($all_seasons); // Build chronological list of active coaches per season, resolving multi-coach tie-breaks $chronological_stints = []; foreach ($all_seasons as $i => $season_str) { $coaches_in_season = array_keys($season_to_coaches[$season_str]); sort($coaches_in_season); // Default fallback: lowest CoachID first if (count($coaches_in_season) > 1) { $resolved_order = []; $unresolved = $coaches_in_season; // 1. Check previous season to see who coached then (put them first) if ($i > 0) { $prev_season = $all_seasons[$i - 1]; $prev_coaches = $season_to_coaches[$prev_season]; foreach ($unresolved as $key => $c_id) { if (isset($prev_coaches[$c_id])) { $resolved_order[] = $c_id; unset($unresolved[$key]); } } $unresolved = array_values($unresolved); } // 2. Check next season to see who coaches next (put them last among remaining) if ($i < count($all_seasons) - 1 && count($unresolved) > 1) { $next_season = $all_seasons[$i + 1]; $next_coaches = $season_to_coaches[$next_season]; $matched_next = []; foreach ($unresolved as $key => $c_id) { if (isset($next_coaches[$c_id])) { $matched_next[] = $c_id; unset($unresolved[$key]); } } $unresolved = array_values($unresolved); // Append remaining unresolved, then the next-season coaches at the very end $coaches_in_season = array_merge($resolved_order, $unresolved, $matched_next); } else { $coaches_in_season = array_merge($resolved_order, $unresolved); } } foreach ($coaches_in_season as $c_id) { $chronological_stints[] = ['CoachID' => $c_id, 'Season' => $season_str]; } } // --- RANK 1: CoachID Order (Chronological appearance of each unique coach) --- $unique_coach_ranks = []; $coach_counter = 1; foreach ($chronological_stints as $stint) { $c_id = intval($stint['CoachID']); if (!isset($unique_coach_ranks[$c_id])) { $unique_coach_ranks[$c_id] = $coach_counter; $coach_counter++; } } $primary_coach_rank = isset($unique_coach_ranks[$coach_id]) ? $unique_coach_ranks[$coach_id] : null; // --- RANK 2: Stint / Tenure Order (Collapsing consecutive seasons into tenure blocks) --- $coach_tenures = []; $current_coach = null; $last_season_start = null; $current_tenure_id = null; foreach ($chronological_stints as $stint) { $c_id = intval($stint['CoachID']); $season_str = $stint['Season']; $season_start = intval(substr($season_str, 0, 4)); if ($current_coach !== $c_id || $last_season_start === null || $season_start !== ($last_season_start + 1)) { $current_tenure_id = $c_id . '_' . $season_start; $current_coach = $c_id; } $coach_tenures[$c_id][$season_str] = $current_tenure_id; $last_season_start = $season_start; } $unique_tenure_ranks = []; $global_rank = 1; foreach ($coach_tenures as $c_id => $seasons) { $tenure_map = []; foreach ($seasons as $season_str => $tenure_id) { if (!isset($tenure_map[$tenure_id])) { $tenure_map[$tenure_id] = $global_rank; $global_rank++; } $unique_tenure_ranks[$c_id][$season_str] = $tenure_map[$tenure_id]; } } // Fetch all coaches for the dropdown pivot selector (ordered alphabetically by name) $all_coaches = $wpdb->get_results("SELECT CoachID, Name FROM otg_coach ORDER BY Name ASC"); // Query stats based on CoachID $stats = $wpdb->get_results($wpdb->prepare("SELECT * FROM otg_coach_statistics WHERE CoachID = %d ORDER BY Season ASC", $coach_id)); // --- Bio Calculations & Career Totals Logic --- $bday = (empty($c->Birthday) || $c->Birthday === '1800-01-01') ? '-' : (new DateTime($c->Birthday))->format('F jS, Y'); $birthplace = esc_html($c->Birthplace) . (!empty($c->State) ? ' ' . esc_html($c->State) : ''); $scf_season_display = []; $sc_season_display = []; $coach_rank_displays = []; // Accumulators for NYR career totals $reg_w = 0; $reg_l = 0; $reg_t = 0; $reg_ot = 0; $play_w = 0; $play_l = 0; $play_t = 0; $total_sw = 0; $total_sl = 0; if (!empty($stats)) { foreach ($stats as $s) { $y1 = substr($s->Season, 0, 4); $y2 = str_pad((int)substr($s->Season, 0, 4) + 1, 4, '0', STR_PAD_LEFT); $disp_season = $y1 . '-' . substr($y2, 2, 2); $season_url = esc_url(home_url('/schedule/?season=' . $disp_season)); $season_link_html = '' . $disp_season . ''; // Gather rank for each NYR tenure stint, tracking start and end seasons if ($s->Team === 'New York Rangers' && isset($unique_tenure_ranks[$coach_id][$s->Season])) { $rank_num = $unique_tenure_ranks[$coach_id][$s->Season]; if (!isset($coach_rank_displays[$rank_num])) { $coach_rank_displays[$rank_num] = [ 'rank' => $rank_num, 'start' => $disp_season, 'end' => $disp_season ]; } else { $coach_rank_displays[$rank_num]['end'] = $disp_season; } } if (isset($s->SCF) && intval($s->SCF) == 1) { if ($s->Team === 'New York Rangers') { $scf_season_display[] = $season_link_html; } else { $scf_season_display[] = $disp_season; } } if (isset($s->SC) && intval($s->SC) == 1) { if ($s->Team === 'New York Rangers') { $sc_season_display[] = $season_link_html; } else { $sc_season_display[] = $disp_season; } } // Only accumulate stats where the team is New York Rangers if ($s->Team === 'New York Rangers') { if (isset($s->SW)) { $total_sw += intval($s->SW); } if (isset($s->SL)) { $total_sl += intval($s->SL); } // Regular Season totals accumulation $reg_w += isset($s->W) ? intval($s->W) : 0; $reg_l += isset($s->L) ? intval($s->L) : 0; $reg_t += isset($s->T) ? intval($s->T) : 0; $reg_ot += isset($s->OT) ? intval($s->OT) : 0; // Playoff totals accumulation $play_w += isset($s->PW) ? intval($s->PW) : 0; $play_l += isset($s->PL) ? intval($s->PL) : 0; $play_t += isset($s->PT) ? intval($s->PT) : 0; } } } // Format Stint rank strings (e.g. "#5 (2012-13)" or "#5 (2012-13 – 2014-15)") $formatted_stint_ranks = []; foreach ($coach_rank_displays as $stint_info) { $span = ($stint_info['start'] === $stint_info['end']) ? $stint_info['start'] : $stint_info['start'] . ' – ' . $stint_info['end']; $formatted_stint_ranks[] = '#' . $stint_info['rank'] . ' (' . $span . ')'; } if (empty($formatted_stint_ranks)) { $earlier_coaches_count = $wpdb->get_var( "SELECT COUNT(DISTINCT c.CoachID) FROM otg_coach c INNER JOIN otg_coach_statistics s ON c.CoachID = s.CoachID WHERE s.Team = 'New York Rangers' AND c.CoachID < " . intval($coach_id) ); $formatted_stint_ranks[] = '#' . (intval($earlier_coaches_count) + 1); } // Format Coach rank string $formatted_coach_rank = $primary_coach_rank ? '#' . $primary_coach_rank : '-'; // Format Regular Season Record $reg_record_parts = [$reg_w, $reg_l]; if ($reg_t > 0) $reg_record_parts[] = $reg_t; if ($reg_ot > 0) $reg_record_parts[] = $reg_ot; $reg_record_display = implode('–', $reg_record_parts); if (empty($reg_record_display)) { $reg_record_display = '0–0'; } // Format Playoff Record $play_record_parts = [$play_w, $play_l]; if ($play_t > 0) $play_record_parts[] = $play_t; $play_record_display = implode('–', $play_record_parts); if (empty($play_record_display)) { $play_record_display = '0–0'; } // Format Playoff Series Record $series_record_display = "{$total_sw}–{$total_sl}"; $data = []; $data[] = ['Birthday', $bday]; $data[] = ['Birthplace', $birthplace]; $data[] = ['Coach', $formatted_coach_rank]; $data[] = ['Stint', implode(', ', $formatted_stint_ranks)]; $data[] = ['NYR Regular Season Record', $reg_record_display]; $data[] = ['NYR Playoff Record', $play_record_display]; $data[] = ['NYR Playoff Series Record', $series_record_display]; if (!empty($sc_season_display)) { $data[] = ['Stanley Cups', implode(', ', $sc_season_display)]; } if (!empty($scf_season_display)) { $filtered_scf_display = []; foreach ($stats as $s) { if (isset($s->SCF) && intval($s->SCF) == 1) { $y1 = substr($s->Season, 0, 4); $y2 = str_pad((int)substr($s->Season, 0, 4) + 1, 4, '0', STR_PAD_LEFT); $disp_season = $y1 . '-' . substr($y2, 2, 2); if ($s->Team === 'New York Rangers') { $season_url = esc_url(home_url('/schedule/?season=' . $disp_season)); $filtered_scf_display[] = '' . $disp_season . ''; } else { $filtered_scf_display[] = $disp_season; } } } $data[] = ['Stanley Cup Finals', implode(', ', array_unique($filtered_scf_display))]; } // --- Conditional Profile Cross-Links --- $cross_links = []; if (!empty($c->player_id) && intval($c->player_id) > 0) { $cross_links[] = 'View Player Profile'; } $gm_exists = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM otg_general_managers WHERE name = %s", $c->Name)); if ($gm_exists > 0) { $cross_links[] = 'View General Manager Profile'; } if (!empty($cross_links)) { $data[] = ['Profiles', implode(' | ', $cross_links)]; } // --- Image Fallback Logic (Using Local File Check) --- $coach_id_val = intval($c->CoachID); $base_dir = $_SERVER['DOCUMENT_ROOT'] . '/images/coaches/'; $base_url = 'https://outsidethegarden.com/images/coaches/'; $default_img = 'https://outsidethegarden.com/images/players/default_player.png'; $display_img = $default_img; $extensions = ['jpg', 'jpeg', 'png']; foreach ($extensions as $ext) { if (file_exists($base_dir . $coach_id_val . '.' . $ext)) { $display_img = $base_url . $coach_id_val . '.' . $ext; break; } } // --- HTML Generation --- $html = ''; $html .= '
';
}
$html .= strtoupper(esc_html($c->Name)) . '| ID | '; } $html .= 'Season | Team | League | GC | W | L | T | OT | Pts | P% |
|---|---|---|---|---|---|---|---|---|---|---|
| '.$unique_val.' | '; } $html .= ''.esc_html($disp_season).' | '.esc_html($s->Team).' | '.esc_html($s->League).' | '.esc_html($s->GC).' | '.esc_html($s->W).' | '.esc_html($s->L).' | '.esc_html($s->T).' | '.esc_html($s->OT).' | '.$calculated_pts.' | '.$pt_pct.' |
| - | '; } $html .= 'Career Totals | '.$scope.' | NHL | '; $html .= ''.$t['GC'].' | '.$t['W'].' | '.$t['L'].' | '; $html .= ''.$t['T'].' | '.$t['OT'].' | '.$t['PT'].' | '.$tot_pt_pct.' | '; $html .= '
| ID | '; } $html .= 'Season | Team | League | GC | W | L | T | Series | Stanley Cup |
|---|---|---|---|---|---|---|---|---|---|
| '.$unique_val.' | '; } $html .= ''.esc_html($disp_season).' | '.esc_html($s->Team).' | '.esc_html($s->League).' | '.$pgc.' | '.$pw.' | '.$pl.' | '.$pt.' | '.$series_display.' | '.$sc_display.' |
| - | '; } $html .= 'Career Totals | '.$scope.' | NHL | '; $html .= ''.$tot_gc.' | '.$tot_w.' | '.$tot_l.' | '; $html .= ''.$tot_t.' | '.$tot_series.' | - | '; $html .= '
No coaching statistics found.
'; } // JavaScript for row filtering and switching between regular season / playoffs $html .= ''; return $html; }