xref: /webtrees/resources/views/lists/notes-table.phtml (revision ade86bc351a97d0852775e49c96dd4ac061c739b)
1<?php
2
3declare(strict_types=1);
4
5use Fisharebest\Webtrees\DB;
6use Fisharebest\Webtrees\I18N;
7use Fisharebest\Webtrees\Note;
8use Fisharebest\Webtrees\Tree;
9use Illuminate\Database\Query\Expression;
10use Illuminate\Database\Query\JoinClause;
11use Illuminate\Support\Collection;
12
13/**
14 * @var Collection<int,Note> $notes
15 * @var Tree                 $tree
16 */
17
18?>
19
20<?php
21// Count the number of linked records. These numbers include private records.
22// It is not good to bypass privacy, but many servers do not have the resources
23// to process privacy for every record in the tree
24$count_individuals = DB::table('individuals')
25    ->join('link', static function (JoinClause $join): void {
26        $join->on('l_from', '=', 'i_id');
27        $join->on('l_file', '=', 'i_file');
28    })
29    ->where('l_type', '=', 'NOTE')
30    ->where('l_file', '=', $tree->id())
31    ->groupBy(['l_to'])
32    ->pluck(new Expression('COUNT(*) AS total'), 'l_to')
33    ->map(static fn ($n) => (int) $n)
34    ->all();
35
36$count_families = DB::table('families')
37    ->join('link', static function (JoinClause $join): void {
38        $join->on('l_from', '=', 'f_id');
39        $join->on('l_file', '=', 'f_file');
40    })
41    ->where('l_type', '=', 'NOTE')
42    ->where('l_file', '=', $tree->id())
43    ->groupBy(['l_to'])
44    ->pluck(new Expression('COUNT(*) AS total'), 'l_to')
45    ->map(static fn ($n) => (int) $n)
46    ->all();
47
48$count_media = DB::table('media')
49    ->join('link', static function (JoinClause $join): void {
50        $join->on('l_from', '=', 'm_id');
51        $join->on('l_file', '=', 'm_file');
52    })
53    ->where('l_type', '=', 'NOTE')
54    ->where('l_file', '=', $tree->id())
55    ->groupBy(['l_to'])
56    ->pluck(new Expression('COUNT(*) AS total'), 'l_to')
57    ->map(static fn ($n) => (int) $n)
58    ->all();
59
60$count_sources = DB::table('sources')
61    ->join('link', static function (JoinClause $join): void {
62        $join->on('l_from', '=', 's_id');
63        $join->on('l_file', '=', 's_file');
64    })
65    ->where('l_type', '=', 'NOTE')
66    ->where('l_file', '=', $tree->id())
67    ->groupBy(['l_to'])
68    ->pluck(new Expression('COUNT(*) AS total'), 'l_to')
69    ->map(static fn ($n) => (int) $n)
70    ->all();
71?>
72
73<table
74    class="table table-bordered table-sm wt-table-note datatables d-none"
75    <?= view('lists/datatables-attributes') ?>
76    data-columns="<?= e(json_encode([
77        ['type' => 'html'],
78        ['visible' => array_sum($count_individuals) > 0],
79        ['visible' => array_sum($count_families) > 0],
80        ['visible' => array_sum($count_media) > 0],
81        ['visible' => array_sum($count_sources) > 0],
82        ['visible' => (bool) $tree->getPreference('SHOW_LAST_CHANGE'), 'searchable' => false],
83    ], JSON_THROW_ON_ERROR)) ?>"
84>
85    <caption class="visually-hidden">
86        <?= $caption ?? I18N::translate('Sources') ?>
87    </caption>
88
89    <thead>
90        <tr>
91            <th><?= I18N::translate('Title') ?></th>
92            <th><?= I18N::translate('Individuals') ?></th>
93            <th><?= I18N::translate('Families') ?></th>
94            <th><?= I18N::translate('Media objects') ?></th>
95            <th><?= I18N::translate('Sources') ?></th>
96            <th><?= I18N::translate('Last change') ?></th>
97        </tr>
98    </thead>
99
100    <tbody>
101        <?php foreach ($notes as $note) : ?>
102            <tr class="<?= $note->isPendingAddition() ? 'wt-new' : '' ?> <?= $note->isPendingDeletion() ? 'wt-old' : '' ?>">
103                <!-- Title -->
104                <td data-sort="<?= e($note->sortName()) ?>">
105                    <a href="<?= e($note->url()) ?>">
106                        <?= $note->fullName() ?>
107                    </a>
108                </td>
109
110                <!-- Count of linked individuals -->
111                <td class="text-center" data-sort="<?= $count_individuals[$note->xref()] ?? 0 ?>">
112                    <?= I18N::number($count_individuals[$note->xref()] ?? 0) ?>
113                </td>
114
115                <!-- Count of linked families -->
116                <td class="text-center" data-sort="<?= $count_families[$note->xref()] ?? 0 ?>">
117                    <?= I18N::number($count_families[$note->xref()] ?? 0) ?>
118                </td>
119
120                <!-- Count of linked media objects -->
121                <td class="text-center" data-sort="<?= $count_media[$note->xref()] ?? 0 ?>">
122                    <?= I18N::number($count_media[$note->xref()] ?? 0) ?>
123                </td>
124
125                <!-- Count of sources -->
126                <td class="text-center" data-sort="<?= $count_sources[$note->xref()] ?? 0 ?>">
127                    <?= I18N::number($count_sources[$note->xref()] ?? 0) ?>
128                </td>
129
130                <!-- Last change -->
131                <td data-sort="<?= $note->lastChangeTimestamp()->timestamp() ?>">
132                    <?= view('components/datetime', ['timestamp' => $note->lastChangeTimestamp()]) ?>
133                </td>
134            </tr>
135        <?php endforeach ?>
136    </tbody>
137</table>
138