Passed
Pull Request — main (#4115)
by David
05:43
created

CensusAssistantModule::censusPartToList()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 2
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2021 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 <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Module;
21
22
use Fisharebest\Webtrees\Census\CensusInterface;
23
use Fisharebest\Webtrees\I18N;
24
use Fisharebest\Webtrees\Individual;
25
use Fisharebest\Webtrees\Registry;
26
use Fisharebest\Webtrees\Tree;
27
use Psr\Http\Message\ResponseInterface;
28
use Psr\Http\Message\ServerRequestInterface;
29
30
use function array_keys;
31
use function assert;
32
use function count;
33
use function e;
34
use function response;
35
use function str_repeat;
36
use function str_replace;
37
use function view;
38
39
/**
40
 * Class CensusAssistantModule
41
 */
42
class CensusAssistantModule extends AbstractModule
43
{
44
    /**
45
     * How should this module be identified in the control panel, etc.?
46
     *
47
     * @return string
48
     */
49
    public function title(): string
50
    {
51
        /* I18N: Name of a module */
52
        return I18N::translate('Census assistant');
53
    }
54
55
    /**
56
     * A sentence describing what this module does.
57
     *
58
     * @return string
59
     */
60
    public function description(): string
61
    {
62
        /* I18N: Description of the “Census assistant” module */
63
        return I18N::translate('An alternative way to enter census transcripts and link them to individuals.');
64
    }
65
66
    /**
67
     * @param ServerRequestInterface $request
68
     *
69
     * @return ResponseInterface
70
     */
71
    public function postCensusHeaderAction(ServerRequestInterface $request): ResponseInterface
72
    {
73
        $params = (array) $request->getParsedBody();
74
75
        $census = $params['census'];
76
77
        $html = $this->censusTableHeader(new $census());
78
79
        return response($html);
80
    }
81
82
    /**
83
     * @param ServerRequestInterface $request
84
     *
85
     * @return ResponseInterface
86
     */
87
    public function postCensusIndividualAction(ServerRequestInterface $request): ResponseInterface
88
    {
89
        $tree = $request->getAttribute('tree');
90
        assert($tree instanceof Tree);
91
92
        $params       = (array) $request->getParsedBody();
93
        $individual   = Registry::individualFactory()->make($params['xref'], $tree);
94
        $head         = Registry::individualFactory()->make($params['head'], $tree);
95
        $census_class = $params['census'];
96
        /** @var CensusInterface $census */
97
        $census       = new $census_class();
98
99
        // No head of household?  Create a fake one.
100
        $head = $head ?? Registry::individualFactory()->new('X', '0 @X@ INDI', null, $tree);
101
102
        // Generate columns (e.g. relationship name) using the correct language.
103
        I18N::init($census->censusLanguage());
104
105
        if ($individual instanceof Individual && $head instanceof Individual) {
106
            $html = $this->censusTableRow($census, $individual, $head);
107
        } else {
108
            $html = $this->censusTableEmptyRow($census);
109
        }
110
111
        return response($html);
112
    }
113
114
    /**
115
     * @param Individual $individual
116
     *
117
     * @return string
118
     */
119
    public function createCensusAssistant(Individual $individual): string
120
    {
121
        return view('modules/census-assistant', [
122
            'individual' => $individual,
123
        ]);
124
    }
125
126
    /**
127
     * @param ServerRequestInterface $request
128
     * @param Individual             $individual
129
     * @param string                 $fact_id
130
     * @param string                 $newged
131
     * @param bool                   $keep_chan
132
     *
133
     * @return string
134
     */
135
    public function updateCensusAssistant(ServerRequestInterface $request, Individual $individual, string $fact_id, string $newged, bool $keep_chan): string
136
    {
137
        $params = (array) $request->getParsedBody();
138
139
        $ca_title       = $params['ca_title'] ?? '';
140
        $ca_place       = $params['ca_place'] ?? '';
141
        $ca_citation    = $params['ca_citation'] ?? '';
142
        $ca_individuals = $params['ca_individuals'] ?? [];
143
        $ca_notes       = $params['ca_notes'] ?? '';
144
        $ca_census      = $params['ca_census'] ?? '';
145
146
        if ($ca_census !== '' && $ca_individuals !== []) {
147
            /** @var CensusInterface $census */
148
            $census = new $ca_census();
149
150
            $note_text   = $this->createNoteText($census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes);
151
            $note_gedcom = '0 @@ NOTE ' . str_replace("\n", "\n1 CONT ", $note_text);
152
            $note        = $individual->tree()->createRecord($note_gedcom);
153
154
            $newged .= "\n2 NOTE @" . $note->xref() . '@';
155
156
            // Add the census fact to the rest of the household
157
            foreach ($ca_individuals['xref'] ?? [] as $xref) {
158
                if ($xref !== '' && $xref !== $individual->xref()) {
159
                    Registry::individualFactory()->make($xref, $individual->tree())
160
                        ->updateFact($fact_id, $newged, !$keep_chan);
161
                }
162
            }
163
        }
164
165
        return $newged;
166
    }
167
168
    /**
169
     * @param CensusInterface      $census
170
     * @param string               $ca_title
171
     * @param string               $ca_place
172
     * @param string               $ca_citation
173
     * @param array<array<string>> $ca_individuals
174
     * @param string               $ca_notes
175
     *
176
     * @return string
177
     */
178
    private function createNoteText(CensusInterface $census, string $ca_title, string $ca_place, string $ca_citation, array $ca_individuals, string $ca_notes): string
179
    {
180
        $text = $this->censusPartToList($ca_title);
181
        $text .= $this->censusPartToList($ca_citation);
182
        $text .= $this->censusPartToList($ca_place);
183
184
        $text .= "\n\n|";
185
186
        foreach ($census->columns() as $column) {
187
            $text .= ' ' . $column->abbreviation() . ' |';
188
        }
189
190
        $text .= "\n|" . str_repeat(' ----- |', count($census->columns()));
191
192
        foreach (array_keys($ca_individuals['xref'] ?? []) as $key) {
193
            $text .= "\n|";
194
195
            foreach ($census->columns() as $n => $column) {
196
                $text .= ' ' . $ca_individuals[$n][$key] . ' |';
197
            }
198
        }
199
200
        $text .= $this->censusPartToList($ca_notes);
201
202
        return $text;
203
    }
204
205
    /**
206
     * Make each paragraph a markdown list element
207
     *
208
     * @param string $census_part
209
     * @return string
210
     */
211
    private function censusPartToList($census_part)
212
    {
213
        $lines = preg_split('/\r?\n/', $census_part, -1, PREG_SPLIT_NO_EMPTY);
214
        $text  = '';
215
        if ($lines !== false && count($lines) > 0) {
216
            foreach ($lines as $line) {
217
                $text .= "\n+ " . $line;
218
            }
219
        }
220
        return $text;
221
    }
222
223
    /**
224
     * Generate an HTML row of data for the census header
225
     * Add prefix cell (store XREF and drag/drop)
226
     * Add suffix cell (delete button)
227
     *
228
     * @param CensusInterface $census
229
     *
230
     * @return string
231
     */
232
    protected function censusTableHeader(CensusInterface $census): string
233
    {
234
        $html = '';
235
        foreach ($census->columns() as $column) {
236
            $html .= '<th class="wt-census-assistant-field" title="' . $column->title() . '">' . $column->abbreviation() . '</th>';
237
        }
238
239
        return '<tr class="wt-census-assistant-row"><th hidden></th>' . $html . '<th></th></tr>';
240
    }
241
242
    /**
243
     * Generate an HTML row of data for the census
244
     * Add prefix cell (store XREF and drag/drop)
245
     * Add suffix cell (delete button)
246
     *
247
     * @param CensusInterface $census
248
     *
249
     * @return string
250
     */
251
    public function censusTableEmptyRow(CensusInterface $census): string
252
    {
253
        $html = '<td class="wt-census-assistant-field" hidden><input type="hidden" name="ca_individuals[xref][]"></td>';
254
255
        foreach ($census->columns() as $n => $column) {
256
            $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>';
257
        }
258
259
        $html .= '<td class="wt-census-assistant-field"><a href="#" title="' . I18N::translate('Remove') . '">' . view('icons/delete') . '</a></td>';
260
261
        return '<tr class="wt-census-assistant-row">' . $html . '</tr>';
262
    }
263
264
    /**
265
     * Generate an HTML row of data for the census
266
     * Add prefix cell (store XREF and drag/drop)
267
     * Add suffix cell (delete button)
268
     *
269
     * @param CensusInterface $census
270
     * @param Individual      $individual
271
     * @param Individual      $head
272
     *
273
     * @return string
274
     */
275
    public function censusTableRow(CensusInterface $census, Individual $individual, Individual $head): string
276
    {
277
        $html = '<td class="wt-census-assistant-field" hidden><input type="hidden" name="ca_individuals[xref][]" value="' . e($individual->xref()) . '"></td>';
278
279
        foreach ($census->columns() as $n => $column) {
280
            $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>';
281
        }
282
283
        $html .= '<td class="wt-census-assistant-field"><a href="#" title="' . I18N::translate('Remove') . '">' . view('icons/delete') . '</a></td>';
284
285
        return '<tr class="wt-census-assistant-row">' . $html . '</tr>';
286
    }
287
}
288