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 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 /** 71 * Create a label/value pair for this element. 72 * 73 * @param string $value 74 * @param Tree $tree 75 * 76 * @return string 77 */ 78 public function labelValue(string $value, Tree $tree): string 79 { 80 // Show the image instead of the label. 81 if (preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $value, $match) === 1) { 82 $media = Registry::mediaFactory()->make($match[1], $tree); 83 84 if ($media === null) { 85 return parent::labelValue($value, $tree); 86 } 87 88 $media_file = $media->mediaFiles()->first(); 89 90 if ($media_file === null) { 91 return parent::labelValue($value, $tree); 92 } 93 94 $label = $media_file->displayImage(100, 100, 'contain', []); 95 $value = '<a href="' . e($media->url()) . '">' . $media->fullName() . '</a>'; 96 97 return '<div class="d-flex"><div class="pe-1 pb-1">' . $label . '</div>' . $value . '</div>'; 98 } 99 100 return parent::labelValue($value, $tree); 101 } 102 103 /** 104 * Display the value of this type of element. 105 * 106 * @param string $value 107 * @param Tree $tree 108 * 109 * @return string 110 */ 111 public function value(string $value, Tree $tree): string 112 { 113 return $this->valueXrefLink($value, $tree, Registry::mediaFactory()); 114 } 115} 116