Skip to content

Commit 0433b5d

Browse files
authored
Add files via upload
1 parent 88c8265 commit 0433b5d

8 files changed

Lines changed: 147 additions & 75 deletions

File tree

src/ActivityLog.php

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Illuminate\Http\Request;
66
use Exception;
7+
use Illuminate\Support\Facades\DB;
8+
use Illuminate\Support\Facades\Route;
79
use Lcw\Activitylog\Models\ActivityLog as ModelsActivityLog;
810
use Lcw\Activitylog\Traits\ActivityLogActions;
911

@@ -14,6 +16,7 @@
1416
*
1517
* Default Route is Your-Domain/log
1618
*
19+
* @param ignore_route [If set system ignore to create the activity on that route]
1720
* @param log [string]
1821
* @param server_ip [The server ip address]
1922
* @param user_ip [The client/user ip address]
@@ -44,23 +47,22 @@ public function get(Request $request)
4447

4548
// Delete all old Records
4649
self::logDelete();
47-
48-
$condition = '1';
50+
$activityLog = new ModelsActivityLog();
4951
if (!empty($request->from_created_at)) {
50-
$condition .= ' AND DATE(created_at) >= "' . $request->from_created_at . '"';
52+
$activityLog = $activityLog->where(DB::raw('DATE(created_at)'), '>=', $request->from_created_at);
5153
}
5254
if (!empty($request->to_created_at)) {
53-
$condition .= ' AND DATE(created_at) <= "' . $request->to_created_at . '"';
55+
$activityLog = $activityLog->where(DB::raw('DATE(created_at)'), '<=', $request->to_created_at);
5456
}
5557
if (!empty($request->log)) {
56-
$condition .= ' AND log LIKE "%' . $request->log . '%"';
58+
$activityLog = $activityLog->where('log', 'LIKE', '%' . $request->log . '%');
5759
}
5860
if (!empty($request->user_id)) {
59-
$condition .= ' AND user_id = "' . $request->user_id . '"';
61+
$activityLog = $activityLog->where('user_id', $request->user_id);
6062
}
6163

62-
$activityLog = new ModelsActivityLog();
63-
return $activityLog->whereRaw($condition)->orderBy('id', 'DESC')->paginate($limit);
64+
65+
return $activityLog->orderBy('id', 'DESC')->paginate($limit);
6466
} catch (Exception $e) {
6567
return '[Get Method] Fetch data not working: ' . $e->getMessage();
6668
}
@@ -79,21 +81,22 @@ public function getUsers(array $parameters = [])
7981
$aData = [];
8082
foreach ($allUsers as $key => $value) {
8183
$user = json_decode($value['user'], true);
82-
if (array_key_exists('username', $user)) {
83-
$aData[$user['id']] = $user['username'];
84-
} elseif (array_key_exists('name', $user)) {
85-
$aData[$user['id']] = $user['name'];
86-
} elseif (array_key_exists('firstname', $user)) {
87-
$aData[$user['id']] = $user['firstname'];
88-
} elseif (array_key_exists('lastname', $user)) {
89-
$aData[$user['id']] = $user['lastname'];
90-
} elseif (array_key_exists('first_name', $user)) {
91-
$aData[$user['id']] = $user['first_name'];
92-
} elseif (array_key_exists('last_name', $user)) {
93-
$aData[$user['id']] = $user['last_name'];
94-
} else {
95-
$aData[$user['id']] = $user['id'];
96-
}
84+
if (!empty($user))
85+
if (array_key_exists('username', $user)) {
86+
$aData[$user['id']] = $user['username'];
87+
} elseif (array_key_exists('name', $user)) {
88+
$aData[$user['id']] = $user['name'];
89+
} elseif (array_key_exists('firstname', $user)) {
90+
$aData[$user['id']] = $user['firstname'];
91+
} elseif (array_key_exists('lastname', $user)) {
92+
$aData[$user['id']] = $user['lastname'];
93+
} elseif (array_key_exists('first_name', $user)) {
94+
$aData[$user['id']] = $user['first_name'];
95+
} elseif (array_key_exists('last_name', $user)) {
96+
$aData[$user['id']] = $user['last_name'];
97+
} else {
98+
$aData[$user['id']] = $user['id'];
99+
}
97100
}
98101
asort($aData);
99102
return array_unique($aData);
@@ -150,11 +153,25 @@ public function defaultData()
150153

