Skip to content

Commit

Permalink
rewrite add and delete entities FederationEntityController.php
Browse files Browse the repository at this point in the history
  • Loading branch information
temaotl committed Aug 1, 2024
1 parent 2d7269e commit 192a85c
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 275 deletions.
97 changes: 0 additions & 97 deletions app/Http/Controllers/FederationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@

use App\Http\Requests\StoreFederation;
use App\Http\Requests\UpdateFederation;
use App\Jobs\GitDeleteMembers;
use App\Models\Entity;
use App\Models\Federation;
use App\Models\User;
use App\Notifications\FederationDestroyed;
use App\Notifications\FederationMembersChanged;
use App\Notifications\FederationOperatorsChanged;
use App\Notifications\FederationRequested;
use App\Notifications\FederationUpdated;
use App\Notifications\YourFederationRightsChanged;
use App\Services\NotificationService;
use App\Traits\GitTrait;
use Illuminate\Support\Facades\Auth;
Expand Down Expand Up @@ -155,98 +150,6 @@ public function update(UpdateFederation $request, Federation $federation)
->route('federations.show', $federation)
->with('status', __('federations.updated'));

break;

/* case 'add_operators':
$this->authorize('update', $federation);
if (! request('operators')) {
return to_route('federations.operators', $federation)
->with('status', __('federations.add_empty_operators'))
->with('color', 'red');
}
$old_operators = $federation->operators;
$new_operators = User::whereIn('id', request('operators'))->get();
$federation->operators()->attach(request('operators'));
Notification::sendNow($new_operators, new YourFederationRightsChanged($federation, 'added'));
NotificationService::sendOperatorNotification($old_operators, new FederationOperatorsChanged($federation, $new_operators, 'added'));
return redirect()
->route('federations.operators', $federation)
->with('status', __('federations.operators_added'));*/

/* case 'delete_operators':
$this->authorize('update', $federation);
if (! request('operators')) {
return to_route('federations.operators', $federation)
->with('status', __('federations.delete_empty_operators'))
->with('color', 'red');
}
$old_operators = User::whereIn('id', request('operators'))->get();
$federation->operators()->toggle(request('operators'));
$new_operators = $federation->operators;
Notification::sendNow($old_operators, new YourFederationRightsChanged($federation, 'deleted'));
NotificationService::sendOperatorNotification($new_operators, new FederationOperatorsChanged($federation, $old_operators, 'added'));
return redirect()
->route('federations.operators', $federation)
->with('status', __('federations.operators_deleted'));*/

case 'add_entities':
$this->authorize('update', $federation);

if (! request('entities')) {
return to_route('federations.entities', $federation)
->with('status', __('federations.add_empty_entities'))
->with('color', 'red');
}

$explanation = "Operator's decision";
$federation->entities()->attach(request('entities'), [
'requested_by' => Auth::id(),
'approved_by' => Auth::id(),
'approved' => true,
'explanation' => $explanation,
]);

$new_entities = Entity::whereIn('id', request('entities'))->get();
NotificationService::sendModelNotification($federation, new FederationMembersChanged($federation, $new_entities, 'added'));

//TODO add members to federation
// GitAddMembers::dispatch($federation, $new_entities, Auth::user());

return redirect()
->route('federations.entities', $federation)
->with('status', __('federations.entities_added'));

break;

case 'delete_entities':
$this->authorize('update', $federation);

if (! request('entities')) {
return to_route('federations.entities', $federation)
->with('status', __('federations.delete_empty_entities'))
->with('color', 'red');
}

$federation->entities()->detach(request('entities'));

$old_entities = Entity::whereIn('id', request('entities'))->get();
// GitDeleteMembers::dispatch($federation, $old_entities, Auth::user());
NotificationService::sendModelNotification($federation, new FederationMembersChanged($federation, $old_entities, 'deleted'));

return redirect()
->route('federations.entities', $federation)
->with('status', __('federations.entities_deleted'));

break;

default:
return redirect()->route('federations.show', $federation);
}
Expand Down
61 changes: 56 additions & 5 deletions app/Http/Controllers/FederationEntityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

use App\Models\Entity;
use App\Models\Federation;
use App\Notifications\FederationMembersChanged;
use App\Services\NotificationService;
use Illuminate\Support\Facades\Auth;

class FederationEntityController extends Controller
{
public function __construct()
{

}

public function index(Federation $federation)
{
$this->authorize('view', $federation);
Expand All @@ -31,4 +29,57 @@ public function index(Federation $federation)
'entities' => $entities,
]);
}

