Skip to content

Commit

Permalink
rewrite Entity add and delete operators to new EntityOperatorControll…
Browse files Browse the repository at this point in the history
…er.php
  • Loading branch information
temaotl committed Aug 2, 2024
1 parent 80ed569 commit a619881
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 209 deletions.
58 changes: 0 additions & 58 deletions app/Http/Controllers/EntityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@
use App\Notifications\EntityDeletedFromRs;
use App\Notifications\EntityDestroyed;
use App\Notifications\EntityEdugainStatusChanged;
use App\Notifications\EntityOperatorsChanged;
use App\Notifications\EntityRequested;
use App\Notifications\EntityUpdated;
use App\Notifications\IdpCategoryChanged;
use App\Notifications\YourEntityRightsChanged;
use App\Services\NotificationService;
use App\Traits\DumpFromGit\EntitiesHelp\DeleteFromEntity;
use App\Traits\DumpFromGit\EntitiesHelp\UpdateEntity;
use App\Traits\GitTrait;
Expand Down Expand Up @@ -302,61 +299,6 @@ function () use ($entity) {

break;

case 'state':

break;

case 'add_operators':
$this->authorize('update', $entity);

if (! request('operators')) {
return to_route('entities.operators', $entity)
->with('status', __('entities.add_empty_operators'))
->with('color', 'red');
}

$old_operators = $entity->operators;
$new_operators = User::whereIn('id', request('operators'))->get();
$entity->operators()->attach(request('operators'));

$admins = User::activeAdmins()->select('id', 'email')->get();
Notification::sendNow($new_operators, new YourEntityRightsChanged($entity, 'added'));
NotificationService::sendOperatorNotification($old_operators, new EntityOperatorsChanged($entity, $new_operators, 'added'));

/* Notification::send($old_operators, new EntityOperatorsChanged($entity, $new_operators, 'added'));
Notification::send($admins, new EntityOperatorsChanged($entity, $new_operators, 'added'));*/

return redirect()
->route('entities.show', $entity)
->with('status', __('entities.operators_added'));

break;

case 'delete_operators':
$this->authorize('update', $entity);

if (! request('operators')) {
return to_route('entities.operators', $entity)
->with('status', __('entities.delete_empty_operators'))
->with('color', 'red');
}

$old_operators = User::whereIn('id', request('operators'))->get();
$entity->operators()->detach(request('operators'));
$new_operators = $entity->operators;

$admins = User::activeAdmins()->select('id', 'email')->get();
Notification::sendNow($old_operators, new YourEntityRightsChanged($entity, 'deleted'));
NotificationService::sendOperatorNotification($old_operators, new EntityOperatorsChanged($entity, $old_operators, 'deleted'));
/* Notification::send($new_operators, new EntityOperatorsChanged($entity, $old_operators, 'deleted'));
Notification::send($admins, new EntityOperatorsChanged($entity, $old_operators, 'deleted'));*/

return redirect()
->route('entities.show', $entity)
->with('status', __('entities.operators_deleted'));

break;

case 'edugain':
$this->authorize('update', $entity);

Expand Down
59 changes: 54 additions & 5 deletions app/Http/Controllers/EntityOperatorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

use App\Models\Entity;
use App\Models\User;
use App\Notifications\EntityOperatorsChanged;
use App\Notifications\YourEntityRightsChanged;
use App\Services\NotificationService;
use Illuminate\Support\Facades\Notification;

class EntityOperatorController extends Controller
{
public function __construct()
{

}

public function index(Entity $entity)
{
$this->authorize('view', $entity);
Expand All @@ -29,4 +28,54 @@ public function index(Entity $entity)
'users' => $users,
]);
}

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

if (! request('operators')) {
return to_route('entities.operators.index', $entity)
->with('status', __('entities.add_empty_operators'))
->with('color', 'red');
}

$old_operators = $entity->operators;
$new_operators = User::whereIn('id', request('operators'))->get();
$entity->operators()->attach(request('operators'));

Notification::sendNow($new_operators, new YourEntityRightsChanged($entity, 'added'));
NotificationService::sendOperatorNotification($old_operators, new EntityOperatorsChanged($entity, $new_operators, 'added'));

return redirect()
->route('entities.operators.index', $entity)
->with('status', __('entities.operators_added'));
}

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

if (! request('operators')) {
return to_route('entities.operators.index', $entity)
->with('status', __('entities.delete_empty_operators'))
->with('color', 'red');
}

$old_operators = User::whereIn('id', request('operators'))->get();
$entity->operators()->detach(request('operators'));

