xref: /webtrees/app/Module/CensusAssistantModule.php (revision 9f1f531b76be8d725214b03940bdee3674b55e88)
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        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        $params = (array) $request->getParsedBody();
133
134        $ca_title       = $params['ca_title'] ?? '';
135        $ca_place       = $params['ca_place'] ?? '';
136        $ca_citation    = $params['ca_citation'] ?? '';
137        $ca_individuals = $params['ca_individuals'] ?? [];
138        $ca_notes       = $params['ca_notes'] ?? '';
139        $ca_census      = $params['ca_census'] ?? '';
140
141        if ($ca_census !== '' && $ca_individuals !== []) {
142            $census = new $ca_census();
143
144            $note_text   = $this->createNoteText($census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes);
145            $note_gedcom = '0 @@ NOTE ' . str_replace("\n", "\n1 CONT ", $note_text);
146            $note        = $individual->tree()->createRecord($note_gedcom);
147
148            $newged .= "\n2 NOTE @" . $note->xref() . '@';
149
150            // Add the census fact to the rest of the household
151            foreach ($ca_individuals['xref'] ?? [] as $xref) {
152                if ($xref !== '' && $xref !== $individual->xref()) {
153                    Individual::getInstance($xref, $individual->tree())
154                        ->updateFact($fact_id, $newged, !$keep_chan);
155                }
156            }
157        }
158
159        return $newged;
160    }
161
162    /**
163     * @param CensusInterface $census
164     * @param string          $ca_title
165     * @param string          $ca_place
166     * @param string          $ca_citation
167     * @param string[][]      $ca_individuals
168     * @param string          $ca_notes
169     *
170     * @return string
171     */
172    private function createNoteText(CensusInterface $census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes): string
173    {
174        $text = $ca_title . "\n" . $ca_citation . "\n" . $ca_place . "\n\n|";
175
176        foreach ($census->columns() as $column) {
177            $text .= ' ' . $column->abbreviation() . ' |';
178        }
179
180        $text .= "\n|" . str_repeat(' ----- |', count($census->columns()));
181
182        foreach (array_keys($ca_individuals['xref'] ?? []) as $key) {
183            $text .= "\n|";
184
185            foreach ($census->columns() as $n => $column) {
186                $text .= ' ' . $ca_individuals[$n][$key] . ' |';
187            }
188        }
189
190        return $text . "\n\n" . $ca_notes;
191    }
192
193    /**
194     * Generate an HTML row of data for the census header
195     * Add prefix cell (store XREF and drag/drop)
196     * Add suffix cell (delete button)
197     *
198     * @param CensusInterface $census
199     *
200     * @return string
201     */
202    protected function censusTableHeader(CensusInterface $census): string
203    {
204        $html = '';
205        foreach ($census->columns() as $column) {
206            $html .= '<th class="wt-census-assistant-field" title="' . $column->title() . '">' . $column->abbreviation() . '</th>';
207        }
208
209        return '<tr class="wt-census-assistant-row"><th hidden></th>' . $html . '<th></th></tr>';
210    }
211
212    /**
213     * Generate an HTML row of data for the census
214     * Add prefix cell (store XREF and drag/drop)
215     * Add suffix cell (delete button)
216     *
217     * @param CensusInterface $census
218     *
219     * @return string
220     */
221    public function censusTableEmptyRow(CensusInterface $census): string
222    {
223        $html = '<td class="wt-census-assistant-field" hidden><input type="hidden" name="ca_individuals[xref][]"></td>';
224
225        foreach ($census->columns() as $n => $column) {
226            $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>';
227        }
228
229        $html .= '<td class="wt-census-assistant-field"><a href="#" title="' . I18N::translate('Remove') . '">' . view('icons/delete') . '</a></td>';
230
231        return '<tr class="wt-census-assistant-row">' . $html . '</tr>';
232    }
233
234    /**
235     * Generate an HTML row of data for the census
236     * Add prefix cell (store XREF and drag/drop)
237     * Add suffix cell (delete button)
238     *
239     * @param CensusInterface $census
240     * @param Individual      $individual
241     * @param Individual      $head
242     *
243     * @return string
244     */
245    public function censusTableRow(CensusInterface $census, Individual $individual, Individual $head): string
246    {
247        $html = '<td class="wt-census-assistant-field" hidden><input type="hidden" name="ca_individuals[xref][]" value="' . e($individual->xref()) . '"></td>';
248
249        foreach ($census->columns() as $n => $column) {
250            $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>';
251        }
252
253        $html .= '<td class="wt-census-assistant-field"><a href="#" title="' . I18N::translate('Remove') . '">' . view('icons/delete') . '</a></td>';
254
255        return '<tr class="wt-census-assistant-row">' . $html . '</tr>';
256    }
257}
258