xref: /webtrees/app/Module/CensusAssistantModule.php (revision 94582e90d3cfca0ccb8293b56de3ad502f9f0f70)
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\Module;
21
22use Fisharebest\Webtrees\Census\CensusInterface;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Individual;
25use Fisharebest\Webtrees\Registry;
26use Fisharebest\Webtrees\Validator;
27use Psr\Http\Message\ResponseInterface;
28use Psr\Http\Message\ServerRequestInterface;
29
30use function array_keys;
31use function count;
32use function e;
33use function response;
34use function str_repeat;
35use function str_replace;
36use function view;
37
38/**
39 * Class CensusAssistantModule
40 */
41class CensusAssistantModule extends AbstractModule
42{
43    /**
44     * How should this module be identified in the control panel, etc.?
45     *
46     * @return string
47     */
48    public function title(): string
49    {
50        /* I18N: Name of a module */
51        return I18N::translate('Census assistant');
52    }
53
54    public function description(): string
55    {
56        /* I18N: Description of the “Census assistant” module */
57        return I18N::translate('An alternative way to enter census transcripts and link them to individuals.');
58    }
59
60    /**
61     * @param ServerRequestInterface $request
62     *
63     * @return ResponseInterface
64     */
65    public function postCensusHeaderAction(ServerRequestInterface $request): ResponseInterface
66    {
67        $census_class = Validator::parsedBody($request)->string('census');
68
69        $html = $this->censusTableHeader(new $census_class());
70
71        return response($html);
72    }
73
74    /**
75     * @param ServerRequestInterface $request
76     *
77     * @return ResponseInterface
78     */
79    public function postCensusIndividualAction(ServerRequestInterface $request): ResponseInterface
80    {
81        $tree         = Validator::attributes($request)->tree();
82        $indi_xref    = Validator::parsedBody($request)->isXref()->string('xref', '');
83        $head_xref    = Validator::parsedBody($request)->isXref()->string('head', '');
84        $individual   = Registry::individualFactory()->make($indi_xref, $tree);
85        $head         = Registry::individualFactory()->make($head_xref, $tree);
86        $census_class = Validator::parsedBody($request)->string('census');
87        $census       = new $census_class();
88
89        // No head of household?  Create a fake one.
90        $head ??= Registry::individualFactory()->new('X', '0 @X@ INDI', null, $tree);
91
92        // Generate columns (e.g. relationship name) using the correct language.
93        I18N::init($census->censusLanguage());
94
95        if ($individual instanceof Individual && $head instanceof Individual) {
96            $html = $this->censusTableRow($census, $individual, $head);
97        } else {
98            $html = $this->censusTableEmptyRow($census);
99        }
100
101        return response($html);
102    }
103
104    /**
105     * @param Individual $individual
106     *
107     * @return string
108     */
109    public function createCensusAssistant(Individual $individual): string
110    {
111        return view('modules/census-assistant', [
112            'individual' => $individual,
113        ]);
114    }
115
116    /**
117     * @param ServerRequestInterface $request
118     * @param Individual             $individual
119     * @param string                 $fact_id
120     * @param string                 $newged
121     * @param bool                   $keep_chan
122     *
123     * @return string
124     */
125    public function updateCensusAssistant(ServerRequestInterface $request, Individual $individual, string $fact_id, string $newged, bool $keep_chan): string
126    {
127        $ca_title       = Validator::parsedBody($request)->string('ca_title');
128        $ca_place       = Validator::parsedBody($request)->string('ca_place');
129        $ca_citation    = Validator::parsedBody($request)->string('ca_citation');
130        $ca_individuals = Validator::parsedBody($request)->array('ca_individuals');
131        $ca_notes       = Validator::parsedBody($request)->string('ca_notes');
132        $ca_census      = Validator::parsedBody($request)->string('ca_census');
133
134        if ($ca_census !== '' && $ca_individuals !== []) {
135            $census = new $ca_census();
136
137            $note_text   = $this->createNoteText($census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes);
138            $note_gedcom = '0 @@ NOTE ' . str_replace("\n", "\n1 CONT ", $note_text);
139            $note        = $individual->tree()->createRecord($note_gedcom);
140
141            $newged .= "\n2 NOTE @" . $note->xref() . '@';
142
143            // Add the census fact to the rest of the household
144            foreach ($ca_individuals['xref'] ?? [] as $xref) {
145                if ($xref !== '' && $xref !== $individual->xref()) {
146                    Registry::individualFactory()->make($xref, $individual->tree())
147                        ->updateFact($fact_id, $newged, !$keep_chan);
148                }
149            }
150        }
151
152        return $newged;
153    }
154
155    /**
156     * @param CensusInterface      $census
157     * @param string               $ca_title
158     * @param string               $ca_place
159     * @param string               $ca_citation
160     * @param array<array<string>> $ca_individuals
161     * @param string               $ca_notes
162     *
163     * @return string
164     */
165    private function createNoteText(CensusInterface $census, string $ca_title, string $ca_place, string $ca_citation, array $ca_individuals, string $ca_notes): string
166    {
167        $text = $ca_title;
168
169        if ($ca_citation !== '') {
170            $text .= "\n" . $ca_citation;
171        }
172
173        if ($ca_place !== '') {
174            $text .= "\n" . $ca_place;
175        }
176
177        $text .= "\n\n|";
178
179        foreach ($census->columns() as $column) {
180            $text .= ' ' . $column->abbreviation() . ' |';
181        }
182
183        $text .= "\n|" . str_repeat(' ----- |', count($census->columns()));
184
185        foreach (array_keys($ca_individuals['xref'] ?? []) as $key) {
186            $text .= "\n|";
187
188            foreach ($census->columns() as $n => $column) {
189                $text .= ' ' . $ca_individuals[$n][$key] . ' |';
190            }
191        }
192
193        if ($ca_notes !== '') {
194            $text .= "\n\n" . strtr($ca_notes, ["\r" => '']);
195        }
196
197        return $text;
198    }
199
200    /**
201     * Generate an HTML row of data for the census header
202     * Add prefix cell (store XREF and drag/drop)
203     * Add suffix cell (delete button)
204     *
205     * @param CensusInterface $census
206     *
207     * @return string
208     */
209    protected function censusTableHeader(CensusInterface $census): string
210    {
211        $html = '';
212        foreach ($census->columns() as $column) {
213            $html .= '<th class="wt-census-assistant-field" title="' . $column->title() . '">' . $column->abbreviation() . '</th>';
214        }
215
216        return '<tr class="wt-census-assistant-row"><th hidden></th>' . $html . '<th></th></tr>';
217    }
218
219    /**
220     * Generate an HTML row of data for the census
221     * Add prefix cell (store XREF and drag/drop)
222     * Add suffix cell (delete button)
223     *
224     * @param CensusInterface $census
225     *
226     * @return string
227     */
228    public function censusTableEmptyRow(CensusInterface $census): string
229    {
230        $html = '<td class="wt-census-assistant-field" hidden><input type="hidden" name="ca_individuals[xref][]"></td>';
231
232        foreach ($census->columns() as $n => $column) {
233            $html .= '<td class="wt-census-assistant-field p-0"><input class="form-control wt-census-assistant-form-control p-0" type="text" name="ca_individuals[' . $n . '][]"></td>';
234        }
235
236        $html .= '<td class="wt-census-assistant-field"><a href="#" title="' . I18N::translate('Remove') . '">' . view('icons/delete') . '</a></td>';
237
238        return '<tr class="wt-census-assistant-row">' . $html . '</tr>';
239    }
240
241    /**
242     * Generate an HTML row of data for the census
243     * Add prefix cell (store XREF and drag/drop)
244     * Add suffix cell (delete button)
245     *
246     * @param CensusInterface $census
247     * @param Individual      $individual
248     * @param Individual      $head
249     *
250     * @return string
251     */
252    public function censusTableRow(CensusInterface $census, Individual $individual, Individual $head): string
253    {
254        $html = '<td class="wt-census-assistant-field" hidden><input type="hidden" name="ca_individuals[xref][]" value="' . e($individual->xref()) . '"></td>';
255
256        foreach ($census->columns() as $n => $column) {
257            $html .= '<td class="wt-census-assistant-field p-0"><input class="form-control wt-census-assistant-form-control p-0" type="text" value="' . $column->generate($individual, $head) . '" name="ca_individuals[' . $n . '][]"></td>';
258        }
259
260        $html .= '<td class="wt-census-assistant-field"><a href="#" title="' . I18N::translate('Remove') . '">' . view('icons/delete') . '</a></td>';
261
262        return '<tr class="wt-census-assistant-row">' . $html . '</tr>';
263    }
264}
265