xref: /webtrees/app/Module/LifespansChartModule.php (revision e5a6b4d4f6f6e7ff2fba7ae2cf27546ae68a79cc)
1168ff6f3Sric2016<?php
2168ff6f3Sric2016/**
3168ff6f3Sric2016 * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
5168ff6f3Sric2016 * This program is free software: you can redistribute it and/or modify
6168ff6f3Sric2016 * it under the terms of the GNU General Public License as published by
7168ff6f3Sric2016 * the Free Software Foundation, either version 3 of the License, or
8168ff6f3Sric2016 * (at your option) any later version.
9168ff6f3Sric2016 * This program is distributed in the hope that it will be useful,
10168ff6f3Sric2016 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11168ff6f3Sric2016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12168ff6f3Sric2016 * GNU General Public License for more details.
13168ff6f3Sric2016 * You should have received a copy of the GNU General Public License
14168ff6f3Sric2016 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15168ff6f3Sric2016 */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
18168ff6f3Sric2016namespace Fisharebest\Webtrees\Module;
19168ff6f3Sric2016
20e2b8114dSGreg Roachuse Fisharebest\ExtCalendar\GregorianCalendar;
219867b2f0SGreg Roachuse Fisharebest\Webtrees\Auth;
22e2b8114dSGreg Roachuse Fisharebest\Webtrees\ColorGenerator;
23*e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
24e2b8114dSGreg Roachuse Fisharebest\Webtrees\Date;
25168ff6f3Sric2016use Fisharebest\Webtrees\I18N;
26168ff6f3Sric2016use Fisharebest\Webtrees\Individual;
27e2b8114dSGreg Roachuse Fisharebest\Webtrees\Place;
28e2b8114dSGreg Roachuse Fisharebest\Webtrees\Tree;
29e2b8114dSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
30e2b8114dSGreg Roachuse Illuminate\Database\Query\JoinClause;
31e2b8114dSGreg Roachuse stdClass;
32e2b8114dSGreg Roachuse Symfony\Component\HttpFoundation\Request;
33e2b8114dSGreg Roachuse Symfony\Component\HttpFoundation\Response;
34168ff6f3Sric2016
35168ff6f3Sric2016/**
36168ff6f3Sric2016 * Class LifespansChartModule
37168ff6f3Sric2016 */
3837eb8894SGreg Roachclass LifespansChartModule extends AbstractModule implements ModuleChartInterface
39c1010edaSGreg Roach{
4049a243cbSGreg Roach    use ModuleChartTrait;
4149a243cbSGreg Roach
42e2b8114dSGreg Roach    // Parameters for generating colors
43e2b8114dSGreg Roach    protected const RANGE      = 120; // degrees
44e2b8114dSGreg Roach    protected const SATURATION = 100; // percent
45e2b8114dSGreg Roach    protected const LIGHTNESS  = 30; // percent
46e2b8114dSGreg Roach    protected const ALPHA      = 0.25;
47e2b8114dSGreg Roach
48168ff6f3Sric2016    /**
49168ff6f3Sric2016     * How should this module be labelled on tabs, menus, etc.?
50168ff6f3Sric2016     *
51168ff6f3Sric2016     * @return string
52168ff6f3Sric2016     */
5349a243cbSGreg Roach    public function title(): string
54c1010edaSGreg Roach    {
55bbb76c12SGreg Roach        /* I18N: Name of a module/chart */
56bbb76c12SGreg Roach        return I18N::translate('Lifespans');
57168ff6f3Sric2016    }
58168ff6f3Sric2016
59168ff6f3Sric2016    /**
60168ff6f3Sric2016     * A sentence describing what this module does.
61168ff6f3Sric2016     *
62168ff6f3Sric2016     * @return string
63168ff6f3Sric2016     */
6449a243cbSGreg Roach    public function description(): string
65c1010edaSGreg Roach    {
66bbb76c12SGreg Roach        /* I18N: Description of the “LifespansChart” module */
67bbb76c12SGreg Roach        return I18N::translate('A chart of individuals’ lifespans.');
68168ff6f3Sric2016    }
69168ff6f3Sric2016
70168ff6f3Sric2016    /**
71377a2979SGreg Roach     * CSS class for the URL.
72377a2979SGreg Roach     *
73377a2979SGreg Roach     * @return string
74377a2979SGreg Roach     */
75377a2979SGreg Roach    public function chartMenuClass(): string
76377a2979SGreg Roach    {
77377a2979SGreg Roach        return 'menu-chart-lifespan';
78377a2979SGreg Roach    }
79377a2979SGreg Roach
80377a2979SGreg Roach    /**
81e6562982SGreg Roach     * The URL for this chart.
82168ff6f3Sric2016     *
8360bc3e3fSGreg Roach     * @param Individual $individual
84e6562982SGreg Roach     * @param string[]   $parameters
8560bc3e3fSGreg Roach     *
86e6562982SGreg Roach     * @return string
87168ff6f3Sric2016     */
88e6562982SGreg Roach    public function chartUrl(Individual $individual, array $parameters = []): string
89c1010edaSGreg Roach    {
90e2b8114dSGreg Roach        return route('module', [
91e2b8114dSGreg Roach                'module'  => $this->name(),
92e2b8114dSGreg Roach                'action'  => 'Chart',
93c0935879SGreg Roach                'xrefs[]' => $individual->xref(),
94f4afa648SGreg Roach                'ged'     => $individual->tree()->name(),
95e6562982SGreg Roach            ] + $parameters);
96168ff6f3Sric2016    }
97e2b8114dSGreg Roach
98e2b8114dSGreg Roach    /**
99e2b8114dSGreg Roach     * A form to request the chart parameters.
100e2b8114dSGreg Roach     *
101e2b8114dSGreg Roach     * @param Request       $request
102e2b8114dSGreg Roach     * @param Tree          $tree
103*e5a6b4d4SGreg Roach     * @param UserInterface $user
104e2b8114dSGreg Roach     *
105e2b8114dSGreg Roach     * @return Response
106e2b8114dSGreg Roach     */
107*e5a6b4d4SGreg Roach    public function getChartAction(Request $request, Tree $tree, UserInterface $user): Response
108e2b8114dSGreg Roach    {
1099867b2f0SGreg Roach        Auth::checkComponentAccess($this, 'chart', $tree, $user);
1109867b2f0SGreg Roach
1119b5537c3SGreg Roach        $ajax      = (bool) $request->get('ajax');
112e2b8114dSGreg Roach        $xrefs     = (array) $request->get('xrefs', []);
113e2b8114dSGreg Roach        $addxref   = $request->get('addxref', '');
114e2b8114dSGreg Roach        $addfam    = (bool) $request->get('addfam', false);
115e2b8114dSGreg Roach        $placename = $request->get('placename', '');
116e2b8114dSGreg Roach        $start     = $request->get('start', '');
117e2b8114dSGreg Roach        $end       = $request->get('end', '');
118e2b8114dSGreg Roach
119e2b8114dSGreg Roach        $place      = new Place($placename, $tree);
120e2b8114dSGreg Roach        $start_date = new Date($start);
121e2b8114dSGreg Roach        $end_date   = new Date($end);
122e2b8114dSGreg Roach
123e2b8114dSGreg Roach        $xrefs = array_unique($xrefs);
124e2b8114dSGreg Roach
125e2b8114dSGreg Roach        // Add an individual, and family members
126e2b8114dSGreg Roach        $individual = Individual::getInstance($addxref, $tree);
127e2b8114dSGreg Roach        if ($individual !== null) {
128e2b8114dSGreg Roach            $xrefs[] = $addxref;
129e2b8114dSGreg Roach            if ($addfam) {
130e2b8114dSGreg Roach                $xrefs = array_merge($xrefs, $this->closeFamily($individual));
131e2b8114dSGreg Roach            }
132e2b8114dSGreg Roach        }
133e2b8114dSGreg Roach
134e2b8114dSGreg Roach        // Select by date and/or place.
135e2b8114dSGreg Roach        if ($start_date->isOK() && $end_date->isOK() && $placename !== '') {
136e2b8114dSGreg Roach            $date_xrefs  = $this->findIndividualsByDate($start_date, $end_date, $tree);
137e2b8114dSGreg Roach            $place_xrefs = $this->findIndividualsByPlace($place, $tree);
138e2b8114dSGreg Roach            $xrefs       = array_intersect($date_xrefs, $place_xrefs);
139e2b8114dSGreg Roach        } elseif ($start_date->isOK() && $end_date->isOK()) {
140e2b8114dSGreg Roach            $xrefs = $this->findIndividualsByDate($start_date, $end_date, $tree);
141e2b8114dSGreg Roach        } elseif ($placename !== '') {
142e2b8114dSGreg Roach            $xrefs = $this->findIndividualsByPlace($place, $tree);
143e2b8114dSGreg Roach        }
144e2b8114dSGreg Roach
145e2b8114dSGreg Roach        // Filter duplicates and private individuals.
146e2b8114dSGreg Roach        $xrefs = array_unique($xrefs);
147e2b8114dSGreg Roach        $xrefs = array_filter($xrefs, function (string $xref) use ($tree): bool {
148e2b8114dSGreg Roach            $individual = Individual::getInstance($xref, $tree);
149e2b8114dSGreg Roach
150e2b8114dSGreg Roach            return $individual !== null && $individual->canShow();
151e2b8114dSGreg Roach        });
152e2b8114dSGreg Roach
1539b5537c3SGreg Roach        if ($ajax) {
154e2b8114dSGreg Roach            $subtitle = $this->subtitle(count($xrefs), $start_date, $end_date, $placename);
155e2b8114dSGreg Roach
156e2b8114dSGreg Roach            return $this->chart($tree, $xrefs, $subtitle);
157e2b8114dSGreg Roach        }
158e2b8114dSGreg Roach
159e2b8114dSGreg Roach        $ajax_url = route('module', [
1609b5537c3SGreg Roach            'ajax'   => true,
161e2b8114dSGreg Roach            'module' => $this->name(),
162e2b8114dSGreg Roach            'action' => 'Chart',
163e2b8114dSGreg Roach            'ged'    => $tree->name(),
164e2b8114dSGreg Roach            'xrefs'  => $xrefs,
165e2b8114dSGreg Roach        ]);
166e2b8114dSGreg Roach
167e2b8114dSGreg Roach        $reset_url = route('module', [
168e2b8114dSGreg Roach            'module' => $this->name(),
169e2b8114dSGreg Roach            'action' => 'Chart',
170e2b8114dSGreg Roach            'ged'    => $tree->name(),
171e2b8114dSGreg Roach        ]);
172e2b8114dSGreg Roach
1739b5537c3SGreg Roach        return $this->viewResponse('modules/lifespans-chart/page', [
174e2b8114dSGreg Roach            'ajax_url'    => $ajax_url,
175e2b8114dSGreg Roach            'module_name' => $this->name(),
176e2b8114dSGreg Roach            'reset_url'   => $reset_url,
177e2b8114dSGreg Roach            'title'       => $this->title(),
178e2b8114dSGreg Roach            'xrefs'       => $xrefs,
179e2b8114dSGreg Roach        ]);
180e2b8114dSGreg Roach    }
181e2b8114dSGreg Roach
182e2b8114dSGreg Roach    /**
183e2b8114dSGreg Roach     * @param Tree   $tree
184e2b8114dSGreg Roach     * @param array  $xrefs
185e2b8114dSGreg Roach     * @param string $subtitle
186e2b8114dSGreg Roach     *
187e2b8114dSGreg Roach     * @return Response
188e2b8114dSGreg Roach     */
189e2b8114dSGreg Roach    protected function chart(Tree $tree, array $xrefs, string $subtitle): Response
190e2b8114dSGreg Roach    {
191e2b8114dSGreg Roach        /** @var Individual[] $individuals */
192e2b8114dSGreg Roach        $individuals = array_map(function (string $xref) use ($tree) {
193e2b8114dSGreg Roach            return Individual::getInstance($xref, $tree);
194e2b8114dSGreg Roach        }, $xrefs);
195e2b8114dSGreg Roach
196e2b8114dSGreg Roach        $individuals = array_filter($individuals, function (Individual $individual = null): bool {
197e2b8114dSGreg Roach            return $individual !== null && $individual->canShow();
198e2b8114dSGreg Roach        });
199e2b8114dSGreg Roach
200e2b8114dSGreg Roach        // Sort the array in order of birth year
201e2b8114dSGreg Roach        usort($individuals, function (Individual $a, Individual $b) {
202e2b8114dSGreg Roach            return Date::compare($a->getEstimatedBirthDate(), $b->getEstimatedBirthDate());
203e2b8114dSGreg Roach        });
204e2b8114dSGreg Roach
205e2b8114dSGreg Roach        // Round to whole decades
206e2b8114dSGreg Roach        $start_year = (int) floor($this->minYear($individuals) / 10) * 10;
207e2b8114dSGreg Roach        $end_year   = (int) ceil($this->maxYear($individuals) / 10) * 10;
208e2b8114dSGreg Roach
209e2b8114dSGreg Roach        $lifespans = $this->layoutIndividuals($individuals);
210e2b8114dSGreg Roach
211e2b8114dSGreg Roach        $max_rows = array_reduce($lifespans, function ($carry, stdClass $item) {
212e2b8114dSGreg Roach            return max($carry, $item->row);
213e2b8114dSGreg Roach        }, 0);
214e2b8114dSGreg Roach
215e2b8114dSGreg Roach        $html = view('modules/lifespans-chart/chart', [
216e2b8114dSGreg Roach            'dir'        => I18N::direction(),
217e2b8114dSGreg Roach            'end_year'   => $end_year,
218e2b8114dSGreg Roach            'lifespans'  => $lifespans,
219e2b8114dSGreg Roach            'max_rows'   => $max_rows,
220e2b8114dSGreg Roach            'start_year' => $start_year,
221e2b8114dSGreg Roach            'subtitle'   => $subtitle,
222e2b8114dSGreg Roach        ]);
223e2b8114dSGreg Roach
224e2b8114dSGreg Roach        return new Response($html);
225e2b8114dSGreg Roach    }
226e2b8114dSGreg Roach
227e2b8114dSGreg Roach    /**
228e2b8114dSGreg Roach     * @param Individual[] $individuals
229e2b8114dSGreg Roach     *
230e2b8114dSGreg Roach     * @return stdClass[]
231e2b8114dSGreg Roach     */
232e2b8114dSGreg Roach    private function layoutIndividuals(array $individuals): array
233e2b8114dSGreg Roach    {
234e2b8114dSGreg Roach        $colors = [
235e2b8114dSGreg Roach            'M' => new ColorGenerator(240, self::SATURATION, self::LIGHTNESS, self::ALPHA, self::RANGE * -1),
236e2b8114dSGreg Roach            'F' => new ColorGenerator(000, self::SATURATION, self::LIGHTNESS, self::ALPHA, self::RANGE),
237e2b8114dSGreg Roach            'U' => new ColorGenerator(120, self::SATURATION, self::LIGHTNESS, self::ALPHA, self::RANGE),
238e2b8114dSGreg Roach        ];
239e2b8114dSGreg Roach
240e2b8114dSGreg Roach        $current_year = (int) date('Y');
241e2b8114dSGreg Roach
242e2b8114dSGreg Roach        // Latest year used in each row
243e2b8114dSGreg Roach        $rows = [];
244e2b8114dSGreg Roach
245e2b8114dSGreg Roach        $lifespans = [];
246e2b8114dSGreg Roach
247e2b8114dSGreg Roach        foreach ($individuals as $individual) {
248e2b8114dSGreg Roach            $birth_jd   = $individual->getEstimatedBirthDate()->minimumJulianDay();
249e2b8114dSGreg Roach            $birth_year = $this->jdToYear($birth_jd);
250e2b8114dSGreg Roach            $death_jd   = $individual->getEstimatedDeathDate()->maximumJulianDay();
251e2b8114dSGreg Roach            $death_year = $this->jdToYear($death_jd);
252e2b8114dSGreg Roach
253e2b8114dSGreg Roach            // Don't show death dates in the future.
254e2b8114dSGreg Roach            $death_year = min($death_year, $current_year);
255e2b8114dSGreg Roach
256e2b8114dSGreg Roach            // Add this individual to the next row in the chart...
257e2b8114dSGreg Roach            $next_row = count($rows);
258e2b8114dSGreg Roach            // ...unless we can find an existing row where it fits.
259e2b8114dSGreg Roach            foreach ($rows as $row => $year) {
260e2b8114dSGreg Roach                if ($year < $birth_year) {
261e2b8114dSGreg Roach                    $next_row = $row;
262e2b8114dSGreg Roach                    break;
263e2b8114dSGreg Roach                }
264e2b8114dSGreg Roach            }
265e2b8114dSGreg Roach
266e2b8114dSGreg Roach            // Fill the row up to the year (leaving a small gap)
267e2b8114dSGreg Roach            $rows[$next_row] = $death_year;
268e2b8114dSGreg Roach
269e2b8114dSGreg Roach            $lifespans[] = (object) [
270e2b8114dSGreg Roach                'background' => $colors[$individual->getSex()]->getNextColor(),
271e2b8114dSGreg Roach                'birth_year' => $birth_year,
272e2b8114dSGreg Roach                'death_year' => $death_year,
273e2b8114dSGreg Roach                'id'         => 'individual-' . md5($individual->xref()),
274e2b8114dSGreg Roach                'individual' => $individual,
275e2b8114dSGreg Roach                'row'        => $next_row,
276e2b8114dSGreg Roach            ];
277e2b8114dSGreg Roach        }
278e2b8114dSGreg Roach
279e2b8114dSGreg Roach        return $lifespans;
280e2b8114dSGreg Roach    }
281e2b8114dSGreg Roach
282e2b8114dSGreg Roach    /**
283e2b8114dSGreg Roach     * Find the latest event year for individuals
284e2b8114dSGreg Roach     *
285e2b8114dSGreg Roach     * @param array $individuals
286e2b8114dSGreg Roach     *
287e2b8114dSGreg Roach     * @return int
288e2b8114dSGreg Roach     */
289e2b8114dSGreg Roach    protected function maxYear(array $individuals): int
290e2b8114dSGreg Roach    {
291e2b8114dSGreg Roach        $jd = array_reduce($individuals, function ($carry, Individual $item) {
292e2b8114dSGreg Roach            return max($carry, $item->getEstimatedDeathDate()->maximumJulianDay());
293e2b8114dSGreg Roach        }, 0);
294e2b8114dSGreg Roach
295e2b8114dSGreg Roach        $year = $this->jdToYear($jd);
296e2b8114dSGreg Roach
297e2b8114dSGreg Roach        // Don't show future dates
298e2b8114dSGreg Roach        return min($year, (int) date('Y'));
299e2b8114dSGreg Roach    }
300e2b8114dSGreg Roach
301e2b8114dSGreg Roach    /**
302e2b8114dSGreg Roach     * Find the earliest event year for individuals
303e2b8114dSGreg Roach     *
304e2b8114dSGreg Roach     * @param array $individuals
305e2b8114dSGreg Roach     *
306e2b8114dSGreg Roach     * @return int
307e2b8114dSGreg Roach     */
308e2b8114dSGreg Roach    protected function minYear(array $individuals): int
309e2b8114dSGreg Roach    {
310e2b8114dSGreg Roach        $jd = array_reduce($individuals, function ($carry, Individual $item) {
311e2b8114dSGreg Roach            return min($carry, $item->getEstimatedBirthDate()->minimumJulianDay());
312e2b8114dSGreg Roach        }, PHP_INT_MAX);
313e2b8114dSGreg Roach
314e2b8114dSGreg Roach        return $this->jdToYear($jd);
315e2b8114dSGreg Roach    }
316e2b8114dSGreg Roach
317e2b8114dSGreg Roach    /**
318e2b8114dSGreg Roach     * Convert a julian day to a gregorian year
319e2b8114dSGreg Roach     *
320e2b8114dSGreg Roach     * @param int $jd
321e2b8114dSGreg Roach     *
322e2b8114dSGreg Roach     * @return int
323e2b8114dSGreg Roach     */
324e2b8114dSGreg Roach    protected function jdToYear(int $jd): int
325e2b8114dSGreg Roach    {
326e2b8114dSGreg Roach        if ($jd === 0) {
327e2b8114dSGreg Roach            return 0;
328e2b8114dSGreg Roach        }
329e2b8114dSGreg Roach
330e2b8114dSGreg Roach        $gregorian = new GregorianCalendar();
331e2b8114dSGreg Roach        [$y] = $gregorian->jdToYmd($jd);
332e2b8114dSGreg Roach
333e2b8114dSGreg Roach        return $y;
334e2b8114dSGreg Roach    }
335e2b8114dSGreg Roach
336e2b8114dSGreg Roach    /**
337e2b8114dSGreg Roach     * @param Date $start
338e2b8114dSGreg Roach     * @param Date $end
339e2b8114dSGreg Roach     * @param Tree $tree
340e2b8114dSGreg Roach     *
341e2b8114dSGreg Roach     * @return string[]
342e2b8114dSGreg Roach     */
343e2b8114dSGreg Roach    protected function findIndividualsByDate(Date $start, Date $end, Tree $tree): array
344e2b8114dSGreg Roach    {
345e2b8114dSGreg Roach        return DB::table('individuals')
346e2b8114dSGreg Roach            ->join('dates', function (JoinClause $join): void {
347e2b8114dSGreg Roach                $join
348e2b8114dSGreg Roach                    ->on('d_file', '=', 'i_file')
349e2b8114dSGreg Roach                    ->on('d_gid', '=', 'i_id');
350e2b8114dSGreg Roach            })
351e2b8114dSGreg Roach            ->where('i_file', '=', $tree->id())
352e2b8114dSGreg Roach            ->where('d_julianday1', '<=', $end->maximumJulianDay())
353e2b8114dSGreg Roach            ->where('d_julianday2', '>=', $start->minimumJulianDay())
354e2b8114dSGreg Roach            ->whereNotIn('d_fact', ['BAPL', 'ENDL', 'SLGC', 'SLGS', '_TODO', 'CHAN'])
355e2b8114dSGreg Roach            ->pluck('i_id')
356e2b8114dSGreg Roach            ->all();
357e2b8114dSGreg Roach    }
358e2b8114dSGreg Roach
359e2b8114dSGreg Roach    /**
360e2b8114dSGreg Roach     * @param Place $place
361e2b8114dSGreg Roach     * @param Tree  $tree
362e2b8114dSGreg Roach     *
363e2b8114dSGreg Roach     * @return string[]
364e2b8114dSGreg Roach     */
365e2b8114dSGreg Roach    protected function findIndividualsByPlace(Place $place, Tree $tree): array
366e2b8114dSGreg Roach    {
367e2b8114dSGreg Roach        return DB::table('individuals')
368e2b8114dSGreg Roach            ->join('placelinks', function (JoinClause $join): void {
369e2b8114dSGreg Roach                $join
370e2b8114dSGreg Roach                    ->on('pl_file', '=', 'i_file')
371e2b8114dSGreg Roach                    ->on('pl_gid', '=', 'i_id');
372e2b8114dSGreg Roach            })
373e2b8114dSGreg Roach            ->where('i_file', '=', $tree->id())
374392561bbSGreg Roach            ->where('pl_p_id', '=', $place->id())
375e2b8114dSGreg Roach            ->pluck('i_id')
376e2b8114dSGreg Roach            ->all();
377e2b8114dSGreg Roach    }
378e2b8114dSGreg Roach
379e2b8114dSGreg Roach    /**
380e2b8114dSGreg Roach     * Find the close family members of an individual.
381e2b8114dSGreg Roach     *
382e2b8114dSGreg Roach     * @param Individual $individual
383e2b8114dSGreg Roach     *
384e2b8114dSGreg Roach     * @return string[]
385e2b8114dSGreg Roach     */
386e2b8114dSGreg Roach    protected function closeFamily(Individual $individual): array
387e2b8114dSGreg Roach    {
388e2b8114dSGreg Roach        $xrefs = [];
389e2b8114dSGreg Roach
390e2b8114dSGreg Roach        foreach ($individual->getSpouseFamilies() as $family) {
391e2b8114dSGreg Roach            foreach ($family->getChildren() as $child) {
392e2b8114dSGreg Roach                $xrefs[] = $child->xref();
393e2b8114dSGreg Roach            }
394e2b8114dSGreg Roach
395e2b8114dSGreg Roach            foreach ($family->getSpouses() as $spouse) {
396e2b8114dSGreg Roach                $xrefs[] = $spouse->xref();
397e2b8114dSGreg Roach            }
398e2b8114dSGreg Roach        }
399e2b8114dSGreg Roach
400e2b8114dSGreg Roach        foreach ($individual->getChildFamilies() as $family) {
401e2b8114dSGreg Roach            foreach ($family->getChildren() as $child) {
402e2b8114dSGreg Roach                $xrefs[] = $child->xref();
403e2b8114dSGreg Roach            }
404e2b8114dSGreg Roach
405e2b8114dSGreg Roach            foreach ($family->getSpouses() as $spouse) {
406e2b8114dSGreg Roach                $xrefs[] = $spouse->xref();
407e2b8114dSGreg Roach            }
408e2b8114dSGreg Roach        }
409e2b8114dSGreg Roach
410e2b8114dSGreg Roach        return $xrefs;
411e2b8114dSGreg Roach    }
412e2b8114dSGreg Roach
413e2b8114dSGreg Roach    /**
414e2b8114dSGreg Roach     * Generate a subtitle, based on filter parameters
415e2b8114dSGreg Roach     *
416e2b8114dSGreg Roach     * @param int    $count
417e2b8114dSGreg Roach     * @param Date   $start
418e2b8114dSGreg Roach     * @param Date   $end
419e2b8114dSGreg Roach     * @param string $placename
420e2b8114dSGreg Roach     *
421e2b8114dSGreg Roach     * @return string
422e2b8114dSGreg Roach     */
423e2b8114dSGreg Roach    protected function subtitle(int $count, Date $start, Date $end, string $placename): string
424e2b8114dSGreg Roach    {
425e2b8114dSGreg Roach        if ($start->isOK() && $end->isOK() && $placename !== '') {
426e2b8114dSGreg Roach            return I18N::plural(
427e2b8114dSGreg Roach                '%s individual with events in %s between %s and %s',
428e2b8114dSGreg Roach                '%s individuals with events in %s between %s and %s',
429e2b8114dSGreg Roach                $count,
430e2b8114dSGreg Roach                I18N::number($count),
431e2b8114dSGreg Roach                $placename,
432e2b8114dSGreg Roach                $start->display(false, '%Y'),
433e2b8114dSGreg Roach                $end->display(false, '%Y')
434e2b8114dSGreg Roach            );
435e2b8114dSGreg Roach        }
436e2b8114dSGreg Roach
437e2b8114dSGreg Roach        if ($placename !== '') {
438e2b8114dSGreg Roach            return I18N::plural(
439e2b8114dSGreg Roach                '%s individual with events in %s',
440e2b8114dSGreg Roach                '%s individuals with events in %s',
441e2b8114dSGreg Roach                $count,
442e2b8114dSGreg Roach                I18N::number($count),
443e2b8114dSGreg Roach                $placename
444e2b8114dSGreg Roach            );
445e2b8114dSGreg Roach        }
446e2b8114dSGreg Roach
447e2b8114dSGreg Roach        if ($start->isOK() && $end->isOK()) {
448e2b8114dSGreg Roach            return I18N::plural(
449e2b8114dSGreg Roach                '%s individual with events between %s and %s',
450e2b8114dSGreg Roach                '%s individuals with events between %s and %s',
451e2b8114dSGreg Roach                $count,
452e2b8114dSGreg Roach                I18N::number($count),
453e2b8114dSGreg Roach                $start->display(false, '%Y'),
454e2b8114dSGreg Roach                $end->display(false, '%Y')
455e2b8114dSGreg Roach            );
456e2b8114dSGreg Roach        }
457e2b8114dSGreg Roach
458e2b8114dSGreg Roach        return I18N::plural('%s individual', '%s individuals', $count, I18N::number($count));
459e2b8114dSGreg Roach    }
460168ff6f3Sric2016}
461