Notification::sendNow($old_operators, new YourEntityRightsChanged($entity, 'deleted'));
NotificationService::sendOperatorNotification($old_operators, new EntityOperatorsChanged($entity, $old_operators, 'deleted'));

return redirect()
->route('entities.operators.index', $entity)
->with('status', __('entities.operators_deleted'));

}
}
2 changes: 1 addition & 1 deletion resources/views/entities/navigation.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
),
'cursor-default' => request()->routeIs('entities.operators'),
])
href="{{ route('entities.operators', $entity) }}">{{ __('common.operators') }}</a>
href="{{ route('entities.operators.index', $entity) }}">{{ __('common.operators') }}</a>
<a @class([
'px-2',
'py-1',
Expand Down
60 changes: 60 additions & 0 deletions resources/views/entities/operatorForm/add.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<form x-data="{ open: false }" id="add_operators" action="{{ route('entities.operators.store', $entity) }}" method="post">
@csrf
<div class="dark:bg-transparent overflow-x-auto bg-gray-100 border rounded-lg">
<table class="min-w-full border-b border-gray-300">
<thead>
<tr>
<th
class="dark:bg-gray-700 px-6 py-3 text-xs tracking-widest text-left uppercase bg-gray-100 border-b">
&nbsp;
</th>
<th
class="dark:bg-gray-700 px-6 py-3 text-xs tracking-widest text-left uppercase bg-gray-100 border-b">
{{ __('common.name') }}
</th>
<th
class="dark:bg-gray-700 px-6 py-3 text-xs tracking-widest text-left uppercase bg-gray-100 border-b">
{{ __('common.email') }}
</th>
</tr>
</thead>
<tbody class="divide-y divide-gray-200">
@forelse ($users as $user)
<tr x-data class="bg-white" 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="operators[]"
value="{{ $user->id }}">
</td>
<td class="whitespace-nowrap px-6 py-3 text-sm">
{{ $user->name }}
</td>
<td class="px-6 py-3 text-sm">
<a class="hover:underline text-blue-500"
href="mailto:{{ $user->email }}">{{ $user->email }}</a>
</td>
</tr>
@empty
<tr>
<td class="px-6 py-3 font-bold text-center bg-white" colspan="4">
{{ __('common.no_operators') }}</td>
</tr>
@endforelse
</tbody>
</table>
{{ $users->links() }}
@if (count($users))
<div class="px-4 py-3 bg-gray-100">
<x-button @click.prevent="open = !open">{{ __('common.add_operators') }}
</x-button>

<x-modal>
<x-slot:title>
{{ __('common.confirm_add_operators') }}
</x-slot:title>
{{ __('common.confirm_add_operators_body') }}
</x-modal>
</div>
@endif
</div>
</form>
61 changes: 61 additions & 0 deletions resources/views/entities/operatorForm/delete.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<form x-data="{ open: false }" id="delete_operators" action="{{ route('entities.operators.destroy', $entity) }}" method="post">
@csrf
@method('DELETE')
<div class="dark:bg-transparent overflow-x-auto bg-gray-100 border rounded-lg">
<table class="min-w-full border-b border-gray-300">
<thead>
<tr>
<th
class="dark:bg-gray-700 px-6 py-3 text-xs tracking-widest text-left uppercase bg-gray-100 border-b">
&nbsp;
</th>
<th
class="dark:bg-gray-700 px-6 py-3 text-xs tracking-widest text-left uppercase bg-gray-100 border-b">
{{ __('common.name') }}
</th>
<th
class="dark:bg-gray-700 px-6 py-3 text-xs tracking-widest text-left uppercase bg-gray-100 border-b">
{{ __('common.email') }}
</th>
</tr>
</thead>
<tbody class="divide-y divide-gray-200">
@forelse ($operators->sortBy('name') as $operator)
<tr x-data class="bg-white" 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="operators[]"
value="{{ $operator->id }}">
</td>
<td class="whitespace-nowrap px-6 py-3 text-sm">
{{ $operator->name }}
</td>
<td class="px-6 py-3 text-sm">
<a class="hover:underline text-blue-500"
href="mailto:{{ $operator->email }}">{{ $operator->email }}</a>
</td>
</tr>
@empty
<tr>
<td class="px-6 py-3 font-bold text-center bg-white" colspan="4">
{{ __('common.no_operators') }}</td>
</tr>
@endforelse
</tbody>
</table>
{{ $operators->links() }}
@if (count($operators))
<div class="px-4 py-3 bg-gray-100">
<x-button @click.prevent="open = !open" color="red">{{ __('common.delete_operators') }}
</x-button>

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

0 comments on commit a619881

Please sign in to comment.