Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add activity logger #1755

Merged
merged 29 commits into from
Mar 5, 2024
Merged
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
276ea51
Apply fixes from StyleCI
StyleCIBot Dec 25, 2023
8053b82
Merge branch 'nabeelio:dev' into dev
arthurpar06 Jan 15, 2024
3002eea
Add spatie/laravel-activitylog
arthurpar06 Jan 21, 2024
dacd472
Add admin interface for activities
arthurpar06 Jan 21, 2024
f8c4535
Disable activity logging in api routes
arthurpar06 Jan 21, 2024
2e63704
Add activity watcher to models
arthurpar06 Jan 21, 2024
9bdf353
Add clean up command
arthurpar06 Jan 21, 2024
408801c
Apply fixes from StyleCI
StyleCIBot Jan 21, 2024
11657aa
Remove activities from menu if activity logger is disabled
arthurpar06 Jan 21, 2024
c748204
Fix migrations structure
arthurpar06 Jan 21, 2024
664006f
Apply fixes from StyleCI
StyleCIBot Jan 21, 2024
5a2b531
Fix tests
arthurpar06 Jan 21, 2024
e526e75
Fix display of changes if newValue is an array
arthurpar06 Jan 21, 2024
6fe938f
Disable activity logger for frontend routes
arthurpar06 Jan 21, 2024
c43db0f
Enable activity logger only in admin routes
arthurpar06 Jan 21, 2024
ff1d133
Apply fixes from StyleCI
StyleCIBot Jan 21, 2024
fc3ab8d
Merge branch 'dev' into activityLog
arthurpar06 Jan 24, 2024
6cbe1e6
Update composer.lock
arthurpar06 Jan 24, 2024
ceb127f
Merge branch 'dev' into activityLog
arthurpar06 Feb 7, 2024
9ec4f50
Update composer.lock
arthurpar06 Feb 7, 2024
a81a982
Merge branch 'dev' into activityLog
arthurpar06 Feb 13, 2024
6eb37e4
Merge branch 'dev' into activityLog
arthurpar06 Feb 14, 2024
91adae8
Merge branch 'dev' into activityLog
nabeelio Feb 21, 2024
341ae79
Merge branch 'dev' into activityLog
arthurpar06 Feb 28, 2024
6d50ba9
Update composer.lock
arthurpar06 Feb 28, 2024
26e1ce2
Merge branch 'dev' into activityLog
nabeelio Mar 5, 2024
09e220e
Merge branch 'dev' into activityLog
arthurpar06 Mar 5, 2024
31fda94
Update composer.lock
arthurpar06 Mar 5, 2024
1598960
Merge branch 'dev' into activityLog
nabeelio Mar 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add admin interface for activities
  • Loading branch information
arthurpar06 committed Jan 21, 2024
commit dacd4727ce5ef1b70d2e3982a61887f4c2bf44d4
35 changes: 35 additions & 0 deletions app/Http/Controllers/Admin/ActivityController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Contracts\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Spatie\Activitylog\Models\Activity;

class ActivityController extends Controller
{
public function index(Request $request): View
{
$activities = Activity::with('causer')->orderBy('created_at', 'desc')->paginate();

return view('admin.activities.index', [
'activities' => $activities
]);
}

public function show(Request $request, int $id): RedirectResponse|View
{
$activity = Activity::with('causer', 'subject')->find($id);

if (!$activity) {
flash()->error('Activity not found');
return redirect()->route('admin.activities.index');
}

return view('admin.activities.show', [
'activity' => $activity
]);
}
}
4 changes: 4 additions & 0 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
@@ -513,6 +513,10 @@ private function mapAdminRoutes()
], 'dashboard/news', ['uses' => 'DashboardController@news'])
->name('dashboard.news')->middleware('update_pending', 'ability:admin,admin-access');

Route::resource('activities', 'ActivityController')
->only(['index', 'show'])
->middleware('ability:admin,admin-access');

