| Total Complexity | 81 |
| Total Lines | 596 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like RelationshipsChartModule often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RelationshipsChartModule, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class RelationshipsChartModule extends AbstractModule implements ModuleChartInterface, ModuleConfigInterface |
||
| 41 | { |
||
| 42 | use ModuleChartTrait; |
||
| 43 | use ModuleConfigTrait; |
||
| 44 | |||
| 45 | /** It would be more correct to use PHP_INT_MAX, but this isn't friendly in URLs */ |
||
| 46 | public const UNLIMITED_RECURSION = 99; |
||
| 47 | |||
| 48 | /** By default new trees allow unlimited recursion */ |
||
| 49 | public const DEFAULT_RECURSION = '99'; |
||
| 50 | |||
| 51 | /** By default new trees search for all relationships (not via ancestors) */ |
||
| 52 | public const DEFAULT_ANCESTORS = '0'; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * A sentence describing what this module does. |
||
| 56 | * |
||
| 57 | * @return string |
||
| 58 | */ |
||
| 59 | public function description(): string |
||
| 60 | { |
||
| 61 | /* I18N: Description of the “RelationshipsChart” module */ |
||
| 62 | return I18N::translate('A chart displaying relationships between two individuals.'); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Return a menu item for this chart - for use in individual boxes. |
||
| 67 | * |
||
| 68 | * @param Individual $individual |
||
| 69 | * |
||
| 70 | * @return Menu|null |
||
| 71 | */ |
||
| 72 | public function chartBoxMenu(Individual $individual): ?Menu |
||
| 73 | { |
||
| 74 | return $this->chartMenu($individual); |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * A main menu item for this chart. |
||
| 79 | * |
||
| 80 | * @param Individual $individual |
||
| 81 | * |
||
| 82 | * @return Menu |
||
| 83 | */ |
||
| 84 | public function chartMenu(Individual $individual): Menu |
||
| 102 | ); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * CSS class for the URL. |
||
| 107 | * |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | public function chartMenuClass(): string |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * How should this module be identified in the control panel, etc.? |
||
| 117 | * |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | public function title(): string |
||
| 121 | { |
||
| 122 | /* I18N: Name of a module/chart */ |
||
| 123 | return I18N::translate('Relationships'); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @return ResponseInterface |
||
| 128 | */ |
||
| 129 | public function getAdminAction(): ResponseInterface |
||
| 140 | ]); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Possible options for the ancestors option |
||
| 145 | * |
||
| 146 | * @return string[] |
||
| 147 | */ |
||
| 148 | private function ancestorsOptions(): array |
||
| 149 | { |
||
| 150 | return [ |
||
| 151 | 0 => I18N::translate('Find any relationship'), |
||
| 152 | 1 => I18N::translate('Find relationships via ancestors'), |
||
| 153 | ]; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Possible options for the recursion option |
||
| 158 | * |
||
| 159 | * @return string[] |
||
| 160 | */ |
||
| 161 | private function recursionConfigOptions(): array |
||
| 162 | { |
||
| 163 | return [ |
||
| 164 | 0 => I18N::translate('none'), |
||
| 165 | 1 => I18N::number(1), |
||
| 166 | 2 => I18N::number(2), |
||
| 167 | 3 => I18N::number(3), |
||
| 168 | self::UNLIMITED_RECURSION => I18N::translate('unlimited'), |
||
| 169 | ]; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param ServerRequestInterface $request |
||
| 174 | * |
||
| 175 | * @return ResponseInterface |
||
| 176 | */ |
||
| 177 | public function postAdminAction(ServerRequestInterface $request): ResponseInterface |
||
| 178 | { |
||
| 179 | foreach (Tree::getAll() as $tree) { |
||
| 180 | $recursion = $request->getQueryParams()['relationship-recursion-' . $tree->id()] ?? ''; |
||
| 181 | $ancestors = $request->getQueryParams()['relationship-ancestors-' . $tree->id()] ?? ''; |
||
| 182 | |||
| 183 | $tree->setPreference('RELATIONSHIP_RECURSION', $recursion); |
||
| 184 | $tree->setPreference('RELATIONSHIP_ANCESTORS', $ancestors); |
||
| 185 | } |
||
| 186 | |||
| 187 | FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->title()), 'success'); |
||
| 188 | |||
| 189 | return redirect($this->getConfigLink()); |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * A form to request the chart parameters. |
||
| 194 | * |
||
| 195 | * @param ServerRequestInterface $request |
||
| 196 | * @param Tree $tree |
||
| 197 | * @param UserInterface $user |
||
| 198 | * |
||
| 199 | * @return ResponseInterface |
||
| 200 | */ |
||
| 201 | public function getChartAction(ServerRequestInterface $request, Tree $tree, UserInterface $user): ResponseInterface |
||
| 261 | ]); |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param Individual $individual1 |
||
| 266 | * @param Individual $individual2 |
||
| 267 | * @param int $recursion |
||
| 268 | * @param int $ancestors |
||
| 269 | * |
||
| 270 | * @return ResponseInterface |
||
| 271 | */ |
||
| 272 | public function chart(Individual $individual1, Individual $individual2, int $recursion, int $ancestors): ResponseInterface |
||
| 273 | { |
||
| 274 | $tree = $individual1->tree(); |
||
| 275 | |||
| 276 | $max_recursion = (int) $tree->getPreference('RELATIONSHIP_RECURSION', static::DEFAULT_RECURSION); |
||
| 277 | |||
| 278 | $recursion = min($recursion, $max_recursion); |
||
| 279 | |||
| 280 | $paths = $this->calculateRelationships($individual1, $individual2, $recursion, (bool) $ancestors); |
||
| 281 | |||
| 282 | // @TODO - convert to views |
||
| 283 | ob_start(); |
||
| 284 | if (I18N::direction() === 'ltr') { |
||
| 285 | $diagonal1 = asset('css/images/dline.png'); |
||
| 286 | $diagonal2 = asset('css/images/dline2.png'); |
||
| 287 | } else { |
||
| 288 | $diagonal1 = asset('css/images/dline2.png'); |
||
| 289 | $diagonal2 = asset('css/images/dline.png'); |
||
| 290 | } |
||
| 291 | |||
| 292 | $num_paths = 0; |
||
| 293 | foreach ($paths as $path) { |
||
| 294 | // Extract the relationship names between pairs of individuals |
||
| 295 | $relationships = $this->oldStyleRelationshipPath($tree, $path); |
||
| 296 | if (empty($relationships)) { |
||
| 297 | // Cannot see one of the families/individuals, due to privacy; |
||
| 298 | continue; |
||
| 299 | } |
||
| 300 | echo '<h3>', I18N::translate('Relationship: %s', Functions::getRelationshipNameFromPath(implode('', $relationships), $individual1, $individual2)), '</h3>'; |
||
| 301 | $num_paths++; |
||
| 302 | |||
| 303 | // Use a table/grid for layout. |
||
| 304 | $table = []; |
||
| 305 | // Current position in the grid. |
||
| 306 | $x = 0; |
||
| 307 | $y = 0; |
||
| 308 | // Extent of the grid. |
||
| 309 | $min_y = 0; |
||
| 310 | $max_y = 0; |
||
| 311 | $max_x = 0; |
||
| 312 | // For each node in the path. |
||
| 313 | foreach ($path as $n => $xref) { |
||
| 314 | if ($n % 2 === 1) { |
||
| 315 | switch ($relationships[$n]) { |
||
| 316 | case 'hus': |
||
| 317 | case 'wif': |
||
| 318 | case 'spo': |
||
| 319 | case 'bro': |
||
| 320 | case 'sis': |
||
| 321 | case 'sib': |
||
| 322 | $table[$x + 1][$y] = '<div style="background:url(' . e(asset('css/images/hline.png')) . ') repeat-x center; width: 94px; text-align: center"><div class="hline-text" style="height: 32px;">' . Functions::getRelationshipNameFromPath($relationships[$n], Individual::getInstance($path[$n - 1], $tree), Individual::getInstance($path[$n + 1], $tree)) . '</div><div style="height: 32px;">' . view('icons/arrow-right') . '</div></div>'; |
||
| 323 | $x += 2; |
||
| 324 | break; |
||
| 325 | case 'son': |
||
| 326 | case 'dau': |
||
| 327 | case 'chi': |
||
| 328 | if ($n > 2 && preg_match('/fat|mot|par/', $relationships[$n - 2])) { |
||
| 329 | $table[$x + 1][$y - 1] = '<div style="background:url(' . $diagonal2 . '); width: 64px; height: 64px; text-align: center;"><div style="height: 32px; text-align: end;">' . Functions::getRelationshipNameFromPath($relationships[$n], Individual::getInstance($path[$n - 1], $tree), Individual::getInstance($path[$n + 1], $tree)) . '</div><div style="height: 32px; text-align: start;">' . view('icons/arrow-down') . '</div></div>'; |
||
| 330 | $x += 2; |
||
| 331 | } else { |
||
| 332 | $table[$x][$y - 1] = '<div style="background:url(' . e('"' . asset('css/images/vline.png') . '"') . ') repeat-y center; height: 64px; text-align: center;"><div class="vline-text" style="display: inline-block; width:50%; line-height: 64px;">' . Functions::getRelationshipNameFromPath($relationships[$n], Individual::getInstance($path[$n - 1], $tree), Individual::getInstance($path[$n + 1], $tree)) . '</div><div style="display: inline-block; width:50%; line-height: 64px;">' . view('icons/arrow-down') . '</div></div>'; |
||
| 333 | } |
||
| 334 | $y -= 2; |
||
| 335 | break; |
||
| 336 | case 'fat': |
||
| 337 | case 'mot': |
||
| 338 | case 'par': |
||
| 339 | if ($n > 2 && preg_match('/son|dau|chi/', $relationships[$n - 2])) { |
||
| 340 | $table[$x + 1][$y + 1] = '<div style="background:url(' . $diagonal1 . '); background-position: top right; width: 64px; height: 64px; text-align: center;"><div style="height: 32px; text-align: start;">' . Functions::getRelationshipNameFromPath($relationships[$n], Individual::getInstance($path[$n - 1], $tree), Individual::getInstance($path[$n + 1], $tree)) . '</div><div style="height: 32px; text-align: end;">' . view('icons/arrow-down') . '</div></div>'; |
||
| 341 | $x += 2; |
||
| 342 | } else { |
||
| 343 | $table[$x][$y + 1] = '<div style="background:url(' . e('"' . asset('css/images/vline.png') . '"') . ') repeat-y center; height: 64px; text-align:center; "><div class="vline-text" style="display: inline-block; width: 50%; line-height: 64px;">' . Functions::getRelationshipNameFromPath($relationships[$n], Individual::getInstance($path[$n - 1], $tree), Individual::getInstance($path[$n + 1], $tree)) . '</div><div style="display: inline-block; width: 50%; line-height: 32px">' . view('icons/arrow-up') . '</div></div>'; |
||
| 344 | } |
||
| 345 | $y += 2; |
||
| 346 | break; |
||
| 347 | } |
||
| 348 | $max_x = max($max_x, $x); |
||
| 349 | $min_y = min($min_y, $y); |
||
| 350 | $max_y = max($max_y, $y); |
||
| 351 | } else { |
||
| 352 | $individual = Individual::getInstance($xref, $tree); |
||
| 353 | $table[$x][$y] = view('chart-box', ['individual' => $individual]); |
||
| 354 | } |
||
| 355 | } |
||
| 356 | echo '<div class="wt-chart wt-chart-relationships">'; |
||
| 357 | echo '<table style="border-collapse: collapse; margin: 20px 50px;">'; |
||
| 358 | for ($y = $max_y; $y >= $min_y; --$y) { |
||
| 359 | echo '<tr>'; |
||
| 360 | for ($x = 0; $x <= $max_x; ++$x) { |
||
| 361 | echo '<td style="padding: 0;">'; |
||
| 362 | if (isset($table[$x][$y])) { |
||
| 363 | echo $table[$x][$y]; |
||
| 364 | } |
||
| 365 | echo '</td>'; |
||
| 366 | } |
||
| 367 | echo '</tr>'; |
||
| 368 | } |
||
| 369 | echo '</table>'; |
||
| 370 | echo '</div>'; |
||
| 371 | } |
||
| 372 | |||
| 373 | if (!$num_paths) { |
||
| 374 | echo '<p>', I18N::translate('No link between the two individuals could be found.'), '</p>'; |
||
| 375 | } |
||
| 376 | |||
| 377 | $html = ob_get_clean(); |
||
| 378 | |||
| 379 | return response($html); |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Calculate the shortest paths - or all paths - between two individuals. |
||
| 384 | * |
||
| 385 | * @param Individual $individual1 |
||
| 386 | * @param Individual $individual2 |
||
| 387 | * @param int $recursion How many levels of recursion to use |
||
| 388 | * @param bool $ancestor Restrict to relationships via a common ancestor |
||
| 389 | * |
||
| 390 | * @return string[][] |
||
| 391 | */ |
||
| 392 | private function calculateRelationships(Individual $individual1, Individual $individual2, $recursion, $ancestor = false): array |
||
| 473 | } |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Convert numeric values to strings |
||
| 477 | * |
||
| 478 | * @return Closure |
||
| 479 | */ |
||
| 480 | private function stringMapper(): Closure { |
||
| 481 | return static function ($xref) { |
||
| 482 | return (string) $xref; |
||
| 483 | }; |
||
| 484 | } |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Find all ancestors of a list of individuals |
||
| 488 | * |
||
| 489 | * @param string $xref1 |
||
| 490 | * @param string $xref2 |
||
| 491 | * @param int $tree_id |
||
| 492 | * |
||
| 493 | * @return string[] |
||
| 494 | */ |
||
| 495 | private function allAncestors($xref1, $xref2, $tree_id): array |
||
| 496 | { |
||
| 497 | $ancestors = [ |
||
| 498 | $xref1, |
||
| 499 | $xref2, |
||
| 500 | ]; |
||
| 501 | |||
| 502 | $queue = [ |
||
| 503 | $xref1, |
||
| 504 | $xref2, |
||
| 505 | ]; |
||
| 506 | while (!empty($queue)) { |
||
| 507 | $parents = DB::table('link AS l1') |
||
| 508 | ->join('link AS l2', static function (JoinClause $join): void { |
||
| 509 | $join |
||
| 510 | ->on('l1.l_to', '=', 'l2.l_to') |
||
| 511 | ->on('l1.l_file', '=', 'l2.l_file'); |
||
| 512 | }) |
||
| 513 | ->where('l1.l_file', '=', $tree_id) |
||
| 514 | ->where('l1.l_type', '=', 'FAMC') |
||
| 515 | ->where('l2.l_type', '=', 'FAMS') |
||
| 516 | ->whereIn('l1.l_from', $queue) |
||
| 517 | ->pluck('l2.l_from'); |
||
| 518 | |||
| 519 | $queue = []; |
||
| 520 | foreach ($parents as $parent) { |
||
| 521 | if (!in_array($parent, $ancestors, true)) { |
||
| 522 | $ancestors[] = $parent; |
||
| 523 | $queue[] = $parent; |
||
| 524 | } |
||
| 525 | } |
||
| 526 | } |
||
| 527 | |||
| 528 | return $ancestors; |
||
| 529 | } |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Find all families of two individuals |
||
| 533 | * |
||
| 534 | * @param string $xref1 |
||
| 535 | * @param string $xref2 |
||
| 536 | * @param int $tree_id |
||
| 537 | * |
||
| 538 | * @return string[] |
||
| 539 | */ |
||
| 540 | private function excludeFamilies($xref1, $xref2, $tree_id): array |
||
| 541 | { |
||
| 542 | return DB::table('link AS l1') |
||
| 543 | ->join('link AS l2', static function (JoinClause $join): void { |
||
| 544 | $join |
||
| 545 | ->on('l1.l_to', '=', 'l2.l_to') |
||
| 546 | ->on('l1.l_type', '=', 'l2.l_type') |
||
| 547 | ->on('l1.l_file', '=', 'l2.l_file'); |
||
| 548 | }) |
||
| 549 | ->where('l1.l_file', '=', $tree_id) |
||
| 550 | ->where('l1.l_type', '=', 'FAMS') |
||
| 551 | ->where('l1.l_from', '=', $xref1) |
||
| 552 | ->where('l2.l_from', '=', $xref2) |
||
| 553 | ->pluck('l1.l_to') |
||
| 554 | ->all(); |
||
| 555 | } |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Convert a path (list of XREFs) to an "old-style" string of relationships. |
||
| 559 | * Return an empty array, if privacy rules prevent us viewing any node. |
||
| 560 | * |
||
| 561 | * @param Tree $tree |
||
| 562 | * @param string[] $path Alternately Individual / Family |
||
| 563 | * |
||
| 564 | * @return string[] |
||
| 565 | */ |
||
| 566 | private function oldStyleRelationshipPath(Tree $tree, array $path): array |
||
| 616 | } |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Possible options for the recursion option |
||
| 620 | * |
||
| 621 | * @param int $max_recursion |
||
| 622 | * |
||
| 623 | * @return string[] |
||
| 624 | */ |
||
| 625 | private function recursionOptions(int $max_recursion): array |
||
| 636 | ]; |
||
| 637 | } |
||
| 638 | } |
||
| 639 |