Passed
Push — master ( 8f3742...50f570 )
by Darko
08:41
created

DeletedUsersController::index()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 40
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 40
rs 8.8817
cc 6
nc 32
nop 1
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Http\Controllers\BasePageController;
6
use App\Models\User;
7
use Illuminate\Http\Request;
8
9
class DeletedUsersController extends BasePageController
10
{
11
    /**
12
     * Display a listing of soft-deleted users.
13
     */
14
    public function index(Request $request)
15
    {
16
        $this->setAdminPrefs();
17
18
        $username = $request->has('username') ? $request->input('username') : '';
19
        $email = $request->has('email') ? $request->input('email') : '';
20
        $host = $request->has('host') ? $request->input('host') : '';
21
        $orderBy = $request->has('ob') && ! empty($request->input('ob')) ? $request->input('ob') : 'deleted_at_desc';
22
23
        $deletedUsers = User::onlyTrashed()
24
            ->when($username !== '', function ($query) use ($username) {
25
                return $query->where('username', 'like', '%'.$username.'%');
26
            })
27
            ->when($email !== '', function ($query) use ($email) {
28
                return $query->where('email', 'like', '%'.$email.'%');
29
            })
30
            ->when($host !== '', function ($query) use ($host) {
31
                return $query->where('host', 'like', '%'.$host.'%');
32
            });
33
34
        // Determine sort order
35
        [$orderField, $orderSort] = $this->getSortOrder($orderBy);
36
        $deletedUsers = $deletedUsers->orderBy($orderField, $orderSort)->paginate(25);
37
38
        $this->smarty->assign([
39
            'deletedusers' => $deletedUsers,
40
            'username' => $username,
41
            'email' => $email,
42
            'host' => $host,
43
            'orderby' => $orderBy,
44
        ]);
45
46
        $meta_title = 'Deleted Users';
47
        $meta_keywords = 'view,deleted,users,softdeleted';
48
        $meta_description = 'View and restore soft-deleted user accounts';
49
50
        $content = $this->smarty->fetch('deleted_users.tpl');
51
        $this->smarty->assign(compact('content', 'meta_title', 'meta_keywords', 'meta_description'));
52
53
        $this->adminrender();
54
    }
55
56
    /**
57
     * Restore a soft-deleted user.
58
     */
59
    public function restore($id)
60
    {
61
        $user = User::onlyTrashed()->find($id);
62
63
        if ($user) {
64
            $user->restore();
65
66
            return redirect()->route('admin.deleted.users.index')
67
                ->with('success', "User '{$user->username}' has been restored successfully.");
68
        }
69
70
        return redirect()->route('admin.deleted.users.index')
71
            ->with('error', 'User not found.');
72
    }
73
74
    /**
75
     * Permanently delete a soft-deleted user.
76
     */
77
    public function permanentDelete($id)
78
    {
79
        $user = User::onlyTrashed()->find($id);
80
81
        if ($user) {
82
            $username = $user->username;
83
            $user->forceDelete();
84
85
            return redirect()->route('admin.deleted.users.index')
86
                ->with('success', "User '{$username}' has been permanently deleted.");
87
        }
88
89
        return redirect()->route('admin.deleted.users.index')
90
            ->with('error', 'User not found.');
91
    }
92
93
    /**
94
     * Parse sort order from the orderBy parameter.
95
     */
96
    private function getSortOrder($orderBy): array
97
    {
98
        $orderArr = explode('_', $orderBy);
99
        $orderField = match ($orderArr[0]) {
100
            'email' => 'email',
101
            'host' => 'host',
102
            'createdat' => 'created_at',
103
            'deletedat' => 'deleted_at',
104
            'lastlogin' => 'lastlogin',
105
            'apiaccess' => 'apiaccess',
106
            'grabs' => 'grabs',
107
            'role' => 'roles_id',
108
            default => 'username',
109
        };
110
        $orderSort = (isset($orderArr[1]) && preg_match('/^asc|desc$/i', $orderArr[1])) ? $orderArr[1] : 'desc';
111
112
        return [$orderField, $orderSort];
113
    }
114
}
115