1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 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 function e; 29use function preg_match; 30use function route; 31use function trim; 32use function view; 33 34/** 35 * XREF:OBJE := {Size=1:22} 36 * A pointer to, or a cross-reference identifier of, a multimedia object. 37 */ 38class XrefMedia extends AbstractXrefElement 39{ 40 /** 41 * An edit control for this data. 42 * 43 * @param string $id 44 * @param string $name 45 * @param string $value 46 * @param Tree $tree 47 * 48 * @return string 49 */ 50 public function edit(string $id, string $name, string $value, Tree $tree): string 51 { 52 $select = view('components/select-media', [ 53 'id' => $id, 54 'name' => $name, 55 'media' => Registry::mediaFactory()->make(trim($value, '@'), $tree), 56 'tree' => $tree, 57 'at' => '@', 58 ]); 59 60 return 61 '<div class="input-group">' . 62 '<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') . '">' . 63 view('icons/add') . 64 '</button>' . 65 $select . 66 '</div>'; 67 } 68 69 /** 70 * Create a label/value pair for this element. 71 * 72 * @param string $value 73 * @param Tree $tree 74 * 75 * @return string 76 */ 77 public function labelValue(string $value, Tree $tree): string 78 { 79 // Show the image instead of the label. 80 if (preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $value, $match) === 1) { 81 $media = Registry::mediaFactory()->make($match[1], $tree); 82 83 if ($media === null) { 84 return parent::labelValue($value, $tree); 85 } 86 87 $media_file = $media->mediaFiles()->first(); 88 89 if ($media_file === null) { 90 return parent::labelValue($value, $tree); 91 } 92 93 $label = $media_file->displayImage(100, 100, 'contain', []); 94 $value = '<a href="' . e($media->url()) . '">' . $media->fullName() . '</a>'; 95 96 $label_html = '<div class="pe-1 pb-1 wt-media-link-image">' . $label . '</div>'; 97 $value_html = '<div class="wt-media-link-title">' . $value . '</div>'; 98 99 return '<div class="d-flex align-items-center wt-media-link">' . $label_html . $value_html . '</div>'; 100 } 101 102 return parent::labelValue($value, $tree); 103 } 104 105 /** 106 * Display the value of this type of element. 107 * 108 * @param string $value 109 * @param Tree $tree 110 * 111 * @return string 112 */ 113 public function value(string $value, Tree $tree): string 114 { 115 return $this->valueXrefLink($value, $tree, Registry::mediaFactory()); 116 } 117} 118