public function show($id) { try { $person = Person::findOrFail($id); // Existing code for basic person data... $all_associated_cases = $this->getAssociatedCases($person); $categorized_cases = $this->categorizeCases($all_associated_cases, $person); $total_unique_associated_cases = count($all_associated_cases); // Activity analysis data $data = $this->getPersonActivityData($all_associated_cases, $categorized_cases); // Get categorized professional relationships $professional_relationships = $this->getCategorizedRelationships($person, $all_associated_cases); return view('persons.show', array_merge(compact('person'), $data, $professional_relationships)); } catch (\Exception $e) { \Log::error('Error in PersonController show method: ' . $e->getMessage()); return view('persons.show', compact('person'))->with('error', 'Unable to load complete person data.'); } } /** * Get categorized professional relationships for a person */ private function getCategorizedRelationships($person, $all_associated_cases) { $all_person_ids = collect(); $person_case_map = []; foreach ($all_associated_cases as $case) { $case_name = $case['name']; $case_year = $case['year'] ?? null; // Panel members if (!empty($case['panel_ids'])) { $panel_ids = array_map('trim', explode(',', $case['panel_ids'])); foreach ($panel_ids as $panel_id) { if (!empty($panel_id) && is_numeric($panel_id) && $panel_id != $person->id) { $all_person_ids->push($panel_id); $person_case_map[$panel_id][] = [ 'case_name' => $case_name, 'year' => $case_year, 'relationship_type' => 'panel_colleague', 'person_role' => $this->getPersonRoleInCase($person->id, $case), 'other_role' => 'panel' ]; } } } // Appellants if (!empty($case['appellant_ids'])) { $appellant_ids = array_map('trim', explode(',', $case['appellant_ids'])); foreach ($appellant_ids as $appellant_id) { if (!empty($appellant_id) && is_numeric($appellant_id) && $appellant_id != $person->id) { $all_person_ids->push($appellant_id); $person_case_map[$appellant_id][] = [ 'case_name' => $case_name, 'year' => $case_year, 'relationship_type' => $this->determineRelationshipType($person->id, $appellant_id, $case, 'appellant'), 'person_role' => $this->getPersonRoleInCase($person->id, $case), 'other_role' => 'appellant' ]; } } } // Respondents if (!empty($case['respondent_ids'])) { $respondent_ids = array_map('trim', explode(',', $case['respondent_ids'])); foreach ($respondent_ids as $respondent_id) { if (!empty($respondent_id) && is_numeric($respondent_id) && $respondent_id != $person->id) { $all_person_ids->push($respondent_id); $person_case_map[$respondent_id][] = [ 'case_name' => $case_name, 'year' => $case_year, 'relationship_type' => $this->determineRelationshipType($person->id, $respondent_id, $case, 'respondent'), 'person_role' => $this->getPersonRoleInCase($person->id, $case), 'other_role' => 'respondent' ]; } } } // Appellant Representatives if (!empty($case['appellant_rep_ids'])) { $appellant_rep_ids = array_map('trim', explode(',', $case['appellant_rep_ids'])); foreach ($appellant_rep_ids as $rep_id) { if (!empty($rep_id) && is_numeric($rep_id) && $rep_id != $person->id) { $all_person_ids->push($rep_id); $person_case_map[$rep_id][] = [ 'case_name' => $case_name, 'year' => $case_year, 'relationship_type' => $this->determineRelationshipType($person->id, $rep_id, $case, 'appellant_rep'), 'person_role' => $this->getPersonRoleInCase($person->id, $case), 'other_role' => 'appellant_rep' ]; } } } // Respondent Representatives if (!empty($case['respondent_rep_ids'])) { $respondent_rep_ids = array_map('trim', explode(',', $case['respondent_rep_ids'])); foreach ($respondent_rep_ids as $rep_id) { if (!empty($rep_id) && is_numeric($rep_id) && $rep_id != $person->id) { $all_person_ids->push($rep_id); $person_case_map[$rep_id][] = [ 'case_name' => $case_name, 'year' => $case_year, 'relationship_type' => $this->determineRelationshipType($person->id, $rep_id, $case, 'respondent_rep'), 'person_role' => $this->getPersonRoleInCase($person->id, $case), 'other_role' => 'respondent_rep' ]; } } } } // Get unique person IDs $all_person_ids = $all_person_ids->unique()->filter(function($id) { return !empty($id) && is_numeric($id); }); if ($all_person_ids->isEmpty()) { return [ 'collaborators' => collect(), 'opponents' => collect(), 'case_relationships' => collect() ]; } // Get all persons data $persons_data = Person::whereIn('id', $all_person_ids)->get()->keyBy('id'); // Categorize relationships $collaborators = collect(); $opponents = collect(); $case_relationships = collect(); foreach ($person_case_map as $person_id => $relationships) { $related_person = $persons_data->get($person_id); if (!$related_person) continue; // Categorize relationship types $collaboration_types = []; $opponent_types = []; $case_relation_types = []; $shared_cases = []; $years = []; foreach ($relationships as $rel) { $shared_cases[] = $rel['case_name']; if ($rel['year']) { $years[] = $rel['year']; } if (in_array($rel['relationship_type'], ['panel_colleague', 'co_counsel'])) { $collaboration_types[] = $rel['relationship_type']; } elseif (in_array($rel['relationship_type'], ['opposing_counsel', 'opposing_party'])) { $opponent_types[] = $rel['relationship_type']; } else { $case_relation_types[] = $rel['relationship_type']; } } $person_data = [ 'id' => $related_person->id, 'name' => $related_person->name, 'role' => $related_person->role, 'organization' => $related_person->organization, 'image_path' => $related_person->image_path, 'shared_cases_count' => count($shared_cases), 'shared_cases_list' => array_unique($shared_cases), 'years_list' => array_unique($years), 'collaboration_types' => array_unique($collaboration_types), 'opponent_types' => array_unique($opponent_types), 'case_relation_types' => array_unique($case_relation_types) ]; // Categorize based on primary relationship type if (!empty($collaboration_types)) { $collaborators->push($person_data); } elseif (!empty($opponent_types)) { $opponents->push($person_data); } else { $case_relationships->push($person_data); } } // Sort by shared cases count $collaborators = $collaborators->sortByDesc('shared_cases_count')->take(20); $opponents = $opponents->sortByDesc('shared_cases_count')->take(20); $case_relationships = $case_relationships->sortByDesc('shared_cases_count')->take(20); return [ 'collaborators' => $collaborators, 'opponents' => $opponents, 'case_relationships' => $case_relationships ]; } /** * Determine relationship type between two persons in a case */ private function determineRelationshipType($person_id, $other_person_id, $case, $other_role) { $person_role = $this->getPersonRoleInCase($person_id, $case); // Panel colleagues if ($person_role === 'panel' && $other_role === 'panel') { return 'panel_colleague'; } // Co-counsels (same side representatives) if (($person_role === 'appellant_rep' && $other_role === 'appellant_rep') || ($person_role === 'respondent_rep' && $other_role === 'respondent_rep')) { return 'co_counsel'; } // Opposing counsels if (($person_role === 'appellant_rep' && $other_role === 'respondent_rep') || ($person_role === 'respondent_rep' && $other_role === 'appellant_rep')) { return 'opposing_counsel'; } // Opposing parties if (($person_role === 'appellant' && $other_role === 'respondent') || ($person_role === 'respondent' && $other_role === 'appellant')) { return 'opposing_party'; } // Client relationships if (($person_role === 'appellant_rep' && $other_role === 'appellant') || ($person_role === 'respondent_rep' && $other_role === 'respondent')) { return 'client_relationship'; } // Judge relationships if ($person_role === 'panel' || $other_role === 'panel') { return 'judge_relationship'; } return 'case_participant'; } /** * Get person's role in a specific case */ private function getPersonRoleInCase($person_id, $case) { // Check panel if (!empty($case['panel_ids'])) { $panel_ids = array_map('trim', explode(',', $case['panel_ids'])); if (in_array($person_id, $panel_ids)) { return 'panel'; } } // Check appellant if (!empty($case['appellant_ids'])) { $appellant_ids = array_map('trim', explode(',', $case['appellant_ids'])); if (in_array($person_id, $appellant_ids)) { return 'appellant'; } } // Check respondent if (!empty($case['respondent_ids'])) { $respondent_ids = array_map('trim', explode(',', $case['respondent_ids'])); if (in_array($person_id, $respondent_ids)) { return 'respondent'; } } // Check appellant rep if (!empty($case['appellant_rep_ids'])) { $appellant_rep_ids = array_map('trim', explode(',', $case['appellant_rep_ids'])); if (in_array($person_id, $appellant_rep_ids)) { return 'appellant_rep'; } } // Check respondent rep if (!empty($case['respondent_rep_ids'])) { $respondent_rep_ids = array_map('trim', explode(',', $case['respondent_rep_ids'])); if (in_array($person_id, $respondent_rep_ids)) { return 'respondent_rep'; } } return 'unknown'; }public function show($id) { try { $person = Person::findOrFail($id); // Existing code for basic person data... $all_associated_cases = $this->getAssociatedCases($person); $categorized_cases = $this->categorizeCases($all_associated_cases, $person); $total_unique_associated_cases = count($all_associated_cases); // Activity analysis data $data = $this->getPersonActivityData($all_associated_cases, $categorized_cases); // Get categorized professional relationships $professional_relationships = $this->getCategorizedRelationships($person, $all_associated_cases); return view('persons.show', array_merge(compact('person'), $data, $professional_relationships)); } catch (\Exception $e) { \Log::error('Error in PersonController show method: ' . $e->getMessage()); return view('persons.show', compact('person'))->with('error', 'Unable to load complete person data.'); } } /** * Get categorized professional relationships for a person */ private function getCategorizedRelationships($person, $all_associated_cases) { $all_person_ids = collect(); $person_case_map = []; foreach ($all_associated_cases as $case) { $case_name = $case['name']; $case_year = $case['year'] ?? null; // Panel members if (!empty($case['panel_ids'])) { $panel_ids = array_map('trim', explode(',', $case['panel_ids'])); foreach ($panel_ids as $panel_id) { if (!empty($panel_id) && is_numeric($panel_id) && $panel_id != $person->id) { $all_person_ids->push($panel_id); $person_case_map[$panel_id][] = [ 'case_name' => $case_name, 'year' => $case_year, 'relationship_type' => 'panel_colleague', 'person_role' => $this->getPersonRoleInCase($person->id, $case), 'other_role' => 'panel' ]; } } } // Appellants if (!empty($case['appellant_ids'])) { $appellant_ids = array_map('trim', explode(',', $case['appellant_ids'])); foreach ($appellant_ids as $appellant_id) { if (!empty($appellant_id) && is_numeric($appellant_id) && $appellant_id != $person->id) { $all_person_ids->push($appellant_id); $person_case_map[$appellant_id][] = [ 'case_name' => $case_name, 'year' => $case_year, 'relationship_type' => $this->determineRelationshipType($person->id, $appellant_id, $case, 'appellant'), 'person_role' => $this->getPersonRoleInCase($person->id, $case), 'other_role' => 'appellant' ]; } } } // Respondents if (!empty($case['respondent_ids'])) { $respondent_ids = array_map('trim', explode(',', $case['respondent_ids'])); foreach ($respondent_ids as $respondent_id) { if (!empty($respondent_id) && is_numeric($respondent_id) && $respondent_id != $person->id) { $all_person_ids->push($respondent_id); $person_case_map[$respondent_id][] = [ 'case_name' => $case_name, 'year' => $case_year, 'relationship_type' => $this->determineRelationshipType($person->id, $respondent_id, $case, 'respondent'), 'person_role' => $this->getPersonRoleInCase($person->id, $case), 'other_role' => 'respondent' ]; } } } // Appellant Representatives if (!empty($case['appellant_rep_ids'])) { $appellant_rep_ids = array_map('trim', explode(',', $case['appellant_rep_ids'])); foreach ($appellant_rep_ids as $rep_id) { if (!empty($rep_id) && is_numeric($rep_id) && $rep_id != $person->id) { $all_person_ids->push($rep_id); $person_case_map[$rep_id][] = [ 'case_name' => $case_name, 'year' => $case_year, 'relationship_type' => $this->determineRelationshipType($person->id, $rep_id, $case, 'appellant_rep'), 'person_role' => $this->getPersonRoleInCase($person->id, $case), 'other_role' => 'appellant_rep' ]; } } } // Respondent Representatives if (!empty($case['respondent_rep_ids'])) { $respondent_rep_ids = array_map('trim', explode(',', $case['respondent_rep_ids'])); foreach ($respondent_rep_ids as $rep_id) { if (!empty($rep_id) && is_numeric($rep_id) && $rep_id != $person->id) { $all_person_ids->push($rep_id); $person_case_map[$rep_id][] = [ 'case_name' => $case_name, 'year' => $case_year, 'relationship_type' => $this->determineRelationshipType($person->id, $rep_id, $case, 'respondent_rep'), 'person_role' => $this->getPersonRoleInCase($person->id, $case), 'other_role' => 'respondent_rep' ]; } } } } // Get unique person IDs $all_person_ids = $all_person_ids->unique()->filter(function($id) { return !empty($id) && is_numeric($id); }); if ($all_person_ids->isEmpty()) { return [ 'collaborators' => collect(), 'opponents' => collect(), 'case_relationships' => collect() ]; } // Get all persons data $persons_data = Person::whereIn('id', $all_person_ids)->get()->keyBy('id'); // Categorize relationships $collaborators = collect(); $opponents = collect(); $case_relationships = collect(); foreach ($person_case_map as $person_id => $relationships) { $related_person = $persons_data->get($person_id); if (!$related_person) continue; // Categorize relationship types $collaboration_types = []; $opponent_types = []; $case_relation_types = []; $shared_cases = []; $years = []; foreach ($relationships as $rel) { $shared_cases[] = $rel['case_name']; if ($rel['year']) { $years[] = $rel['year']; } if (in_array($rel['relationship_type'], ['panel_colleague', 'co_counsel'])) { $collaboration_types[] = $rel['relationship_type']; } elseif (in_array($rel['relationship_type'], ['opposing_counsel', 'opposing_party'])) { $opponent_types[] = $rel['relationship_type']; } else { $case_relation_types[] = $rel['relationship_type']; } } $person_data = [ 'id' => $related_person->id, 'name' => $related_person->name, 'role' => $related_person->role, 'organization' => $related_person->organization, 'image_path' => $related_person->image_path, 'shared_cases_count' => count($shared_cases), 'shared_cases_list' => array_unique($shared_cases), 'years_list' => array_unique($years), 'collaboration_types' => array_unique($collaboration_types), 'opponent_types' => array_unique($opponent_types), 'case_relation_types' => array_unique($case_relation_types) ]; // Categorize based on primary relationship type if (!empty($collaboration_types)) { $collaborators->push($person_data); } elseif (!empty($opponent_types)) { $opponents->push($person_data); } else { $case_relationships->push($person_data); } } // Sort by shared cases count $collaborators = $collaborators->sortByDesc('shared_cases_count')->take(20); $opponents = $opponents->sortByDesc('shared_cases_count')->take(20); $case_relationships = $case_relationships->sortByDesc('shared_cases_count')->take(20); return [ 'collaborators' => $collaborators, 'opponents' => $opponents, 'case_relationships' => $case_relationships ]; } /** * Determine relationship type between two persons in a case */ private function determineRelationshipType($person_id, $other_person_id, $case, $other_role) { $person_role = $this->getPersonRoleInCase($person_id, $case); // Panel colleagues if ($person_role === 'panel' && $other_role === 'panel') { return 'panel_colleague'; } // Co-counsels (same side representatives) if (($person_role === 'appellant_rep' && $other_role === 'appellant_rep') || ($person_role === 'respondent_rep' && $other_role === 'respondent_rep')) { return 'co_counsel'; } // Opposing counsels if (($person_role === 'appellant_rep' && $other_role === 'respondent_rep') || ($person_role === 'respondent_rep' && $other_role === 'appellant_rep')) { return 'opposing_counsel'; } // Opposing parties if (($person_role === 'appellant' && $other_role === 'respondent') || ($person_role === 'respondent' && $other_role === 'appellant')) { return 'opposing_party'; } // Client relationships if (($person_role === 'appellant_rep' && $other_role === 'appellant') || ($person_role === 'respondent_rep' && $other_role === 'respondent')) { return 'client_relationship'; } // Judge relationships if ($person_role === 'panel' || $other_role === 'panel') { return 'judge_relationship'; } return 'case_participant'; } /** * Get person's role in a specific case */ private function getPersonRoleInCase($person_id, $case) { // Check panel if (!empty($case['panel_ids'])) { $panel_ids = array_map('trim', explode(',', $case['panel_ids'])); if (in_array($person_id, $panel_ids)) { return 'panel'; } } // Check appellant if (!empty($case['appellant_ids'])) { $appellant_ids = array_map('trim', explode(',', $case['appellant_ids'])); if (in_array($person_id, $appellant_ids)) { return 'appellant'; } } // Check respondent if (!empty($case['respondent_ids'])) { $respondent_ids = array_map('trim', explode(',', $case['respondent_ids'])); if (in_array($person_id, $respondent_ids)) { return 'respondent'; } } // Check appellant rep if (!empty($case['appellant_rep_ids'])) { $appellant_rep_ids = array_map('trim', explode(',', $case['appellant_rep_ids'])); if (in_array($person_id, $appellant_rep_ids)) { return 'appellant_rep'; } } // Check respondent rep if (!empty($case['respondent_rep_ids'])) { $respondent_rep_ids = array_map('trim', explode(',', $case['respondent_rep_ids'])); if (in_array($person_id, $respondent_rep_ids)) { return 'respondent_rep'; } } return 'unknown'; }public function show($id) { try { $person = Person::findOrFail($id); // Existing code for basic person data... $all_associated_cases = $this->getAssociatedCases($person); $categorized_cases = $this->categorizeCases($all_associated_cases, $person); $total_unique_associated_cases = count($all_associated_cases); // Activity analysis data $data = $this->getPersonActivityData($all_associated_cases, $categorized_cases); // Get categorized professional relationships $professional_relationships = $this->getCategorizedRelationships($person, $all_associated_cases); return view('persons.show', array_merge(compact('person'), $data, $professional_relationships)); } catch (\Exception $e) { \Log::error('Error in PersonController show method: ' . $e->getMessage()); return view('persons.show', compact('person'))->with('error', 'Unable to load complete person data.'); } } /** * Get categorized professional relationships for a person */ private function getCategorizedRelationships($person, $all_associated_cases) { $all_person_ids = collect(); $person_case_map = []; foreach ($all_associated_cases as $case) { $case_name = $case['name']; $case_year = $case['year'] ?? null; // Panel members if (!empty($case['panel_ids'])) { $panel_ids = array_map('trim', explode(',', $case['panel_ids'])); foreach ($panel_ids as $panel_id) { if (!empty($panel_id) && is_numeric($panel_id) && $panel_id != $person->id) { $all_person_ids->push($panel_id); $person_case_map[$panel_id][] = [ 'case_name' => $case_name, 'year' => $case_year, 'relationship_type' => 'panel_colleague', 'person_role' => $this->getPersonRoleInCase($person->id, $case), 'other_role' => 'panel' ]; } } } // Appellants if (!empty($case['appellant_ids'])) { $appellant_ids = array_map('trim', explode(',', $case['appellant_ids'])); foreach ($appellant_ids as $appellant_id) { if (!empty($appellant_id) && is_numeric($appellant_id) && $appellant_id != $person->id) { $all_person_ids->push($appellant_id); $person_case_map[$appellant_id][] = [ 'case_name' => $case_name, 'year' => $case_year, 'relationship_type' => $this->determineRelationshipType($person->id, $appellant_id, $case, 'appellant'), 'person_role' => $this->getPersonRoleInCase($person->id, $case), 'other_role' => 'appellant' ]; } } } // Respondents if (!empty($case['respondent_ids'])) { $respondent_ids = array_map('trim', explode(',', $case['respondent_ids'])); foreach ($respondent_ids as $respondent_id) { if (!empty($respondent_id) && is_numeric($respondent_id) && $respondent_id != $person->id) { $all_person_ids->push($respondent_id); $person_case_map[$respondent_id][] = [ 'case_name' => $case_name, 'year' => $case_year, 'relationship_type' => $this->determineRelationshipType($person->id, $respondent_id, $case, 'respondent'), 'person_role' => $this->getPersonRoleInCase($person->id, $case), 'other_role' => 'respondent' ]; } } } // Appellant Representatives if (!empty($case['appellant_rep_ids'])) { $appellant_rep_ids = array_map('trim', explode(',', $case['appellant_rep_ids'])); foreach ($appellant_rep_ids as $rep_id) { if (!empty($rep_id) && is_numeric($rep_id) && $rep_id != $person->id) { $all_person_ids->push($rep_id); $person_case_map[$rep_id][] = [ 'case_name' => $case_name, 'year' => $case_year, 'relationship_type' => $this->determineRelationshipType($person->id, $rep_id, $case, 'appellant_rep'), 'person_role' => $this->getPersonRoleInCase($person->id, $case), 'other_role' => 'appellant_rep' ]; } } } // Respondent Representatives if (!empty($case['respondent_rep_ids'])) { $respondent_rep_ids = array_map('trim', explode(',', $case['respondent_rep_ids'])); foreach ($respondent_rep_ids as $rep_id) { if (!empty($rep_id) && is_numeric($rep_id) && $rep_id != $person->id) { $all_person_ids->push($rep_id); $person_case_map[$rep_id][] = [ 'case_name' => $case_name, 'year' => $case_year, 'relationship_type' => $this->determineRelationshipType($person->id, $rep_id, $case, 'respondent_rep'), 'person_role' => $this->getPersonRoleInCase($person->id, $case), 'other_role' => 'respondent_rep' ]; } } } } // Get unique person IDs $all_person_ids = $all_person_ids->unique()->filter(function($id) { return !empty($id) && is_numeric($id); }); if ($all_person_ids->isEmpty()) { return [ 'collaborators' => collect(), 'opponents' => collect(), 'case_relationships' => collect() ]; } // Get all persons data $persons_data = Person::whereIn('id', $all_person_ids)->get()->keyBy('id'); // Categorize relationships $collaborators = collect(); $opponents = collect(); $case_relationships = collect(); foreach ($person_case_map as $person_id => $relationships) { $related_person = $persons_data->get($person_id); if (!$related_person) continue; // Categorize relationship types $collaboration_types = []; $opponent_types = []; $case_relation_types = []; $shared_cases = []; $years = []; foreach ($relationships as $rel) { $shared_cases[] = $rel['case_name']; if ($rel['year']) { $years[] = $rel['year']; } if (in_array($rel['relationship_type'], ['panel_colleague', 'co_counsel'])) { $collaboration_types[] = $rel['relationship_type']; } elseif (in_array($rel['relationship_type'], ['opposing_counsel', 'opposing_party'])) { $opponent_types[] = $rel['relationship_type']; } else { $case_relation_types[] = $rel['relationship_type']; } } $person_data = [ 'id' => $related_person->id, 'name' => $related_person->name, 'role' => $related_person->role, 'organization' => $related_person->organization, 'image_path' => $related_person->image_path, 'shared_cases_count' => count($shared_cases), 'shared_cases_list' => array_unique($shared_cases), 'years_list' => array_unique($years), 'collaboration_types' => array_unique($collaboration_types), 'opponent_types' => array_unique($opponent_types), 'case_relation_types' => array_unique($case_relation_types) ]; // Categorize based on primary relationship type if (!empty($collaboration_types)) { $collaborators->push($person_data); } elseif (!empty($opponent_types)) { $opponents->push($person_data); } else { $case_relationships->push($person_data); } } // Sort by shared cases count $collaborators = $collaborators->sortByDesc('shared_cases_count')->take(20); $opponents = $opponents->sortByDesc('shared_cases_count')->take(20); $case_relationships = $case_relationships->sortByDesc('shared_cases_count')->take(20); return [ 'collaborators' => $collaborators, 'opponents' => $opponents, 'case_relationships' => $case_relationships ]; } /** * Determine relationship type between two persons in a case */ private function determineRelationshipType($person_id, $other_person_id, $case, $other_role) { $person_role = $this->getPersonRoleInCase($person_id, $case); // Panel colleagues if ($person_role === 'panel' && $other_role === 'panel') { return 'panel_colleague'; } // Co-counsels (same side representatives) if (($person_role === 'appellant_rep' && $other_role === 'appellant_rep') || ($person_role === 'respondent_rep' && $other_role === 'respondent_rep')) { return 'co_counsel'; } // Opposing counsels if (($person_role === 'appellant_rep' && $other_role === 'respondent_rep') || ($person_role === 'respondent_rep' && $other_role === 'appellant_rep')) { return 'opposing_counsel'; } // Opposing parties if (($person_role === 'appellant' && $other_role === 'respondent') || ($person_role === 'respondent' && $other_role === 'appellant')) { return 'opposing_party'; } // Client relationships if (($person_role === 'appellant_rep' && $other_role === 'appellant') || ($person_role === 'respondent_rep' && $other_role === 'respondent')) { return 'client_relationship'; } // Judge relationships if ($person_role === 'panel' || $other_role === 'panel') { return 'judge_relationship'; } return 'case_participant'; } /** * Get person's role in a specific case */ private function getPersonRoleInCase($person_id, $case) { // Check panel if (!empty($case['panel_ids'])) { $panel_ids = array_map('trim', explode(',', $case['panel_ids'])); if (in_array($person_id, $panel_ids)) { return 'panel'; } } // Check appellant if (!empty($case['appellant_ids'])) { $appellant_ids = array_map('trim', explode(',', $case['appellant_ids'])); if (in_array($person_id, $appellant_ids)) { return 'appellant'; } } // Check respondent if (!empty($case['respondent_ids'])) { $respondent_ids = array_map('trim', explode(',', $case['respondent_ids'])); if (in_array($person_id, $respondent_ids)) { return 'respondent'; } } // Check appellant rep if (!empty($case['appellant_rep_ids'])) { $appellant_rep_ids = array_map('trim', explode(',', $case['appellant_rep_ids'])); if (in_array($person_id, $appellant_rep_ids)) { return 'appellant_rep'; } } // Check respondent rep if (!empty($case['respondent_rep_ids'])) { $respondent_rep_ids = array_map('trim', explode(',', $case['respondent_rep_ids'])); if (in_array($person_id, $respondent_rep_ids)) { return 'respondent_rep'; } } return 'unknown'; }public function show($id) { try { $person = Person::findOrFail($id); // Existing code for basic person data... $all_associated_cases = $this->getAssociatedCases($person); $categorized_cases = $this->categorizeCases($all_associated_cases, $person); $total_unique_associated_cases = count($all_associated_cases); // Activity analysis data $data = $this->getPersonActivityData($all_associated_cases, $categorized_cases); // Get categorized professional relationships $professional_relationships = $this->getCategorizedRelationships($person, $all_associated_cases); return view('persons.show', array_merge(compact('person'), $data, $professional_relationships)); } catch (\Exception $e) { \Log::error('Error in PersonController show method: ' . $e->getMessage()); return view('persons.show', compact('person'))->with('error', 'Unable to load complete person data.'); } } /** * Get categorized professional relationships for a person */ private function getCategorizedRelationships($person, $all_associated_cases) { $all_person_ids = collect(); $person_case_map = []; foreach ($all_associated_cases as $case) { $case_name = $case['name']; $case_year = $case['year'] ?? null; // Panel members if (!empty($case['panel_ids'])) { $panel_ids = array_map('trim', explode(',', $case['panel_ids'])); foreach ($panel_ids as $panel_id) { if (!empty($panel_id) && is_numeric($panel_id) && $panel_id != $person->id) { $all_person_ids->push($panel_id); $person_case_map[$panel_id][] = [ 'case_name' => $case_name, 'year' => $case_year, 'relationship_type' => 'panel_colleague', 'person_role' => $this->getPersonRoleInCase($person->id, $case), 'other_role' => 'panel' ]; } } } // Appellants if (!empty($case['appellant_ids'])) { $appellant_ids = array_map('trim', explode(',', $case['appellant_ids'])); foreach ($appellant_ids as $appellant_id) { if (!empty($appellant_id) && is_numeric($appellant_id) && $appellant_id != $person->id) { $all_person_ids->push($appellant_id); $person_case_map[$appellant_id][] = [ 'case_name' => $case_name, 'year' => $case_year, 'relationship_type' => $this->determineRelationshipType($person->id, $appellant_id, $case, 'appellant'), 'person_role' => $this->getPersonRoleInCase($person->id, $case), 'other_role' => 'appellant' ]; } } } // Respondents if (!empty($case['respondent_ids'])) { $respondent_ids = array_map('trim', explode(',', $case['respondent_ids'])); foreach ($respondent_ids as $respondent_id) { if (!empty($respondent_id) && is_numeric($respondent_id) && $respondent_id != $person->id) { $all_person_ids->push($respondent_id); $person_case_map[$respondent_id][] = [ 'case_name' => $case_name, 'year' => $case_year, 'relationship_type' => $this->determineRelationshipType($person->id, $respondent_id, $case, 'respondent'), 'person_role' => $this->getPersonRoleInCase($person->id, $case), 'other_role' => 'respondent' ]; } } } // Appellant Representatives if (!empty($case['appellant_rep_ids'])) { $appellant_rep_ids = array_map('trim', explode(',', $case['appellant_rep_ids'])); foreach ($appellant_rep_ids as $rep_id) { if (!empty($rep_id) && is_numeric($rep_id) && $rep_id != $person->id) { $all_person_ids->push($rep_id); $person_case_map[$rep_id][] = [ 'case_name' => $case_name, 'year' => $case_year, 'relationship_type' => $this->determineRelationshipType($person->id, $rep_id, $case, 'appellant_rep'), 'person_role' => $this->getPersonRoleInCase($person->id, $case), 'other_role' => 'appellant_rep' ]; } } } // Respondent Representatives if (!empty($case['respondent_rep_ids'])) { $respondent_rep_ids = array_map('trim', explode(',', $case['respondent_rep_ids'])); foreach ($respondent_rep_ids as $rep_id) { if (!empty($rep_id) && is_numeric($rep_id) && $rep_id != $person->id) { $all_person_ids->push($rep_id); $person_case_map[$rep_id][] = [ 'case_name' => $case_name, 'year' => $case_year, 'relationship_type' => $this->determineRelationshipType($person->id, $rep_id, $case, 'respondent_rep'), 'person_role' => $this->getPersonRoleInCase($person->id, $case), 'other_role' => 'respondent_rep' ]; } } } } // Get unique person IDs $all_person_ids = $all_person_ids->unique()->filter(function($id) { return !empty($id) && is_numeric($id); }); if ($all_person_ids->isEmpty()) { return [ 'collaborators' => collect(), 'opponents' => collect(), 'case_relationships' => collect() ]; } // Get all persons data $persons_data = Person::whereIn('id', $all_person_ids)->get()->keyBy('id'); // Categorize relationships $collaborators = collect(); $opponents = collect(); $case_relationships = collect(); foreach ($person_case_map as $person_id => $relationships) { $related_person = $persons_data->get($person_id); if (!$related_person) continue; // Categorize relationship types $collaboration_types = []; $opponent_types = []; $case_relation_types = []; $shared_cases = []; $years = []; foreach ($relationships as $rel) { $shared_cases[] = $rel['case_name']; if ($rel['year']) { $years[] = $rel['year']; } if (in_array($rel['relationship_type'], ['panel_colleague', 'co_counsel'])) { $collaboration_types[] = $rel['relationship_type']; } elseif (in_array($rel['relationship_type'], ['opposing_counsel', 'opposing_party'])) { $opponent_types[] = $rel['relationship_type']; } else { $case_relation_types[] = $rel['relationship_type']; } } $person_data = [ 'id' => $related_person->id, 'name' => $related_person->name, 'role' => $related_person->role, 'organization' => $related_person->organization, 'image_path' => $related_person->image_path, 'shared_cases_count' => count($shared_cases), 'shared_cases_list' => array_unique($shared_cases), 'years_list' => array_unique($years), 'collaboration_types' => array_unique($collaboration_types), 'opponent_types' => array_unique($opponent_types), 'case_relation_types' => array_unique($case_relation_types) ]; // Categorize based on primary relationship type if (!empty($collaboration_types)) { $collaborators->push($person_data); } elseif (!empty($opponent_types)) { $opponents->push($person_data); } else { $case_relationships->push($person_data); } } // Sort by shared cases count $collaborators = $collaborators->sortByDesc('shared_cases_count')->take(20); $opponents = $opponents->sortByDesc('shared_cases_count')->take(20); $case_relationships = $case_relationships->sortByDesc('shared_cases_count')->take(20); return [ 'collaborators' => $collaborators, 'opponents' => $opponents, 'case_relationships' => $case_relationships ]; } /** * Determine relationship type between two persons in a case */ private function determineRelationshipType($person_id, $other_person_id, $case, $other_role) { $person_role = $this->getPersonRoleInCase($person_id, $case); // Panel colleagues if ($person_role === 'panel' && $other_role === 'panel') { return 'panel_colleague'; } // Co-counsels (same side representatives) if (($person_role === 'appellant_rep' && $other_role === 'appellant_rep') || ($person_role === 'respondent_rep' && $other_role === 'respondent_rep')) { return 'co_counsel'; } // Opposing counsels if (($person_role === 'appellant_rep' && $other_role === 'respondent_rep') || ($person_role === 'respondent_rep' && $other_role === 'appellant_rep')) { return 'opposing_counsel'; } // Opposing parties if (($person_role === 'appellant' && $other_role === 'respondent') || ($person_role === 'respondent' && $other_role === 'appellant')) { return 'opposing_party'; } // Client relationships if (($person_role === 'appellant_rep' && $other_role === 'appellant') || ($person_role === 'respondent_rep' && $other_role === 'respondent')) { return 'client_relationship'; } // Judge relationships if ($person_role === 'panel' || $other_role === 'panel') { return 'judge_relationship'; } return 'case_participant'; } /** * Get person's role in a specific case */ private function getPersonRoleInCase($person_id, $case) { // Check panel if (!empty($case['panel_ids'])) { $panel_ids = array_map('trim', explode(',', $case['panel_ids'])); if (in_array($person_id, $panel_ids)) { return 'panel'; } } // Check appellant if (!empty($case['appellant_ids'])) { $appellant_ids = array_map('trim', explode(',', $case['appellant_ids'])); if (in_array($person_id, $appellant_ids)) { return 'appellant'; } } // Check respondent if (!empty($case['respondent_ids'])) { $respondent_ids = array_map('trim', explode(',', $case['respondent_ids'])); if (in_array($person_id, $respondent_ids)) { return 'respondent'; } } // Check appellant rep if (!empty($case['appellant_rep_ids'])) { $appellant_rep_ids = array_map('trim', explode(',', $case['appellant_rep_ids'])); if (in_array($person_id, $appellant_rep_ids)) { return 'appellant_rep'; } } // Check respondent rep if (!empty($case['respondent_rep_ids'])) { $respondent_rep_ids = array_map('trim', explode(',', $case['respondent_rep_ids'])); if (in_array($person_id, $respondent_rep_ids)) { return 'respondent_rep'; } } return 'unknown'; }public function show($id) { try { $person = Person::findOrFail($id); // Existing code for basic person data... $all_associated_cases = $this->getAssociatedCases($person); $categorized_cases = $this->categorizeCases($all_associated_cases, $person); $total_unique_associated_cases = count($all_associated_cases); // Activity analysis data $data = $this->getPersonActivityData($all_associated_cases, $categorized_cases); // Get categorized professional relationships $professional_relationships = $this->getCategorizedRelationships($person, $all_associated_cases); return view('persons.show', array_merge(compact('person'), $data, $professional_relationships)); } catch (\Exception $e) { \Log::error('Error in PersonController show method: ' . $e->getMessage()); return view('persons.show', compact('person'))->with('error', 'Unable to load complete person data.'); } } /** * Get categorized professional relationships for a person */ private function getCategorizedRelationships($person, $all_associated_cases) { $all_person_ids = collect(); $person_case_map = []; foreach ($all_associated_cases as $case) { $case_name = $case['name']; $case_year = $case['year'] ?? null; // Panel members if (!empty($case['panel_ids'])) { $panel_ids = array_map('trim', explode(',', $case['panel_ids'])); foreach ($panel_ids as $panel_id) { if (!empty($panel_id) && is_numeric($panel_id) && $panel_id != $person->id) { $all_person_ids->push($panel_id); $person_case_map[$panel_id][] = [ 'case_name' => $case_name, 'year' => $case_year, 'relationship_type' => 'panel_colleague', 'person_role' => $this->getPersonRoleInCase($person->id, $case), 'other_role' => 'panel' ]; } } } // Appellants if (!empty($case['appellant_ids'])) { $appellant_ids = array_map('trim', explode(',', $case['appellant_ids'])); foreach ($appellant_ids as $appellant_id) { if (!empty($appellant_id) && is_numeric($appellant_id) && $appellant_id != $person->id) { $all_person_ids->push($appellant_id); $person_case_map[$appellant_id][] = [ 'case_name' => $case_name, 'year' => $case_year, 'relationship_type' => $this->determineRelationshipType($person->id, $appellant_id, $case, 'appellant'), 'person_role' => $this->getPersonRoleInCase($person->id, $case), 'other_role' => 'appellant' ]; } } } // Respondents if (!empty($case['respondent_ids'])) { $respondent_ids = array_map('trim', explode(',', $case['respondent_ids'])); foreach ($respondent_ids as $respondent_id) { if (!empty($respondent_id) && is_numeric($respondent_id) && $respondent_id != $person->id) { $all_person_ids->push($respondent_id); $person_case_map[$respondent_id][] = [ 'case_name' => $case_name, 'year' => $case_year, 'relationship_type' => $this->determineRelationshipType($person->id, $respondent_id, $case, 'respondent'), 'person_role' => $this->getPersonRoleInCase($person->id, $case), 'other_role' => 'respondent' ]; } } } // Appellant Representatives if (!empty($case['appellant_rep_ids'])) { $appellant_rep_ids = array_map('trim', explode(',', $case['appellant_rep_ids'])); foreach ($appellant_rep_ids as $rep_id) { if (!empty($rep_id) && is_numeric($rep_id) && $rep_id != $person->id) { $all_person_ids->push($rep_id); $person_case_map[$rep_id][] = [ 'case_name' => $case_name, 'year' => $case_year, 'relationship_type' => $this->determineRelationshipType($person->id, $rep_id, $case, 'appellant_rep'), 'person_role' => $this->getPersonRoleInCase($person->id, $case), 'other_role' => 'appellant_rep' ]; } } } // Respondent Representatives if (!empty($case['respondent_rep_ids'])) { $respondent_rep_ids = array_map('trim', explode(',', $case['respondent_rep_ids'])); foreach ($respondent_rep_ids as $rep_id) { if (!empty($rep_id) && is_numeric($rep_id) && $rep_id != $person->id) { $all_person_ids->push($rep_id); $person_case_map[$rep_id][] = [ 'case_name' => $case_name, 'year' => $case_year, 'relationship_type' => $this->determineRelationshipType($person->id, $rep_id, $case, 'respondent_rep'), 'person_role' => $this->getPersonRoleInCase($person->id, $case), 'other_role' => 'respondent_rep' ]; } } } } // Get unique person IDs $all_person_ids = $all_person_ids->unique()->filter(function($id) { return !empty($id) && is_numeric($id); }); if ($all_person_ids->isEmpty()) { return [ 'collaborators' => collect(), 'opponents' => collect(), 'case_relationships' => collect() ]; } // Get all persons data $persons_data = Person::whereIn('id', $all_person_ids)->get()->keyBy('id'); // Categorize relationships $collaborators = collect(); $opponents = collect(); $case_relationships = collect(); foreach ($person_case_map as $person_id => $relationships) { $related_person = $persons_data->get($person_id); if (!$related_person) continue; // Categorize relationship types $collaboration_types = []; $opponent_types = []; $case_relation_types = []; $shared_cases = []; $years = []; foreach ($relationships as $rel) { $shared_cases[] = $rel['case_name']; if ($rel['year']) { $years[] = $rel['year']; } if (in_array($rel['relationship_type'], ['panel_colleague', 'co_counsel'])) { $collaboration_types[] = $rel['relationship_type']; } elseif (in_array($rel['relationship_type'], ['opposing_counsel', 'opposing_party'])) { $opponent_types[] = $rel['relationship_type']; } else { $case_relation_types[] = $rel['relationship_type']; } } $person_data = [ 'id' => $related_person->id, 'name' => $related_person->name, 'role' => $related_person->role, 'organization' => $related_person->organization, 'image_path' => $related_person->image_path, 'shared_cases_count' => count($shared_cases), 'shared_cases_list' => array_unique($shared_cases), 'years_list' => array_unique($years), 'collaboration_types' => array_unique($collaboration_types), 'opponent_types' => array_unique($opponent_types), 'case_relation_types' => array_unique($case_relation_types) ]; // Categorize based on primary relationship type if (!empty($collaboration_types)) { $collaborators->push($person_data); } elseif (!empty($opponent_types)) { $opponents->push($person_data); } else { $case_relationships->push($person_data); } } // Sort by shared cases count $collaborators = $collaborators->sortByDesc('shared_cases_count')->take(20); $opponents = $opponents->sortByDesc('shared_cases_count')->take(20); $case_relationships = $case_relationships->sortByDesc('shared_cases_count')->take(20); return [ 'collaborators' => $collaborators, 'opponents' => $opponents, 'case_relationships' => $case_relationships ]; } /** * Determine relationship type between two persons in a case */ private function determineRelationshipType($person_id, $other_person_id, $case, $other_role) { $person_role = $this->getPersonRoleInCase($person_id, $case); // Panel colleagues if ($person_role === 'panel' && $other_role === 'panel') { return 'panel_colleague'; } // Co-counsels (same side representatives) if (($person_role === 'appellant_rep' && $other_role === 'appellant_rep') || ($person_role === 'respondent_rep' && $other_role === 'respondent_rep')) { return 'co_counsel'; } // Opposing counsels if (($person_role === 'appellant_rep' && $other_role === 'respondent_rep') || ($person_role === 'respondent_rep' && $other_role === 'appellant_rep')) { return 'opposing_counsel'; } // Opposing parties if (($person_role === 'appellant' && $other_role === 'respondent') || ($person_role === 'respondent' && $other_role === 'appellant')) { return 'opposing_party'; } // Client relationships if (($person_role === 'appellant_rep' && $other_role === 'appellant') || ($person_role === 'respondent_rep' && $other_role === 'respondent')) { return 'client_relationship'; } // Judge relationships if ($person_role === 'panel' || $other_role === 'panel') { return 'judge_relationship'; } return 'case_participant'; } /** * Get person's role in a specific case */ private function getPersonRoleInCase($person_id, $case) { // Check panel if (!empty($case['panel_ids'])) { $panel_ids = array_map('trim', explode(',', $case['panel_ids'])); if (in_array($person_id, $panel_ids)) { return 'panel'; } } // Check appellant if (!empty($case['appellant_ids'])) { $appellant_ids = array_map('trim', explode(',', $case['appellant_ids'])); if (in_array($person_id, $appellant_ids)) { return 'appellant'; } } // Check respondent if (!empty($case['respondent_ids'])) { $respondent_ids = array_map('trim', explode(',', $case['respondent_ids'])); if (in_array($person_id, $respondent_ids)) { return 'respondent'; } } // Check appellant rep if (!empty($case['appellant_rep_ids'])) { $appellant_rep_ids = array_map('trim', explode(',', $case['appellant_rep_ids'])); if (in_array($person_id, $appellant_rep_ids)) { return 'appellant_rep'; } } // Check respondent rep if (!empty($case['respondent_rep_ids'])) { $respondent_rep_ids = array_map('trim', explode(',', $case['respondent_rep_ids'])); if (in_array($person_id, $respondent_rep_ids)) { return 'respondent_rep'; } } return 'unknown'; }