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