1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sfneal\Tracking\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
7
|
|
|
use Illuminate\Database\Eloquent\SoftDeletingScope; |
8
|
|
|
use Sfneal\Scopes\CreatedOrderScope; |
9
|
|
|
use Sfneal\Scopes\IdOrderScope; |
10
|
|
|
use Sfneal\Tracking\Builders\TrackActivityBuilder; |
11
|
|
|
use Sfneal\Tracking\Models\Base\AbstractTracking; |
12
|
|
|
use Sfneal\Tracking\Models\Traits\TrackingRelationships; |
13
|
|
|
|
14
|
|
|
class TrackActivity extends AbstractTracking |
15
|
|
|
{ |
16
|
|
|
// todo: add use of polymorphic relationships |
17
|
|
|
use TrackingRelationships; |
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The "booting" method of the model. |
21
|
|
|
* |
22
|
|
|
* @return void |
23
|
|
|
*/ |
24
|
|
|
protected static function boot() |
25
|
|
|
{ |
26
|
|
|
parent::boot(); |
27
|
|
|
static::addGlobalScope(new CreatedOrderScope()); |
28
|
|
|
static::addGlobalScope(new IdOrderScope()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
protected $table = 'track_activity'; |
32
|
|
|
protected $primaryKey = 'track_activity_id'; |
33
|
|
|
|
34
|
|
|
protected $fillable = [ |
35
|
|
|
'track_activity_id', |
36
|
|
|
'user_id', |
37
|
|
|
'route', |
38
|
|
|
'description', |
39
|
|
|
'model_table', |
40
|
|
|
'model_key', |
41
|
|
|
'model_changes', |
42
|
|
|
'request_token', |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Query Builder. |
47
|
|
|
* |
48
|
|
|
* @param $query |
49
|
|
|
* |
50
|
|
|
* @return TrackActivityBuilder |
51
|
|
|
*/ |
52
|
|
|
public function newEloquentBuilder($query) |
53
|
|
|
{ |
54
|
|
|
return new TrackActivityBuilder($query); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return TrackActivityBuilder|Builder |
59
|
|
|
*/ |
60
|
|
|
public static function query(): TrackActivityBuilder |
61
|
|
|
{ |
62
|
|
|
return parent::query(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Related TrackTraffic data. |
67
|
|
|
* |
68
|
|
|
* @return BelongsTo |
69
|
|
|
*/ |
70
|
|
|
public function tracking() |
71
|
|
|
{ |
72
|
|
|
return $this->belongsTo(TrackTraffic::class, 'request_token', 'request_token') |
73
|
|
|
->withoutGlobalScope(SoftDeletingScope::class); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Determine if the Activity has model changes or a request payload. |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
public function getHasModelChangesAttribute() |
82
|
|
|
{ |
83
|
|
|
return parent::getHasModelChangesAttribute() || $this->hasTrackingLoadedWithPayload(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Determine if the 'tracking' relationship has been eager loaded and that it has a request_payload. |
88
|
|
|
* |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
public function hasTrackingLoadedWithPayload() |
92
|
|
|
{ |
93
|
|
|
return $this->relationLoaded('tracking') && isset($this->tracking->request_payload); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|