1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Chriscreates\Projects\Models; |
4
|
|
|
|
5
|
|
|
use BadMethodCallException; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use Chriscreates\Projects\Traits\HasStatus; |
8
|
|
|
use Chriscreates\Projects\Traits\HasUsers; |
9
|
|
|
use Chriscreates\Projects\Traits\IsMeasurable; |
10
|
|
|
use Illuminate\Database\Eloquent\Model; |
11
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
12
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany; |
13
|
|
|
|
14
|
|
|
class Project extends Model |
15
|
|
|
{ |
16
|
|
|
use IsMeasurable, HasUsers, HasStatus; |
17
|
|
|
|
18
|
|
|
protected $table = 'projects'; |
19
|
|
|
|
20
|
|
|
public $primaryKey = 'id'; |
21
|
|
|
|
22
|
|
|
public $guarded = ['id']; |
23
|
|
|
|
24
|
|
|
public $timestamps = true; |
25
|
|
|
|
26
|
|
|
private $user_model; |
27
|
|
|
|
28
|
|
|
protected $dates = [ |
29
|
|
|
'started_at', |
30
|
|
|
'delivered_at', |
31
|
|
|
'expected_at', |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
protected $casts = [ |
35
|
|
|
'visible' => 'bool', |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
public function __construct(array $attributes = []) |
39
|
|
|
{ |
40
|
|
|
$this->user_model = config('projects.user_class'); |
41
|
|
|
|
42
|
|
|
parent::__construct($attributes); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function owner() : BelongsTo |
46
|
|
|
{ |
47
|
|
|
return $this->belongsTo(get_class(user_model())); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function status() : BelongsTo |
51
|
|
|
{ |
52
|
|
|
return $this->belongsTo(Status::class); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function tasks() : MorphToMany |
56
|
|
|
{ |
57
|
|
|
return $this->morphedByMany(Task::class, 'projectable'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function isVisible() : bool |
61
|
|
|
{ |
62
|
|
|
return $this->attributes['visible'] == true; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function isNotVisible() : bool |
66
|
|
|
{ |
67
|
|
|
return $this->attributes['visible'] == false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function dueIn(string $format = 'days') : int |
71
|
|
|
{ |
72
|
|
|
$method = 'diffIn'.ucfirst($format); |
73
|
|
|
|
74
|
|
|
if ( ! $this->hasDateTarget()) { |
75
|
|
|
return 0; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ( ! method_exists(Carbon::class, $method)) { |
79
|
|
|
throw new BadMethodCallException(sprintf( |
80
|
|
|
'Method %s::%s does not exist.', |
81
|
|
|
Carbon::class, |
82
|
|
|
$method |
83
|
|
|
)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->started_at->$method($this->expected_at); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function assignTask(Task $task) : void |
90
|
|
|
{ |
91
|
|
|
$this->tasks()->save($task); |
92
|
|
|
|
93
|
|
|
$this->refresh(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function removeTask(Task $task) : void |
97
|
|
|
{ |
98
|
|
|
$this->tasks()->detach($task); |
99
|
|
|
|
100
|
|
|
$this->refresh(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function hasTask(Task $task) : bool |
104
|
|
|
{ |
105
|
|
|
$this->refresh(); |
106
|
|
|
|
107
|
|
|
return $this->tasks->contains($task->id); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|