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