//Modules
Route::group([
'as' => 'modules.',
16 changes: 16 additions & 0 deletions resources/views/admin/activities/index.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@extends('admin.app')
@section('title', 'Activities')

@section('content')
<div class="card border-blue-bottom">
<div class="content">
@include('admin.activities.table')
</div>
</div>

<div class="row">
<div class="col-12 text-center">
{{ $activities->withQueryString()->links('admin.pagination.default') }}
</div>
</div>
@endsection
97 changes: 97 additions & 0 deletions resources/views/admin/activities/show.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
@extends('admin.app')
@section('title', 'Activity Details')

@section('content')
<div class="card border-blue-bottom">
<div class="content">
<div class="row">

<div class="col-xl-12" style="padding: 0 15px;">
<div class="form-container">
<h6><i class="fas fa-info-circle"></i>&nbsp;Causer Information</h6>
<div class="form-container-body">
<div class="row">
<div class="form-group col-sm-4">
<label>Causer Type</label>
<p>{{ class_basename($activity->causer_type) }}</p>
</div>
<div class="form-group col-sm-4">
<label>Causer</label>
<p>
@if (class_basename($activity->causer_type) === 'User')
<a href="{{ route('admin.users.edit', [$activity->causer_id]) }}">
{{ $activity->causer_id .' | '. $activity->causer->name_private }}
</a>
@else
{{ $activity->causer_id.' | '. class_basename($activity->causer_type) }}
@endif
</p>
</div>
<div class="form-group col-sm-4">
<label>Caused At</label>
<p>{{ $activity->created_at->diffForHumans() . ' | ' .$activity->created_at->format('d.M') }}</p>

</div>
</div>
</div>
</div>
</div>

<div class="col-xl-12" style="padding: 0 15px;">
<div class="form-container">
<h6><i class="fas fa-info-circle"></i>&nbsp;Subject Information</h6>
<div class="form-container-body">
<div class="row">
<div class="form-group col-sm-3">
<label>Subject Type</label>
<p>{{ class_basename($activity->subject_type) }}</p>
</div>
<div class="form-group col-sm-3">
<label>Subject Id</label>
<p>
{{ $activity->subject_id }}
</p>
</div>
<div class="form-group col-sm-3">
<label>Subject Name</label>
<p>
{{ $activity->subject->name ?? 'N/A' }}
</p>
</div>
<div class="form-group col-sm-3">
<label>Event Type</label>
<p>{{ $activity->event }}</p>
</div>
</div>

@if (isset($activity->changes['attributes']) && is_array($activity->changes['attributes']))
<div class="table-responsive table-full-width">
<table class="table table-hover" id="flights-table">
<thead>
<th>Field</th>
<th>New Value</th>
<th>Old Value</th>
</thead>
<tbody>
{{-- Check if 'attributes' key exists --}}
@foreach($activity->changes['attributes'] as $field => $newValue)
<tr>
<td>{{ $field }}</td>
<td>{{ $newValue }}</td>
{{-- Check if 'old' key exists --}}
<td>{{ $activity->changes['old'][$field] ?? 'N/A' }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
@endif

</div>
</div>
</div>

</div>
</div>
</div>
@endsection
30 changes: 30 additions & 0 deletions resources/views/admin/activities/table.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<div class="content table-responsive table-full-width">
<table class="table table-hover" id="activities-table">
<thead>
<th>Action</th>
<th>Causer</th>
<th>Date</th>
<th class="text-right">Actions</th>
</thead>
<tbody>
@foreach($activities as $activity)
<tr>
<td>{{ class_basename($activity->subject_type).' '. $activity->event}}</td>
<td>
@if (class_basename($activity->causer_type) === 'User')
<a href="{{ route('admin.users.edit', [$activity->causer_id]) }}">
{{ $activity->causer_id .' | '. $activity->causer->name_private }}
</a>
@else
{{ $activity->causer_id.' | '. class_basename($activity->causer_type) }}
@endif
</td>
<td>{{ $activity->created_at->diffForHumans().' | '.$activity->created_at->format('d.M') }}</td>
<td class="text-right">
<a href="{{ route('admin.activities.show', [$activity->id]) }}" class='btn btn-sm btn-success btn-icon'><i class="fas fa-eye"></i> View Details</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
4 changes: 4 additions & 0 deletions resources/views/admin/menu.blade.php
Original file line number Diff line number Diff line change
@@ -86,6 +86,10 @@
<li><a href="{{ url('/admin/maintenance') }}"><i class="pe-7s-tools"></i>maintenance</a></li>
@endability

@ability('admin', 'admin-access')
<li><a href="{{ url('/admin/activities') }}"><i class="pe-7s-news-paper"></i>activities</a></li>
@endability

@ability('admin', 'settings')
<li><a href="{{ url('/admin/settings') }}"><i class="pe-7s-config"></i>settings</a></li>
@endability