xref: /webtrees/app/Module/CensusAssistantModule.php (revision a6656bb5f7cff260502deaf88d26942745ca1e1e)
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;
31
32/**
33 * Class CensusAssistantModule
34 */
35class CensusAssistantModule extends AbstractModule
36{
37    /**
38     * How should this module be identified in the control panel, etc.?
39     *
40     * @return string
41     */
42    public function title(): string
43    {
44        /* I18N: Name of a module */
45        return I18N::translate('Census assistant');
46    }
47
48    /**
49     * A sentence describing what this module does.
50     *
51     * @return string
52     */
53    public function description(): string
54    {
55        /* I18N: Description of the “Census assistant” module */
56        return I18N::translate('An alternative way to enter census transcripts and link them to individuals.');
57    }
58
59    /**
60     * @param ServerRequestInterface $request
61     *
62     * @return ResponseInterface
63     */
64    public function postCensusHeaderAction(ServerRequestInterface $request): ResponseInterface
65    {
66        $params = (array) $request->getParsedBody();
67
68        $census = $params['census'];
69
70        $html = $this->censusTableHeader(new $census());
71
72        return response($html);
73    }
74
75    /**
76     * @param ServerRequestInterface $request
77     *
78     * @return ResponseInterface
79     */
80    public function postCensusIndividualAction(ServerRequestInterface $request): ResponseInterface
81    {
82        $tree = $request->getAttribute('tree');
83        assert($tree instanceof Tree);
84
85        $params     = (array) $request->getParsedBody();
86        $individual = Individual::getInstance($params['xref'], $tree);
87        $head       = Individual::getInstance($params['head'], $tree);
88        $census     = $params['census'];
89
90        if ($individual instanceof Individual && $head instanceof Individual) {
91            $html = $this->censusTableRow(new $census(), $individual, $head);
92
93            return response($html);
94        }
95
96        throw new HttpNotFoundException();
97    }
98
99    /**
100     * @param Individual $individual
101     *
102     * @return string
103     */
104    public function createCensusAssistant(Individual $individual): string
105    {
106        return view('modules/census-assistant', [
107            'individual' => $individual,
108        ]);
109    }
110
111    /**
112     * @param ServerRequestInterface $request
113     * @param Individual             $individual
114     * @param string                 $fact_id
115     * @param string                 $newged
116     * @param bool                   $keep_chan
117     *
118     * @return string
119     */
120    public function updateCensusAssistant(ServerRequestInterface $request, Individual $individual, string $fact_id, string $newged, bool $keep_chan): string
121    {
122        $params = (array) $request->getParsedBody();
123
124        $ca_title       = $params['ca_title'] ?? '';
125        $ca_place       = $params['ca_place'] ?? '';
126        $ca_citation    = $params['ca_citation'] ?? '';
127        $ca_individuals = $params['ca_individuals'] ?? [];
128        $ca_notes       = $params['ca_notes'] ?? '';
129        $ca_census      = $params['ca_census'] ?? '';
130
131        if ($ca_census !== '' && $ca_individuals !== []) {
132            $census = new $ca_census();
133
134            $note_text   = $this->createNoteText($census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes);
135            $note_gedcom = '0 @@ NOTE ' . str_replace("\n", "\n1 CONT ", $note_text);
136            $note        = $individual->tree()->createRecord($note_gedcom);
137
138            $newged .= "\n2 NOTE @" . $note->xref() . '@';
139
140            // Add the census fact to the rest of the household
141            foreach (array_keys($ca_individuals) as $xref) {
142                if ($xref !== $individual->xref()) {
143                    Individual::getInstance($xref, $individual->tree())
144                        ->updateFact($fact_id, $newged, !$keep_chan);
145                }
146            }
147        }
148
149        return $newged;
150    }
151
152    /**
153     * @param CensusInterface $census
154     * @param string          $ca_title
155     * @param string          $ca_place
156     * @param string          $ca_citation
157     * @param string[][]      $ca_individuals
158     * @param string          $ca_notes
159     *
160     * @return string
161     */
162    private function createNoteText(CensusInterface $census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes): string
163    {
164        $text = $ca_title . "\n" . $ca_citation . "\n" . $ca_place . "\n\n";
165
166        foreach ($census->columns() as $n => $column) {
167            if ($n === 0) {
168                $text .= "\n";
169            } else {
170                $text .= ' | ';
171            }
172            $text .= $column->abbreviation();
173        }
174
175        foreach ($census->columns() as $n => $column) {
176            if ($n === 0) {
177                $text .= "\n";
178            } else {
179                $text .= ' | ';
180            }
181            $text .= '-----';
182        }
183
184        foreach ($ca_individuals as $xref => $columns) {
185            $text .= "\n" . implode(' | ', $columns);
186        }
187
188        return $text . "\n\n" . $ca_notes;
189    }
190
191    /**
192     * Generate an HTML row of data for the census header
193     * Add prefix cell (store XREF and drag/drop)
194     * Add suffix cell (delete button)
195     *
196     * @param CensusInterface $census
197     *
198     * @return string
199     */
200    protected function censusTableHeader(CensusInterface $census): string
201    {
202        $html = '';
203        foreach ($census->columns() as $column) {
204            $html .= '<th class="wt-census-assistant-field" title="' . $column->title() . '">' . $column->abbreviation() . '</th>';
205        }
206
207        return '<tr class="wt-census-assistant-row"><th hidden></th>' . $html . '<th></th></tr>';
208    }
209
210    /**
211     * Generate an HTML row of data for the census
212     * Add prefix cell (store XREF and drag/drop)
213     * Add suffix cell (delete button)
214     *
215     * @param CensusInterface $census
216     *
217     * @return string
218     */
219    public function censusTableEmptyRow(CensusInterface $census): string
220    {
221        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>';
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     * @param Individual      $individual
231     * @param Individual      $head
232     *
233     * @return string
234     */
235    public function censusTableRow(CensusInterface $census, Individual $individual, Individual $head): string
236    {
237        $html = '';
238        foreach ($census->columns() as $column) {
239            $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>';
240        }
241
242        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>';
243    }
244}
245