151154
/**
152155
* Save the log record in table
156+
* If ignore route set system will not create any activity on that route
153157
* @param parameters array
154158
* @return resources with created log
155159
*/
156160
public function create(array $paramerts = [])
157161
{
162+
163+
$configSettings = $this->getConfigSettings();
164+
if (isset($configSettings['ignore_routes']) && !empty($configSettings['ignore_routes'])) {
165+
$routeName = Route::currentRouteName();
166+
$routePath = Route::getFacadeRoot()->current()->uri();
167+
if (in_array($routeName, $configSettings['ignore_routes'])) {
168+
return true;
169+
}
170+
if (in_array($routePath, $configSettings['ignore_routes'])) {
171+
return true;
172+
}
173+
}
174+
158175
$activityLog = new ModelsActivityLog();
159176
$paramerts = array_filter($paramerts);
160177
$defaultParams = self::defaultData();
@@ -172,22 +189,23 @@ public function create(array $paramerts = [])
172189

173190
/**
174191
* Delete older data
175-
* Set in .env [ACTIVITY_LOG_DEL = 3] to change the default delete limit
192+
* Set [delete_limit] in config file.
176193
* @param pass any number in months
177194
* @return bool
178195
*/
179196
public function logDelete(int $month = 3)
180197
{
181198
try {
182-
if (getenv("ACTIVITY_LOG_DEL")) {
183-
$month = env("ACTIVITY_LOG_DEL");
199+
if (!isset($month)) {
200+
$configSettings = $this->getConfigSettings();
201+
$month = $configSettings['delete_limit'];
184202
}
185203
$oldDate = date('Y-m-d', strtotime('-' . $month . ' months'));
186204

187205
$activityLog = new ModelsActivityLog();
188-
return $activityLog->where('created_at', '<', $oldDate)->delete();
206+
return $activityLog->where(DB::raw('DATE(created_at)'), '<', $oldDate)->delete();
189207
} catch (Exception $e) {
190-
return '[Get Method] Fetch data not working: ' . $e->getMessage();
208+
return '[Delete Method] log delete not working: ' . $e->getMessage();
191209
}
192210
}
193-
}
211+
}

src/Controllers/ActivityLogController.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55
use Exception;
66
use Illuminate\Http\Request;
77
use Lcw\Activitylog\ActivityLog;
8+
use Lcw\Activitylog\Traits\ActivityLogActions;
9+
810

911
class ActivityLogController
1012
{
13+
14+
use ActivityLogActions;
15+
1116
/**
1217
* Get all logs
1318
* @param request paramerters and model
@@ -27,4 +32,4 @@ public function index(Request $request, ActivityLog $activityLog)
2732
return '[Controller Index] Fetch data not working: ' . $e->getMessage();
2833
}
2934
}
30-
}
35+
}

src/Models/ActivityLog.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,10 @@
77

88
class ActivityLog extends Model
99
{
10+
11+
1012
use HasFactory;
1113
protected $table = 'activity_log_master';
12-
protected $fillable = [
13-
'id',
14-
'log',
15-
'server_ip_detail',
16-
'user_ip_detail',
17-
'route_detail',
18-
'query_string',
19-
'user_id',
20-
'user',
21-
'created_at',
22-
'updated_at',
23-
];
24-
}
14+
// Disable Laravel's mass assignment protection
15+
protected $guarded = [];
16+
}

src/Providers/ActivityLogProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ public function boot()
4040
], 'config');
4141
}
4242
}
43-
}
43+
}

src/Traits/ActivityLogActions.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,49 @@ public function queryString(array $queryString = [])
120120
if (!empty($queryString)) {
121121
$response = json_encode($queryString);
122122
} else {
123-
$response = json_encode(request()->all());
123+
$routeParams = request()->route()->parameters();
124+
$requestParams = request()->all();
125+
$response = json_encode($routeParams + $requestParams);
124126
}
125127
return $response;
126128
}
127-
}
129+
130+
131+
132+
/**
133+
* Check the config settings if user set and publish
134+
* We will use user settings
135+
* @return array
136+
*/
137+
public function getConfigSettings()
138+
{
139+
$configData = config('lcwActivityLogConfig.lcw_activity_log');
140+
if (file_exists(config_path('lcw_activity_log_config.php'))) {
141+
$configData = config('lcw_activity_log_config.lcw_activity_log');
142+
}
143+
return $configData;
144+
}
145+
146+
147+
/**
148+
* Covert nested to single array
149+
* @param array
150+
* @return array
151+
*/
152+
public function singleArray($array)
153+
{
154+
$result = [];
155+
156+
foreach ($array as $key => $value) {
157+
$currentKey = $key;
158+
159+
if (is_array($value)) {
160+
$result = array_merge($result, $this->singleArray($value));
161+
} else {
162+
$result[$currentKey] = $value;
163+
}
164+
}
165+
166+
return $result;
167+
}
168+
}

src/config/config.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/**
4+
* The basic config file that manage
5+
* Activity main actions
6+
* There are only few settings we need in activity log
7+
*
8+
*
9+
* Main settings are listed below you can customize them after publishing the config
10+
*
11+
*/
12+
13+
return [
14+
'lcw_activity_log' => [
15+
/**
16+
* Change the value to change the activity log delete limit
17+
* By default it is 3 in months
18+
* @param integer in months
19+
*/
20+
'delete_limit' => 3,
21+
22+
/**
23+
* Pass route path or names
24+
* If you don't want to create the activity log use ignore_routes
25+
* @param route URI/NAMES
26+
*/
27+
'ignore_routes' => [],
28+
]
29+
];

