Page 1 / 27
Next
// ========================================================================= // FUNCTION: Injury Manager & Player Bucket Synchronizer // ========================================================================= /** * Handles adding, editing, and deleting records in `otg_injuries`, * and automatically synchronizes the `player_bucket` field in `otg_players`. */ function otg_handle_injury_management_submission() { global $wpdb; if (!isset($_POST['otg_injury_nonce']) || !wp_verify_nonce($_POST['otg_injury_nonce'], 'otg_save_injury_action')) { return; } if (!current_user_can('manage_options')) { return; } $action = isset($_POST['injury_action']) ? sanitize_text_field($_POST['injury_action']) : ''; if ($action === 'delete') { $injury_index = isset($_POST['InjuryIndex']) ? intval($_POST['InjuryIndex']) : 0; if ($injury_index > 0) { $injury_record = $wpdb->get_row($wpdb->prepare("SELECT ID FROM otg_injuries WHERE InjuryIndex = %d", $injury_index)); if ($injury_record) { $player_id = $injury_record->ID; $wpdb->delete('otg_injuries', ['InjuryIndex' => $injury_index]); otg_recalculate_player_bucket($player_id); } } } elseif ($action === 'save') { $injury_index = isset($_POST['InjuryIndex']) ? intval($_POST['InjuryIndex']) : 0; $player_id = isset($_POST['player_id']) ? intval($_POST['player_id']) : 0; if ($player_id > 0) { $player_name = $wpdb->get_var($wpdb->prepare("SELECT Name FROM otg_players WHERE nhl_id = %d", $player_id)); $data = [ 'ID' => $player_id, 'Name' => $player_name ?: '', 'Injured' => sanitize_text_field($_POST['Injured']), 'during_game' => sanitize_text_field($_POST['during_game']), 'Injury' => sanitize_text_field($_POST['Injury']), 'Updated' => sanitize_text_field($_POST['Updated']), 'Status' => sanitize_text_field($_POST['Status']), 'Notes' => sanitize_text_field($_POST['Notes']) ]; if ($injury_index > 0) { $wpdb->update('otg_injuries', $data, ['InjuryIndex' => $injury_index]); } else { $wpdb->insert('otg_injuries', $data); } otg_recalculate_player_bucket($player_id); } } } add_action('init', 'otg_handle_injury_management_submission'); /** * Recalculates and updates the player_bucket field in otg_players * based on active records remaining in otg_injuries. */ function otg_recalculate_player_bucket($player_id) { global $wpdb; $active_injury = $wpdb->get_row($wpdb->prepare( "SELECT Status FROM otg_injuries WHERE ID = %s AND (Status != 'Returned' AND Status != '') ORDER BY InjuryIndex DESC LIMIT 1", $player_id )); if ($active_injury) { $status_lower = strtolower($active_injury->Status); if (strpos($status_lower, 'ltir') !== false) { $bucket = 'LTIR'; } elseif (strpos($status_lower, 'reserve') !== false || strpos($status_lower, 'ir') !== false) { $bucket = 'Injured Reserve'; } else { $bucket = 'Injured'; } } else { $bucket = 'Active'; } $wpdb->update( 'otg_players', ['player_bucket' => $bucket], ['nhl_id' => $player_id] ); } // ========================================================================= // ADMIN PAGE RENDERER: Injury Manager Dashboard View // ========================================================================= function otg_render_injury_manager_admin_page() { if (!current_user_can('manage_options')) { wp_die(__('You do not have sufficient permissions to access this page.')); } global $wpdb; $rangers_players = $wpdb->get_results("SELECT nhl_id, Name FROM otg_players ORDER BY Name ASC"); $injuries = $wpdb->get_results("SELECT * FROM otg_injuries ORDER BY Injured DESC, InjuryIndex DESC"); $input_style = 'width: 100%; height: 38px; padding: 0 10px; background: #222; border: 1px solid #444; color: #fff; border-radius: 4px; box-sizing: border-box; font-size: 0.9em;'; echo '
| ID | Player | Injured | Injury | Status | Updated | Notes | Actions | '; echo '
|---|---|---|---|---|---|---|---|
| No injury records found. | |||||||
| ' . esc_html($row->InjuryIndex) . ' | '; echo '' . esc_html($row->Name) . ' | '; echo '' . esc_html($row->Injured) . ' | '; echo '' . esc_html($row->Injury) . ' | '; echo '' . esc_html($row->Status) . ' | '; echo '' . esc_html($row->Updated) . ' | '; echo '' . esc_html($row->Notes) . ' | '; echo ''; // Edit button trigger echo ''; // Delete form echo ''; echo ' | '; echo '