|
1
|
|
|
<?php namespace JackJoe\ActivityLog\Models; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Database\Eloquent\Model as Eloquent; |
|
4
|
|
|
use Illuminate\Support\Facades\Auth; |
|
5
|
|
|
use Illuminate\Support\Facades\Request; |
|
6
|
|
|
|
|
7
|
|
|
class Activity extends Eloquent { |
|
8
|
|
|
/** |
|
9
|
|
|
* The database table used by the model. |
|
10
|
|
|
* |
|
11
|
|
|
* @var string |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $table = 'activity_log'; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* The fillable fields for the model. |
|
17
|
|
|
* |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $fillable = [ |
|
21
|
|
|
'user_id', |
|
22
|
|
|
'content', |
|
23
|
|
|
'content_id', |
|
24
|
|
|
'action', |
|
25
|
|
|
'state', |
|
26
|
|
|
'details', |
|
27
|
|
|
'data', |
|
28
|
|
|
'version', |
|
29
|
|
|
'ip_address', |
|
30
|
|
|
'user_agent', |
|
31
|
|
|
]; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Get the user that the activity belongs to. |
|
35
|
|
|
* |
|
36
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
37
|
|
|
*/ |
|
38
|
|
|
public function user() |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->belongsTo(config('auth.providers.users.model'), 'user_id'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Create an activity log entry. |
|
45
|
|
|
* |
|
46
|
|
|
* @param mixed $data |
|
47
|
|
|
* @return boolean |
|
48
|
|
|
*/ |
|
49
|
|
|
public static function log($data = []) |
|
|
|
|
|
|
50
|
|
|
{ |
|
51
|
|
|
// set the defaults from config |
|
52
|
|
|
$defaults = config('activity-log.defaults'); |
|
53
|
|
|
if (!is_array($defaults)) { |
|
54
|
|
|
$defaults = []; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if (is_object($data)) { |
|
58
|
|
|
$data = (array) $data; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// conversion path to content |
|
62
|
|
|
if (isset($data['contentType']) && !isset($data['content'])) { |
|
63
|
|
|
$data['content'] = $data['contentType']; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// conversion path to state |
|
67
|
|
|
if (isset($data['description']) && !isset($data['state'])) { |
|
68
|
|
|
$data['state'] = $data['description']; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// set the user ID |
|
72
|
|
|
if (config('activity-log.auto_set_user_id') && !isset($data['userId'])) { |
|
73
|
|
|
$user = call_user_func(config('activity-log.auth_method')); |
|
|
|
|
|
|
74
|
|
|
$data['userId'] = isset($user->id) ? $user->id : null; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// set IP address |
|
78
|
|
|
if (!isset($data['ipAddress'])) { |
|
79
|
|
|
$data['ipAddress'] = Request::getClientIp(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
// set user agent |
|
83
|
|
|
if (!isset($data['userAgent'])) { |
|
84
|
|
|
$data['userAgent'] = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'No User Agent'; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// set additional data and encode it as JSON if it is an array or an object |
|
88
|
|
|
if (isset($data['data']) && (is_array($data['data']) || is_object($data['data']))) { |
|
89
|
|
|
$data['data'] = json_encode($data['data']); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// format array keys to snake case for insertion into database |
|
93
|
|
|
$dataFormatted = []; |
|
94
|
|
|
foreach ($data as $key => $value) { |
|
|
|
|
|
|
95
|
|
|
$dataFormatted[snake_case($key)] = $value; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// merge defaults array with formatted data array |
|
99
|
|
|
$data = array_merge($defaults, $dataFormatted); |
|
100
|
|
|
|
|
101
|
|
|
// create the record |
|
102
|
|
|
$self = static::create($data); |
|
103
|
|
|
|
|
104
|
|
|
return $self; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
|
|
|
|
|
107
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: