xref: /webtrees/app/Module/CensusAssistantModule.php (revision 68136c4f6e10efb8b69e797562a7efe2d96fde70)
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\I18N;
24use Fisharebest\Webtrees\Individual;
25use Fisharebest\Webtrees\Tree;
26use Psr\Http\Message\ResponseInterface;
27use Psr\Http\Message\ServerRequestInterface;
28
29use function array_keys;
30use function assert;
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        $params = (array) $request->getParsedBody();
73
74        $census = $params['census'];
75
76        $html = $this->censusTableHeader(new $census());
77
78        return response($html);
79    }
80
81    /**
82     * @param ServerRequestInterface $request
83     *
84     * @return ResponseInterface
85     */
86    public function postCensusIndividualAction(ServerRequestInterface $request): ResponseInterface
87    {
88        $tree = $request->getAttribute('tree');
89        assert($tree instanceof Tree);
90
91        $params       = (array) $request->getParsedBody();
92        $individual   = Individual::getInstance($params['xref'], $tree);
93        $head         = Individual::getInstance($params['head'], $tree);
94        $census_class = $params['census'];
95        $census       = new $census_class();
96
97        // No head of household?  Create a dummy one.
98        $head = $head ?? new Individual('X', '0 @X@ INDI', null, $tree);
99
100        // Generate columns (e.g. relationship name) using the correct language.
101        I18N::init($census->censusLanguage());
102
103        if ($individual instanceof Individual && $head instanceof Individual) {
104            $html = $this->censusTableRow($census, $individual, $head);
105        } else {
106            $html = $this->censusTableEmptyRow($census);
107        }
108
109        return response($html);
110    }
111
112    /**
113     * @param Individual $individual
114     *
115     * @return string
116     */
117    public function createCensusAssistant(Individual $individual): string
118    {
119        return view('modules/census-assistant', [
120            'individual' => $individual,
121        ]);
122    }
123
124    /**
125     * @param ServerRequestInterface $request
126     * @param Individual             $individual
127     * @param string                 $fact_id
128     * @param string                 $newged
129     * @param bool                   $keep_chan
130     *
131     * @return string
132     */
133    public function updateCensusAssistant(ServerRequestInterface $request, Individual $individual, string $fact_id, string $newged, bool $keep_chan): string
134    {
135        $params = (array) $request->getParsedBody();
136
137        $ca_title       = $params['ca_title'] ?? '';
138        $ca_place       = $params['ca_place'] ?? '';
139        $ca_citation    = $params['ca_citation'] ?? '';
140        $ca_individuals = $params['ca_individuals'] ?? [];
141        $ca_notes       = $params['ca_notes'] ?? '';
142        $ca_census      = $params['ca_census'] ?? '';
143
144        if ($ca_census !== '' && $ca_individuals !== []) {
145            $census = new $ca_census();
146
147            $note_text   = $this->createNoteText($census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes);
148            $note_gedcom = '0 @@ NOTE ' . str_replace("\n", "\n1 CONT ", $note_text);
149            $note        = $individual->tree()->createRecord($note_gedcom);
150
151            $newged .= "\n2 NOTE @" . $note->xref() . '@';
152
153            // Add the census fact to the rest of the household
154            foreach ($ca_individuals['xref'] ?? [] as $xref) {
155                if ($xref !== '' && $xref !== $individual->xref()) {
156                    Individual::getInstance($xref, $individual->tree())
157                        ->updateFact($fact_id, $newged, !$keep_chan);
158                }
159            }
160        }
161
162        return $newged;
163    }
164
165    /**
166     * @param CensusInterface $census
167     * @param string          $ca_title
168     * @param string          $ca_place
169     * @param string          $ca_citation
170     * @param string[][]      $ca_individuals
171     * @param string          $ca_notes
172     *
173     * @return string
174     */
175    private function createNoteText(CensusInterface $census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes): string
176    {
177        $text = $ca_title . "\n" . $ca_citation . "\n" . $ca_place . "\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        return $text . "\n\n" . $ca_notes;
194    }
195
196    /**
197     * Generate an HTML row of data for the census header
198     * Add prefix cell (store XREF and drag/drop)
199     * Add suffix cell (delete button)
200     *
201     * @param CensusInterface $census
202     *
203     * @return string
204     */
205    protected function censusTableHeader(CensusInterface $census): string
206    {
207        $html = '';
208        foreach ($census->columns() as $column) {
209            $html .= '<th class="wt-census-assistant-field" title="' . $column->title() . '">' . $column->abbreviation() . '</th>';
210        }
211
212        return '<tr class="wt-census-assistant-row"><th hidden></th>' . $html . '<th></th></tr>';
213    }
214
215    /**
216     * Generate an HTML row of data for the census
217     * Add prefix cell (store XREF and drag/drop)
218     * Add suffix cell (delete button)
219     *
220     * @param CensusInterface $census
221     *
222     * @return string
223     */
224    public function censusTableEmptyRow(CensusInterface $census): string
225    {
226        $html = '<td class="wt-census-assistant-field" hidden><input type="hidden" name="ca_individuals[xref][]"></td>';
227
228        foreach ($census->columns() as $n => $column) {
229            $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>';
230        }
231
232        $html .= '<td class="wt-census-assistant-field"><a href="#" title="' . I18N::translate('Remove') . '">' . view('icons/delete') . '</a></td>';
233
234        return '<tr class="wt-census-assistant-row">' . $html . '</tr>';
235    }
236
237    /**
238     * Generate an HTML row of data for the census
239     * Add prefix cell (store XREF and drag/drop)
240     * Add suffix cell (delete button)
241     *
242     * @param CensusInterface $census
243     * @param Individual      $individual
244     * @param Individual      $head
245     *
246     * @return string
247     */
248    public function censusTableRow(CensusInterface $census, Individual $individual, Individual $head): string
249    {
250        $html = '<td class="wt-census-assistant-field" hidden><input type="hidden" name="ca_individuals[xref][]" value="' . e($individual->xref()) . '"></td>';
251
252        foreach ($census->columns() as $n => $column) {
253            $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>';
254        }
255
256        $html .= '<td class="wt-census-assistant-field"><a href="#" title="' . I18N::translate('Remove') . '">' . view('icons/delete') . '</a></td>';
257
258        return '<tr class="wt-census-assistant-row">' . $html . '</tr>';
259    }
260}
261