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