Skip to content

Commit

Permalink
activitylog has been integrated
Browse files Browse the repository at this point in the history
  • Loading branch information
Raza9798 committed Jun 17, 2024
1 parent 7a9cebd commit 1d1581b
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 9 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
"minimum-stability": "dev",
"require": {
"php": "^8.2",
"laravel/telescope": "*",
"spatie/laravel-backup": "*",
"laravel/telescope": "*"
"spatie/laravel-activitylog": "*"
},
"extra": {
"laravel": {
Expand Down
10 changes: 5 additions & 5 deletions src/Console/Commands/InitilizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Intelrx\Rapidkit\assets\Banner;
use Intelrx\Rapidkit\Controller\ActivityController;
use Intelrx\Rapidkit\Controller\BackupController;
use Intelrx\Rapidkit\Controller\BuildController;
use Intelrx\Rapidkit\Controller\TelescopeController;

class InitilizeCommand extends Command
Expand All @@ -30,13 +32,11 @@ class InitilizeCommand extends Command
public function handle()
{
(new Banner())->renderTitle('Installing and configuring RapidKit package...');

Artisan::call("vendor:publish", [
"--provider" => "Spatie\Backup\BackupServiceProvider"
]);

(new BackupController())->setup();
(new BackupController())->configureDatabaseDump();
(new TelescopeController())->setup();
(new ActivityController())->setup();
(new BuildController());
Artisan::call("rapid:support");
}
}
29 changes: 29 additions & 0 deletions src/Controller/ActivityController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Intelrx\Rapidkit\Controller;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;

class ActivityController extends Controller
{
public function setup(){
File::mkdir(app_path('Traits'));
$stubPath = dirname(__DIR__, 2) . '/src/stubs/config/HasActivityLog.stub';
$traitPath = app_path('Traits/Activitylog.php');
$stubContent = File::get($stubPath);
if (!File::exists($traitPath)) {
File::put($traitPath, $stubContent);
}

Artisan::call('vendor:publish', [
'--provider' => 'Spatie\Activitylog\ActivitylogServiceProvider',
'--tag' => 'activitylog-migrations'
]);
Artisan::call('migrate');
Artisan::call("vendor:publish", [
"--provider" => "Spatie\Activitylog\ActivitylogServiceProvider",
"--tag" => "activitylog-config"
]);
}
}
7 changes: 7 additions & 0 deletions src/Controller/BackupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

namespace Intelrx\Rapidkit\Controller;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
use Intelrx\Rapidkit\assets\Banner;

class BackupController extends Controller
{
public function setup(){
Artisan::call("vendor:publish", [
"--provider" => "Spatie\Backup\BackupServiceProvider"
]);
}

public function configureDatabaseDump(){
if (!File::exists(config_path('database.php'))) {
(new Banner())->error('Database Config file does not exist');
Expand Down
13 changes: 13 additions & 0 deletions src/Controller/BuildController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Intelrx\Rapidkit\Controller;
use Illuminate\Routing\Controller;

class BuildController extends Controller
{
private $app_name;
public function __construct($app_name = null) {
$this->$app_name = $app_name;
dd($this->$app_name);
}
}
2 changes: 0 additions & 2 deletions src/RapidKitProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,5 @@ public function boot(): void

(new Banner())->renderTitle("✅ LARAVEL RAPID KIT " . Config::VERSION . " > php artisan rapid:support");
}


}
}
2 changes: 1 addition & 1 deletion src/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

class Config
{
const VERSION = '1.7.5';
const VERSION = '1.8';
}
101 changes: 101 additions & 0 deletions src/stubs/traits/HasActivityLog.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace App\Traits;

use Carbon\Carbon;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Contracts\Activity;
use Illuminate\Support\Facades\Auth;
use Spatie\Activitylog\Models\Activity as ModelsActivity;
use App\Http\Controllers\Controller;

trait HasActivityLog
{
use LogsActivity;

public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()->logOnly(['*'])->logOnlyDirty();
}

/**
* activity logging description, causer name, and log name.
*
* @param String $activity_module
* @param String $activity_column
* @return void activityInfo ["activity_column" => ..., "activity_description" => ...];
*
*/
public function tapActivity(Activity $activity, $eventName)
{
try {
if (method_exists($this, 'activityInfo')) {
$info = $this->activityInfo();
$info["class"] = get_class($this);
$info["description"] = $info["activity_description"];
}

$user_name = Auth::user()->name ?? "GUST";
$data = $activity->subject[$this->activity_column];

switch ($eventName) {
case 'created':
$data = $activity->properties["attributes"][$this->activity_column];
break;

case 'updated':
if (isset($activity->properties['old'][$this->activity_column])) {
$new_data = $activity->properties['attributes'][$this->activity_column] ?? 'NULL';
$data = $activity->properties['old'][$this->activity_column] ." to ". $new_data;
}
break;

default:
$data = $activity->properties['old'][$this->activity_column] ?? $data;
break;
}
$activity->description = "$user_name has $eventName " . ($info["description"] ?? " $data ") . " on $this->activity_module";
$activity->causer_name = $user_name;
$activity->log_name = $this->activity_module;
}
catch (\Exception $e) {
throw $e;
}
}

/**
* Loads the audit data for a given ID.
*
* @param int $id The ID of the subject.
* @return Illuminate\Support\Collection The transformed audit data.
*/
public function getAudit(int $id){
try{
$data = ModelsActivity::orderby('id', 'desc')->where(function ($query) use ($id) {
$query->where('subject_id', $id);
$query->where('log_name', $this->activity_module);
})->get();

return collect($data->toArray())->transform(function($item){
$carbon_format = 'Y-m-d h:i A';
foreach (["attributes", "old"] as $key => $value) {
if (isset($item["properties"][$value]['updated_at'])) {
$item["properties"][$value]["updated_at"] = Carbon::parse($item["properties"][$value]["updated_at"])->format($carbon_format);
}
if (isset($item["properties"][$value]['created_at'])) {
$item["properties"][$value]["created_at"] = Carbon::parse($item["properties"][$value]["created_at"])->format($carbon_format);
}
}
$item["created_at"] = Carbon::parse($item["created_at"])->format($carbon_format);
$item["updated_at"] = Carbon::parse($item["updated_at"])->format($carbon_format);
$item["log_name"] = $this->activity_module;
$item["module_name"] = $this->activity_module;
return $item;
});
}
catch (\Exception $e) {
throw $e;
}
}
}

0 comments on commit 1d1581b

Please sign in to comment.