xref: /webtrees/app/Elements/Census.php (revision a6081838ed8b5fd0ed499200d63e496112f9f536)
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\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 AbstractElement
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        'CAUS' => '0:1:?',
47        'AGNC' => '0:1:?',
48        'RELI' => '0:1:?',
49        'NOTE' => '0:M',
50        'OBJE' => '0:M',
51        'SOUR' => '0:M',
52        'RESN' => '0:1',
53    ];
54
55    /**
56     * An edit control for this data.
57     *
58     * @param string $id
59     * @param string $name
60     * @param string $value
61     * @param Tree   $tree
62     *
63     * @return string
64     */
65    public function edit(string $id, string $name, string $value, Tree $tree): string
66    {
67        $html = $this->editHidden($id, $name, $value);
68
69        $html .= view('modules/GEDFact_assistant/select-census', [
70            'census_places' => Censuses::censusPlaces(I18N::languageTag()),
71        ]);
72
73        $request = app(ServerRequestInterface::class);
74        assert($request instanceof ServerRequestInterface);
75
76        $xref = Validator::attributes($request)->isXref()->string('xref', '');
77
78        $module_service = app(ModuleService::class);
79        assert($module_service instanceof ModuleService);
80
81        $census_assistant = $module_service->findByInterface(CensusAssistantModule::class)->first();
82        $record           = Registry::individualFactory()->make($xref, $tree);
83
84        if ($census_assistant instanceof CensusAssistantModule && $record instanceof Individual) {
85            $html .= $census_assistant->createCensusAssistant($record);
86        }
87
88        return $html;
89    }
90}
91