Passed
Pull Request — master (#1242)
by Curtis
04:51
created

Person::death()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
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 Illuminate\Database\Eloquent\SoftDeletes;
7
8
class Person extends \LaravelEnso\People\App\Models\Person
9
{
10
    use SoftDeletes;
11
12
    /**
13
     * The attributes that should be mutated to dates.
14
     *
15
     * @var array
16
     */
17
    protected $dates = ['deleted_at', 'birthday'];
18
19
    protected $fillable = [
20
        'givn',
21
        'surn',
22
        'sex',
23
        'child_in_family_id',
24
        'description',
25
        'title', 'name', 'appellative', 'uid', 'email', 'phone', 'birthday',
26
        'bank', 'bank_account', 'obs',
27
        'uid','chan', 'rin', 'resn', 'rfn', 'afn'
28
    ];
29
30
    public function events()
31
    {
32
        return $this->hasMany(PersonEvent::class);
33
    }
34
35
    public function child_in_family()
36
    {
37
        return $this->belongsTo(Family::class, 'child_in_family_id');
38
    }
39
40
    public function husband_in_family()
41
    {
42
        return $this->hasMany(Family::class, 'husband_id');
43
    }
44
45
    public function wife_in_family()
46
    {
47
        return $this->hasMany(Family::class, 'wife_id');
48
    }
49
50
    public function fullname()
51
    {
52
        return $this->givn.' '.$this->surn;
0 ignored issues
show
Bug introduced by
The property givn does not seem to exist on App\Person. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
Bug introduced by
The property surn does not seem to exist on App\Person. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
53
    }
54
55
    public function getSex()
56
    {
57
        if ($this->sex == 'F') {
0 ignored issues
show
Bug introduced by
The property sex does not seem to exist on App\Person. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
58
            return 'Female';
59
        }
60
61
        return 'Male';
62
    }
63
64
    public static function getList()
65
    {
66
        $persons = self::get();
67
        $result = [];
68
        foreach ($persons as $person) {
69
            $result[$person->id] = $person->fullname();
70
        }
71
72
        return collect($result);
73
    }
74
75
    public function addEvent($title, $date, $place, $description = '')
76
    {
77
        $place_id = Place::getIdByTitle($place);
78
        $event = PersonEvent::updateOrCreate(
79
            [
80
                'person_id' => $this->id,
81
                'title' => $title,
82
            ],
83
            [
84
            'person_id' => $this->id,
85
            'title' => $title,
86
            'description' => $description,
87
        ]);
88
89
        if ($date) {
90
            $event->date = $date;
91
            $event->save();
92
        }
93
94
        if ($place) {
95
            $event->places_id = $place_id;
96
            $event->save();
97
        }
98
99
        // add birthyear to person table ( for form builder )
100
        if($title == 'BIRT' && !empty($date)){
101
            $this->birthday =  date('Y-m-d', strtotime($date));
0 ignored issues
show
Bug introduced by
The property birthday does not seem to exist on App\Person. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
102
        }
103
        // add deathyear to person table ( for form builder )
104
        if($title == 'DEAT'  && !empty($date)){
105
            $this->deathday =  date('Y-m-d', strtotime($date));
0 ignored issues
show
Bug introduced by
The property deathday does not seem to exist on App\Person. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
106
        }
107
        $this->save();
108
    }
109
110
    public function birth()
111
    {
112
        return $this->events->where('title', '=', 'BIRT')->first();
113
    }
114
115
    public function death()
116
    {
117
        return $this->events->where('title', '=', 'DEAT')->first();
118
    }
119
120
    public function appellative()
121
    {
122
        return $this->givn;
0 ignored issues
show
Bug introduced by
The property givn does not seem to exist on App\Person. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
123
    }
124
125
126
}
127