|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace GenealogiaWebsite\LaravelGedcom\Models; |
| 4 | + |
| 5 | +use Illuminate\Database\Eloquent\Model; |
| 6 | +use LaravelEnso\People\Models\Person; |
| 7 | +// use App\Person; |
| 8 | + |
| 9 | + |
| 10 | +class Family extends Model |
| 11 | +{ |
| 12 | + |
| 13 | + |
| 14 | + protected $fillable = ['description', 'is_active', 'husband_id', 'wife_id', 'type_id', 'chan', 'nchi', 'rin']; |
| 15 | + |
| 16 | + protected $attributes = ['is_active' => false]; |
| 17 | + |
| 18 | + protected $casts = ['is_active' => 'boolean']; |
| 19 | + |
| 20 | + public function events() |
| 21 | + { |
| 22 | + return $this->hasMany(FamilyEvent::class); |
| 23 | + } |
| 24 | + |
| 25 | + public function children() |
| 26 | + { |
| 27 | + return $this->hasMany(Person::class, 'child_in_family_id'); |
| 28 | + } |
| 29 | + |
| 30 | + public function husband() |
| 31 | + { |
| 32 | + return $this->hasOne(Person::class, 'id', 'husband_id'); |
| 33 | + } |
| 34 | + |
| 35 | + public function wife() |
| 36 | + { |
| 37 | + return $this->hasOne(Person::class, 'id', 'wife_id'); |
| 38 | + } |
| 39 | + |
| 40 | + public function title() |
| 41 | + { |
| 42 | + return (($this->husband) ? $this->husband->fullname() : '?'). |
| 43 | + ' + '. |
| 44 | + (($this->wife) ? $this->wife->fullname() : '?'); |
| 45 | + } |
| 46 | + |
| 47 | + public static function getList() |
| 48 | + { |
| 49 | + $families = self::get(); |
| 50 | + $result = []; |
| 51 | + foreach ($families as $family) { |
| 52 | + $result[$family->id] = $family->title(); |
| 53 | + } |
| 54 | + |
| 55 | + return collect($result); |
| 56 | + } |
| 57 | + |
| 58 | + public function addEvent($title, $date, $place, $description = '') |
| 59 | + { |
| 60 | + $place_id = Place::getIdByTitle($place); |
| 61 | + $event = FamilyEvent::updateOrCreate( |
| 62 | + [ |
| 63 | + 'family_id' => $this->id, |
| 64 | + 'title' => $title, |
| 65 | + ], |
| 66 | + [ |
| 67 | + 'family_id' => $this->id, |
| 68 | + 'title' => $title, |
| 69 | + 'description' => $description, |
| 70 | + ]); |
| 71 | + if ($date) { |
| 72 | + $event->date = $date; |
| 73 | + $event->save(); |
| 74 | + } |
| 75 | + if ($place) { |
| 76 | + $event->places_id = $place_id; |
| 77 | + $event->save(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public function getWifeName() |
| 82 | + { |
| 83 | + if ($this->wife) { |
| 84 | + return $this->wife->fullname(); |
| 85 | + } else { |
| 86 | + return 'unknown woman'; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public function getHusbandName() |
| 91 | + { |
| 92 | + if ($this->husband) { |
| 93 | + return $this->husband->fullname(); |
| 94 | + } else { |
| 95 | + return 'unknown man'; |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments