xref: /webtrees/app/Elements/PlaceName.php (revision b6017f990d38d8c56e04c0096ce9a7e8745ad4ba)
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    /**
39     * Convert a value to a canonical form.
40     *
41     * @param string $value
42     *
43     * @return string
44     */
45    public function canonical(string $value): string
46    {
47        $value = parent::canonical($value);
48
49        // Arabic, Chinese and Japanese commas.
50        $value = strtr($value, ['،' => ',', ',' => ',', '、' => ',']);
51
52        // Spaces before commas.
53        $value = strtr($value, [' ,' => ',']);
54
55        // Spaces after commas.
56        $value = strtr($value, [',' => ', ']);
57        $value = strtr($value, [',  ' => ', ']);
58
59        return $value;
60    }
61
62    /**
63     * An edit control for this data.
64     *
65     * @param string $id
66     * @param string $name
67     * @param string $value
68     * @param Tree   $tree
69     *
70     * @return string
71     */
72    public function edit(string $id, string $name, string $value, Tree $tree): string
73    {
74        return '<input data-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) . '">';
75    }
76}
77