xref: /webtrees/app/Elements/AbstractElement.php (revision 9d477377bf893ad3a11fabbd665af27ebb5e2a2e)
1c2ed51d1SGreg Roach<?php
2c2ed51d1SGreg Roach
3c2ed51d1SGreg Roach/**
4c2ed51d1SGreg Roach * webtrees: online genealogy
5c2ed51d1SGreg Roach * Copyright (C) 2021 webtrees development team
6c2ed51d1SGreg Roach * This program is free software: you can redistribute it and/or modify
7c2ed51d1SGreg Roach * it under the terms of the GNU General Public License as published by
8c2ed51d1SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9c2ed51d1SGreg Roach * (at your option) any later version.
10c2ed51d1SGreg Roach * This program is distributed in the hope that it will be useful,
11c2ed51d1SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12c2ed51d1SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13c2ed51d1SGreg Roach * GNU General Public License for more details.
14c2ed51d1SGreg Roach * You should have received a copy of the GNU General Public License
15c2ed51d1SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16c2ed51d1SGreg Roach */
17c2ed51d1SGreg Roach
18c2ed51d1SGreg Roachdeclare(strict_types=1);
19c2ed51d1SGreg Roach
20c2ed51d1SGreg Roachnamespace Fisharebest\Webtrees\Elements;
21c2ed51d1SGreg Roach
22c2ed51d1SGreg Roachuse Fisharebest\Webtrees\Contracts\ElementInterface;
23c2ed51d1SGreg Roachuse Fisharebest\Webtrees\Html;
24c2ed51d1SGreg Roachuse Fisharebest\Webtrees\I18N;
258392edd9SGreg Roachuse Fisharebest\Webtrees\Registry;
26c2ed51d1SGreg Roachuse Fisharebest\Webtrees\Tree;
27c2ed51d1SGreg Roach
28c2ed51d1SGreg Roachuse function array_key_exists;
29c2ed51d1SGreg Roachuse function array_map;
30c2ed51d1SGreg Roachuse function e;
31c2ed51d1SGreg Roachuse function is_numeric;
32*9d477377SGreg Roachuse function preg_replace;
334da96842SGreg Roachuse function str_contains;
34cef4fcc0SGreg Roachuse function str_starts_with;
354da96842SGreg Roachuse function strip_tags;
36c2ed51d1SGreg Roachuse function trim;
37c2ed51d1SGreg Roachuse function view;
38c2ed51d1SGreg Roach
39c2ed51d1SGreg Roach/**
40c2ed51d1SGreg Roach * A GEDCOM element is a tag/primitive in a GEDCOM file.
41c2ed51d1SGreg Roach */
42c2ed51d1SGreg Roachabstract class AbstractElement implements ElementInterface
43c2ed51d1SGreg Roach{
44c2ed51d1SGreg Roach    // HTML attributes for an <input>
45ae0043b7SGreg Roach    protected const MAXIMUM_LENGTH = false;
46ae0043b7SGreg Roach    protected const PATTERN        = false;
47c2ed51d1SGreg Roach
48c2ed51d1SGreg Roach    // Which child elements can appear under this element.
49c2ed51d1SGreg Roach    protected const SUBTAGS = [];
50c2ed51d1SGreg Roach
514da96842SGreg Roach    // A label to describe this element
524da96842SGreg Roach    private string $label;
53c2ed51d1SGreg Roach
544da96842SGreg Roach    /** @var array<string,string> Subtags of this element */
554da96842SGreg Roach    private array $subtags;
56c2ed51d1SGreg Roach
57c2ed51d1SGreg Roach    /**
58c2ed51d1SGreg Roach     * AbstractGedcomElement constructor.
59c2ed51d1SGreg Roach     *
60c2ed51d1SGreg Roach     * @param string             $label
61c2ed51d1SGreg Roach     * @param array<string>|null $subtags
62c2ed51d1SGreg Roach     */
63c2ed51d1SGreg Roach    public function __construct(string $label, array $subtags = null)
64c2ed51d1SGreg Roach    {
65c2ed51d1SGreg Roach        $this->label   = $label;
66c2ed51d1SGreg Roach        $this->subtags = $subtags ?? static::SUBTAGS;
67c2ed51d1SGreg Roach    }
68c2ed51d1SGreg Roach
69c2ed51d1SGreg Roach    /**
70c2ed51d1SGreg Roach     * Convert a value to a canonical form.
71c2ed51d1SGreg Roach     *
72c2ed51d1SGreg Roach     * @param string $value
73c2ed51d1SGreg Roach     *
74c2ed51d1SGreg Roach     * @return string
75c2ed51d1SGreg Roach     */
76c2ed51d1SGreg Roach    public function canonical(string $value): string
77c2ed51d1SGreg Roach    {
78c2ed51d1SGreg Roach        $value = strtr($value, ["\t" => ' ', "\r" => ' ', "\n" => ' ']);
79c2ed51d1SGreg Roach
804da96842SGreg Roach        while (str_contains($value, '  ')) {
81c2ed51d1SGreg Roach            $value = strtr($value, ['  ' => ' ']);
82c2ed51d1SGreg Roach        }
83c2ed51d1SGreg Roach
84c2ed51d1SGreg Roach        return trim($value);
85c2ed51d1SGreg Roach    }
86c2ed51d1SGreg Roach
87c2ed51d1SGreg Roach    /**
88*9d477377SGreg Roach     * Convert a multi-line value to a canonical form.
89*9d477377SGreg Roach     *
90*9d477377SGreg Roach     * @param string $value
91*9d477377SGreg Roach     *
92*9d477377SGreg Roach     * @return string
93*9d477377SGreg Roach     */
94*9d477377SGreg Roach    protected function canonicalText(string $value): string
95*9d477377SGreg Roach    {
96*9d477377SGreg Roach        // Browsers use MS-DOS line endings in multi-line data.
97*9d477377SGreg Roach        $value = strtr($value, ["\t" => ' ', "\r\n" => "\n", "\r" => "\n"]);
98*9d477377SGreg Roach
99*9d477377SGreg Roach        // Remove trailing spaces at the end of lines.
100*9d477377SGreg Roach        $value = preg_replace('/ +\n/', "\n", $value);
101*9d477377SGreg Roach
102*9d477377SGreg Roach        // Remove leading/trailing empty lines.
103*9d477377SGreg Roach        return trim($value, "\n");
104*9d477377SGreg Roach    }
105*9d477377SGreg Roach
106*9d477377SGreg Roach    /**
107c2ed51d1SGreg Roach     * Create a default value for this element.
108c2ed51d1SGreg Roach     *
109c2ed51d1SGreg Roach     * @param Tree $tree
110c2ed51d1SGreg Roach     *
111c2ed51d1SGreg Roach     * @return string
112c2ed51d1SGreg Roach     */
113c2ed51d1SGreg Roach    public function default(Tree $tree): string
114c2ed51d1SGreg Roach    {
115c2ed51d1SGreg Roach        return '';
116c2ed51d1SGreg Roach    }
117c2ed51d1SGreg Roach
118c2ed51d1SGreg Roach    /**
119c2ed51d1SGreg Roach     * An edit control for this data.
120c2ed51d1SGreg Roach     *
121c2ed51d1SGreg Roach     * @param string $id
122c2ed51d1SGreg Roach     * @param string $name
123c2ed51d1SGreg Roach     * @param string $value
124c2ed51d1SGreg Roach     * @param Tree   $tree
125c2ed51d1SGreg Roach     *
126c2ed51d1SGreg Roach     * @return string
127c2ed51d1SGreg Roach     */
128c2ed51d1SGreg Roach    public function edit(string $id, string $name, string $value, Tree $tree): string
129c2ed51d1SGreg Roach    {
130c2ed51d1SGreg Roach        $values = $this->values();
131c2ed51d1SGreg Roach
132c2ed51d1SGreg Roach        if ($values !== []) {
133c2ed51d1SGreg Roach            $value = $this->canonical($value);
134c2ed51d1SGreg Roach
135c2ed51d1SGreg Roach            // Ensure the current data is in the list.
136c2ed51d1SGreg Roach            if (!array_key_exists($value, $values)) {
137c2ed51d1SGreg Roach                $values = [$value => $value] + $values;
138c2ed51d1SGreg Roach            }
139c2ed51d1SGreg Roach
140c2ed51d1SGreg Roach            // We may use markup to display values, but not when editing them.
1414da96842SGreg Roach            $values = array_map(fn (string $x): string => strip_tags($x), $values);
142c2ed51d1SGreg Roach
143c2ed51d1SGreg Roach            return view('components/select', [
144c2ed51d1SGreg Roach                'id'       => $id,
145c2ed51d1SGreg Roach                'name'     => $name,
146c2ed51d1SGreg Roach                'options'  => $values,
147c2ed51d1SGreg Roach                'selected' => $value,
148c2ed51d1SGreg Roach            ]);
149c2ed51d1SGreg Roach        }
150c2ed51d1SGreg Roach
151c2ed51d1SGreg Roach        $attributes = [
152c2ed51d1SGreg Roach            'class'     => 'form-control',
1539deadf1cSGreg Roach            'dir'       => 'auto',
154c2ed51d1SGreg Roach            'type'      => 'text',
155c2ed51d1SGreg Roach            'id'        => $id,
156c2ed51d1SGreg Roach            'name'      => $name,
157c2ed51d1SGreg Roach            'value'     => $value,
158ca561ce7SGreg Roach            'maxlength' => static::MAXIMUM_LENGTH,
159ae0043b7SGreg Roach            'pattern'   => static::PATTERN,
160c2ed51d1SGreg Roach        ];
161c2ed51d1SGreg Roach
1624a213054SGreg Roach        return '<input ' . Html::attributes($attributes) . ' />';
163c2ed51d1SGreg Roach    }
164c2ed51d1SGreg Roach
165c2ed51d1SGreg Roach    /**
166c2ed51d1SGreg Roach     * An edit control for this data.
167c2ed51d1SGreg Roach     *
168c2ed51d1SGreg Roach     * @param string $id
169c2ed51d1SGreg Roach     * @param string $name
170c2ed51d1SGreg Roach     * @param string $value
171c2ed51d1SGreg Roach     *
172c2ed51d1SGreg Roach     * @return string
173c2ed51d1SGreg Roach     */
174c2ed51d1SGreg Roach    public function editHidden(string $id, string $name, string $value): string
175c2ed51d1SGreg Roach    {
1764a213054SGreg Roach        return '<input class="form-control" type="hidden" id="' . e($id) . '" name="' . e($name) . '" value="' . e($value) . '" />';
177c2ed51d1SGreg Roach    }
178c2ed51d1SGreg Roach
179c2ed51d1SGreg Roach    /**
180c2ed51d1SGreg Roach     * An edit control for this data.
181c2ed51d1SGreg Roach     *
182c2ed51d1SGreg Roach     * @param string $id
183c2ed51d1SGreg Roach     * @param string $name
184c2ed51d1SGreg Roach     * @param string $value
185c2ed51d1SGreg Roach     *
186c2ed51d1SGreg Roach     * @return string
187c2ed51d1SGreg Roach     */
188c2ed51d1SGreg Roach    public function editTextArea(string $id, string $name, string $value): string
189c2ed51d1SGreg Roach    {
190c2ed51d1SGreg Roach        return '<textarea class="form-control" id="' . e($id) . '" name="' . e($name) . '" rows="5" dir="auto">' . e($value) . '</textarea>';
191c2ed51d1SGreg Roach    }
192c2ed51d1SGreg Roach
193c2ed51d1SGreg Roach    /**
194c2ed51d1SGreg Roach     * Escape @ signs in a GEDCOM export.
195c2ed51d1SGreg Roach     *
196c2ed51d1SGreg Roach     * @param string $value
197c2ed51d1SGreg Roach     *
198c2ed51d1SGreg Roach     * @return string
199c2ed51d1SGreg Roach     */
200c2ed51d1SGreg Roach    public function escape(string $value): string
201c2ed51d1SGreg Roach    {
202c2ed51d1SGreg Roach        return strtr($value, ['@' => '@@']);
203c2ed51d1SGreg Roach    }
204c2ed51d1SGreg Roach
205c2ed51d1SGreg Roach    /**
206c2ed51d1SGreg Roach     * Create a label for this element.
207c2ed51d1SGreg Roach     *
208c2ed51d1SGreg Roach     * @return string
209c2ed51d1SGreg Roach     */
210c2ed51d1SGreg Roach    public function label(): string
211c2ed51d1SGreg Roach    {
212c2ed51d1SGreg Roach        return $this->label;
213c2ed51d1SGreg Roach    }
214c2ed51d1SGreg Roach
215c2ed51d1SGreg Roach    /**
216c2ed51d1SGreg Roach     * Create a label/value pair for this element.
217c2ed51d1SGreg Roach     *
218c2ed51d1SGreg Roach     * @param string $value
219c2ed51d1SGreg Roach     * @param Tree   $tree
220c2ed51d1SGreg Roach     *
221c2ed51d1SGreg Roach     * @return string
222c2ed51d1SGreg Roach     */
223c2ed51d1SGreg Roach    public function labelValue(string $value, Tree $tree): string
224c2ed51d1SGreg Roach    {
225c2ed51d1SGreg Roach        $label = '<span class="label">' . $this->label() . '</span>';
2268da28f8eSGreg Roach        $value = '<span class="value align-top">' . $this->value($value, $tree) . '</span>';
227c2ed51d1SGreg Roach        $html  = I18N::translate(/* I18N: e.g. "Occupation: farmer" */ '%1$s: %2$s', $label, $value);
228c2ed51d1SGreg Roach
229c2ed51d1SGreg Roach        return '<div>' . $html . '</div>';
230c2ed51d1SGreg Roach    }
231c2ed51d1SGreg Roach
232c2ed51d1SGreg Roach    /**
2334dbb2a39SGreg Roach     * Set, remove or replace a subtag.
2344dbb2a39SGreg Roach     *
2354dbb2a39SGreg Roach     * @param string $subtag
2364dbb2a39SGreg Roach     * @param string $repeat
2374da96842SGreg Roach     * @param string $before
2384dbb2a39SGreg Roach     *
2394dbb2a39SGreg Roach     * @return void
2404dbb2a39SGreg Roach     */
241efd4768bSGreg Roach    public function subtag(string $subtag, string $repeat = '0:1', string $before = ''): void
2424dbb2a39SGreg Roach    {
2434dbb2a39SGreg Roach        if ($repeat === '') {
2444dbb2a39SGreg Roach            unset($this->subtags[$subtag]);
2454da96842SGreg Roach        } elseif ($before === '' || ($this->subtags[$before] ?? null) === null) {
2464dbb2a39SGreg Roach            $this->subtags[$subtag] = $repeat;
2474dbb2a39SGreg Roach        } else {
2484dbb2a39SGreg Roach            $tmp = [];
2494dbb2a39SGreg Roach
2504dbb2a39SGreg Roach            foreach ($this->subtags as $key => $value) {
2514da96842SGreg Roach                if ($key === $before) {
252efd4768bSGreg Roach                    $tmp[$subtag] = $repeat;
2534dbb2a39SGreg Roach                }
2544da96842SGreg Roach                $tmp[$key] = $value;
2554dbb2a39SGreg Roach            }
2564dbb2a39SGreg Roach
2574dbb2a39SGreg Roach            $this->subtags = $tmp;
2584dbb2a39SGreg Roach        }
2594dbb2a39SGreg Roach    }
2604dbb2a39SGreg Roach
2614dbb2a39SGreg Roach    /**
262c2ed51d1SGreg Roach     * @return array<string,string>
263c2ed51d1SGreg Roach     */
2643d2c98d1SGreg Roach    public function subtags(): array
265c2ed51d1SGreg Roach    {
266c2ed51d1SGreg Roach        return $this->subtags;
267c2ed51d1SGreg Roach    }
268c2ed51d1SGreg Roach
269c2ed51d1SGreg Roach    /**
270c2ed51d1SGreg Roach     * Display the value of this type of element.
271c2ed51d1SGreg Roach     *
272c2ed51d1SGreg Roach     * @param string $value
273c2ed51d1SGreg Roach     * @param Tree   $tree
274c2ed51d1SGreg Roach     *
275c2ed51d1SGreg Roach     * @return string
276c2ed51d1SGreg Roach     */
277c2ed51d1SGreg Roach    public function value(string $value, Tree $tree): string
278c2ed51d1SGreg Roach    {
279c2ed51d1SGreg Roach        $values = $this->values();
280c2ed51d1SGreg Roach
281c2ed51d1SGreg Roach        if ($values === []) {
282c2ed51d1SGreg Roach            if (str_contains($value, "\n")) {
283315eb316SGreg Roach                return '<bdi class="d-inline-block" style="white-space: pre-wrap;">' . e($value) . '</bdi>';
284c2ed51d1SGreg Roach            }
285c2ed51d1SGreg Roach
286315eb316SGreg Roach            return '<bdi>' . e($value) . '</bdi>';
287c2ed51d1SGreg Roach        }
288c2ed51d1SGreg Roach
289c2ed51d1SGreg Roach        $canonical = $this->canonical($value);
290c2ed51d1SGreg Roach
291315eb316SGreg Roach        return $values[$canonical] ?? '<bdi>' . e($value) . '</bdi>';
292c2ed51d1SGreg Roach    }
293c2ed51d1SGreg Roach
294c2ed51d1SGreg Roach    /**
295c2ed51d1SGreg Roach     * A list of controlled values for this element
296c2ed51d1SGreg Roach     *
297c2ed51d1SGreg Roach     * @return array<int|string,string>
298c2ed51d1SGreg Roach     */
299c2ed51d1SGreg Roach    public function values(): array
300c2ed51d1SGreg Roach    {
301c2ed51d1SGreg Roach        return [];
302c2ed51d1SGreg Roach    }
303c2ed51d1SGreg Roach
304c2ed51d1SGreg Roach    /**
305cef4fcc0SGreg Roach     * Display the value of this type of element - convert URLs to links.
306c2ed51d1SGreg Roach     *
307c2ed51d1SGreg Roach     * @param string $value
308c2ed51d1SGreg Roach     *
309c2ed51d1SGreg Roach     * @return string
310c2ed51d1SGreg Roach     */
311c2ed51d1SGreg Roach    protected function valueAutoLink(string $value): string
312c2ed51d1SGreg Roach    {
313a3287c67SGreg Roach        $canonical = $this->canonical($value);
314c2ed51d1SGreg Roach
3158392edd9SGreg Roach        if (str_contains($canonical, 'http://') || str_contains($canonical, 'https://')) {
3168392edd9SGreg Roach            $html = Registry::markdownFactory()->autolink()->convertToHtml($canonical);
3178392edd9SGreg Roach
3188392edd9SGreg Roach            return strip_tags($html, ['a']);
319a3287c67SGreg Roach        }
320c2ed51d1SGreg Roach
321a3287c67SGreg Roach        return e($canonical);
322c2ed51d1SGreg Roach    }
323c2ed51d1SGreg Roach
324c2ed51d1SGreg Roach    /**
325cef4fcc0SGreg Roach     * Display the value of this type of element - convert to URL.
326cef4fcc0SGreg Roach     *
327cef4fcc0SGreg Roach     * @param string $value
328cef4fcc0SGreg Roach     *
329cef4fcc0SGreg Roach     * @return string
330cef4fcc0SGreg Roach     */
331cef4fcc0SGreg Roach    protected function valueLink(string $value): string
332cef4fcc0SGreg Roach    {
333cef4fcc0SGreg Roach        $canonical = $this->canonical($value);
334cef4fcc0SGreg Roach
335cef4fcc0SGreg Roach        if (str_starts_with($canonical, 'https://') || str_starts_with($canonical, 'http://')) {
336cef4fcc0SGreg Roach            return '<a dir="auto" href="' . e($canonical) . '">' . e($value) . '</a>';
337cef4fcc0SGreg Roach        }
338cef4fcc0SGreg Roach
339cef4fcc0SGreg Roach        return e($value);
340cef4fcc0SGreg Roach    }
341cef4fcc0SGreg Roach
342cef4fcc0SGreg Roach    /**
343c2ed51d1SGreg Roach     * Display the value of this type of element.
344c2ed51d1SGreg Roach     *
345c2ed51d1SGreg Roach     * @param string $value
346c2ed51d1SGreg Roach     *
347c2ed51d1SGreg Roach     * @return string
348c2ed51d1SGreg Roach     */
349c2ed51d1SGreg Roach    public function valueNumeric(string $value): string
350c2ed51d1SGreg Roach    {
351c2ed51d1SGreg Roach        $canonical = $this->canonical($value);
352c2ed51d1SGreg Roach
353c2ed51d1SGreg Roach        if (is_numeric($canonical)) {
354c2ed51d1SGreg Roach            return I18N::number((int) $canonical);
355c2ed51d1SGreg Roach        }
356c2ed51d1SGreg Roach
357c2ed51d1SGreg Roach        return e($value);
358c2ed51d1SGreg Roach    }
359c2ed51d1SGreg Roach}
360