Passed
Push — main ( 97905b...f46541 )
by PRATIK
14:03 queued 10:17
created

ActivityTable::resetActivities()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 6
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
namespace Pratiksh\Adminetic\Http\Livewire\Admin\Activity;
4
5
use App\Models\User;
0 ignored issues
show
Bug introduced by
The type App\Models\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Carbon\Carbon;
7
use Livewire\Component;
8
use Livewire\WithPagination;
9
use Spatie\Activitylog\Models\Activity;
10
11
class ActivityTable extends Component
12
{
13
    use WithPagination;
14
15
    protected $paginationTheme = 'bootstrap';
16
17
    public $log_name;
18
    public $log_names;
19
20
    public $description;
21
22
    public $model;
23
    public $models;
24
25
    public $user_id;
26
    public $users;
27
    public $descriptions;
28
29
    public $start_date;
30
    public $end_date;
31
    public $data;
32
    public $interval = 30;
33
    public $to_from = 1;
34
    public $delete_limit = 30;
35
36
    public $activity_count = 0;
37
38
    protected $listeners = ['date_range_filter' => 'dateRangeFilter', 'date_filter' => 'dateFilter'];
39
40
    public function mount($model = null)
41
    {
42
        $this->model = $model;
43
        $activities = Activity::all();
44
        $this->descriptions = !is_null($activities) ? array_unique($activities->pluck('description')->toArray()) : null;
45
        $this->log_names = array_unique($activities->pluck('log_name')->toArray());
46
        $this->models = array_unique($activities->pluck('subject_type')->toArray());
47
        $this->users = User::find($activities->pluck('causer_id')->toArray());
48
    }
49
50
    public function deleteAll()
51
    {
52
        Activity::truncate();
53
        $this->emit('activity_success', 'All Activities Deleted Successfully');
54
    }
55
56
    public function deleteWithLimit()
57
    {
58
        if (!is_null($this->delete_limit)) {
0 ignored issues
show
introduced by
The condition is_null($this->delete_limit) is always false.
Loading history...
59
            Activity::whereDate('created_at', '<', Carbon::now()->subDays($this->delete_limit))->delete();
60
            $this->emit('activity_success', 'All activities Deleted except last ' . $this->delete_limit . 'days activities');
61
        }
62
    }
63
64
    public function dateFilter($date)
65
    {
66
        $date = Carbon::create($date);
67
        $to_from = $this->to_from;
68
69
        if ($to_from == 1) {
70
            $this->start_date = $date;
71
            $this->end_date = $date->addDays($this->interval ?? 30);
72
        } elseif ($to_from == 2) {
73
            $this->end_date = $date;
74
            $this->start_date = $date->subDays($this->interval ?? 30);
75
        }
76
    }
77
78
    public function dateRangeFilter($start_date, $end_date)
79
    {
80
        $this->start_date = Carbon::create($start_date);
81
        $this->end_date = Carbon::create($end_date);
82
    }
83
84
    public function deleteActivity(Activity $activity)
85
    {
86
        $activity->delete();
87
        $this->emit('activity_success', 'Activity Deleted Successfully');
88
    }
89
90
    public function render()
91
    {
92
        $activities = $this->getActivities();
93
94
        return view('adminetic::livewire.admin.activity.activity-table', compact('activities'));
95
    }
96
97
    private function getActivities()
98
    {
99
        $this->resetPage();
100
        $data = Activity::query();
101
        // Filter By Log Name
102
        if (!is_null($this->log_name)) {
103
            $data = $data->where('log_name', $this->log_name);
104
        }
105
        // Filter By Model
106
        if (!is_null($this->model)) {
107
            $data = $data->where('subject_type', $this->model);
108
        }
109
        // Filter By User
110
        if (!is_null($this->user_id)) {
111
            $data = $data->where('causer_id', $this->user_id);
112
        }
113
        // Filter By Date
114
        if (!is_null($this->start_date) && !is_null($this->end_date)) {
115
            $data = $data->whereDate('created_at', [$this->start_date, $this->end_date]);
116
        }
117
        // Filter Description
118
        if (!is_null($this->description)) {
119
            $data = $data->where('description', $this->description);
120
        }
121
        $this->activity_count = with($data)->count();
122
        $this->emit('initialize_activity');
123
124
        return
125
            $data->latest()->paginate(10);
126
    }
127
128
    public function resetActivities()
129
    {
130
        $this->log_name = null;
131
        $this->model = null;
132
        $this->user_id = null;
133
        $this->start_date = null;
134
        $this->end_date = null;
135
        $this->description = null;
136
    }
137
}
138