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