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