xref: /webtrees/resources/views/lists/locations-table.phtml (revision 6f4ec3cadc983f0a7294108c634bef48846b4311)
1e8ded2caSGreg Roach<?php
2e8ded2caSGreg Roach
310e06497SGreg Roachdeclare(strict_types=1);
410e06497SGreg Roach
5*6f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
6e8ded2caSGreg Roachuse Fisharebest\Webtrees\I18N;
7e8ded2caSGreg Roachuse Fisharebest\Webtrees\Location;
8e8ded2caSGreg Roachuse Fisharebest\Webtrees\Tree;
9e8ded2caSGreg Roachuse Illuminate\Database\Query\Expression;
10e8ded2caSGreg Roachuse Illuminate\Database\Query\JoinClause;
11e8ded2caSGreg Roachuse Illuminate\Support\Collection;
12e8ded2caSGreg Roach
13e8ded2caSGreg Roach/**
1436779af1SGreg Roach * @var Collection<int,Location> $locations
15e8ded2caSGreg Roach * @var Tree                     $tree
16e8ded2caSGreg Roach */
17e8ded2caSGreg Roach
18e8ded2caSGreg Roach?>
19e8ded2caSGreg Roach
20e8ded2caSGreg Roach<?php
21e8ded2caSGreg Roach// Count the number of linked records. These numbers include private records.
22e8ded2caSGreg Roach// It is not good to bypass privacy, but many servers do not have the resources
23e8ded2caSGreg Roach// to process privacy for every record in the tree
24e8ded2caSGreg Roach$count_individuals = DB::table('individuals')
25e8ded2caSGreg Roach    ->join('link', static function (JoinClause $join): void {
26e8ded2caSGreg Roach        $join->on('l_from', '=', 'i_id');
27e8ded2caSGreg Roach        $join->on('l_file', '=', 'i_file');
28e8ded2caSGreg Roach    })
29e8ded2caSGreg Roach    ->where('l_type', '=', '_LOC')
30e8ded2caSGreg Roach    ->where('l_file', '=', $tree->id())
31e8ded2caSGreg Roach    ->groupBy(['l_to'])
32059898c9SGreg Roach    ->pluck(new Expression('COUNT(*) AS total'), 'l_to')
33a962cdb5SGreg Roach    ->map(static fn ($n) => (int) $n)
34e8ded2caSGreg Roach    ->all();
35e8ded2caSGreg Roach
36e8ded2caSGreg Roach$count_families = DB::table('families')
37e8ded2caSGreg Roach    ->join('link', static function (JoinClause $join): void {
38e8ded2caSGreg Roach        $join->on('l_from', '=', 'f_id');
39e8ded2caSGreg Roach        $join->on('l_file', '=', 'f_file');
40e8ded2caSGreg Roach    })
41e8ded2caSGreg Roach    ->where('l_type', '=', '_LOC')
42e8ded2caSGreg Roach    ->where('l_file', '=', $tree->id())
43e8ded2caSGreg Roach    ->groupBy(['l_to'])
44059898c9SGreg Roach    ->pluck(new Expression('COUNT(*) AS total'), 'l_to')
45a962cdb5SGreg Roach    ->map(static fn ($n) => (int) $n)
46e8ded2caSGreg Roach    ->all();
47e8ded2caSGreg Roach?>
48e8ded2caSGreg Roach
49e8ded2caSGreg Roach<table
50e8ded2caSGreg Roach    class="table table-bordered table-sm wt-table-location datatables d-none"
51e8ded2caSGreg Roach    <?= view('lists/datatables-attributes') ?>
52e8ded2caSGreg Roach    data-columns="<?= e(json_encode([
53eef4add8SGreg Roach        ['type' => 'html'],
54e8ded2caSGreg Roach        ['visible' => array_sum($count_individuals) > 0],
55e8ded2caSGreg Roach        ['visible' => array_sum($count_families) > 0],
56e8ded2caSGreg Roach        ['visible' => (bool) $tree->getPreference('SHOW_LAST_CHANGE'), 'searchable' => false],
57728c8c27SGreg Roach    ], JSON_THROW_ON_ERROR)) ?>"
58e8ded2caSGreg Roach>
59315eb316SGreg Roach    <caption class="visually-hidden">
60e8ded2caSGreg Roach        <?= $caption ?? I18N::translate('Locations') ?>
61e8ded2caSGreg Roach    </caption>
62e8ded2caSGreg Roach
63e8ded2caSGreg Roach    <thead>
64e8ded2caSGreg Roach        <tr>
65e8ded2caSGreg Roach            <th><?= I18N::translate('Location') ?></th>
66e8ded2caSGreg Roach            <th><?= I18N::translate('Individuals') ?></th>
67e8ded2caSGreg Roach            <th><?= I18N::translate('Families') ?></th>
68e8ded2caSGreg Roach            <th><?= I18N::translate('Last change') ?></th>
69e8ded2caSGreg Roach        </tr>
70e8ded2caSGreg Roach    </thead>
71e8ded2caSGreg Roach
72e8ded2caSGreg Roach    <tbody>
73e8ded2caSGreg Roach        <?php foreach ($locations as $location) : ?>
74e8ded2caSGreg Roach            <tr class="<?= $location->isPendingAddition() ? 'wt-new' : '' ?> <?= $location->isPendingDeletion() ? 'wt-old' : '' ?>">
75e8ded2caSGreg Roach                <!-- Location name -->
76e8ded2caSGreg Roach                <td data-sort="<?= e($location->sortName()) ?>">
77e8ded2caSGreg Roach                    <a href="<?= e($location->url()) ?>">
78e8ded2caSGreg Roach                        <?= $location->fullName() ?>
79e8ded2caSGreg Roach                    </a>
80e8ded2caSGreg Roach                </td>
81e8ded2caSGreg Roach
82e8ded2caSGreg Roach                <!-- Count of linked individuals -->
83e8ded2caSGreg Roach                <td class="text-center" data-sort="<?= $count_individuals[$location->xref()] ?? 0 ?>">
84e8ded2caSGreg Roach                    <?= I18N::number($count_individuals[$location->xref()] ?? 0) ?>
85e8ded2caSGreg Roach                </td>
86e8ded2caSGreg Roach
87e8ded2caSGreg Roach                <!-- Count of linked families -->
88e8ded2caSGreg Roach                <td class="text-center" data-sort="<?= $count_families[$location->xref()] ?? 0 ?>">
89e8ded2caSGreg Roach                    <?= I18N::number($count_families[$location->xref()] ?? 0) ?>
90e8ded2caSGreg Roach                </td>
91e8ded2caSGreg Roach
92e8ded2caSGreg Roach                <!-- Last change -->
93d97083feSGreg Roach                <td data-sort="<?= $location->lastChangeTimestamp()->timestamp() ?>">
94e8ded2caSGreg Roach                    <?= view('components/datetime', ['timestamp' => $location->lastChangeTimestamp()]) ?>
95e8ded2caSGreg Roach                </td>
96e8ded2caSGreg Roach            </tr>
97e8ded2caSGreg Roach        <?php endforeach ?>
98e8ded2caSGreg Roach    </tbody>
99e8ded2caSGreg Roach</table>
100