Skip to content

Commit

Permalink
Added edit/delete groups
Browse files Browse the repository at this point in the history
  • Loading branch information
hobsRKM committed May 3, 2024
1 parent da25704 commit bafe973
Show file tree
Hide file tree
Showing 18 changed files with 296 additions and 13 deletions.
93 changes: 92 additions & 1 deletion app/Http/Controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,11 +482,14 @@ public function getGroupsList(Request $request)

$formattedData = [];
// Format each group record
$siteDir = env('VITE_SITE_DIR');
foreach ($groups as $group) {
$preLoadDefaultServerId = SaGroupsServers::where('group_id', $group->id)->first()->server_id;
$formattedData[] = [
"id" => $group->id,
"name" => $group->name,
"name" => "<span style='font-size: 12px;' class='badge badge-dark'>{$group->name}</span>",
"flags" => $group->flags ?: "No flags assigned",
'actions' => "<a href='$siteDir/group/edit/{$group->id}/{$preLoadDefaultServerId}' class='btn btn-info btn-sm'><i class='fa fa-edit'></i></a> <a href='$siteDir/group/delete/{$group->id}' class='btn btn-danger btn-sm'><i class='fa fa-trash'></i></a>",
];
}

Expand All @@ -504,4 +507,92 @@ public function groups()
{
return view('admin.groups.list');
}

public function editGroup(Request $request, $group_id, $server_id) {
$groupServer = SaGroupsServers::with('groupsFlags')
->where('server_id', $server_id)
->where('group_id', $group_id)
->get();

if ($groupServer->isEmpty()) {
return redirect()->route('groups.list')->with('error', 'Group does not exist for the selected server!. Add Group to the server!');
}
$groupPermissions = $groupServer->pluck('groupsFlags.*.flag')->flatten()->toArray();
$groupDetails = SaGroups::where('id', $group_id)->first();
$permissions = Permission::all();
$servers = SaServer::all();
return view('admin.groups.edit', compact('servers', 'permissions', 'groupDetails', 'groupPermissions', 'server_id'));
}

public function updateGroup(Request $request, $groupId) {
$validated = $request->validate([
'permissions' => 'required|array',
'permissions.*' => 'exists:permissions,permission',
'server_id' => 'exists:sa_servers,id',
'immunity' => 'required',
'name' => 'required',
]);

$submittedPermissions = $validated['permissions'];
$groupServer = SaGroupsServers::with('groupsFlags')
->where('server_id',$validated['server_id'])
->where('group_id', $groupId)
->get();
// Fetch current permissions from the database
$currentPermissions = $groupServer->pluck('groupsFlags.*.flag')->flatten()->toArray();
// Determine permissions to add and delete
$permissionsToAdd = array_diff($submittedPermissions, $currentPermissions);
$permissionsToDelete = array_diff($currentPermissions, $submittedPermissions);

$groupDetails = SaGroups::where('id', $groupId)->first();
SaAdminsFlags::where('flag', $groupDetails->name)->update([
'flag' => $validated['name']
]);
$groupDetails->name = $validated['name'];
$groupDetails->save();
foreach($permissionsToAdd as $permission){
$groupFlags = new SaGroupsFlags();
$groupFlags->group_id = $groupId;
$groupFlags->flag = $permission;
$groupFlags->save();
}
SaGroupsFlags::whereIn('flag', $permissionsToDelete)
->where('group_id',$groupId)
->delete();

return redirect()->route('groups.list')->with('success', 'Group updated successfully.');
}

public function showGroupDeleteForm(Request $request, $groupId) {
$groupDetails = SaGroups::where('id', $groupId)->firstOrFail();
$servers = SaServer::all();
return view('admin.groups.delete', compact('groupDetails', 'servers'));
}

public function deleteGroup(Request $request, $groupId) {
$validated = $request->validate([
'server_ids.*' => [
'required',
function ($attribute, $value, $fail) {
if ($value !== 'all' && !DB::table('sa_servers')->where('id', $value)->exists()) {
$fail($attribute.' is invalid.');
}
},
],
]);

if(in_array('all', $validated['server_ids'])) {
SaGroups::where('id', $groupId)->delete();
}else {
$validated['server_ids'] = SaServer::all()->pluck('id')->toArray();
SaGroupsServers::where('group_id', $groupId)
->whereIn('server_id', $validated['server_ids'])->delete();

SaAdmin::where('group_id', $groupId)
->whereIn('server_id', $validated['server_ids'])->delete();
}

return redirect()->route('groups.list')->with('success', 'Group deleted successfully.');

}
}
2 changes: 1 addition & 1 deletion app/Models/SaGroupsServers.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public function groups() {
}

