GedcomWriter::getNote()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace FamilyTree365\LaravelGedcom\Utils;
4
5
use FamilyTree365\LaravelGedcom\Events\GedComProgressSent;
6
use FamilyTree365\LaravelGedcom\Models\Family;
7
use FamilyTree365\LaravelGedcom\Models\MediaObject;
8
use FamilyTree365\LaravelGedcom\Models\Note;
9
use FamilyTree365\LaravelGedcom\Models\Person;
10
use FamilyTree365\LaravelGedcom\Models\Repository;
11
use FamilyTree365\LaravelGedcom\Models\Source;
12
use FamilyTree365\LaravelGedcom\Models\Subm;
13
use FamilyTree365\LaravelGedcom\Models\Subn;
14
use Gedcom\Parser;
15
use Illuminate\Console\OutputStyle;
0 ignored issues
show
Bug introduced by
The type Illuminate\Console\OutputStyle was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Symfony\Component\Console\Input\StringInput;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\StringInput was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Symfony\Component\Console\Output\StreamOutput;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Output\StreamOutput was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
class GedcomWriter
20
{
21
    /**
22
     * Array of persons ID
23
     * key - old GEDCOM ID
24
     * value - new autoincrement ID.
25
     *
26
     * @var string
27
     */
28
    protected $persons_id = [];
29
30
    public function parse(string $filename, string $slug, bool $progressBar = false)
31
    {
32
        $parser = new Parser();
33
        $gedcom = @$parser->parse($filename);
34
35
        /**
36
         * work.
37
         */
38
        $subn = $gedcom->getSubn();
39
        $subm = $gedcom->getSubm();
40
        $sour = $gedcom->getSour();
41
        $note = $gedcom->getNote();
42
        $repo = $gedcom->getRepo();
43
        $obje = $gedcom->getObje();
44
45
        /**
46
         * work end.
47
         */
48
        $c_subn = 0;
49
        $c_subm = count($subm);
50
        $c_sour = count($sour);
51
        $c_note = count($note);
52
        $c_repo = count($repo);
53
        $c_obje = count($obje);
54
        if ($subn != null) {
55
            //
56
            $c_subn = 1;
57
        }
58
59
        $individuals = $gedcom->getIndi();
60
        $families = $gedcom->getFam();
61
        $total = count($individuals) + count($families) + $c_subn + $c_subm + $c_sour + $c_note + $c_repo + $c_obje;
62
        $complete = 0;
63
        if ($progressBar === true) {
64
            $bar = $this->getProgressBar(count($individuals) + count($families));
65
            event(new GedComProgressSent($slug, $total, $complete));
0 ignored issues
show
Bug introduced by
The function event was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
            /** @scrutinizer ignore-call */ 
66
            event(new GedComProgressSent($slug, $total, $complete));
Loading history...
66
        }
67
68
        if ($subn != null) {
69
            // store the submission information for the GEDCOM file.
70
            $this->getSubn($subn);
71
            if ($progressBar === true) {
72
                $bar->advance();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $bar does not seem to be defined for all execution paths leading up to this point.
Loading history...
73
                $complete++;
74
                event(new GedComProgressSent($slug, $total, $complete));
75
            }
76
        }
77
78
        // store information about all the submitters to the GEDCOM file.
79
        foreach ($subm as $item) {
80
            $this->getSubm($item);
81
            if ($progressBar === true) {
82
                $bar->advance();
83
                $complete++;
84
                event(new GedComProgressSent($slug, $total, $complete));
85
            }
86
        }
87
88
        // store sources cited throughout the GEDCOM file.
89
        foreach ($sour as $item) {
90
            $this->getSour($item);
91
            if ($progressBar === true) {
92
                $bar->advance();
93
                $complete++;
94
                event(new GedComProgressSent($slug, $total, $complete));
95
            }
96
        }
97
98
        // store all the notes contained within the GEDCOM file that are not inline.
99
        foreach ($note as $item) {
100
            $this->getNote($item);
101
            if ($progressBar === true) {
102
                $bar->advance();
103
                $complete++;
104
                event(new GedComProgressSent($slug, $total, $complete));
105
            }
106
        }
107
108
        // store all repositories that are contained within the GEDCOM file and referenced by sources.
109
        foreach ($repo as $item) {
110
            $this->getRepo($item);
111
            if ($progressBar === true) {
112
                $bar->advance();
113
                $complete++;
114
                event(new GedComProgressSent($slug, $total, $complete));
115
            }
116
        }
117
        // store all the media objects that are contained within the GEDCOM file.
118
        foreach ($obje as $item) {
119
            $this->getObje($item);
120
            if ($progressBar === true) {
121
                $bar->advance();
122
                $complete++;
123
                event(new GedComProgressSent($slug, $total, $complete));
124
            }
125
        }
126
127
        foreach ($individuals as $individual) {
128
            $this->getPerson($individual);
129
            if ($progressBar === true) {
130
                $bar->advance();
131
                $complete++;
132
                event(new GedComProgressSent($slug, $total, $complete));
133
            }
134
        }
135
136
        foreach ($families as $family) {
137
            $this->getFamily($family);
138
            if ($progressBar === true) {
139
                $bar->advance();
140
                $complete++;
141
                event(new GedComProgressSent($slug, $total, $complete));
142
            }
143
        }
144
145
        if ($progressBar === true) {
146
            $bar->finish();
147
        }
148
    }
149
150
    private function getProgressBar(int $max)
151
    {
152
        return (new OutputStyle(
153
            new StringInput(''),
154
            new StreamOutput(fopen('php://stdout', 'w'))
155
        ))->createProgressBar($max);
156
    }
157
158
    private function getDate($input_date)
159
    {
160
        return "$input_date";
161
    }
162
163
    private function getPlace($place)
164
    {
165
        if (is_object($place)) {
166
            $place = $place->getPlac();
167
        }
168
169
        return $place;
170
    }
171
172
    private function getPerson($individual)
173
    {
174
        $g_id = $individual->getId();
175
        $name = '';
176
        $givn = '';
177
        $surn = '';
178
179
        if (!empty($individual->getName())) {
180
            $surn = current($individual->getName())->getSurn();
181
            $givn = current($individual->getName())->getGivn();
182
            $name = current($individual->getName())->getName();
183
        }
184
185
        // string value
186
        $uid = $individual->getUid();
187
        $chan = $individual->getChan();
188
        $rin = $individual->getRin();
189
        $resn = $individual->getResn();
190
        $rfn = $individual->getRfn();
191
        $afn = $individual->getAfn();
192
193
        $sex = preg_replace('/[^MF]/', '', $individual->getSex());
194
        $attr = $individual->getAllAttr();
195
        $events = $individual->getAllEven();
196
197
        if ($givn == '') {
198
            $givn = $name;
199
        }
200
        $person = Person::query()->updateOrCreate(compact('name', 'givn', 'surn', 'sex'), compact('name', 'givn', 'surn', 'sex', 'uid', 'chan', 'rin', 'resn', 'rfn', 'afn'));
201
        $this->persons_id[$g_id] = $person->id;
202
203
        if ($events !== null) {
204
            foreach ($events as $event) {
205
                $date = $this->getDate($event->getDate());
206
                $place = $this->getPlace($event->getPlac());
207
                $person->addEvent($event->getType(), $date, $place);
208
            }
209
        }
210
211
        if ($attr !== null) {
212
            foreach ($attr as $event) {
213
                $date = $this->getDate($event->getDate());
214
                $place = $this->getPlace($event->getPlac());
215
                if (count($event->getNote()) > 0) {
216
                    $note = current($event->getNote())->getNote();
217
                } else {
218
                    $note = '';
219
                }
220
                $person->addEvent($event->getType(), $date, $place, $event->getAttr().' '.$note);
221
            }
222
        }
223
    }
224
225
    private function getFamily($family)
226
    {
227
        $husb = $family->getHusb();
228
        $wife = $family->getWife();
229
230
        // string
231
        $chan = $family->getChan();
232
        $nchi = $family->getNchi();
233
234
        $description = null;
235
        $type_id = 0;
236
        $children = $family->getChil();
237
        $events = $family->getAllEven();
238
239
        $husband_id = (isset($this->persons_id[$husb])) ? $this->persons_id[$husb] : 0;
240
        $wife_id = (isset($this->persons_id[$wife])) ? $this->persons_id[$wife] : 0;
241
242
        $family = Family::query()->updateOrCreate(compact('husband_id', 'wife_id'), compact('husband_id', 'wife_id', 'description', 'type_id', 'chan', 'nchi'));
243
244
        if ($children !== null) {
245
            foreach ($children as $child) {
246
                if (isset($this->persons_id[$child])) {
247
                    $person = Person::query()->find($this->persons_id[$child]);
248
                    $person->child_in_family_id = $family->id;
249
                    $person->save();
250
                }
251
            }
252
        }
253
254
        if ($events !== null) {
255
            foreach ($events as $event) {
256
                $date = $this->getDate($event->getDate());
257
                $place = $this->getPlace($event->getPlac());
258
                $family->addEvent($event->getType(), $date, $place);
259
            }
260
        }
261
    }
262
263
    private function getSubn($subn)
264
    {
265
        $subm = $subn->getSubm();
266
        $famf = $subn->getFamf();
267
        $temp = $subn->getTemp();
268
        $ance = $subn->getAnce();
269
        $desc = $subn->getDesc();
270
        $ordi = $subn->getOrdi();
271
        $rin = $subn->getRin();
272
        Subn::query()->updateOrCreate(compact('subm', 'famf', 'temp', 'ance', 'desc', 'ordi', 'rin'), compact('subm', 'famf', 'temp', 'ance', 'desc', 'ordi', 'rin'));
273
    }
274
275
    // insert subm data to model
276
    private function getSubm($_subm)
277
    {
278
        $subm = $_subm->getSubm() ?? 'Unknown'; // string
279
        $chan = $_subm->getChan() ?? ['Unknown']; // Record\Chan---
0 ignored issues
show
Unused Code introduced by
The assignment to $chan is dead and can be removed.
Loading history...
280
        $name = $_subm->getName() ?? 'Unknown'; // string
281
        if ($_subm->getAddr() != null) { // Record/Addr
282
         $addr = $_subm->getAddr();
283
            $addr->getAddr() ?? 'Unknown';
284
            $addr->getAdr1() ?? 'Unknown';
285
            $addr->getAdr2() ?? 'Unknown';
286
            $addr->getCity() ?? 'Unknown';
287
            $addr->getStae() ?? 'Unknown';
288
            $addr->getCtry() ?? 'Unknown';
289
        } else {
290
            $addr = null;
291
        }
292
293
        $rin = $_subm->getRin() ?? 'Unknown'; // string
294
        $rfn = $_subm->getRfn() ?? 'Unknown'; // string
295
        $_lang = $_subm->getLang() ?? ['Unknown']; // array
296
        $_phon = $_subm->getPhon() ?? ['Unknown']; // array
297
        $obje = $_subm->getObje() ?? ['Unknown']; // array ---
0 ignored issues
show
Unused Code introduced by
The assignment to $obje is dead and can be removed.
Loading history...
298
299
        // create chan model - id, ref_type (subm), date, time
300
        // create note model - id, ref_type ( chan ), note
301
        // create sour model - id, ref_type ( note), sour, Chan, titl, auth, data, text, publ, Repo, abbr, rin, refn_a, Note_a, Obje_a
302
        // $arr_chan = array('date'=>$chan->getDate(), 'time'=>$chan->getTime());
303
        // create obje model - id, _isRef, _obje, _form, _titl, _file, _Note_a
304
305
        if ($addr != null) {
306
            $arr_addr = [
307
                'addr' => $addr->getAddr() ?? 'Unknown',
308
                'adr1' => $addr->getAdr1() ?? 'Unknown',
309
                'adr2' => $addr->getAdr2() ?? 'Unknown',
310
                'city' => $addr->getCity() ?? 'Unknown',
311
                'stae' => $addr->getStae() ?? 'Unknown',
312
                'ctry' => $addr->getCtry() ?? 'Unknown',
313
            ];
314
        } else {
315
            $arr_addr = [
316
                'addr' => 'Unknown',
317
                'adr1' => 'Unknown',
318
                'adr2' => 'Unknown',
319
                'city' => 'Unknown',
320
                'stae' => 'Unknown',
321
                'ctry' => 'Unknown',
322
            ];
323
        }
324
325
        $addr = json_encode($arr_addr);
326
        $lang = json_encode($_lang);
327
        $arr_phon = [];
328
        foreach ($_phon as $item) {
329
            $__phon = $item->getPhon();
330
            array_push($arr_phon, $__phon);
331
        }
332
        $phon = json_encode($arr_phon);
333
        Subm::query()->updateOrCreate(compact('subm', 'name', 'addr', 'rin', 'rfn', 'lang', 'phon'), compact('subm', 'name', 'addr', 'rin', 'rfn', 'lang', 'phon'));
334
    }
335
336
    // insert sour data to database
337
    private function getSour($_sour)
338
    {
339
        $sour = $_sour->getSour(); // string
340
        $titl = $_sour->getTitl(); // string
341
        $auth = $_sour->getAuth(); // string
342
        $data = $_sour->getData(); // string
343
        $text = $_sour->getText(); // string
344
        $publ = $_sour->getPubl(); // string
345
        $abbr = $_sour->getAbbr(); // string
346
        Source::query()->updateOrCreate(compact('sour', 'titl', 'auth', 'data', 'text', 'publ', 'abbr'), compact('sour', 'titl', 'auth', 'data', 'text', 'publ', 'abbr'));
347
    }
348
349
    // insert note data to database
350
    private function getNote($_note)
351
    {
352
        $gid = $_note->getId(); // string
353
        $note = $_note->getNote(); // string
354
        $rin = $_note->getRin(); // string
355
        Note::query()->updateOrCreate(compact('gid', 'note', 'rin'), compact('gid', 'note', 'rin'));
356
    }
357
358
    // insert repo data to database
359
    private function getRepo($_repo)
360
    {
361
        $repo = $_repo->getRepo(); // string
362
        $name = $_repo->getName(); // string
363
        $_addr = $_repo->getAddr(); // Record/Addr
364
        $rin = $_repo->getRin(); // string
365
        $_phon = $_repo->getPhon(); // array
366
        $arr_addr = [
367
            'addr' => $_addr->getAddr(),
368
            'adr1' => $_addr->getAdr1(),
369
            'adr2' => $_addr->getAdr2(),
370
            'city' => $_addr->getCity(),
371
            'stae' => $_addr->getStae(),
372
            'ctry' => $_addr->getCtry(),
373
        ];
374
        $addr = json_encode($arr_addr);
375
        $arr_phon = [];
376
        foreach ($_phon as $item) {
377
            $__phon = $item->getPhon();
378
            array_push($arr_phon, $__phon);
379
        }
380
        $phon = json_encode($arr_phon);
381
        Repository::query()->updateOrCreate(compact('repo', 'name', 'addr', 'rin', 'phon'), compact('repo', 'name', 'addr', 'rin', 'phon'));
382
    }
383
384
    // insert obje data to database
385
    private function getObje($_obje)
386
    {
387
        $gid = $_obje->getId(); // string
388
        $_form = $_obje->getForm(); // string
0 ignored issues
show
Unused Code introduced by
The assignment to $_form is dead and can be removed.
Loading history...
389
        $_titl = $_obje->getTitl(); // string
0 ignored issues
show
Unused Code introduced by
The assignment to $_titl is dead and can be removed.
Loading history...
390
        $_blob = $_obje->getBlob(); // string
0 ignored issues
show
Unused Code introduced by
The assignment to $_blob is dead and can be removed.
Loading history...
391
        $_rin = $_obje->getRin(); // string
0 ignored issues
show
Unused Code introduced by
The assignment to $_rin is dead and can be removed.
Loading history...
392
        $_chan = $_obje->getChan(); // Chan
0 ignored issues
show
Unused Code introduced by
The assignment to $_chan is dead and can be removed.
Loading history...
393
        $_file = $_obje->getFile(); // string
0 ignored issues
show
Unused Code introduced by
The assignment to $_file is dead and can be removed.
Loading history...
394
        MediaObject::updateOrCreate(compact('gid', 'form', 'titl', 'blob', 'rin', 'file'), compact('gid', 'form', 'titl', 'blob', 'rin', 'file'));
395
    }
396
}
397