Passed
Push — master ( 6758f1...14e78e )
by Curtis
05:20
created

Family::person()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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