Total Complexity | 86 |
Total Lines | 661 |
Duplicated Lines | 0 % |
Changes | 3 | ||
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 |
||
48 | class RelationshipsChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface |
||
49 | { |
||
50 | use ModuleChartTrait; |
||
51 | use ModuleConfigTrait; |
||
52 | |||
53 | private const ROUTE_NAME = 'relationships'; |
||
54 | private const ROUTE_URL = '/tree/{tree}/relationships-{ancestors}-{recursion}/{xref}{/xref2}'; |
||
55 | |||
56 | /** It would be more correct to use PHP_INT_MAX, but this isn't friendly in URLs */ |
||
57 | public const UNLIMITED_RECURSION = 99; |
||
58 | |||
59 | /** By default new trees allow unlimited recursion */ |
||
60 | public const DEFAULT_RECURSION = '99'; |
||
61 | |||
62 | /** By default new trees search for all relationships (not via ancestors) */ |
||
63 | public const DEFAULT_ANCESTORS = '0'; |
||
64 | public const DEFAULT_PARAMETERS = [ |
||
65 | 'ancestors' => self::DEFAULT_ANCESTORS, |
||
66 | 'recursion' => self::DEFAULT_RECURSION, |
||
67 | ]; |
||
68 | |||
69 | /** @var TreeService */ |
||
70 | private $tree_service; |
||
71 | |||
72 | /** |
||
73 | * ModuleController constructor. |
||
74 | * |
||
75 | * @param TreeService $tree_service |
||
76 | */ |
||
77 | public function __construct(TreeService $tree_service) |
||
78 | { |
||
79 | $this->tree_service = $tree_service; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Initialization. |
||
84 | * |
||
85 | * @param RouterContainer $router_container |
||
86 | */ |
||
87 | public function boot(RouterContainer $router_container) |
||
88 | { |
||
89 | $router_container->getMap() |
||
90 | ->get(self::ROUTE_NAME, self::ROUTE_URL, self::class) |
||
91 | ->allows(RequestMethodInterface::METHOD_POST) |
||
92 | ->tokens([ |
||
93 | 'ancestors' => '\d+', |
||
94 | 'recursion' => '\d+', |
||
95 | ])->defaults([ |
||
96 | 'xref2' => '', |
||
97 | ]); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * A sentence describing what this module does. |
||
102 | * |
||
103 | * @return string |
||
104 | */ |
||
105 | public function description(): string |
||
106 | { |
||
107 | /* I18N: Description of the “RelationshipsChart” module */ |
||
108 | return I18N::translate('A chart displaying relationships between two individuals.'); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Return a menu item for this chart - for use in individual boxes. |
||
113 | * |
||
114 | * @param Individual $individual |
||
115 | * |
||
116 | * @return Menu|null |
||
117 | */ |
||
118 | public function chartBoxMenu(Individual $individual): ?Menu |
||
119 | { |
||
120 | return $this->chartMenu($individual); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * A main menu item for this chart. |
||
125 | * |
||
126 | * @param Individual $individual |
||
127 | * |
||
128 | * @return Menu |
||
129 | */ |
||
130 | public function chartMenu(Individual $individual): Menu |
||
131 | { |
||
132 | $gedcomid = $individual->tree()->getUserPreference(Auth::user(), 'gedcomid'); |
||
133 | |||
134 | if ($gedcomid !== '' && $gedcomid !== $individual->xref()) { |
||
135 | return new Menu( |
||
136 | I18N::translate('Relationship to me'), |
||
137 | $this->chartUrl($individual, ['xref2' => $gedcomid]), |
||
138 | $this->chartMenuClass(), |
||
139 | $this->chartUrlAttributes() |
||
140 | ); |
||
141 | } |
||
142 | |||
143 | return new Menu( |
||
144 | $this->title(), |
||
145 | $this->chartUrl($individual), |
||
146 | $this->chartMenuClass(), |
||
147 | $this->chartUrlAttributes() |
||
148 | ); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * CSS class for the URL. |
||
153 | * |
||
154 | * @return string |
||
155 | */ |
||
156 | public function chartMenuClass(): string |
||
157 | { |
||
158 | return 'menu-chart-relationship'; |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * How should this module be identified in the control panel, etc.? |
||
163 | * |
||
164 | * @return string |
||
165 | */ |
||
166 | public function title(): string |
||
167 | { |
||
168 | /* I18N: Name of a module/chart */ |
||
169 | return I18N::translate('Relationships'); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * The URL for a page showing chart options. |
||
174 | * |
||
175 | * @param Individual $individual |
||
176 | * @param mixed[] $parameters |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | public function chartUrl(Individual $individual, array $parameters = []): string |
||
181 | { |
||
182 | return route(self::ROUTE_NAME, [ |
||
183 | 'xref' => $individual->xref(), |
||
184 | 'tree' => $individual->tree()->name(), |
||
185 | ] + $parameters + self::DEFAULT_PARAMETERS); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * @param ServerRequestInterface $request |
||
190 | * |
||
191 | * @return ResponseInterface |
||
192 | */ |
||
193 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
194 | { |
||
195 | $ajax = $request->getQueryParams()['ajax'] ?? ''; |
||
196 | $ancestors = (int) $request->getAttribute('ancestors'); |
||
197 | $recursion = (int) $request->getAttribute('recursion'); |
||
198 | $tree = $request->getAttribute('tree'); |
||
199 | $user = $request->getAttribute('user'); |
||
200 | $xref = $request->getAttribute('xref'); |
||
201 | $xref2 = $request->getAttribute('xref2'); |
||
202 | |||
203 | // Convert POST requests into GET requests for pretty URLs. |
||
204 | if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { |
||
205 | return redirect(route(self::ROUTE_NAME, [ |
||
206 | 'ancestors' => $request->getParsedBody()['ancestors'], |
||
207 | 'recursion' => $request->getParsedBody()['recursion'], |
||
208 | 'tree' => $request->getAttribute('tree')->name(), |
||
209 | 'xref' => $request->getParsedBody()['xref'], |
||
210 | 'xref2' => $request->getParsedBody()['xref2'], |
||
211 | ])); |
||
212 | } |
||
213 | |||
214 | $individual1 = Individual::getInstance($xref, $tree); |
||
215 | $individual2 = Individual::getInstance($xref2, $tree); |
||
216 | |||
217 | $ancestors_only = (int) $tree->getPreference('RELATIONSHIP_ANCESTORS', static::DEFAULT_ANCESTORS); |
||
218 | $max_recursion = (int) $tree->getPreference('RELATIONSHIP_RECURSION', static::DEFAULT_RECURSION); |
||
219 | |||
220 | $recursion = min($recursion, $max_recursion); |
||
221 | |||
222 | if ($tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') !== '1') { |
||
223 | if ($individual1 instanceof Individual) { |
||
224 | Auth::checkIndividualAccess($individual1); |
||
225 | } |
||
226 | |||
227 | if ($individual2 instanceof Individual) { |
||
228 | Auth::checkIndividualAccess($individual2); |
||
229 | } |
||
230 | } |
||
231 | |||
232 | Auth::checkComponentAccess($this, 'chart', $tree, $user); |
||
233 | |||
234 | if ($individual1 instanceof Individual && $individual2 instanceof Individual) { |
||
235 | if ($ajax === '1') { |
||
236 | return $this->chart($individual1, $individual2, $recursion, $ancestors); |
||
237 | } |
||
238 | |||
239 | /* I18N: %s are individual’s names */ |
||
240 | $title = I18N::translate('Relationships between %1$s and %2$s', $individual1->fullName(), $individual2->fullName()); |
||
241 | $ajax_url = $this->chartUrl($individual1, [ |
||
242 | 'ajax' => true, |
||
243 | 'ancestors' => $ancestors, |
||
244 | 'recursion' => $recursion, |
||
245 | 'xref2' => $individual2->xref(), |
||
246 | ]); |
||
247 | } else { |
||
248 | $title = I18N::translate('Relationships'); |
||
249 | $ajax_url = ''; |
||
250 | } |
||
251 | |||
252 | return $this->viewResponse('modules/relationships-chart/page', [ |
||
253 | 'ajax_url' => $ajax_url, |
||
254 | 'ancestors' => $ancestors, |
||
255 | 'ancestors_only' => $ancestors_only, |
||
256 | 'ancestors_options' => $this->ancestorsOptions(), |
||
257 | 'individual1' => $individual1, |
||
258 | 'individual2' => $individual2, |
||
259 | 'max_recursion' => $max_recursion, |
||
260 | 'module' => $this->name(), |
||
261 | 'recursion' => $recursion, |
||
262 | 'recursion_options' => $this->recursionOptions($max_recursion), |
||
263 | 'title' => $title, |
||
264 | 'tree' => $tree, |
||
265 | ]); |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * @param Individual $individual1 |
||
270 | * @param Individual $individual2 |
||
271 | * @param int $recursion |
||
272 | * @param int $ancestors |
||
273 | * |
||
274 | * @return ResponseInterface |
||
275 | */ |
||
276 | public function chart(Individual $individual1, Individual $individual2, int $recursion, int $ancestors): ResponseInterface |
||
277 | { |
||
278 | $tree = $individual1->tree(); |
||
279 | |||
280 | $max_recursion = (int) $tree->getPreference('RELATIONSHIP_RECURSION', static::DEFAULT_RECURSION); |
||
281 | |||
282 | $recursion = min($recursion, $max_recursion); |
||
283 | |||
284 | $paths = $this->calculateRelationships($individual1, $individual2, $recursion, (bool) $ancestors); |
||
285 | |||
286 | // @TODO - convert to views |
||
287 | ob_start(); |
||
288 | if (I18N::direction() === 'ltr') { |
||
289 | $diagonal1 = asset('css/images/dline.png'); |
||
290 | $diagonal2 = asset('css/images/dline2.png'); |
||
291 | } else { |
||
292 | $diagonal1 = asset('css/images/dline2.png'); |
||
293 | $diagonal2 = asset('css/images/dline.png'); |
||
294 | } |
||
295 | |||
296 | $num_paths = 0; |
||
297 | foreach ($paths as $path) { |
||
298 | // Extract the relationship names between pairs of individuals |
||
299 | $relationships = $this->oldStyleRelationshipPath($tree, $path); |
||
300 | if (empty($relationships)) { |
||
301 | // Cannot see one of the families/individuals, due to privacy; |
||
302 | continue; |
||
303 | } |
||
304 | echo '<h3>', I18N::translate('Relationship: %s', Functions::getRelationshipNameFromPath(implode('', $relationships), $individual1, $individual2)), '</h3>'; |
||
305 | $num_paths++; |
||
306 | |||
307 | // Use a table/grid for layout. |
||
308 | $table = []; |
||
309 | // Current position in the grid. |
||
310 | $x = 0; |
||
311 | $y = 0; |
||
312 | // Extent of the grid. |
||
313 | $min_y = 0; |
||
314 | $max_y = 0; |
||
315 | $max_x = 0; |
||
316 | // For each node in the path. |
||
317 | foreach ($path as $n => $xref) { |
||
318 | if ($n % 2 === 1) { |
||
319 | switch ($relationships[$n]) { |
||
320 | case 'hus': |
||
321 | case 'wif': |
||
322 | case 'spo': |
||
323 | case 'bro': |
||
324 | case 'sis': |
||
325 | case 'sib': |
||
326 | $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>'; |
||
327 | $x += 2; |
||
328 | break; |
||
329 | case 'son': |
||
330 | case 'dau': |
||
331 | case 'chi': |
||
332 | if ($n > 2 && preg_match('/fat|mot|par/', $relationships[$n - 2])) { |
||
333 | $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>'; |
||
334 | $x += 2; |
||
335 | } else { |
||
336 | $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>'; |
||
337 | } |
||
338 | $y -= 2; |
||
339 | break; |
||
340 | case 'fat': |
||
341 | case 'mot': |
||
342 | case 'par': |
||
343 | if ($n > 2 && preg_match('/son|dau|chi/', $relationships[$n - 2])) { |
||
344 | $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>'; |
||
345 | $x += 2; |
||
346 | } else { |
||
347 | $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>'; |
||
348 | } |
||
349 | $y += 2; |
||
350 | break; |
||
351 | } |
||
352 | $max_x = max($max_x, $x); |
||
353 | $min_y = min($min_y, $y); |
||
354 | $max_y = max($max_y, $y); |
||
355 | } else { |
||
356 | $individual = Individual::getInstance($xref, $tree); |
||
357 | $table[$x][$y] = view('chart-box', ['individual' => $individual]); |
||
358 | } |
||
359 | } |
||
360 | echo '<div class="wt-chart wt-chart-relationships">'; |
||
361 | echo '<table style="border-collapse: collapse; margin: 20px 50px;">'; |
||
362 | for ($y = $max_y; $y >= $min_y; --$y) { |
||
363 | echo '<tr>'; |
||
364 | for ($x = 0; $x <= $max_x; ++$x) { |
||
365 | echo '<td style="padding: 0;">'; |
||
366 | if (isset($table[$x][$y])) { |
||
367 | echo $table[$x][$y]; |
||
368 | } |
||
369 | echo '</td>'; |
||
370 | } |
||
371 | echo '</tr>'; |
||
372 | } |
||
373 | echo '</table>'; |
||
374 | echo '</div>'; |
||
375 | } |
||
376 | |||
377 | if (!$num_paths) { |
||
378 | echo '<p>', I18N::translate('No link between the two individuals could be found.'), '</p>'; |
||
379 | } |
||
380 | |||
381 | $html = ob_get_clean(); |
||
382 | |||
383 | return response($html); |
||
384 | } |
||
385 | |||
386 | /** |
||
387 | * @param ServerRequestInterface $request |
||
388 | * |
||
389 | * @return ResponseInterface |
||
390 | */ |
||
391 | public function getAdminAction(ServerRequestInterface $request): ResponseInterface |
||
|
|||
392 | { |
||
393 | $this->layout = 'layouts/administration'; |
||
394 | |||
395 | return $this->viewResponse('modules/relationships-chart/config', [ |
||
396 | 'all_trees' => $this->tree_service->all(), |
||
397 | 'ancestors_options' => $this->ancestorsOptions(), |
||
398 | 'default_ancestors' => self::DEFAULT_ANCESTORS, |
||
399 | 'default_recursion' => self::DEFAULT_RECURSION, |
||
400 | 'recursion_options' => $this->recursionConfigOptions(), |
||
401 | 'title' => I18N::translate('Chart preferences') . ' — ' . $this->title(), |
||
402 | ]); |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * @param ServerRequestInterface $request |
||
407 | * |
||
408 | * @return ResponseInterface |
||
409 | */ |
||
410 | public function postAdminAction(ServerRequestInterface $request): ResponseInterface |
||
411 | { |
||
412 | foreach ($this->tree_service->all() as $tree) { |
||
413 | $recursion = $request->getParsedBody()['relationship-recursion-' . $tree->id()] ?? ''; |
||
414 | $ancestors = $request->getParsedBody()['relationship-ancestors-' . $tree->id()] ?? ''; |
||
415 | |||
416 | $tree->setPreference('RELATIONSHIP_RECURSION', $recursion); |
||
417 | $tree->setPreference('RELATIONSHIP_ANCESTORS', $ancestors); |
||
418 | } |
||
419 | |||
420 | FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->title()), 'success'); |
||
421 | |||
422 | return redirect($this->getConfigLink()); |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * Possible options for the ancestors option |
||
427 | * |
||
428 | * @return string[] |
||
429 | */ |
||
430 | private function ancestorsOptions(): array |
||
431 | { |
||
432 | return [ |
||
433 | 0 => I18N::translate('Find any relationship'), |
||
434 | 1 => I18N::translate('Find relationships via ancestors'), |
||
435 | ]; |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * Possible options for the recursion option |
||
440 | * |
||
441 | * @return string[] |
||
442 | */ |
||
443 | private function recursionConfigOptions(): array |
||
451 | ]; |
||
452 | } |
||
453 | |||
454 | /** |
||
455 | * Calculate the shortest paths - or all paths - between two individuals. |
||
456 | * |
||
457 | * @param Individual $individual1 |
||
458 | * @param Individual $individual2 |
||
459 | * @param int $recursion How many levels of recursion to use |
||
460 | * @param bool $ancestor Restrict to relationships via a common ancestor |
||
461 | * |
||
462 | * @return string[][] |
||
463 | */ |
||
464 | private function calculateRelationships(Individual $individual1, Individual $individual2, $recursion, $ancestor = false): array |
||
545 | } |
||
546 | |||
547 | /** |
||
548 | * Convert numeric values to strings |
||
549 | * |
||
550 | * @return Closure |
||
551 | */ |
||
552 | private function stringMapper(): Closure |
||
553 | { |
||
554 | return static function ($xref) { |
||
555 | return (string) $xref; |
||
556 | }; |
||
557 | } |
||
558 | |||
559 | /** |
||
560 | * Find all ancestors of a list of individuals |
||
561 | * |
||
562 | * @param string $xref1 |
||
563 | * @param string $xref2 |
||
564 | * @param int $tree_id |
||
565 | * |
||
566 | * @return string[] |
||
567 | */ |
||
568 | private function allAncestors($xref1, $xref2, $tree_id): array |
||
569 | { |
||
570 | $ancestors = [ |
||
571 | $xref1, |
||
572 | $xref2, |
||
573 | ]; |
||
574 | |||
575 | $queue = [ |
||
576 | $xref1, |
||
577 | $xref2, |
||
578 | ]; |
||
579 | while (!empty($queue)) { |
||
580 | $parents = DB::table('link AS l1') |
||
581 | ->join('link AS l2', static function (JoinClause $join): void { |
||
582 | $join |
||
583 | ->on('l1.l_to', '=', 'l2.l_to') |
||
584 | ->on('l1.l_file', '=', 'l2.l_file'); |
||
585 | }) |
||
586 | ->where('l1.l_file', '=', $tree_id) |
||
587 | ->where('l1.l_type', '=', 'FAMC') |
||
588 | ->where('l2.l_type', '=', 'FAMS') |
||
589 | ->whereIn('l1.l_from', $queue) |
||
590 | ->pluck('l2.l_from'); |
||
591 | |||
592 | $queue = []; |
||
593 | foreach ($parents as $parent) { |
||
594 | if (!in_array($parent, $ancestors, true)) { |
||
595 | $ancestors[] = $parent; |
||
596 | $queue[] = $parent; |
||
597 | } |
||
598 | } |
||
599 | } |
||
600 | |||
601 | return $ancestors; |
||
602 | } |
||
603 | |||
604 | /** |
||
605 | * Find all families of two individuals |
||
606 | * |
||
607 | * @param string $xref1 |
||
608 | * @param string $xref2 |
||
609 | * @param int $tree_id |
||
610 | * |
||
611 | * @return string[] |
||
612 | */ |
||
613 | private function excludeFamilies($xref1, $xref2, $tree_id): array |
||
614 | { |
||
615 | return DB::table('link AS l1') |
||
616 | ->join('link AS l2', static function (JoinClause $join): void { |
||
617 | $join |
||
618 | ->on('l1.l_to', '=', 'l2.l_to') |
||
619 | ->on('l1.l_type', '=', 'l2.l_type') |
||
620 | ->on('l1.l_file', '=', 'l2.l_file'); |
||
621 | }) |
||
622 | ->where('l1.l_file', '=', $tree_id) |
||
623 | ->where('l1.l_type', '=', 'FAMS') |
||
624 | ->where('l1.l_from', '=', $xref1) |
||
625 | ->where('l2.l_from', '=', $xref2) |
||
626 | ->pluck('l1.l_to') |
||
627 | ->all(); |
||
628 | } |
||
629 | |||
630 | /** |
||
631 | * Convert a path (list of XREFs) to an "old-style" string of relationships. |
||
632 | * Return an empty array, if privacy rules prevent us viewing any node. |
||
633 | * |
||
634 | * @param Tree $tree |
||
635 | * @param string[] $path Alternately Individual / Family |
||
636 | * |
||
637 | * @return string[] |
||
638 | */ |
||
639 | private function oldStyleRelationshipPath(Tree $tree, array $path): array |
||
689 | } |
||
690 | |||
691 | /** |
||
692 | * Possible options for the recursion option |
||
693 | * |
||
694 | * @param int $max_recursion |
||
695 | * |
||
696 | * @return string[] |
||
697 | */ |
||
698 | private function recursionOptions(int $max_recursion): array |
||
709 | ]; |
||
710 | } |
||
711 | } |
||
712 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.