Passed
Push — master ( 5eca9d...3cc4e3 )
by Greg
06:01
created

CensusAssistantModule::createNoteText()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 6
nop 6
dl 0
loc 19
rs 9.9666
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
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\Tree;
26
use Psr\Http\Message\ResponseInterface;
27
use Psr\Http\Message\ServerRequestInterface;
28
29
use function array_keys;
30
use function assert;
31
use function count;
32
use function e;
33
use function response;
34
use function str_repeat;
35
use function str_replace;
36
use function view;
37
38
/**
39
 * Class CensusAssistantModule
40
 */
41
class 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
        if ($individual instanceof Individual && $head instanceof Individual) {
98
            $html = $this->censusTableRow($census, $individual, $head);
99
        } else {
100
            $html = $this->censusTableEmptyRow($census);
101
        }
102
103
        return response($html);
104
    }
105
106
    /**
107
     * @param Individual $individual
108
     *
109
     * @return string
110
     */
111
    public function createCensusAssistant(Individual $individual): string
112
    {
113
        return view('modules/census-assistant', [
114
            'individual' => $individual,
115
        ]);
116
    }
117
118
    /**
119
     * @param ServerRequestInterface $request
120
     * @param Individual             $individual
121
     * @param string                 $fact_id
122
     * @param string                 $newged
123
     * @param bool                   $keep_chan
124
     *
125
     * @return string
126
     */
127
    public function updateCensusAssistant(ServerRequestInterface $request, Individual $individual, string $fact_id, string $newged, bool $keep_chan): string
128
    {
129
        $params = (array) $request->getParsedBody();
130
131
        $ca_title       = $params['ca_title'] ?? '';
132
        $ca_place       = $params['ca_place'] ?? '';
133
        $ca_citation    = $params['ca_citation'] ?? '';
134
        $ca_individuals = $params['ca_individuals'] ?? [];
135
        $ca_notes       = $params['ca_notes'] ?? '';
136
        $ca_census      = $params['ca_census'] ?? '';
137
138
        if ($ca_census !== '' && $ca_individuals !== []) {
139
            $census = new $ca_census();
140
141
            $note_text   = $this->createNoteText($census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes);
142
            $note_gedcom = '0 @@ NOTE ' . str_replace("\n", "\n1 CONT ", $note_text);
143
            $note        = $individual->tree()->createRecord($note_gedcom);
144
145
            $newged .= "\n2 NOTE @" . $note->xref() . '@';
146
147
            // Add the census fact to the rest of the household
148
            foreach ($ca_individuals['xref'] ?? [] as $xref) {
149
                if ($xref !== '' && $xref !== $individual->xref()) {
150
                    Individual::getInstance($xref, $individual->tree())
151
                        ->updateFact($fact_id, $newged, !$keep_chan);
152
                }
153
            }
154
        }
155
156
        return $newged;
157
    }
158
159
    /**
160
     * @param CensusInterface $census
161
     * @param string          $ca_title
162
     * @param string          $ca_place
163
     * @param string          $ca_citation
164
     * @param string[][]      $ca_individuals
165
     * @param string          $ca_notes
166
     *
167
     * @return string
168
     */
169
    private function createNoteText(CensusInterface $census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes): string
170
    {
171
        $text = $ca_title . "\n" . $ca_citation . "\n" . $ca_place . "\n\n|";
172
173
        foreach ($census->columns() as $column) {
174
            $text .= ' ' . $column->abbreviation() . ' |';
175
        }
176
177
        $text .= "\n|" . str_repeat(' ----- |', count($census->columns()));
178
179
        foreach (array_keys($ca_individuals['xref'] ?? []) as $key) {
180
            $text .= "\n|";
181
182
            foreach ($census->columns() as $n => $column) {
183
                $text .= ' ' . $ca_individuals[$n][$key] . ' |';
184
            }
185
        }
186
187
        return $text . "\n\n" . $ca_notes;
188
    }
189
190
    /**
191
     * Generate an HTML row of data for the census header
192
     * Add prefix cell (store XREF and drag/drop)
193
     * Add suffix cell (delete button)
194
     *
195
     * @param CensusInterface $census
196
     *
197
     * @return string
198
     */
199
    protected function censusTableHeader(CensusInterface $census): string
200
    {
201
        $html = '';
202
        foreach ($census->columns() as $column) {
203
            $html .= '<th class="wt-census-assistant-field" title="' . $column->title() . '">' . $column->abbreviation() . '</th>';
204
        }
205
206
        return '<tr class="wt-census-assistant-row"><th hidden></th>' . $html . '<th></th></tr>';
207
    }
208
209
    /**
210
     * Generate an HTML row of data for the census
211
     * Add prefix cell (store XREF and drag/drop)
212
     * Add suffix cell (delete button)
213
     *
214
     * @param CensusInterface $census
215
     *
216
     * @return string
217
     */
218
    public function censusTableEmptyRow(CensusInterface $census): string
219
    {
220
        $html = '<td class="wt-census-assistant-field" hidden><input type="hidden" name="ca_individuals[xref][]"></td>';
221
222
        foreach ($census->columns() as $n => $column) {
223
            $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>';
224
        }
225
226
        $html .= '<td class="wt-census-assistant-field"><a href="#" title="' . I18N::translate('Remove') . '">' . view('icons/delete') . '</a></td>';
227
228
        return '<tr class="wt-census-assistant-row">' . $html . '</tr>';
229
    }
230
231
    /**
232
     * Generate an HTML row of data for the census
233
     * Add prefix cell (store XREF and drag/drop)
234
     * Add suffix cell (delete button)
235
     *
236
     * @param CensusInterface $census
237
     * @param Individual      $individual
238
     * @param Individual      $head
239
     *
240
     * @return string
241
     */
242
    public function censusTableRow(CensusInterface $census, Individual $individual, Individual $head): string
243
    {
244
        $html = '<td class="wt-census-assistant-field" hidden><input type="hidden" name="ca_individuals[xref][]" value="' . e($individual->xref()) . '"></td>';
245
246
        foreach ($census->columns() as $n => $column) {
247
            $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>';
248
        }
249
250
        $html .= '<td class="wt-census-assistant-field"><a href="#" title="' . I18N::translate('Remove') . '">' . view('icons/delete') . '</a></td>';
251
252
        return '<tr class="wt-census-assistant-row">' . $html . '</tr>';
253
    }
254
}
255