src/routes/web.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
use Illuminate\Support\Facades\Route;
44
use Lcw\Activitylog\Controllers\ActivityLogController;
55

6-
Route::middleware(['web'])->group(function () {
6+
Route::middleware(['web','auth'])->group(function () {
77
Route::get('log', [ActivityLogController::class, "index"])->name('lcw_activity_log_index');
88
});

src/views/index.blade.php

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
1010
integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
1111
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.3/font/bootstrap-icons.css">
12-
<link rel="stylesheet" href="//code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
13-
<link rel="stylesheet"
14-
href="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/css/bootstrap-select.min.css">
1512
</head>
1613
<style>
1714
.container {
@@ -130,7 +127,7 @@
130127
<main>
131128
<div class="d-flex flex-column flex-shrink-0 p-3 shadow border border-r sidebar" style="width: 250px;">
132129
<a href="/" class="d-flex align-items-center mb-3 mb-md-0 me-md-auto text-decoration-none">
133-
<span class="fs-4">NFS Activity Log</span>
130+
<span class="fs-4">Activity Log</span>
134131
</a>
135132
<hr>
136133
<ul class="nav nav-pills flex-column mb-auto">
@@ -163,13 +160,16 @@
163160
<div class="col-sm-3 mb-3">
164161
<label>Create Date</label>
165162
<div class="input-group">
166-
<input type="text" class="form-control date" name="from_created_at"
163+
<input type="date" class="form-control" name="from_created_at"
167164
value="{{ request('from_created_at') }}" id="from_created_at"
168-
placeholder="Date from" readonly>
165+
placeholder="Date from">
169166
<span class="input-group-text">-</span>
170-
<input type="text" class="form-control date" name="to_created_at"
167+
<input type="date" class="form-control" name="to_created_at"
171168
value="{{ request('to_created_at') }}" id="to_created_at"
172-
placeholder="Date to" readonly>
169+
placeholder="Date to">
170+
<button type="button" class="btn btn-dark"
171+
onclick="$('#from_created_at, #to_created_at').val('');"><i
172+
class="bi bi-arrow-clockwise"></i></button>
173173
</div>
174174
</div>
175175
<div class="col-sm-3 mb-3">
@@ -240,12 +240,14 @@ class="btn btn-danger"><i class="bi bi-arrow-clockwise"></i>
240240
</tfoot>
241241
<tbody>
242242
@if (!$log->isEmpty())
243+
@php $activityLogController = app('Lcw\Activitylog\Controllers\ActivityLogController'); @endphp
243244
@foreach ($log as $key => $item)
244245
@php
245-
$userIpDetail = json_decode($item['user_ip_detail'], true);
246-
$serverIpDetail = json_decode($item['server_ip_detail'], true);
247-
$userDetail = json_decode($item['user'], true);
248-
$parametersDetail = json_decode($item['query_string'], true);
246+
$userIpDetail = $activityLogController->singleArray(json_decode($item['user_ip_detail'], true)) ?? [];
247+
$serverIpDetail = $activityLogController->singleArray(json_decode($item['server_ip_detail'], true)) ?? [];
248+
$userDetail = $activityLogController->singleArray(json_decode($item['user'], true)) ?? [];
249+
$parametersDetail = $activityLogController->singleArray(json_decode($item['query_string'], true)) ?? [];
250+
$routeDetail = $activityLogController->singleArray(json_decode($item['route_detail'], true)) ?? [];
249251
@endphp
250252
<tr>
251253
<td class="align-baseline">{{ $key + 1 }}</td>
@@ -406,7 +408,7 @@ class="btn btn-danger"><i class="bi bi-arrow-clockwise"></i>
406408

407409
</div>
408410
<footer class="pt-5 my-5 text-muted border-top">
409-
Created by <b>Khalid Zaid Bin</b> for <a href="{{ url('/') }}" target="_blank">NFS</a> · ©
411+
Created by <b>Khalid Zaid Bin</b> for <a href="{{ url('/') }}" target="_blank">KOT</a> · ©
410412
2023
411413
</footer>
412414
</div>
@@ -416,22 +418,7 @@ class="btn btn-danger"><i class="bi bi-arrow-clockwise"></i>
416418
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"
417419
integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous">
418420
</script>
419-
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
420-
421-
<script src="https://cdn.jsdelivr.net/npm/bootstrap-select@1.14.0-beta3/dist/js/bootstrap-select.min.js"></script>
422-
423421
<script>
424-
$(document).ready(function() {
425-
$('body').on('focus', ".date", function() {
426-
$(this).datepicker({
427-
dateFormat: 'yy-mm-dd',
428-
changeMonth: true,
429-
changeYear: true,
430-
yearRange: "-80:+10"
431-
});
432-
});
433-
$('.selectpicker').selectpicker();
434-
});
435422
/* global bootstrap: false */
436423
(function() {
437424
'use strict'

0 commit comments

Comments
 (0)