xref: /webtrees/app/Elements/PlaceName.php (revision b5505f697291435abadf92d9c68555144f816161)
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\Http\RequestHandlers\AutoCompletePlace;
23use Fisharebest\Webtrees\Tree;
24
25use function e;
26use function route;
27use function trim;
28
29/**
30 * PLACE_NAME := {1,120}
31 * [ <PLACE_TEXT> | <PLACE_TEXT>, <PLACE_NAME> ]
32 * The jurisdictional name of the place where the event took place. Jurisdictions are separated by
33 * commas, for example, "Cove, Cache, Utah, USA." If the actual jurisdictional names of these
34 * places have been identified, they can be shown using a PLAC.FORM structure either in the HEADER
35 * or in the event structure. (See <PLACE_HIERARCHY>, page 58.)
36 */
37class PlaceName extends AbstractElement
38{
39    protected const SUBTAGS = [
40        'FORM' => '0:1',
41        'MAP' => '0:1',
42        'FONE' => '0:1',
43        'ROMN' => '0:1',
44        'NOTE' => '0:M',
45    ];
46
47    /**
48     * Convert a value to a canonical form.
49     *
50     * @param string $value
51     *
52     * @return string
53     */
54    public function canonical(string $value): string
55    {
56        $value = parent::canonical($value);
57
58        // Arabic, Chinese and Japanese commas.
59        $value = strtr($value, ['،' => ',', ',' => ',', '、' => ',']);
60
61        // Spaces before commas.
62        $value = strtr($value, [' ,' => ',']);
63
64        // Spaces after commas.
65        $value = strtr($value, [',' => ', ']);
66        $value = strtr($value, [',  ' => ', ']);
67
68        return trim($value);
69    }
70
71    /**
72     * Should we collapse the children of this element when editing?
73     *
74     * @return bool
75     */
76    public function collapseChildren(): bool
77    {
78        return true;
79    }
80
81    /**
82     * An edit control for this data.
83     *
84     * @param string $id
85     * @param string $name
86     * @param string $value
87     * @param Tree   $tree
88     *
89     * @return string
90     */
91    public function edit(string $id, string $name, string $value, Tree $tree): string
92    {
93        return '<input data-wt-autocomplete-url="' . e(route(AutoCompletePlace::class, ['tree' => $tree->name()])) . '" autocomplete="off" class="form-control" type="text" id="' . e($id) . '" name="' . e($name) . '" value="' . e($value) . '" />';
94    }
95}
96