/**
* Store a newly created resource in storage.
*/
public function store(Federation $federation)
{
$this->authorize('update', $federation);

if (! request('entities')) {
return to_route('federations.entities.index', $federation)
->with('status', __('federations.add_empty_entities'))
->with('color', 'red');
}

$explanation = "Operator's decision";
$federation->entities()->attach(request('entities'), [
'requested_by' => Auth::id(),
'approved_by' => Auth::id(),
'approved' => true,
'explanation' => $explanation,
]);

$new_entities = Entity::whereIn('id', request('entities'))->get();
NotificationService::sendModelNotification($federation, new FederationMembersChanged($federation, $new_entities, 'added'));

return redirect()
->route('federations.entities.index', $federation)
->with('status', __('federations.entities_added'));

}

/**
* Remove the specified resource from storage.
*/
public function destroy(Federation $federation)
{
$this->authorize('update', $federation);

if (! request('entities')) {
return to_route('federations.entities.index', $federation)
->with('status', __('federations.delete_empty_entities'))
->with('color', 'red');
}

$federation->entities()->detach(request('entities'));

$old_entities = Entity::whereIn('id', request('entities'))->get();
NotificationService::sendModelNotification($federation, new FederationMembersChanged($federation, $old_entities, 'deleted'));

return redirect()
->route('federations.entities.index', $federation)
->with('status', __('federations.entities_deleted'));
}
}
126 changes: 2 additions & 124 deletions resources/views/federations/entities.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,68 +11,7 @@
<h3 class="text-lg font-semibold">
{{ __('common.delete_members') }}
</h3>
<form x-data="{ open: false }" id="delete_members" action="{{ route('federations.update', $federation) }}"
method="post">
@csrf
@method('patch')
<input type="hidden" name="action" value="delete_entities">
<div class="overflow-x-auto bg-white border rounded-lg">
<table class="min-w-full border-b border-gray-300">
<thead>
<tr>
<th class="px-6 py-3 text-xs tracking-widest text-left uppercase bg-gray-100 border-b">&nbsp;
</th>
<th class="px-6 py-3 text-xs tracking-widest text-left uppercase bg-gray-100 border-b">
{{ __('common.name') }}</th>
<th class="px-6 py-3 text-xs tracking-widest text-left uppercase bg-gray-100 border-b">
{{ __('common.description') }}</th>
<th class="px-6 py-3 text-xs tracking-widest text-left uppercase bg-gray-100 border-b">
{{ __('common.status') }}</th>
</tr>
</thead>
<tbody class="divide-y divide-gray-200">
@forelse ($members as $entity)
<tr x-data class="hover:bg-blue-50" role="button"
@click="checkbox = $el.querySelector('input[type=checkbox]'); checkbox.checked = !checkbox.checked">
<td class="px-6 py-3 text-sm">
<input @click.stop class="rounded" type="checkbox" name="entities[]"
value="{{ $entity->id }}">
</td>
<td class="whitespace-nowrap px-6 py-3 text-sm">
{{ $entity->{"name_$locale"} }}
<div class="text-gray-500">
{{ $entity->entityid }}
</div>
</td>
<td class="px-6 py-3 text-sm">
{{ $entity->{"description_$locale"} ?: __('entities.no_description') }}
</td>
<td class="px-6 py-3 text-sm">
<x-status :model="$federation" />
</td>
</tr>
@empty
<tr class="hover:bg-blue-50">
<td class="px-6 py-3 font-bold text-center" colspan="4">
{{ __('federations.no_members') }}
</td>
</tr>
@endforelse
</tbody>
</table>
{{ $members->links() }}
@if (count($members))
<div class="px-4 py-2 bg-gray-100">
<x-button color="red" @click.prevent="open = !open">{{ __('common.delete_members') }}</x-button>

<x-modal>
<x-slot:title>{{ __('common.confirm_delete_members') }}</x-slot:title>
{{ __('common.confirm_delete_members_body') }}
</x-modal>
</div>
@endif
</div>
</form>
@include('federations.entityForm.delete')
</div>

