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