Passed
Push — dev ( 5f589d...824cd4 )
by Greg
08:12
created

XrefMedia::labelValue()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 2
dl 0
loc 23
rs 9.9
c 0
b 0
f 0
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
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Elements;
21
22
use Fisharebest\Webtrees\Gedcom;
23
use Fisharebest\Webtrees\Http\RequestHandlers\CreateMediaObjectModal;
24
use Fisharebest\Webtrees\I18N;
25
use Fisharebest\Webtrees\Registry;
26
use Fisharebest\Webtrees\Tree;
27
28
use Illuminate\Support\Str;
29
use Ramsey\Uuid\Uuid;
30
31
use function e;
32
use function explode;
33
use function preg_match;
34
use function route;
35
use function strip_tags;
36
use function trim;
37
use function view;
38
39
/**
40
 * XREF:OBJE := {Size=1:22}
41
 * A pointer to, or a cross-reference identifier of, a multimedia object.
42
 */
43
class 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">' . $label . $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