GedcomGenerator::setSubn()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace FamilyTree365\LaravelGedcom\Utils;
4
5
use Carbon\Carbon;
0 ignored issues
show
Bug introduced by
The type Carbon\Carbon 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...
6
use FamilyTree365\LaravelGedcom\Models\Family;
7
use FamilyTree365\LaravelGedcom\Models\Person;
8
use FamilyTree365\LaravelGedcom\Models\PersonEvent;
9
use FamilyTree365\LaravelGedcom\Models\Source;
10
use Gedcom\Gedcom;
11
use Gedcom\Record\Fam;
12
use Gedcom\Record\Fam\Even;
13
use Gedcom\Record\Fam\Slgs;
14
use Gedcom\Record\Head;
15
use Gedcom\Record\Head\Sour;
16
use Gedcom\Record\Indi;
17
use Gedcom\Record\Indi\Even as Personal;
18
use Gedcom\Record\Indi\Fams;
19
use Gedcom\Record\Indi\Name;
20
use Gedcom\Record\NoteRef;
21
use Gedcom\Record\ObjeRef;
22
use Gedcom\Record\SourRef;
23
use Gedcom\Record\Subm;
24
use Gedcom\Writer;
25
26
class GedcomGenerator
27
{
28
    protected $family_id;
29
    protected $p_id;
30
    protected $up_nest;
31
    protected $down_nest;
32
    protected $arr_indi_id = [];
33
    protected $arr_fam_id = [];
34
    protected $_gedcom = null;
35
    protected $log = "\n";
36
37
    /**
38
     * Constructor with family_id.
39
     *
40
     * @param int $p_id
41
     * @param int $family_id
42
     * @param int $up_nest
43
     * @param int $down_nest
44
     */
45
    public function __construct($p_id = 0, $family_id = 0, $up_nest = 0, $down_nest = 0)
46
    {
47
        $this->family_id = $family_id;
48
        $this->p_id = $p_id;
49
        $this->up_nest = $up_nest;
50
        $this->down_nest = $down_nest;
51
        $this->arr_indi_id = [];
52
        $this->arr_fam_id = [];
53
        $this->_gedcom = new Gedcom();
54
    }
55
56
    public function getGedcomFamily()
57
    {
58
        $this->setHead();
59
        $writer = new Writer();
60
61
        return $writer->convert($this->_gedcom);
62
    }
63
64
    public function getGedcomPerson()
65
    {
66
        $this->setHead();
67
        $this->addUpData($this->p_id);
68
        $writer = new Writer();
69
70
        return $writer->convert($this->_gedcom);
71
    }
72
73
    public function addUpData($p_id, $nest = 0)
74
    {
75
//        if (empty($p_id) || $p_id < 1) {
76
//            return;
77
//        }
78
79
        if ($this->up_nest < $nest) {
80
            return;
81
        }
82
83
        /* $person = Person::query()->find($p_id);
84
        if ($person == null) {
85
            return;
86
        } */
87
        $persons = Person::query()->get();
88
        if ($persons == null) {
89
            return;
90
        }
91
        foreach ($persons as $key => $person) {
92
            $this->setIndi($person->id);
93
        }
94
95
        // add self to indi
96
        /* if (!in_array($p_id, $this->arr_indi_id)) {
97
            array_push($this->arr_indi_id, $p_id);
98
            $this->setIndi($p_id);
99
        } else {
100
            // already processed this person
101
            return;
102
        } */
103
104
        // process family ( partner, children )
105
        $_families = $p_id
106
            ? Family::query()->where('husband_id', $p_id)->orwhere('wife_id', $p_id)->get()
107
            : Family::all();
108
109
        foreach ($_families as $item) {
110
            // add family
111
            $f_id = $item->id;
112
            if (!in_array($f_id, $this->arr_fam_id)) {
113
                array_push($this->arr_fam_id, $f_id);
114
                $this->setFam($f_id);
115
            }
116
117
            // add partner to indi
118
            $husb_id = $item->husband_id;
119
            $wife_id = $item->wife_id;
120
            $this->log .= $nest.' f_id='.$f_id."\n";
121
            $this->log .= $nest.' husb_id='.$husb_id."\n";
122
            $this->log .= $nest.' wife_id='.$wife_id."\n";
123
//            $this->addUpData($husb_id, $nest);
124
//            $this->addUpData($wife_id, $nest);
125
126
            // add chidrent to indi
127
            $children = Person::query()->where('child_in_family_id', $f_id)->get();
128
            foreach ($children as $item2) {
129
                $child_id = $item2->id;
130
                if (!in_array($child_id, $this->arr_indi_id)) {
131
                    array_push($this->arr_indi_id, $child_id);
132
                    $this->setIndi($child_id);
133
                }
134
            }
135
        }
136
137
        $this->setSour();
138
139
        /* $parent_family_id = $person->child_in_family_id;
140
        $p_family = Family::query()->find($parent_family_id);
141
142
        // there is not parent data.
143
        if ($p_family === null) {
144
            return;
145
        }
146
147
        // process siblings
148
        $siblings = Person::query()->where('child_in_family_id', $parent_family_id)->get();
149
        foreach ($siblings as $item3) {
150
            $sibling_id = $item3->id;
151
            if (!in_array($sibling_id, $this->arr_indi_id)) {
152
                array_push($this->arr_indi_id, $sibling_id);
153
                $this->setIndi($sibling_id);
154
            }
155
        }
156
157
        // process parent
158
        $nest++;
159
        $father_id = $p_family->husband_id;
160
        $mother_id = $p_family->wife_id;
161
        $this->addUpData($father_id, $nest);
162
        $this->addUpData($mother_id, $nest); */
163
    }
164
165
    public function addDownData($p_id, $nest = 0)
166
    {
167
        if (empty($p_id) || $p_id < 1) {
168
            return;
169
        }
170
        if ($this->down_nest < $nest) {
171
            return;
172
        }
173
174
        $person = Person::query()->find($p_id);
175
        if ($person == null) {
176
            return;
177
        }
178
179
        // process self
180
        if (!in_array($p_id, $this->arr_indi_id)) {
181
            // add to indi array
182
            array_push($this->arr_indi_id, $p_id);
183
            $this->setIndi($p_id);
184
        }
185
186
        $_families = Family::query()->where('husband_id', $p_id)->orwhere('wife_id', $p_id)->get();
187
        foreach ($_families as $item) {
188
            // add family
189
            $f_id = $item->id;
190
            if (!in_array($f_id, $this->arr_fam_id)) {
191
                array_push($this->arr_fam_id, $f_id);
192
                $this->setFam($f_id);
193
            }
194
            // process partner
195
            $husband_id = $item->husband_id;
196
            $wife_id = $item->wife_id;
197
            $this->addDownData($husband_id, $nest);
198
            $this->addDownData($wife_id, $nest);
199
200
            // process child
201
            $children = Person::query()->where('child_in_family_id', $item->id);
202
            foreach ($children as $item2) {
203
                $child_id = $item2->id;
204
                $nest_next = $nest + 1;
205
                $this->addDownData($child_id, $nest_next);
206
            }
207
        }
208
209
        // process parent
210
        $parent_family_id = $person->child_in_family_id;
211
        $parent_family = Family::query()->find($parent_family_id);
212
        if ($parent_family != null) {
213
            $father_id = $parent_family->husband_id;
214
            $mother_id = $parent_family->wife_id;
215
            if (!in_array($father_id, $this->arr_indi_id)) {
216
                array_push($this->arr_indi_id, $father_id);
217
                $this->setIndi($father_id);
218
            }
219
            if (!in_array($mother_id, $this->arr_indi_id)) {
220
                array_push($this->arr_indi_id, $mother_id);
221
                $this->setIndi($mother_id);
222
            }
223
        }
224
        // process siblings
225
        $siblings = Person::query()->where('child_in_family_id', $parent_family_id)->get();
226
        foreach ($siblings as $item3) {
227
            $sibling_id = $item3->id;
228
            if (!in_array($sibling_id, $this->arr_indi_id)) {
229
                $this->addDownData($sibling_id, $nest);
230
            }
231
        }
232
    }
233
234
    protected function setHead()
235
    {
236
        $head = new Head();
237
        /**
238
         * @var Sour
239
         */
240
        $sour = new Sour();
241
        $sour->setSour(env('APP_NAME', ''));
0 ignored issues
show
Bug introduced by
The function env 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

241
        $sour->setSour(/** @scrutinizer ignore-call */ env('APP_NAME', ''));
Loading history...
242
        $sour->setVersion('1.0');
243
        $head->setSour($sour);
244
        /**
245
         * @var string
246
         */
247
        $dest = null;
248
        $head->setDest($dest);
249
        /**
250
         * @var Head\Date
251
         */
252
        $date = null;
253
        $head->setDate($date);
254
        /**
255
         * @var string
256
         */
257
        $subm = null;
258
        $head->setSubm($subm);
259
        /**
260
         * @var string
261
         */
262
        $subn = null;
263
        $head->setSubn($subn);
264
        /**
265
         * @var string
266
         */
267
        $file = null;
268
        $head->setFile($file);
269
        /**
270
         * @var string
271
         */
272
        $copr = null;
273
        $head->setCopr($copr);
274
        /**
275
         * @var Head\Gedc
276
         */
277
        $gedc = null;
278
        $head->setGedc($gedc);
279
        /**
280
         * @var Head\Char
281
         */
282
        $char = null;
283
        $head->setChar($char);
284
        /**
285
         * @var string
286
         */
287
        $lang = null;
288
        $head->setLang($lang);
289
        /**
290
         * @var Head\Plac
291
         */
292
        $plac = null;
293
        $head->setPlac($plac);
294
        /**
295
         * @var string
296
         */
297
        $note = null;
298
        $head->setNote($note);
299
        $this->_gedcom->setHead($head);
300
    }
301
302
    protected function setIndi($p_id)
303
    {
304
        $indi = new Indi();
305
        $person = Person::query()->find($p_id);
306
        if ($person == null) {
307
            return;
308
        }
309
        /**
310
         * @var string
311
         */
312
        $id = $person->id;
313
        $indi->setId($id);
314
315
        $gid = $person->gid;
316
        $indi->setGid($gid);
317
318
        $uid = $person->uid;
319
        $indi->setUid($uid);
320
321
        $_name = new Name();
322
        $_name->setName($person->name);
323
        $_name->setGivn($person->givn);
324
        $_name->setNick($person->nick);
325
        $_name->setSurn($person->surn);
326
        $_name->setNsfx($person->nsfx);
327
        $indi->addName($_name);
328
329
        /**
330
         * @var string
331
         */
332
        $sex = $person->sex;
333
        $indi->setSex($sex);
334
335
        if ($person->birthday || $person->birth_year) {
336
            $birt = $person->birthday ? strtoupper($person->birthday->format('j M Y')) : $person->birth_year;
337
            $indi->setBirt($birt);
338
        }
339
340
        if ($person->deathday || $person->death_year) {
341
            $deat = $person->deathday ? strtoupper($person->deathday->format('j M Y')) : $person->death_year;
342
            $indi->setDeat($deat);
343
        }
344
345
        if ($person->burial_day || $person->burial_year) {
346
            $buri = $person->burial_day ? strtoupper(Carbon::parse($person->burial_day)->format('j M Y')) : $person->burial_year;
347
            $indi->setBuri($buri);
348
        }
349
350
        if ($person->chan) {
351
            $chan = Carbon::parse($person->chan);
352
            $chan = [
353
                strtoupper($chan->format('j M Y')),
354
                $chan->format('H:i:s.v'),
355
            ];
356
            $indi->setChan($chan);
0 ignored issues
show
Bug introduced by
$chan of type array<integer,mixed|string> is incompatible with the type string expected by parameter $chan of Gedcom\Record\Indi::setChan(). ( Ignorable by Annotation )

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

356
            $indi->setChan(/** @scrutinizer ignore-type */ $chan);
Loading history...
357
        }
358
359
        $place = PersonEvent::query()->find($p_id);
360
        $_plac = new Personal();
361
        if (!empty($place->type)) {
362
            $_plac->setType($place->type);
363
        }
364
        if (!empty($place->date)) {
365
            $date = \FamilyTree365\LaravelGedcom\Utils\Importer\Date::read('', $place->date);
366
            $_plac->setDate($date);
367
        }
368
        if (!empty($place->type) && !empty($place->date)) {
369
            $indi->getAllEven($_plac);
0 ignored issues
show
Unused Code introduced by
The call to Gedcom\Record\Indi::getAllEven() has too many arguments starting with $_plac. ( Ignorable by Annotation )

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

369
            $indi->/** @scrutinizer ignore-call */ 
370
                   getAllEven($_plac);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
370
        }
371
372
        /**
373
         * @var Fams[]
374
         */
375
        /* $fams = Family::query()->where('husband_id', $p_id)->orwhere('wife_id', $p_id)->get();
376
        foreach ($fams as $item) {
377
            $fam = new Fams();
378
            $fam->setFams($item->id);
379
            $indi->addFams($fam);
380
        } */
381
382
        $this->_gedcom->addIndi($indi);
383
    }
384
385
    protected function setFam($family_id)
386
    {
387
        $famData = Family::query()->where('id', $family_id)->first();
388
        if ($famData == null) {
389
            return null;
390
        }
391
        $fam = new Fam();
392
        $_id = $famData->id;
393
        $fam->setId($_id);
394
395
        $_chan = null;
396
        $fam->setChan($_chan);
397
398
        $_husb = $famData->husband_id;
399
        $fam->setHusb($_husb);
400
401
        // add husb individual
402
        // $this->setIndi($_husb, $family_id);
403
404
        $_wife = $famData->wife_id;
405
        $fam->setWife($_wife);
406
407
        // add wife individual
408
        // $this->setIndi($_wife, $family_id);
409
410
        $_nchi = null;
411
        $fam->setNchi($_nchi);
412
413
        $_chil = Person::query()->where('child_in_family_id', $family_id)->get();
414
        foreach ($_chil as $item) {
415
            $fam->addChil($item->id);
416
            // $this->setIndi($item->id);
417
        }
418
419
        $_even = [];
420
        foreach ($_even as $item) {
421
            $even = new Even();
422
            $_type = null; // string
423
            $_date = null; // string
424
            $_plac = null; // \Gedcom\Record\Indi\Even\Plac
425
            $_caus = null; // string
426
            $_age = null;  // string
0 ignored issues
show
Unused Code introduced by
The assignment to $_age is dead and can be removed.
Loading history...
427
            $_addr = null; // \Gedcom\Record\Addr
428
            $_phon = []; // \Gedcom\Record\Phon
429
            $_agnc = null; // string
430
            $_husb = null; // \Gedcom\Record\Fam\Even\Husb
431
            $_wife = null; // \Gedcom\Record\Fam\Even\Wife
432
            $_obje = []; // \Gedcom\Writer\ObjeRef
433
            $_sour = []; // \Gedcom\Writer\SourRef
434
            $_note = []; // \Gedcom\Writer\NoteRef
435
            $even->setType($_type);
436
            $even->setDate($_date);
437
            $even->setPlac($_plac);
438
            $even->setCaus($_caus);
439
            $even->setAddr($_addr);
440
            $even->setPhon($_phon);
441
            $even->setAgnc($_agnc);
442
            $even->setHusb($_husb);
443
            $even->setWife($_wife);
444
            $even->setObje($_obje);
445
            $even->setSour($_sour);
446
            $even->setNote($_note);
447
            $fam->addEven($even);
448
        }
449
450
        $_slgs = [];
451
        foreach ($_slgs as $item) {
452
            $slgs = new Slgs();
453
            $_stat = null;
454
            $_date = null;
455
            $_plac = null;
456
            $_temp = null;
457
            $_sour = [];
458
            $_note = [];
459
460
            $slgs->setStat($_stat);
461
            $slgs->setDate($_date);
462
            $slgs->setPlac($_plac);
463
            $slgs->setTemp($_temp);
464
            $slgs->setSour($_sour);
465
            $slgs->setNote($_note);
466
            $fam->addSlgs($slgs);
467
        }
468
469
        $_subm = [];
470
        foreach ($_subm as $item) {
471
            $subm = new Subm();
472
            $subm_id = null;
473
            $chan = null; // @var Record\Chan
474
            $name = null;
475
            $addr = null; //@var Record\Addr
476
            $rin = null;
477
            $rfn = null;
478
            $lang = [];
479
            $phon = [];
480
            $obje = [];
481
            $note = [];
482
483
            $subm->setSubm($subm_id);
484
            $subm->setChan($chan);
485
            $subm->setName($name);
486
            $subm->setAddr($addr);
487
            $subm->setRin($rin);
488
            $subm->setRfn($rfn);
489
490
            $subm->setLang($lang);
491
            $subm->setPhon($phon);
492
            $subm->setObje($obje);
493
            $subm->setNote($note);
494
495
            $fam->addSubm($subm);
496
        }
497
498
        $_refn = [];
499
        foreach ($_refn as $item) {
500
            $refn = null;
501
            $type = null;
502
503
            $subm->setRefn($refn);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $subm seems to be defined by a foreach iteration on line 470. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
504
            $subm->setType($type);
505
506
            $fam->addRefn($refn);
507
        }
508
509
        $_rin = null;
510
        $fam->setRin($_rin);
511
512
        $_note = [];
513
        foreach ($_note as $item) {
514
            $note = new NoteRef();
515
            $fam->addNote($note);
516
        }
517
518
        $_sour = Source::all();
519
        foreach ($_sour as $item) {
520
            $sour = new SourRef();
521
            $sour->setSour($item->sour);
522
            $fam->addSour($sour);
523
        }
524
525
        $_obje = [];
526
        foreach ($_obje as $item) {
527
            $obje = new ObjeRef();
528
            $fam->addObje($obje);
529
        }
530
        $this->_gedcom->addFam($fam);
531
532
        return $fam;
533
    }
534
535
    protected function setSubn()
536
    {
537
    }
538
539
    protected function setSubM()
540
    {
541
    }
542
543
    protected function setSour()
544
    {
545
        $sour = new \Gedcom\Record\Sour();
546
        $_sour = Source::all();
547
        foreach ($_sour as $item) {
548
            $sour->setTitl($item->titl);
549
        }
550
551
        $this->_gedcom->addSour($sour);
552
    }
553
554
    protected function setNote()
555
    {
556
    }
557
558
    protected function setRepo()
559
    {
560
    }
561
562
    protected function setObje()
563
    {
564
    }
565
}
566