From 38768b4c71d8bc5fc3186faf6eb190f5a7fa7dbc Mon Sep 17 00:00:00 2001 From: Spitfire Date: Thu, 30 Nov 2023 22:11:20 -0600 Subject: [PATCH 1/7] Add multiple Abilities --- .../Abilities/EntityController.php | 9 ++-- app/Http/Requests/StoreAbilityEntity.php | 5 +- app/Models/Ability.php | 36 +++++++------- lang/en/abilities.php | 6 +-- resources/views/abilities/entities.blade.php | 2 +- .../views/abilities/entities/_form.blade.php | 10 ++-- .../views/abilities/entities/create.blade.php | 12 +++-- .../views/components/form/entities.blade.php | 49 +++++++++++++++++++ 8 files changed, 94 insertions(+), 35 deletions(-) create mode 100644 resources/views/components/form/entities.blade.php diff --git a/app/Http/Controllers/Abilities/EntityController.php b/app/Http/Controllers/Abilities/EntityController.php index e69431623e..0f698fcad6 100644 --- a/app/Http/Controllers/Abilities/EntityController.php +++ b/app/Http/Controllers/Abilities/EntityController.php @@ -65,8 +65,11 @@ public function store(StoreAbilityEntity $request, Campaign $campaign, Ability $ $redirectUrlOptions['ability_id'] = $ability->id; } - $ability->attachEntity($request->only('entity_id', 'visibility_id')); + $count = $ability->attachEntity($request->only('entities', 'visibility_id')); + return redirect()->route('abilities.entities', [$campaign, 'ability' => $ability->id]) - ->with('success', __('abilities.children.create.success', ['name' => $ability->name])); - } + //->with('success', __('abilities.children.create.success', ['name' => $ability->name])); + ->with('success', trans_choice('abilities.children.create.attach_success', $count, ['count' => $count, 'name' => $ability->name])); + + } } diff --git a/app/Http/Requests/StoreAbilityEntity.php b/app/Http/Requests/StoreAbilityEntity.php index 4707ad0b0f..7ffebae0dc 100644 --- a/app/Http/Requests/StoreAbilityEntity.php +++ b/app/Http/Requests/StoreAbilityEntity.php @@ -25,8 +25,11 @@ public function rules() { return [ 'ability_id' => 'required|exists:entities,id', - 'entity_id' => 'required|exists:entities,id|different:ability_id', 'visibility_id' => 'required|exists:visibilities,id', + 'entities' => 'required', + 'entities' => [ + '*' => 'different:ability_id|exists:entities,id', + ], ]; } } diff --git a/app/Models/Ability.php b/app/Models/Ability.php index d9e0ef456f..f072bafcca 100644 --- a/app/Models/Ability.php +++ b/app/Models/Ability.php @@ -205,27 +205,29 @@ public function entryWithAttributes() } /** - * Attach an entity to the tag + * Attach an entity to the ability */ - public function attachEntity(array $request): bool + public function attachEntity(array $request): int { - $entityId = Arr::get($request, 'entity_id'); - $entity = Entity::with('abilities')->findOrFail($entityId); - - // Make sure the tag isn't already attached to the entity - foreach ($entity->abilities as $ability) { - if ($ability->ability_id == $this->id) { - return true; + $entityIds = Arr::get($request, 'entities'); + $entities = Entity::with('abilities')->findOrFail($entityIds); + $count = 0; + foreach ($entities as $entity) { + // Make sure the tag isn't already attached to the entity + foreach ($entity->abilities as $ability) { + if ($ability->ability_id == $this->id) { + continue; + } } - } - $entityAbility = EntityAbility::create([ - 'ability_id' => $this->id, - 'entity_id' => $entityId, - 'visibility_id' => Arr::get($request, 'visibility_id', \App\Enums\Visibility::All), - ]); - - return $entityAbility !== false; + EntityAbility::create([ + 'ability_id' => $this->id, + 'entity_id' => $entity->id, + 'visibility_id' => Arr::get($request, 'visibility_id', \App\Enums\Visibility::All), + ]); + $count++; + } + return $count; } /** diff --git a/lang/en/abilities.php b/lang/en/abilities.php index 3d175b6711..1ac86c1cae 100644 --- a/lang/en/abilities.php +++ b/lang/en/abilities.php @@ -3,11 +3,11 @@ return [ 'children' => [ 'actions' => [ - 'add' => 'Attach entity', + 'attach' => 'Attach to entities', ], 'create' => [ - 'success' => 'Attached the ability :name to the entity.', - 'title' => 'Attach :name to an entity', + 'attach_success' => '{1} Attached the ability :name to :count entity.|[2,*] Attached the ability :name to :count entities.', + 'modal' => 'Attach :name to entities', ], 'description' => 'Entities having the ability', 'title' => 'Ability :name Entities', diff --git a/resources/views/abilities/entities.blade.php b/resources/views/abilities/entities.blade.php index 0cc838d83c..dcad0a94cf 100644 --- a/resources/views/abilities/entities.blade.php +++ b/resources/views/abilities/entities.blade.php @@ -11,7 +11,7 @@
- +
@endcan diff --git a/resources/views/abilities/entities/_form.blade.php b/resources/views/abilities/entities/_form.blade.php index 166862d725..1d8668babb 100644 --- a/resources/views/abilities/entities/_form.blade.php +++ b/resources/views/abilities/entities/_form.blade.php @@ -2,13 +2,9 @@ {{ csrf_field() }} - @include('cruds.fields.entity', [ - 'required' => true, - 'route' => 'search.ability-entities', - 'placeholder' => __('entities/relations.placeholders.target'), - 'preset' => false, - 'dropdownParent' => request()->ajax() ? '#primary-dialog' : null, - ]) + + @include('components.form.entities', ['options' => ['exclude-entity' => $model->entity->id]]) + @include('cruds.fields.visibility_id', ['model' => null]) diff --git a/resources/views/abilities/entities/create.blade.php b/resources/views/abilities/entities/create.blade.php index 0abc234738..f4151998dd 100644 --- a/resources/views/abilities/entities/create.blade.php +++ b/resources/views/abilities/entities/create.blade.php @@ -1,6 +1,6 @@ @extends('layouts.' . (request()->ajax() ? 'ajax' : 'app'), [ - 'title' => __('abilities.children.create.title', ['name' => $model->name]), + 'title' => __('abilities.children.create.modal', ['name' => $model->name]), 'breadcrumbs' => [ Breadcrumb::entity($model->entity)->list(), Breadcrumb::show($model), @@ -9,9 +9,15 @@ ]) @section('content') - {!! Form::open(['route' => $formOptions, 'method' => 'POST']) !!} + {!! Form::open([ + 'route' => $formOptions, + 'method' => 'POST', + 'data-shortcut' => 1, + 'class' => 'ajax-subform', + ]) !!} + @include('partials.forms.form', [ - 'title' => __('abilities.children.create.title', ['name' => $model->name]), + 'title' => __('abilities.children.create.modal', ['name' => $model->name]), 'content' => 'abilities.entities._form', 'dialog' => true, ]) diff --git a/resources/views/components/form/entities.blade.php b/resources/views/components/form/entities.blade.php new file mode 100644 index 0000000000..d31ee32a14 --- /dev/null +++ b/resources/views/components/form/entities.blade.php @@ -0,0 +1,49 @@ + +@if ($label) + +@endif + + From a84a628142e4b7ebfe302770b0ff83b21b67281e Mon Sep 17 00:00:00 2001 From: spitfire305 Date: Fri, 1 Dec 2023 04:13:45 +0000 Subject: [PATCH 2/7] Fix styling --- app/Http/Controllers/Abilities/EntityController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/Abilities/EntityController.php b/app/Http/Controllers/Abilities/EntityController.php index 0f698fcad6..61fdb43ba3 100644 --- a/app/Http/Controllers/Abilities/EntityController.php +++ b/app/Http/Controllers/Abilities/EntityController.php @@ -71,5 +71,5 @@ public function store(StoreAbilityEntity $request, Campaign $campaign, Ability $ //->with('success', __('abilities.children.create.success', ['name' => $ability->name])); ->with('success', trans_choice('abilities.children.create.attach_success', $count, ['count' => $count, 'name' => $ability->name])); - } + } } From d17e22d7c36849897b6872cefe638e374b15289c Mon Sep 17 00:00:00 2001 From: Spitfire Date: Fri, 1 Dec 2023 12:58:26 -0600 Subject: [PATCH 3/7] Implemented requested changes --- app/Models/Ability.php | 21 +++++++------------ .../views/abilities/entities/_form.blade.php | 7 ++++++- .../views/components/form/entities.blade.php | 7 ++----- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/app/Models/Ability.php b/app/Models/Ability.php index f072bafcca..c554f6b75c 100644 --- a/app/Models/Ability.php +++ b/app/Models/Ability.php @@ -210,23 +210,16 @@ public function entryWithAttributes() public function attachEntity(array $request): int { $entityIds = Arr::get($request, 'entities'); - $entities = Entity::with('abilities')->findOrFail($entityIds); $count = 0; - foreach ($entities as $entity) { - // Make sure the tag isn't already attached to the entity - foreach ($entity->abilities as $ability) { - if ($ability->ability_id == $this->id) { - continue; - } - } + $visibility = Arr::get($request, 'visibility_id', \App\Enums\Visibility::All); + $sync = []; - EntityAbility::create([ - 'ability_id' => $this->id, - 'entity_id' => $entity->id, - 'visibility_id' => Arr::get($request, 'visibility_id', \App\Enums\Visibility::All), - ]); - $count++; + foreach ($entityIds as $entity) { + $sync[$entity] = ['visibility_id' => $visibility]; + $count++; } + $this->entities()->syncWithoutDetaching($sync); + return $count; } diff --git a/resources/views/abilities/entities/_form.blade.php b/resources/views/abilities/entities/_form.blade.php index 1d8668babb..164c446ca8 100644 --- a/resources/views/abilities/entities/_form.blade.php +++ b/resources/views/abilities/entities/_form.blade.php @@ -3,7 +3,12 @@ - @include('components.form.entities', ['options' => ['exclude-entity' => $model->entity->id]]) + + @include('cruds.fields.visibility_id', ['model' => null]) diff --git a/resources/views/components/form/entities.blade.php b/resources/views/components/form/entities.blade.php index d31ee32a14..c5dcaf034a 100644 --- a/resources/views/components/form/entities.blade.php +++ b/resources/views/components/form/entities.blade.php @@ -5,11 +5,9 @@ * @var \App\Models\MiscModel $model */ $selectedOption = []; - $model = Arr::get($options, 'model', null); $enableNew = Arr::get($options, 'enableNew', true); $label = Arr::get($options, 'label', true); - // From source to exclude duplicates $searchParams = [$campaign]; if (Arr::has($options, 'exclude', false)) { @@ -17,7 +15,6 @@ } elseif (Arr::has($options, 'exclude-entity', false)) { $searchParams['exclude-entity'] = Arr::get($options, 'exclude-entity'); } - // Try to load what was sent with the form first, in case there was a form validation error $previous = old('entities[]'); if (!empty($previous)) { @@ -36,12 +33,12 @@ id="{{ Arr::get($options, 'id', 'entities[]') }}" class="w-full form-tags form-entities" style="width: 100%" - data-url="{{ route('search.ability-entities', $searchParams) }}" + data-url="{{ route(Arr::get($options, 'route', 'search.ability-entities'), $searchParams) }}" data-allow-new="{{ $enableNew ? 'true' : 'false' }}" data-allow-clear="{{ Arr::get($options, 'allowClear', 'true') }}" data-new-tag="{{ __('entities.new_entity') }}" data-placeholder="{{ __('entities/relations.placeholders.target') }}" - :dropdownParent="$dropdownParent ?? (request()->ajax() ? '#primary-dialog' : null)" + :dropdownParent="$dropdownParent ?? ($ajax ? '#primary-dialog' : null)" > @foreach ($selectedOption as $key => $entity) From 15ec7a249e13ba8a861a0499e053612691ee81fb Mon Sep 17 00:00:00 2001 From: spitfire305 Date: Fri, 1 Dec 2023 18:59:36 +0000 Subject: [PATCH 4/7] Fix styling --- app/Models/Ability.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Models/Ability.php b/app/Models/Ability.php index c554f6b75c..87e67b12dc 100644 --- a/app/Models/Ability.php +++ b/app/Models/Ability.php @@ -215,8 +215,8 @@ public function attachEntity(array $request): int $sync = []; foreach ($entityIds as $entity) { - $sync[$entity] = ['visibility_id' => $visibility]; - $count++; + $sync[$entity] = ['visibility_id' => $visibility]; + $count++; } $this->entities()->syncWithoutDetaching($sync); From 2cd4fb6f48da2a481852ff96e84ef9a0380bb7e6 Mon Sep 17 00:00:00 2001 From: Spitfire Date: Mon, 4 Dec 2023 15:20:23 -0600 Subject: [PATCH 5/7] Refactored entities component into foreign component --- .../views/abilities/entities/_form.blade.php | 20 ++++---- .../views/components/form/entities.blade.php | 46 ------------------- .../views/components/forms/foreign.blade.php | 13 +++++- 3 files changed, 23 insertions(+), 56 deletions(-) delete mode 100644 resources/views/components/form/entities.blade.php diff --git a/resources/views/abilities/entities/_form.blade.php b/resources/views/abilities/entities/_form.blade.php index 164c446ca8..83b51d8d2b 100644 --- a/resources/views/abilities/entities/_form.blade.php +++ b/resources/views/abilities/entities/_form.blade.php @@ -2,14 +2,18 @@ {{ csrf_field() }} - - - - + + @include('cruds.fields.visibility_id', ['model' => null]) diff --git a/resources/views/components/form/entities.blade.php b/resources/views/components/form/entities.blade.php deleted file mode 100644 index c5dcaf034a..0000000000 --- a/resources/views/components/form/entities.blade.php +++ /dev/null @@ -1,46 +0,0 @@ - -@if ($label) - -@endif - - diff --git a/resources/views/components/forms/foreign.blade.php b/resources/views/components/forms/foreign.blade.php index d5debc28a9..162ea87728 100644 --- a/resources/views/components/forms/foreign.blade.php +++ b/resources/views/components/forms/foreign.blade.php @@ -10,7 +10,10 @@
@endif - From ed4dd330d945e607708205de6f791a6877fc68e3 Mon Sep 17 00:00:00 2001 From: Spitfire Date: Mon, 4 Dec 2023 15:32:58 -0600 Subject: [PATCH 6/7] Removed leftover code --- resources/views/abilities/entities/_form.blade.php | 1 - 1 file changed, 1 deletion(-) diff --git a/resources/views/abilities/entities/_form.blade.php b/resources/views/abilities/entities/_form.blade.php index 83b51d8d2b..30e54e3a17 100644 --- a/resources/views/abilities/entities/_form.blade.php +++ b/resources/views/abilities/entities/_form.blade.php @@ -9,7 +9,6 @@ multiple="multiple" name="entities[]" id="entities[]" - :options="['exclude-entity' => $model->entity->id, 'route' => 'search.ability-entities']" :campaign="$campaign" :route="route('search.ability-entities', [$campaign])" > From a1db8c5176b8717262d423339cef3abc97a40d30 Mon Sep 17 00:00:00 2001 From: Spitfire Date: Mon, 4 Dec 2023 16:44:57 -0600 Subject: [PATCH 7/7] Added missing param --- resources/views/abilities/entities/_form.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/abilities/entities/_form.blade.php b/resources/views/abilities/entities/_form.blade.php index 30e54e3a17..d92a61548a 100644 --- a/resources/views/abilities/entities/_form.blade.php +++ b/resources/views/abilities/entities/_form.blade.php @@ -10,7 +10,7 @@ name="entities[]" id="entities[]" :campaign="$campaign" - :route="route('search.ability-entities', [$campaign])" + :route="route('search.ability-entities', [$campaign, 'exclude-entity' => $model->entity->id])" >