xref: /webtrees/app/Module/CensusAssistantModule.php (revision e3c147d0d53873311b7c137c41b4439e01d4189e)
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 (array_keys($ca_individuals) as $xref) {
144                $xref = (string) $xref;
145                if ($xref !== $individual->xref()) {
146                    Individual::getInstance($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 string[][]      $ca_individuals
161     * @param string          $ca_notes
162     *
163     * @return string
164     */
165    private function createNoteText(CensusInterface $census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes): string
166    {
167        $text = $ca_title . "\n" . $ca_citation . "\n" . $ca_place . "\n\n";
168
169        foreach ($census->columns() as $n => $column) {
170            if ($n === 0) {
171                $text .= "\n";
172            } else {
173                $text .= ' | ';
174            }
175            $text .= $column->abbreviation();
176        }
177
178        foreach ($census->columns() as $n => $column) {
179            if ($n === 0) {
180                $text .= "\n";
181            } else {
182                $text .= ' | ';
183            }
184            $text .= '-----';
185        }
186
187        foreach ($ca_individuals as $columns) {
188            $text .= "\n" . implode(' | ', $columns);
189        }
190
191        return $text . "\n\n" . $ca_notes;
192    }
193
194    /**
195     * Generate an HTML row of data for the census header
196     * Add prefix cell (store XREF and drag/drop)
197     * Add suffix cell (delete button)
198     *
199     * @param CensusInterface $census
200     *
201     * @return string
202     */
203    protected function censusTableHeader(CensusInterface $census): string
204    {
205        $html = '';
206        foreach ($census->columns() as $column) {
207            $html .= '<th class="wt-census-assistant-field" title="' . $column->title() . '">' . $column->abbreviation() . '</th>';
208        }
209
210        return '<tr class="wt-census-assistant-row"><th hidden></th>' . $html . '<th></th></tr>';
211    }
212
213    /**
214     * Generate an HTML row of data for the census
215     * Add prefix cell (store XREF and drag/drop)
216     * Add suffix cell (delete button)
217     *
218     * @param CensusInterface $census
219     *
220     * @return string
221     */
222    public function censusTableEmptyRow(CensusInterface $census): string
223    {
224        return '<tr class="wt-census-assistant-row"><td hidden></td>' . str_repeat('<td class="wt-census-assistant-field p-0"><input type="text" class="form-control wt-census-assistant-form-control p-0"></td>', count($census->columns())) . '<td><a href="#" title="' . I18N::translate('Remove') . '">' . view('icons/delete') . '</a></td></tr>';
225    }
226
227    /**
228     * Generate an HTML row of data for the census
229     * Add prefix cell (store XREF and drag/drop)
230     * Add suffix cell (delete button)
231     *
232     * @param CensusInterface $census
233     * @param Individual      $individual
234     * @param Individual      $head
235     *
236     * @return string
237     */
238    public function censusTableRow(CensusInterface $census, Individual $individual, Individual $head): string
239    {
240        $html = '';
241        foreach ($census->columns() as $column) {
242            $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[' . $individual->xref() . '][]"></td>';
243        }
244
245        return '<tr class="wt-census-assistant-row"><td class="wt-census-assistant-field" hidden>' . $individual->xref() . '</td>' . $html . '<td class="wt-census-assistant-field"><a href="#" title="' . I18N::translate('Remove') . '">' . view('icons/delete') . '</a></td></tr>';
246    }
247}
248