1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Te7aHoudini\LaravelApplicant\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Support\Carbon; |
7
|
|
|
use Te7aHoudini\LaravelApplicant\Models\Application; |
8
|
|
|
|
9
|
|
|
trait ReceivesApplications |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* updates application record in db. |
13
|
|
|
* |
14
|
|
|
* @param array|object $model |
15
|
|
|
* @param array $criteria |
16
|
|
|
* @return bool |
17
|
|
|
*/ |
18
|
|
|
public function processApplicationFrom($model, $criteria = [], $newStatus = 'processed') |
19
|
|
|
{ |
20
|
|
|
$applicationType = Arr::get($criteria, 'type', Arr::get($this->receiverCriteria, 'type', 'applicant')); |
|
|
|
|
21
|
|
|
|
22
|
|
|
$applicationStatus = Arr::get($criteria, 'status', Arr::get($this->receiverCriteria, 'status', 'created')); |
23
|
|
|
|
24
|
|
|
$query = Application::query(); |
25
|
|
|
|
26
|
|
View Code Duplication |
if (is_array($model)) { |
|
|
|
|
27
|
|
|
$query->where('type', $model['type'] ?? $applicationType) |
28
|
|
|
->where('status', $model['status'] ?? $applicationStatus) |
29
|
|
|
->where('applicant_id', $model['applicant_id']) |
30
|
|
|
->where('applicant_type', $model['applicant_type']); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
View Code Duplication |
if (is_object($model)) { |
|
|
|
|
34
|
|
|
$query->where('type', $applicationType) |
35
|
|
|
->where('status', $applicationStatus) |
36
|
|
|
->where('applicant_id', $model->id) |
37
|
|
|
->where('applicant_type', get_class($model)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $query->update([ |
41
|
|
|
'status' => is_string($criteria) ? $criteria : $newStatus, |
42
|
|
|
'status_updated_by_id' => $this->id, |
|
|
|
|
43
|
|
|
'status_updated_by_type' => get_class($this), |
44
|
|
|
'status_updated_at' => Carbon::now(), |
45
|
|
|
]); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* check if current model has received application from other model or not. |
50
|
|
|
* |
51
|
|
|
* @param array|object $model |
52
|
|
|
* @param array $criteria |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
|
|
public function hasReceivedApplicationFrom($model, $criteria = []) |
56
|
|
|
{ |
57
|
|
|
return $this->receivedApplicationsFrom($model, $criteria)->exists(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* get applications that applied on this model and this model was the receiver. |
62
|
|
|
* |
63
|
|
|
* @param array|object $model |
64
|
|
|
* @param array $criteria |
65
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
66
|
|
|
*/ |
67
|
|
|
public function receivedApplicationsFrom($model, $criteria = []) |
68
|
|
|
{ |
69
|
|
|
$applicationType = Arr::get($criteria, 'type', Arr::get($this->applicantCriteria, 'type', 'applicant')); |
|
|
|
|
70
|
|
|
|
71
|
|
|
$applicationStatus = Arr::get($criteria, 'status', Arr::get($this->applicantCriteria, 'status', 'created')); |
72
|
|
|
|
73
|
|
|
$query = $this->receivedApplications(); |
74
|
|
|
|
75
|
|
View Code Duplication |
if (is_array($model)) { |
|
|
|
|
76
|
|
|
$query->where('type', $model['type'] ?? $applicationType) |
77
|
|
|
->where('status', $model['status'] ?? $applicationStatus) |
78
|
|
|
->where('applicant_id', $model['applicant_id']) |
79
|
|
|
->where('applicant_type', $model['applicant_type']); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
View Code Duplication |
if (is_object($model)) { |
|
|
|
|
83
|
|
|
$query->where('type', $applicationType) |
84
|
|
|
->where('status', $applicationStatus) |
85
|
|
|
->where('applicant_id', $model->id) |
86
|
|
|
->where('applicant_type', get_class($model)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $query; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* sets receiverCriteria attribute and returns $this to allow fluent api. |
94
|
|
|
* |
95
|
|
|
* @param array $criteria |
96
|
|
|
* @return self |
97
|
|
|
*/ |
98
|
|
|
public function setReceiverCriteria($criteria = []) |
99
|
|
|
{ |
100
|
|
|
$this->receiverCriteria = $criteria; |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* returns received applications for that model. |
107
|
|
|
* |
108
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
109
|
|
|
*/ |
110
|
|
|
public function receivedApplications() |
111
|
|
|
{ |
112
|
|
|
return $this->morphMany(Application::class, 'receiver'); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: