xref: /webtrees/resources/views/lists/sources-table.phtml (revision b6017f990d38d8c56e04c0096ce9a7e8745ad4ba)
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-columns="<?= e(json_encode([
68        null,
69        null,
70        null,
71        null,
72        ['visible' => array_sum($count_individuals) > 0],
73        ['visible' => array_sum($count_families) > 0],
74        ['visible' => array_sum($count_media) > 0],
75        ['visible' => array_sum($count_notes) > 0],
76        ['visible' => (bool) $tree->getPreference('SHOW_LAST_CHANGE'), 'searchable' => false],
77    ])) ?>"
78>
79    <caption class="sr-only">
80        <?= $caption ?? I18N::translate('Sources') ?>
81    </caption>
82
83    <thead>
84        <tr>
85            <th><?= I18N::translate('Title') ?></th>
86            <th class="d-none d-md-table-cell"><?= I18N::translate('Abbreviation') ?></th>
87            <th class="d-none d-md-table-cell"><?= I18N::translate('Author') ?></th>
88            <th class="d-none d-md-table-cell"><?= I18N::translate('Publication') ?></th>
89            <th><?= I18N::translate('Individuals') ?></th>
90            <th><?= I18N::translate('Families') ?></th>
91            <th><?= I18N::translate('Media objects') ?></th>
92            <th><?= I18N::translate('Shared notes') ?></th>
93            <th><?= I18N::translate('Last change') ?></th>
94        </tr>
95    </thead>
96
97    <tbody>
98        <?php foreach ($sources as $source) : ?>
99            <tr class="<?= $source->isPendingDeletion() ? 'wt-old' : ($source->isPendingAddition() ? 'wt-new' : '') ?>">
100                <!-- Title -->
101                <td data-sort="<?= e($source->sortName()) ?>">
102                    <a href="<?= e($source->url()) ?>">
103                        <?= $source->fullName() ?>
104                    </a>
105                </td>
106
107                <!-- Abbreviation -->
108                <td class="d-none d-md-table-cell">
109                    <?= e($source->facts(['ABBR'])->isNotEmpty() ? $source->facts(['ABBR'])->first()->value() : '') ?>
110                </td>
111
112                <!-- Author -->
113                <td class="d-none d-md-table-cell">
114                    <?= e($source->facts(['AUTH'])->isNotEmpty() ? $source->facts(['AUTH'])->first()->value() : '') ?>
115                </td>
116
117                <!-- Publisher -->
118                <td class="d-none d-md-table-cell">
119                    <?= e($source->facts(['PUBL'])->isNotEmpty() ? $source->facts(['PUBL'])->first()->value() : '') ?>
120                </td>
121
122                <!-- Count of linked individuals -->
123                <td class="text-center" data-sort="<?= $count_individuals[$source->xref()] ?? 0 ?>">
124                    <?= I18N::number($count_individuals[$source->xref()] ?? 0) ?>
125                </td>
126
127                <!-- Count of linked families -->
128                <td class="text-center" data-sort="<?= $count_families[$source->xref()] ?? 0 ?>">
129                    <?= I18N::number($count_families[$source->xref()] ?? 0) ?>
130                </td>
131
132                <!-- Count of linked media objects -->
133                <td class="text-center" data-sort="<?= $count_media[$source->xref()] ?? 0 ?>">
134                    <?= I18N::number($count_media[$source->xref()] ?? 0) ?>
135                </td>
136
137                <!-- Count of linked notes -->
138                <td class="text-center" data-sort="<?= $count_notes[$source->xref()] ?? 0 ?>">
139                    <?= I18N::number($count_notes[$source->xref()] ?? 0) ?>
140                </td>
141
142                <!-- Last change -->
143                <td data-sort="<?= $source->lastChangeTimestamp()->unix() ?>">
144                    <?= view('components/datetime', ['timestamp' => $source->lastChangeTimestamp()]) ?>
145                </td>
146            </tr>
147        <?php endforeach ?>
148    </tbody>
149</table>
150