xref: /webtrees/app/Elements/XrefMedia.php (revision cd350b49988384cb19280339a7febd3fda603809)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Elements;
21
22use Fisharebest\Webtrees\Gedcom;
23use Fisharebest\Webtrees\Http\RequestHandlers\CreateMediaObjectModal;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Registry;
26use Fisharebest\Webtrees\Tree;
27
28use Illuminate\Support\Str;
29use Ramsey\Uuid\Uuid;
30
31use function e;
32use function explode;
33use function preg_match;
34use function route;
35use function strip_tags;
36use function trim;
37use function view;
38
39/**
40 * XREF:OBJE := {Size=1:22}
41 * A pointer to, or a cross-reference identifier of, a multimedia object.
42 */
43class XrefMedia extends AbstractXrefElement
44{
45    /**
46     * An edit control for this data.
47     *
48     * @param string $id
49     * @param string $name
50     * @param string $value
51     * @param Tree   $tree
52     *
53     * @return string
54     */
55    public function edit(string $id, string $name, string $value, Tree $tree): string
56    {
57        $select = view('components/select-media', [
58            'id'    => $id,
59            'name'  => $name,
60            'media' => Registry::mediaFactory()->make(trim($value, '@'), $tree),
61            'tree'  => $tree,
62            'at'    => '@',
63        ]);
64
65        return
66            '<div class="input-group">' .
67            '<button class="btn btn-secondary" type="button" data-bs-toggle="modal" data-bs-backdrop="static" data-bs-target="#wt-ajax-modal" data-wt-href="' . e(route(CreateMediaObjectModal::class, ['tree' => $tree->name()])) . '" data-wt-select-id="' . $id . '" title="' . I18N::translate('Create a media object') . '">' .
68            view('icons/add') .
69            '</button>' .
70            $select .
71            '</div>';
72    }
73
74
75    /**
76     * Create a label/value pair for this element.
77     *
78     * @param string $value
79     * @param Tree   $tree
80     *
81     * @return string
82     */
83    public function labelValue(string $value, Tree $tree): string
84    {
85        // Show the image instead of the label.
86        if (preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $value, $match) === 1) {
87            $media = Registry::mediaFactory()->make($match[1], $tree);
88
89            if ($media === null) {
90                return parent::labelValue($value, $tree);
91            }
92
93            $media_file = $media->mediaFiles()->first();
94
95            if ($media_file === null) {
96                return parent::labelValue($value, $tree);
97            }
98
99            $label = $media_file->displayImage(100, 100, 'contain', []);
100            $value = '<a href="' . e($media->url()) . '">' . $media->fullName() . '</a>';
101
102            return '<div class="d-flex"><div class="pe-1 pb-1">' . $label . '</div>' . $value . '</div>';
103        }
104
105        return parent::labelValue($value, $tree);
106    }
107
108    /**
109     * Display the value of this type of element.
110     *
111     * @param string $value
112     * @param Tree   $tree
113     *
114     * @return string
115     */
116    public function value(string $value, Tree $tree): string
117    {
118        return $this->valueXrefLink($value, $tree, Registry::mediaFactory());
119    }
120}
121