public function groupsFlags() {
return $this->belongsTo(SaGroupsFlags::class, 'group_id', 'group_id');
return $this->hasMany(SaGroupsFlags::class, 'group_id', 'group_id');
}
}
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@types/datatables.net": "^1.10.28",
"@types/jquery": "^3.5.29",
"datatables.net-dt": "^2.0.2",
"datatables.net-fixedcolumns": "^5.0.0",
"dotenv": "^16.4.5",
"jquery": "^3.7.1",
"ts-node": "^10.9.2"
Expand Down
6 changes: 6 additions & 0 deletions resources/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,9 @@ section {
.select2 {
width: 100% !important;
}
.dt-scroll-body .dataTable thead {
display: none;
}
.action-container {
width: 100px;
}
17 changes: 14 additions & 3 deletions resources/js/admin/admins.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import DataTable from 'datatables.net-dt';
import {formatDuration, calculateProgress} from '../utility/utility';
import 'datatables.net-fixedcolumns'

const dataTable = new DataTable("#adminsList", {
fixedColumns: {
start: 0,
end: 3,
},
scrollX: true,
scrollY: "800px",
"processing": true,
"serverSide": true,
"ajax": {
Expand All @@ -24,12 +31,16 @@ const dataTable = new DataTable("#adminsList", {
}
},
{"data": "flags"},
{"data": "hostnames", "width":"300px"},
{"data": "hostnames", "width":"50px"},
{"data": "created"},
{"data": "ends"},
{"data": "actions", "width": "200px"},
{
"data": "progress", "render": function (data, type, row, meta) {
"data": "actions", "width": "200px", "render": function (data, type, row, meta) {
return '<div class="action-container">' + data + '</div>';
}
},
{
"data": "progress", "width":"100px", "render": function (data, type, row, meta) {
const progress = calculateProgress(row.created, row.ends);
return `
<div class="progress">
Expand Down
14 changes: 12 additions & 2 deletions resources/js/bans/bans.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import DataTable from 'datatables.net-dt';
import {formatDuration, calculateProgress} from '../utility/utility';

import 'datatables.net-fixedcolumns'
let dataTable = null;
function loadBans() {
dataTable = new DataTable("#bansList", {
fixedColumns: {
start: 0,
end: 3,
},
"processing": true,
scrollX: true,
scrollY: "800px",
"serverSide": true,
"ajax": {
"url": bansListUrl,
Expand Down Expand Up @@ -45,7 +51,11 @@ function loadBans() {
},
{"data": "server_id"},
{"data": "status"},
{"data": "action", "width": "200px"},
{
"data": "action", "width": "200px", "render": function (data, type, row, meta) {
return '<div class="action-container">' + data + '</div>';
}
},
{
"data": "duration", "render": function (data, type, row, meta) {
const progress = calculateProgress(row.created, row.ends);
Expand Down
9 changes: 9 additions & 0 deletions resources/js/groups/edit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
$('#server_id').on('change', function() {
let selectedOption: any = $(this).val();
if (selectedOption) {
window.location.href = selectedOption;
}
});
$(document).ready(function() {
$('#server_id').select2();
})
5 changes: 5 additions & 0 deletions resources/js/groups/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ function loadGroups() {
{"data": "id"},
{"data": "name"},
{"data": "flags"},
{
"data": "actions", "width": "200px", "render": function (data, type, row, meta) {
return '<div class="action-container">' + data + '</div>';
}
},
]
});
}
Expand Down
13 changes: 12 additions & 1 deletion resources/js/mutes/mutes.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import DataTable from 'datatables.net-dt';
import {formatDuration, calculateProgress} from '../utility/utility';
import 'datatables.net-fixedcolumns'

let dataTable = null;

loadMutes();
function loadMutes() {
dataTable = new DataTable("#mutesList", {
fixedColumns: {
start: 0,
end: 3,
},
scrollX: true,
scrollY: "800px",
"processing": true,
"serverSide": true,
"ajax": {
Expand Down Expand Up @@ -47,7 +54,11 @@ function loadMutes() {
},
{"data": "server_id"},
{"data": "status"},
{"data": "action", "width": "200px"},
{
"data": "action", "width": "200px", "render": function (data, type, row, meta) {
return '<div class="action-container">' + data + '</div>';
}
},
{
"data": "duration", "render": function (data, type, row, meta) {
const progress = calculateProgress(row.created, row.ends);
Expand Down
3 changes: 3 additions & 0 deletions resources/views/admin/admins/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
<h5 class="card-title text-center mb-4">Add New Admin</h5>
<form action="{{ route('admin.store') }}" method="POST">
@csrf
<div class="note note-info mb-3">
<strong>Note:</strong> Newly added admins will not receive their permissions on the server until a map change. Alternatively, you can execute css_reloadadmins for immediate effect.
</div>
<div data-mdb-input-init class="form-outline mb-3">
<input type="number" class="form-control" id="steam_id" name="steam_id" required/>
<label class="form-label" for="steam_id">Steam ID</label>
Expand Down
3 changes: 3 additions & 0 deletions resources/views/admin/admins/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
@else
<form action="{{ route('admin.groups.update', ['player_steam' => $admin->first()->player_steamid]) }}" method="POST">
@endif
<div class="note note-info mb-3">
<strong>Note:</strong> Admins will not receive their permissions on the server until a map change. Alternatively, you can execute css_reloadadmins for immediate effect.
</div>
@csrf
<!-- Server Dropdown -->
<div data-mdb-input-init class="form-outline mb-3">
Expand Down
37 changes: 37 additions & 0 deletions resources/views/admin/groups/delete.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
@extends('layouts.app')

@section('content')
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card shadow">
<div class="card-header">
<h5 class="card-title text-center mb-4">Delete Group</h5>
</div>
<div class="card-body">
<form action="{{ route('group.delete', ['id' => $groupDetails->id]) }}" method="POST">
@csrf
<div class="mb-3">
<label><b>Group Name:</b> {{$groupDetails->name}}</label>
</div>
<!-- Servers Multi-Select Dropdown -->
<div class="mb-3">
<select multiple="multiple" class="form-control" id="server_ids" name="server_ids[]">
<option value="all">All Servers</option>
@foreach($servers as $server)
<option value="{{ $server->id }}">
{{ $server->hostname }}
</option>
@endforeach
</select>
</div>

<div class="text-center">
<button type="submit" class="btn btn-danger">Delete</button>
</div>
</form>
</div>
</div>
</div>
</div>
@endsection
@vite(['resources/js/admin/delete.ts'])
Loading

0 comments on commit bafe973

Please sign in to comment.