<div>
Expand All @@ -86,68 +25,7 @@
id="search" value="{{ request('search') }}" placeholder="{{ __('entities.searchbox') }}">
</form>
</div>
<form x-data="{ open: false }" id="add_members" action="{{ route('federations.update', $federation) }}"
method="post">
@csrf
@method('patch')
<input type="hidden" name="action" value="add_entities">
<div class="overflow-x-auto bg-white border rounded-lg">
<table class="min-w-full border-b border-gray-300">
<thead>
<tr>
<th class="px-6 py-3 text-xs tracking-widest text-left uppercase bg-gray-100 border-b">&nbsp;
</th>
<th class="px-6 py-3 text-xs tracking-widest text-left uppercase bg-gray-100 border-b">
{{ __('common.name') }}</th>
<th class="px-6 py-3 text-xs tracking-widest text-left uppercase bg-gray-100 border-b">
{{ __('common.description') }}</th>
<th class="px-6 py-3 text-xs tracking-widest text-left uppercase bg-gray-100 border-b">
{{ __('common.status') }}</th>
</tr>
</thead>
<tbody class="divide-y divide-gray-200">
@forelse ($entities as $entity)
<tr x-data class="hover:bg-blue-50" role="button"
@click="checkbox = $el.querySelector('input[type=checkbox]'); checkbox.checked = !checkbox.checked">
<td class="px-6 py-3 text-sm">
<input @click.stop class="rounded" type="checkbox" name="entities[]"
value="{{ $entity->id }}">
</td>
<td class="whitespace-nowrap px-6 py-3 text-sm">
{{ $entity->{"name_$locale"} }}
<div class="text-gray-500">
{{ $entity->entityid }}
</div>
</td>
<td class="px-6 py-3 text-sm">
{{ $entity->{"description_$locale"} ?: __('entities.no_description') }}
</td>
<td class="px-6 py-3 text-sm">
<x-status :model="$federation" />
</td>
</tr>
@empty
<tr class="hover:bg-blue-50">
<td class="px-6 py-3 font-bold text-center">
{{ __('federations.empty') }}
</td>
</tr>
@endforelse
</tbody>
</table>
{{ $entities->links() }}
@if (count($entities))
<div class="px-4 py-2 bg-gray-100">
<x-button @click.prevent="open = !open">{{ __('common.add_members') }}</x-button>

<x-modal>
<x-slot:title>{{ __('common.confirm_add_members') }}</x-slot:title>
{{ __('common.confirm_add_members_body') }}
</x-modal>
</div>
@endif
</div>
</form>
@include('federations.entityForm.add')
</div>
@else
<h3 class="text-lg font-semibold">{{ __('common.entities_list') }}</h3>
Expand Down
60 changes: 60 additions & 0 deletions resources/views/federations/entityForm/add.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<form x-data="{ open: false }" id="add_members" action="{{ route('federations.entities.store', $federation) }}"
method="post">
@csrf
<div class="overflow-x-auto bg-white border rounded-lg">
<table class="min-w-full border-b border-gray-300">
<thead>
<tr>
<th class="px-6 py-3 text-xs tracking-widest text-left uppercase bg-gray-100 border-b">&nbsp;
</th>
<th class="px-6 py-3 text-xs tracking-widest text-left uppercase bg-gray-100 border-b">
{{ __('common.name') }}</th>
<th class="px-6 py-3 text-xs tracking-widest text-left uppercase bg-gray-100 border-b">
{{ __('common.description') }}</th>
<th class="px-6 py-3 text-xs tracking-widest text-left uppercase bg-gray-100 border-b">
{{ __('common.status') }}</th>
</tr>
</thead>
<tbody class="divide-y divide-gray-200">
@forelse ($entities as $entity)
<tr x-data class="hover:bg-blue-50" role="button"
@click="checkbox = $el.querySelector('input[type=checkbox]'); checkbox.checked = !checkbox.checked">
<td class="px-6 py-3 text-sm">
<input @click.stop class="rounded" type="checkbox" name="entities[]"
value="{{ $entity->id }}">
</td>
<td class="whitespace-nowrap px-6 py-3 text-sm">
{{ $entity->{"name_$locale"} }}
<div class="text-gray-500">
{{ $entity->entityid }}
</div>
</td>
<td class="px-6 py-3 text-sm">
{{ $entity->{"description_$locale"} ?: __('entities.no_description') }}
</td>
<td class="px-6 py-3 text-sm">
<x-status :model="$federation" />
</td>
</tr>
@empty
<tr class="hover:bg-blue-50">
<td class="px-6 py-3 font-bold text-center">
{{ __('federations.empty') }}
</td>
</tr>
@endforelse
</tbody>
</table>
{{ $entities->links() }}
@if (count($entities))
<div class="px-4 py-2 bg-gray-100">
<x-button @click.prevent="open = !open">{{ __('common.add_members') }}</x-button>

<x-modal>
<x-slot:title>{{ __('common.confirm_add_members') }}</x-slot:title>
{{ __('common.confirm_add_members_body') }}
</x-modal>
</div>
@endif
</div>
</form>
Loading

0 comments on commit 192a85c

Please sign in to comment.