summaryrefslogtreecommitdiff
path: root/resources/views/lists/sources-table.phtml
blob: d24872909a4c443a4165798280391693fab0277f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php use Fisharebest\Webtrees\I18N; ?>
<?php use Illuminate\Database\Query\JoinClause; ?>
<?php use Illuminate\Database\Capsule\Manager as DB; ?>

<?php
// Count the number of linked records. These numbers include private records.
// It is not good to bypass privacy, but many servers do not have the resources
// to process privacy for every record in the tree
$count_individuals = DB::table('individuals')
    ->join('link', static function (JoinClause $join): void {
        $join->on('l_from', '=', 'i_id');
        $join->on('l_file', '=', 'i_file');
    })
    ->where('l_type', '=', 'SOUR')
    ->where('l_file', '=', $tree->id())
    ->groupBy('l_to')
    ->select(['l_to', DB::raw('COUNT(*) AS total')])
    ->pluck('total', 'l_to')
    ->all();

$count_families = DB::table('families')
    ->join('link', static function (JoinClause $join): void {
        $join->on('l_from', '=', 'f_id');
        $join->on('l_file', '=', 'f_file');
    })
    ->where('l_type', '=', 'SOUR')
    ->where('l_file', '=', $tree->id())
    ->groupBy('l_to')
    ->select(['l_to', DB::raw('COUNT(*) AS total')])
    ->pluck('total', 'l_to')
    ->all();

$count_media = DB::table('media')
    ->join('link', static function (JoinClause $join): void {
        $join->on('l_from', '=', 'm_id');
        $join->on('l_file', '=', 'm_file');
    })
    ->where('l_type', '=', 'SOUR')
    ->where('l_file', '=', $tree->id())
    ->groupBy('l_to')
    ->select(['l_to', DB::raw('COUNT(*) AS total')])
    ->pluck('total', 'l_to')
    ->all();

$count_notes = DB::table('other')
    ->join('link', static function (JoinClause $join): void {
        $join->on('l_from', '=', 'o_id');
        $join->on('l_file', '=', 'o_file');
    })
    ->where('o_type', '=', 'NOTE')
    ->where('l_type', '=', 'SOUR')
    ->where('l_file', '=', $tree->id())
    ->groupBy('l_to')
    ->select(['l_to', DB::raw('COUNT(*) AS total')])
    ->pluck('total', 'l_to')
    ->all();
?>

<table
    class="table table-bordered table-sm wt-table-source datatables d-none"
    <?= view('lists/datatables-attributes') ?>
    data-auto-width="false"
    data-columns="<?= e(json_encode([
        null,
        null,
        ['visible' => array_sum($count_individuals) > 0],
        ['visible' => array_sum($count_families) > 0],
        ['visible' => array_sum($count_media) > 0],
        ['visible' => array_sum($count_notes) > 0],
        ['visible' => (bool) $tree->getPreference('SHOW_LAST_CHANGE'), 'searchable' => false],
    ])) ?>"
>
    <caption class="sr-only">
        <?= $caption ?? I18N::translate('Sources') ?>
    </caption>

    <thead>
        <tr>
            <th><?= I18N::translate('Title') ?></th>
            <th><?= I18N::translate('Author') ?></th>
            <th><?= I18N::translate('Individuals') ?></th>
            <th><?= I18N::translate('Families') ?></th>
            <th><?= I18N::translate('Media objects') ?></th>
            <th><?= I18N::translate('Shared notes') ?></th>
            <th><?= I18N::translate('Last change') ?></th>
        </tr>
    </thead>

    <tbody>
        <?php foreach ($sources as $source) : ?>
            <tr class="<?= $source->isPendingDeletion() ? 'old' : ($source->isPendingAddition() ? 'new' : '') ?>">
                <!-- Title -->
                <td data-sort="<?= e($source->sortName()) ?>">
                    <a href="<?= e($source->url()) ?>">
                        <?= $source->fullName() ?>
                    </a>
                </td>

                <!-- Author -->
                <td>
                    <?= e($source->facts(['AUTH'])->isNotEmpty() ? $source->facts(['AUTH'])->first()->value() : '') ?>
                </td>

                <!-- Count of linked individuals -->
                <td class="text-center" data-sort="<?= $count_individuals[$source->xref()] ?? 0 ?>">
                    <?= I18N::number($count_individuals[$source->xref()] ?? 0) ?>
                </td>

                <!-- Count of linked families -->
                <td class="text-center" data-sort="<?= $count_families[$source->xref()] ?? 0 ?>">
                    <?= I18N::number($count_families[$source->xref()] ?? 0) ?>
                </td>

                <!-- Count of linked media objects -->
                <td class="text-center" data-sort="<?= $count_media[$source->xref()] ?? 0 ?>">
                    <?= I18N::number($count_media[$source->xref()] ?? 0) ?>
                </td>

                <!-- Count of linked notes -->
                <td class="text-center" data-sort="<?= $count_notes[$source->xref()] ?? 0 ?>">
                    <?= I18N::number($count_notes[$source->xref()] ?? 0) ?>
                </td>

                <!-- Last change -->
                <td data-sort="<?= $source->lastChangeTimestamp()->unix() ?>">
                    <?= view('components/datetime', ['timestamp' => $source->lastChangeTimestamp()]) ?>
                </td>
            </tr>
        <?php endforeach ?>
    </tbody>
</table>