xref: /webtrees/app/Elements/Census.php (revision f15544dc2c2fc10bc387ec571bf4b5c2efe7c539)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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\Census\Census as Censuses;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Individual;
25use Fisharebest\Webtrees\Module\CensusAssistantModule;
26use Fisharebest\Webtrees\Registry;
27use Fisharebest\Webtrees\Services\ModuleService;
28use Fisharebest\Webtrees\Tree;
29use Fisharebest\Webtrees\Validator;
30use Psr\Http\Message\ServerRequestInterface;
31
32use function app;
33use function assert;
34
35/**
36 * Census
37 */
38class Census extends AbstractEventElement
39{
40    protected const SUBTAGS = [
41        'TYPE'  => '0:1:?',
42        'DATE'  => '0:1',
43        'AGE'   => '0:1',
44        'PLAC'  => '0:1',
45        'ADDR'  => '0:1',
46        'EMAIL' => '0:1:?',
47        'WWW'   => '0:1:?',
48        'PHON'  => '0:1:?',
49        'FAX'   => '0:1:?',
50        'CAUS'  => '0:1:?',
51        'AGNC'  => '0:1:?',
52        'RELI'  => '0:1:?',
53        'NOTE'  => '0:M',
54        'OBJE'  => '0:M',
55        'SOUR'  => '0:M',
56        'RESN'  => '0:1',
57    ];
58
59    /**
60     * An edit control for this data.
61     *
62     * @param string $id
63     * @param string $name
64     * @param string $value
65     * @param Tree   $tree
66     *
67     * @return string
68     */
69    public function edit(string $id, string $name, string $value, Tree $tree): string
70    {
71        $html = $this->editHidden($id, $name, $value);
72
73        $html .= view('modules/GEDFact_assistant/select-census', [
74            'census_places' => Censuses::censusPlaces(I18N::languageTag()),
75        ]);
76
77        $request = app(ServerRequestInterface::class);
78        assert($request instanceof ServerRequestInterface);
79
80        $xref = Validator::attributes($request)->isXref()->string('xref', '');
81
82        $module_service = app(ModuleService::class);
83        assert($module_service instanceof ModuleService);
84
85        $census_assistant = $module_service->findByInterface(CensusAssistantModule::class)->first();
86        $record           = Registry::individualFactory()->make($xref, $tree);
87
88        if ($census_assistant instanceof CensusAssistantModule && $record instanceof Individual) {
89            $html .= $census_assistant->createCensusAssistant($record);
90        }
91
92        return $html;
93    }
94}
95