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