xref: /webtrees/app/Module/LifespansChartModule.php (revision 0b93976a9c83f1ad374620df2dc12a210d5be076)
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;
23e5a6b4d4SGreg 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;
316ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
326ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
33e2b8114dSGreg Roachuse stdClass;
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    /**
490cfd6963SGreg Roach     * How should this module be identified in the control panel, 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     *
1016ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
102e2b8114dSGreg Roach     * @param Tree                   $tree
103e5a6b4d4SGreg Roach     * @param UserInterface          $user
104e2b8114dSGreg Roach     *
1056ccdf4f0SGreg Roach     * @return ResponseInterface
106e2b8114dSGreg Roach     */
1076ccdf4f0SGreg Roach    public function getChartAction(ServerRequestInterface $request, Tree $tree, UserInterface $user): ResponseInterface
108e2b8114dSGreg Roach    {
1099867b2f0SGreg Roach        Auth::checkComponentAccess($this, 'chart', $tree, $user);
1109867b2f0SGreg Roach
111*0b93976aSGreg Roach        $ajax      = $request->getQueryParams()['ajax'] ?? '';
112*0b93976aSGreg Roach        $xrefs     = (array) ($request->getQueryParams()['xrefs'] ?? []);
113*0b93976aSGreg Roach        $addxref   = $request->getQueryParams()['addxref'] ?? '';
114*0b93976aSGreg Roach        $addfam    = (bool) ($request->getQueryParams()['addfam'] ?? false);
115*0b93976aSGreg Roach        $placename = $request->getQueryParams()['placename'] ?? '';
116*0b93976aSGreg Roach        $start     = $request->getQueryParams()['start'] ?? '';
117*0b93976aSGreg Roach        $end       = $request->getQueryParams()['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);
1470b5fd0a6SGreg Roach        $xrefs = array_filter($xrefs, static 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
153*0b93976aSGreg Roach        if ($ajax === '1') {
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     *
1876ccdf4f0SGreg Roach     * @return ResponseInterface
188e2b8114dSGreg Roach     */
1896ccdf4f0SGreg Roach    protected function chart(Tree $tree, array $xrefs, string $subtitle): ResponseInterface
190e2b8114dSGreg Roach    {
191e2b8114dSGreg Roach        /** @var Individual[] $individuals */
192*0b93976aSGreg Roach        $individuals = array_map(static function (string $xref) use ($tree): ?Individual {
193e2b8114dSGreg Roach            return Individual::getInstance($xref, $tree);
194e2b8114dSGreg Roach        }, $xrefs);
195e2b8114dSGreg Roach
196*0b93976aSGreg Roach        $individuals = array_filter($individuals, static function (?Individual $individual): bool {
197*0b93976aSGreg Roach            return $individual instanceof Individual && $individual->canShow();
198e2b8114dSGreg Roach        });
199e2b8114dSGreg Roach
200e2b8114dSGreg Roach        // Sort the array in order of birth year
2010c1a5edcSGreg Roach        usort($individuals, Individual::birthDateComparator());
202e2b8114dSGreg Roach
203e2b8114dSGreg Roach        // Round to whole decades
204e2b8114dSGreg Roach        $start_year = (int) floor($this->minYear($individuals) / 10) * 10;
205e2b8114dSGreg Roach        $end_year   = (int) ceil($this->maxYear($individuals) / 10) * 10;
206e2b8114dSGreg Roach
207e2b8114dSGreg Roach        $lifespans = $this->layoutIndividuals($individuals);
208e2b8114dSGreg Roach
2090b5fd0a6SGreg Roach        $max_rows = array_reduce($lifespans, static function ($carry, stdClass $item) {
210e2b8114dSGreg Roach            return max($carry, $item->row);
211e2b8114dSGreg Roach        }, 0);
212e2b8114dSGreg Roach
213e2b8114dSGreg Roach        $html = view('modules/lifespans-chart/chart', [
214e2b8114dSGreg Roach            'dir'        => I18N::direction(),
215e2b8114dSGreg Roach            'end_year'   => $end_year,
216e2b8114dSGreg Roach            'lifespans'  => $lifespans,
217e2b8114dSGreg Roach            'max_rows'   => $max_rows,
218e2b8114dSGreg Roach            'start_year' => $start_year,
219e2b8114dSGreg Roach            'subtitle'   => $subtitle,
220e2b8114dSGreg Roach        ]);
221e2b8114dSGreg Roach
2226ccdf4f0SGreg Roach        return response($html);
223e2b8114dSGreg Roach    }
224e2b8114dSGreg Roach
225e2b8114dSGreg Roach    /**
226e2b8114dSGreg Roach     * @param Individual[] $individuals
227e2b8114dSGreg Roach     *
228e2b8114dSGreg Roach     * @return stdClass[]
229e2b8114dSGreg Roach     */
230e2b8114dSGreg Roach    private function layoutIndividuals(array $individuals): array
231e2b8114dSGreg Roach    {
232e2b8114dSGreg Roach        $colors = [
233e2b8114dSGreg Roach            'M' => new ColorGenerator(240, self::SATURATION, self::LIGHTNESS, self::ALPHA, self::RANGE * -1),
234e2b8114dSGreg Roach            'F' => new ColorGenerator(000, self::SATURATION, self::LIGHTNESS, self::ALPHA, self::RANGE),
235e2b8114dSGreg Roach            'U' => new ColorGenerator(120, self::SATURATION, self::LIGHTNESS, self::ALPHA, self::RANGE),
236e2b8114dSGreg Roach        ];
237e2b8114dSGreg Roach
238e2b8114dSGreg Roach        $current_year = (int) date('Y');
239e2b8114dSGreg Roach
240e2b8114dSGreg Roach        // Latest year used in each row
241e2b8114dSGreg Roach        $rows = [];
242e2b8114dSGreg Roach
243e2b8114dSGreg Roach        $lifespans = [];
244e2b8114dSGreg Roach
245e2b8114dSGreg Roach        foreach ($individuals as $individual) {
246e2b8114dSGreg Roach            $birth_jd   = $individual->getEstimatedBirthDate()->minimumJulianDay();
247e2b8114dSGreg Roach            $birth_year = $this->jdToYear($birth_jd);
248e2b8114dSGreg Roach            $death_jd   = $individual->getEstimatedDeathDate()->maximumJulianDay();
249e2b8114dSGreg Roach            $death_year = $this->jdToYear($death_jd);
250e2b8114dSGreg Roach
2510c1a5edcSGreg Roach            // Died before they were born?  Swapping the dates allows them to be shown.
2520c1a5edcSGreg Roach            if ($death_year < $birth_year) {
2530c1a5edcSGreg Roach                $death_year = $birth_year;
2540c1a5edcSGreg Roach            }
2550c1a5edcSGreg Roach
256e2b8114dSGreg Roach            // Don't show death dates in the future.
257e2b8114dSGreg Roach            $death_year = min($death_year, $current_year);
258e2b8114dSGreg Roach
259e2b8114dSGreg Roach            // Add this individual to the next row in the chart...
260e2b8114dSGreg Roach            $next_row = count($rows);
261e2b8114dSGreg Roach            // ...unless we can find an existing row where it fits.
262e2b8114dSGreg Roach            foreach ($rows as $row => $year) {
263e2b8114dSGreg Roach                if ($year < $birth_year) {
264e2b8114dSGreg Roach                    $next_row = $row;
265e2b8114dSGreg Roach                    break;
266e2b8114dSGreg Roach                }
267e2b8114dSGreg Roach            }
268e2b8114dSGreg Roach
269e2b8114dSGreg Roach            // Fill the row up to the year (leaving a small gap)
270e2b8114dSGreg Roach            $rows[$next_row] = $death_year;
271e2b8114dSGreg Roach
272e2b8114dSGreg Roach            $lifespans[] = (object) [
27339ca88baSGreg Roach                'background' => $colors[$individual->sex()]->getNextColor(),
274e2b8114dSGreg Roach                'birth_year' => $birth_year,
275e2b8114dSGreg Roach                'death_year' => $death_year,
276e2b8114dSGreg Roach                'id'         => 'individual-' . md5($individual->xref()),
277e2b8114dSGreg Roach                'individual' => $individual,
278e2b8114dSGreg Roach                'row'        => $next_row,
279e2b8114dSGreg Roach            ];
280e2b8114dSGreg Roach        }
281e2b8114dSGreg Roach
282e2b8114dSGreg Roach        return $lifespans;
283e2b8114dSGreg Roach    }
284e2b8114dSGreg Roach
285e2b8114dSGreg Roach    /**
286e2b8114dSGreg Roach     * Find the latest event year for individuals
287e2b8114dSGreg Roach     *
288e2b8114dSGreg Roach     * @param array $individuals
289e2b8114dSGreg Roach     *
290e2b8114dSGreg Roach     * @return int
291e2b8114dSGreg Roach     */
292e2b8114dSGreg Roach    protected function maxYear(array $individuals): int
293e2b8114dSGreg Roach    {
2940b5fd0a6SGreg Roach        $jd = array_reduce($individuals, static function ($carry, Individual $item) {
295e2b8114dSGreg Roach            return max($carry, $item->getEstimatedDeathDate()->maximumJulianDay());
296e2b8114dSGreg Roach        }, 0);
297e2b8114dSGreg Roach
298e2b8114dSGreg Roach        $year = $this->jdToYear($jd);
299e2b8114dSGreg Roach
300e2b8114dSGreg Roach        // Don't show future dates
301e2b8114dSGreg Roach        return min($year, (int) date('Y'));
302e2b8114dSGreg Roach    }
303e2b8114dSGreg Roach
304e2b8114dSGreg Roach    /**
305e2b8114dSGreg Roach     * Find the earliest event year for individuals
306e2b8114dSGreg Roach     *
307e2b8114dSGreg Roach     * @param array $individuals
308e2b8114dSGreg Roach     *
309e2b8114dSGreg Roach     * @return int
310e2b8114dSGreg Roach     */
311e2b8114dSGreg Roach    protected function minYear(array $individuals): int
312e2b8114dSGreg Roach    {
3130b5fd0a6SGreg Roach        $jd = array_reduce($individuals, static function ($carry, Individual $item) {
314e2b8114dSGreg Roach            return min($carry, $item->getEstimatedBirthDate()->minimumJulianDay());
315e2b8114dSGreg Roach        }, PHP_INT_MAX);
316e2b8114dSGreg Roach
317e2b8114dSGreg Roach        return $this->jdToYear($jd);
318e2b8114dSGreg Roach    }
319e2b8114dSGreg Roach
320e2b8114dSGreg Roach    /**
321e2b8114dSGreg Roach     * Convert a julian day to a gregorian year
322e2b8114dSGreg Roach     *
323e2b8114dSGreg Roach     * @param int $jd
324e2b8114dSGreg Roach     *
325e2b8114dSGreg Roach     * @return int
326e2b8114dSGreg Roach     */
327e2b8114dSGreg Roach    protected function jdToYear(int $jd): int
328e2b8114dSGreg Roach    {
329e2b8114dSGreg Roach        if ($jd === 0) {
330e2b8114dSGreg Roach            return 0;
331e2b8114dSGreg Roach        }
332e2b8114dSGreg Roach
333e2b8114dSGreg Roach        $gregorian = new GregorianCalendar();
334e2b8114dSGreg Roach        [$y] = $gregorian->jdToYmd($jd);
335e2b8114dSGreg Roach
336e2b8114dSGreg Roach        return $y;
337e2b8114dSGreg Roach    }
338e2b8114dSGreg Roach
339e2b8114dSGreg Roach    /**
340e2b8114dSGreg Roach     * @param Date $start
341e2b8114dSGreg Roach     * @param Date $end
342e2b8114dSGreg Roach     * @param Tree $tree
343e2b8114dSGreg Roach     *
344e2b8114dSGreg Roach     * @return string[]
345e2b8114dSGreg Roach     */
346e2b8114dSGreg Roach    protected function findIndividualsByDate(Date $start, Date $end, Tree $tree): array
347e2b8114dSGreg Roach    {
348e2b8114dSGreg Roach        return DB::table('individuals')
3490b5fd0a6SGreg Roach            ->join('dates', static function (JoinClause $join): void {
350e2b8114dSGreg Roach                $join
351e2b8114dSGreg Roach                    ->on('d_file', '=', 'i_file')
352e2b8114dSGreg Roach                    ->on('d_gid', '=', 'i_id');
353e2b8114dSGreg Roach            })
354e2b8114dSGreg Roach            ->where('i_file', '=', $tree->id())
355e2b8114dSGreg Roach            ->where('d_julianday1', '<=', $end->maximumJulianDay())
356e2b8114dSGreg Roach            ->where('d_julianday2', '>=', $start->minimumJulianDay())
357e2b8114dSGreg Roach            ->whereNotIn('d_fact', ['BAPL', 'ENDL', 'SLGC', 'SLGS', '_TODO', 'CHAN'])
358e2b8114dSGreg Roach            ->pluck('i_id')
359e2b8114dSGreg Roach            ->all();
360e2b8114dSGreg Roach    }
361e2b8114dSGreg Roach
362e2b8114dSGreg Roach    /**
363e2b8114dSGreg Roach     * @param Place $place
364e2b8114dSGreg Roach     * @param Tree  $tree
365e2b8114dSGreg Roach     *
366e2b8114dSGreg Roach     * @return string[]
367e2b8114dSGreg Roach     */
368e2b8114dSGreg Roach    protected function findIndividualsByPlace(Place $place, Tree $tree): array
369e2b8114dSGreg Roach    {
370e2b8114dSGreg Roach        return DB::table('individuals')
3710b5fd0a6SGreg Roach            ->join('placelinks', static function (JoinClause $join): void {
372e2b8114dSGreg Roach                $join
373e2b8114dSGreg Roach                    ->on('pl_file', '=', 'i_file')
374e2b8114dSGreg Roach                    ->on('pl_gid', '=', 'i_id');
375e2b8114dSGreg Roach            })
376e2b8114dSGreg Roach            ->where('i_file', '=', $tree->id())
377392561bbSGreg Roach            ->where('pl_p_id', '=', $place->id())
378e2b8114dSGreg Roach            ->pluck('i_id')
379e2b8114dSGreg Roach            ->all();
380e2b8114dSGreg Roach    }
381e2b8114dSGreg Roach
382e2b8114dSGreg Roach    /**
383e2b8114dSGreg Roach     * Find the close family members of an individual.
384e2b8114dSGreg Roach     *
385e2b8114dSGreg Roach     * @param Individual $individual
386e2b8114dSGreg Roach     *
387e2b8114dSGreg Roach     * @return string[]
388e2b8114dSGreg Roach     */
389e2b8114dSGreg Roach    protected function closeFamily(Individual $individual): array
390e2b8114dSGreg Roach    {
391e2b8114dSGreg Roach        $xrefs = [];
392e2b8114dSGreg Roach
39339ca88baSGreg Roach        foreach ($individual->spouseFamilies() as $family) {
39439ca88baSGreg Roach            foreach ($family->children() as $child) {
395e2b8114dSGreg Roach                $xrefs[] = $child->xref();
396e2b8114dSGreg Roach            }
397e2b8114dSGreg Roach
39839ca88baSGreg Roach            foreach ($family->spouses() as $spouse) {
399e2b8114dSGreg Roach                $xrefs[] = $spouse->xref();
400e2b8114dSGreg Roach            }
401e2b8114dSGreg Roach        }
402e2b8114dSGreg Roach
40339ca88baSGreg Roach        foreach ($individual->childFamilies() as $family) {
40439ca88baSGreg Roach            foreach ($family->children() as $child) {
405e2b8114dSGreg Roach                $xrefs[] = $child->xref();
406e2b8114dSGreg Roach            }
407e2b8114dSGreg Roach
40839ca88baSGreg Roach            foreach ($family->spouses() as $spouse) {
409e2b8114dSGreg Roach                $xrefs[] = $spouse->xref();
410e2b8114dSGreg Roach            }
411e2b8114dSGreg Roach        }
412e2b8114dSGreg Roach
413e2b8114dSGreg Roach        return $xrefs;
414e2b8114dSGreg Roach    }
415e2b8114dSGreg Roach
416e2b8114dSGreg Roach    /**
417e2b8114dSGreg Roach     * Generate a subtitle, based on filter parameters
418e2b8114dSGreg Roach     *
419e2b8114dSGreg Roach     * @param int    $count
420e2b8114dSGreg Roach     * @param Date   $start
421e2b8114dSGreg Roach     * @param Date   $end
422e2b8114dSGreg Roach     * @param string $placename
423e2b8114dSGreg Roach     *
424e2b8114dSGreg Roach     * @return string
425e2b8114dSGreg Roach     */
426e2b8114dSGreg Roach    protected function subtitle(int $count, Date $start, Date $end, string $placename): string
427e2b8114dSGreg Roach    {
428e2b8114dSGreg Roach        if ($start->isOK() && $end->isOK() && $placename !== '') {
429e2b8114dSGreg Roach            return I18N::plural(
430e2b8114dSGreg Roach                '%s individual with events in %s between %s and %s',
431e2b8114dSGreg Roach                '%s individuals with events in %s between %s and %s',
432e2b8114dSGreg Roach                $count,
433e2b8114dSGreg Roach                I18N::number($count),
434e2b8114dSGreg Roach                $placename,
435e2b8114dSGreg Roach                $start->display(false, '%Y'),
436e2b8114dSGreg Roach                $end->display(false, '%Y')
437e2b8114dSGreg Roach            );
438e2b8114dSGreg Roach        }
439e2b8114dSGreg Roach
440e2b8114dSGreg Roach        if ($placename !== '') {
441e2b8114dSGreg Roach            return I18N::plural(
442e2b8114dSGreg Roach                '%s individual with events in %s',
443e2b8114dSGreg Roach                '%s individuals with events in %s',
444e2b8114dSGreg Roach                $count,
445e2b8114dSGreg Roach                I18N::number($count),
446e2b8114dSGreg Roach                $placename
447e2b8114dSGreg Roach            );
448e2b8114dSGreg Roach        }
449e2b8114dSGreg Roach
450e2b8114dSGreg Roach        if ($start->isOK() && $end->isOK()) {
451e2b8114dSGreg Roach            return I18N::plural(
452e2b8114dSGreg Roach                '%s individual with events between %s and %s',
453e2b8114dSGreg Roach                '%s individuals with events between %s and %s',
454e2b8114dSGreg Roach                $count,
455e2b8114dSGreg Roach                I18N::number($count),
456e2b8114dSGreg Roach                $start->display(false, '%Y'),
457e2b8114dSGreg Roach                $end->display(false, '%Y')
458e2b8114dSGreg Roach            );
459e2b8114dSGreg Roach        }
460e2b8114dSGreg Roach
461e2b8114dSGreg Roach        return I18N::plural('%s individual', '%s individuals', $count, I18N::number($count));
462e2b8114dSGreg Roach    }
463168ff6f3Sric2016}
464