add: ability to delete groups

- request #4268
This commit is contained in:
HDVinnie
2025-09-02 09:43:30 -04:00
parent 271962664b
commit 34b9643e24
3 changed files with 75 additions and 0 deletions
@@ -21,7 +21,9 @@ use App\Http\Requests\Staff\StoreGroupRequest;
use App\Http\Requests\Staff\UpdateGroupRequest;
use App\Models\ForumCategory;
use App\Models\Group;
use App\Models\User;
use App\Services\Unit3dAnnounce;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Str;
/**
@@ -101,4 +103,44 @@ class GroupController extends Controller
return to_route('staff.groups.index')
->with('success', 'Group Was Updated Successfully!');
}
/**
* Delete A Group.
*/
public function destroy(Group $group): \Illuminate\Http\RedirectResponse
{
if ($group->system_required) {
return to_route('staff.groups.index')
->with('error', 'Cannot delete system required group: '.$group->name);
}
if ($group->users()->exists()) {
$defaultGroup = Group::query()
->where('system_required', '=', true)
->firstWhere('name', '=', 'User');
if (!$defaultGroup) {
return to_route('staff.groups.index')
->with('error', 'Cannot find default User group to reassign users.');
}
// Get user count before reassignment
$userCount = $group->users()->count();
// Move all users to the default User group using the relationship
$group->users()->update(['group_id' => $defaultGroup->id]);
// Run auto:group command to reassign users to appropriate groups
Artisan::call('auto:group');
return to_route('staff.groups.index')
->with('success', "Group \"{$group->name}\" was deleted successfully. {$userCount} users were moved to their appropriate groups.");
}
Unit3dAnnounce::removeGroup($group);
$group->delete();
return to_route('staff.groups.index')
->with('success', "Group \"{$group->name}\" was deleted successfully.");
}
}
@@ -63,6 +63,7 @@
<th>Min Avg Seedtime</th>
<th>Min Seedsize</th>
<th>Min Uploads</th>
<th>{{ __('common.action') }}</th>
</tr>
</thead>
<tbody>
@@ -332,6 +333,37 @@
<td></td>
<td></td>
@endif
<td>
<menu class="data-table__actions">
<li class="data-table__action">
<a
href="{{ route('staff.groups.edit', ['group' => $group]) }}"
class="form__button form__button--text"
>
{{ __('common.edit') }}
</a>
</li>
@unless ($group->system_required)
<li class="data-table__action">
<form
action="{{ route('staff.groups.destroy', ['group' => $group]) }}"
method="POST"
x-data="confirmation"
>
@csrf
@method('DELETE')
<button
x-on:click.prevent="confirmAction"
data-b64-deletion-message="{{ base64_encode('Are you sure you want to delete this group: ' . $group->name . '? All users in this group will be moved to their appropriate groups.') }}"
class="form__button form__button--text"
>
{{ __('common.delete') }}
</button>
</form>
</li>
@endunless
</menu>
</td>
</tr>
@endforeach
</tbody>
+1
View File
@@ -891,6 +891,7 @@ Route::middleware('language')->group(function (): void {
Route::post('/', [App\Http\Controllers\Staff\GroupController::class, 'store'])->name('store');
Route::get('/{group}/edit', [App\Http\Controllers\Staff\GroupController::class, 'edit'])->name('edit');
Route::patch('/{group}', [App\Http\Controllers\Staff\GroupController::class, 'update'])->name('update');
Route::delete('/{group}', [App\Http\Controllers\Staff\GroupController::class, 'destroy'])->name('destroy');
});
// Gifts Log