xref: /webtrees/app/Module/CensusAssistantModule.php (revision ff7d8543881296197e75932d82c1b0443f8306ad)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\Census\CensusInterface;
23use Fisharebest\Webtrees\Exceptions\HttpNotFoundException;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Individual;
26use Fisharebest\Webtrees\Tree;
27use Psr\Http\Message\ResponseInterface;
28use Psr\Http\Message\ServerRequestInterface;
29
30use function assert;
31use function response;
32
33/**
34 * Class CensusAssistantModule
35 */
36class CensusAssistantModule extends AbstractModule
37{
38    /**
39     * How should this module be identified in the control panel, etc.?
40     *
41     * @return string
42     */
43    public function title(): string
44    {
45        /* I18N: Name of a module */
46        return I18N::translate('Census assistant');
47    }
48
49    /**
50     * A sentence describing what this module does.
51     *
52     * @return string
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        $params = (array) $request->getParsedBody();
68
69        $census = $params['census'];
70
71        $html = $this->censusTableHeader(new $census());
72
73        return response($html);
74    }
75
76    /**
77     * @param ServerRequestInterface $request
78     *
79     * @return ResponseInterface
80     */
81    public function postCensusIndividualAction(ServerRequestInterface $request): ResponseInterface
82    {
83        $tree = $request->getAttribute('tree');
84        assert($tree instanceof Tree);
85
86        $params       = (array) $request->getParsedBody();
87        $individual   = Individual::getInstance($params['xref'], $tree);
88        $head         = Individual::getInstance($params['head'], $tree);
89        $census_class = $params['census'];
90        $census       = new $census_class();
91
92        if ($individual instanceof Individual && $head instanceof Individual) {
93            $html = $this->censusTableRow($census, $individual, $head);
94        } else {
95            $html = $this->censusTableEmptyRow($census);
96        }
97
98        return response($html);
99    }
100
101    /**
102     * @param Individual $individual
103     *
104     * @return string
105     */
106    public function createCensusAssistant(Individual $individual): string
107    {
108        return view('modules/census-assistant', [
109            'individual' => $individual,
110        ]);
111    }
112
113    /**
114     * @param ServerRequestInterface $request
115     * @param Individual             $individual
116     * @param string                 $fact_id
117     * @param string                 $newged
118     * @param bool                   $keep_chan
119     *
120     * @return string
121     */
122    public function updateCensusAssistant(ServerRequestInterface $request, Individual $individual, string $fact_id, string $newged, bool $keep_chan): string
123    {
124        $params = (array) $request->getParsedBody();
125
126        $ca_title       = $params['ca_title'] ?? '';
127        $ca_place       = $params['ca_place'] ?? '';
128        $ca_citation    = $params['ca_citation'] ?? '';
129        $ca_individuals = $params['ca_individuals'] ?? [];
130        $ca_notes       = $params['ca_notes'] ?? '';
131        $ca_census      = $params['ca_census'] ?? '';
132
133        if ($ca_census !== '' && $ca_individuals !== []) {
134            $census = new $ca_census();
135
136            $note_text   = $this->createNoteText($census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes);
137            $note_gedcom = '0 @@ NOTE ' . str_replace("\n", "\n1 CONT ", $note_text);
138            $note        = $individual->tree()->createRecord($note_gedcom);
139
140            $newged .= "\n2 NOTE @" . $note->xref() . '@';
141
142            // Add the census fact to the rest of the household
143            foreach ($ca_individuals['xref'] ?? [] as $xref) {
144                if ($xref !== '' && $xref !== $individual->xref()) {
145                    Individual::getInstance($xref, $individual->tree())
146                        ->updateFact($fact_id, $newged, !$keep_chan);
147                }
148            }
149        }
150
151        return $newged;
152    }
153
154    /**
155     * @param CensusInterface $census
156     * @param string          $ca_title
157     * @param string          $ca_place
158     * @param string          $ca_citation
159     * @param string[][]      $ca_individuals
160     * @param string          $ca_notes
161     *
162     * @return string
163     */
164    private function createNoteText(CensusInterface $census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes): string
165    {
166        $text = $ca_title . "\n" . $ca_citation . "\n" . $ca_place . "\n\n";
167
168        foreach ($census->columns() as $n => $column) {
169            if ($n === 0) {
170                $text .= "\n";
171            } else {
172                $text .= ' | ';
173            }
174            $text .= $column->abbreviation();
175        }
176
177        foreach ($census->columns() as $n => $column) {
178            if ($n === 0) {
179                $text .= "\n";
180            } else {
181                $text .= ' | ';
182            }
183            $text .= '-----';
184        }
185
186        foreach (array_keys($ca_individuals['xref'] ?? []) as $key) {
187            foreach ($census->columns() as $n => $column) {
188                if ($n === 0) {
189                    $text .= "\n";
190                } else {
191                    $text .= ' | ';
192                }
193                $text .= $ca_individuals[$n][$key];
194            }
195        }
196
197        return $text . "\n\n" . $ca_notes;
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