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